kaqing 2.0.14__py3-none-any.whl → 2.0.145__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kaqing might be problematic. Click here for more details.
- adam/apps.py +2 -2
- adam/batch.py +13 -3
- adam/checks/check_utils.py +4 -4
- adam/checks/compactionstats.py +1 -1
- adam/checks/cpu.py +2 -2
- adam/checks/disk.py +1 -1
- adam/checks/gossip.py +1 -1
- adam/checks/memory.py +3 -3
- adam/checks/status.py +1 -1
- adam/commands/alter_tables.py +81 -0
- adam/commands/app.py +3 -3
- adam/commands/app_ping.py +2 -2
- adam/commands/audit/audit.py +86 -0
- adam/commands/audit/audit_repair_tables.py +77 -0
- adam/commands/audit/audit_run.py +58 -0
- adam/commands/audit/show_last10.py +51 -0
- adam/commands/audit/show_slow10.py +50 -0
- adam/commands/audit/show_top10.py +48 -0
- adam/commands/audit/utils_show_top10.py +59 -0
- adam/commands/bash/__init__.py +0 -0
- adam/commands/bash/bash.py +133 -0
- adam/commands/bash/bash_completer.py +93 -0
- adam/commands/cat.py +56 -0
- adam/commands/cd.py +12 -82
- adam/commands/check.py +6 -0
- adam/commands/cli_commands.py +3 -3
- adam/commands/code.py +60 -0
- adam/commands/command.py +48 -12
- adam/commands/commands_utils.py +4 -5
- adam/commands/cql/__init__.py +0 -0
- adam/commands/cql/cql_completions.py +28 -0
- adam/commands/cql/cql_utils.py +209 -0
- adam/commands/{cqlsh.py → cql/cqlsh.py} +15 -10
- adam/commands/deploy/code_utils.py +2 -2
- adam/commands/deploy/deploy.py +8 -21
- adam/commands/deploy/deploy_frontend.py +1 -1
- adam/commands/deploy/deploy_pg_agent.py +3 -3
- adam/commands/deploy/deploy_pod.py +28 -27
- adam/commands/deploy/deploy_utils.py +16 -26
- adam/commands/deploy/undeploy.py +8 -21
- adam/commands/deploy/undeploy_frontend.py +1 -1
- adam/commands/deploy/undeploy_pg_agent.py +5 -3
- adam/commands/deploy/undeploy_pod.py +12 -10
- adam/commands/devices/__init__.py +0 -0
- adam/commands/devices/device.py +27 -0
- adam/commands/devices/device_app.py +146 -0
- adam/commands/devices/device_auit_log.py +43 -0
- adam/commands/devices/device_cass.py +145 -0
- adam/commands/devices/device_export.py +86 -0
- adam/commands/devices/device_postgres.py +109 -0
- adam/commands/devices/devices.py +25 -0
- adam/commands/export/__init__.py +0 -0
- adam/commands/export/clean_up_export_session.py +53 -0
- adam/commands/export/clean_up_export_sessions.py +40 -0
- adam/commands/export/drop_export_database.py +58 -0
- adam/commands/export/drop_export_databases.py +46 -0
- adam/commands/export/export.py +83 -0
- adam/commands/export/export_databases.py +170 -0
- adam/commands/export/export_select.py +85 -0
- adam/commands/export/export_select_x.py +54 -0
- adam/commands/export/export_use.py +55 -0
- adam/commands/export/exporter.py +364 -0
- adam/commands/export/import_session.py +68 -0
- adam/commands/export/importer.py +67 -0
- adam/commands/export/importer_athena.py +80 -0
- adam/commands/export/importer_sqlite.py +47 -0
- adam/commands/export/show_column_counts.py +63 -0
- adam/commands/export/show_export_databases.py +39 -0
- adam/commands/export/show_export_session.py +51 -0
- adam/commands/export/show_export_sessions.py +47 -0
- adam/commands/export/utils_export.py +291 -0
- adam/commands/help.py +12 -7
- adam/commands/issues.py +6 -0
- adam/commands/kubectl.py +41 -0
- adam/commands/login.py +7 -4
- adam/commands/logs.py +2 -1
- adam/commands/ls.py +4 -107
- adam/commands/medusa/medusa.py +2 -26
- adam/commands/medusa/medusa_backup.py +2 -2
- adam/commands/medusa/medusa_restore.py +3 -4
- adam/commands/medusa/medusa_show_backupjobs.py +4 -3
- adam/commands/medusa/medusa_show_restorejobs.py +3 -3
- adam/commands/nodetool.py +9 -4
- adam/commands/param_set.py +1 -1
- adam/commands/postgres/postgres.py +42 -43
- adam/commands/postgres/{postgres_session.py → postgres_context.py} +43 -42
- adam/commands/postgres/postgres_utils.py +31 -0
- adam/commands/postgres/psql_completions.py +10 -0
- adam/commands/preview_table.py +18 -40
- adam/commands/pwd.py +2 -28
- adam/commands/reaper/reaper.py +4 -24
- adam/commands/reaper/reaper_restart.py +1 -1
- adam/commands/reaper/reaper_session.py +2 -2
- adam/commands/repair/repair.py +3 -27
- adam/commands/repair/repair_log.py +1 -1
- adam/commands/repair/repair_run.py +2 -2
- adam/commands/repair/repair_scan.py +1 -1
- adam/commands/repair/repair_stop.py +1 -1
- adam/commands/report.py +6 -0
- adam/commands/restart.py +2 -2
- adam/commands/rollout.py +1 -1
- adam/commands/show/show.py +11 -26
- adam/commands/show/show_app_actions.py +3 -0
- adam/commands/show/show_app_id.py +1 -1
- adam/commands/show/show_app_queues.py +3 -2
- adam/commands/show/show_cassandra_status.py +3 -3
- adam/commands/show/show_cassandra_version.py +3 -3
- adam/commands/show/show_host.py +33 -0
- adam/commands/show/show_login.py +3 -0
- adam/commands/show/show_processes.py +1 -1
- adam/commands/show/show_repairs.py +2 -2
- adam/commands/show/show_storage.py +1 -1
- adam/commands/watch.py +1 -1
- adam/config.py +16 -3
- adam/embedded_params.py +1 -1
- adam/pod_exec_result.py +10 -2
- adam/repl.py +127 -117
- adam/repl_commands.py +51 -16
- adam/repl_state.py +276 -55
- adam/sql/__init__.py +0 -0
- adam/sql/sql_completer.py +120 -0
- adam/sql/sql_state_machine.py +617 -0
- adam/sql/term_completer.py +76 -0
- adam/sso/authn_ad.py +1 -1
- adam/sso/cred_cache.py +1 -1
- adam/sso/idp.py +1 -1
- adam/utils.py +83 -2
- adam/utils_athena.py +145 -0
- adam/utils_audits.py +102 -0
- adam/utils_k8s/__init__.py +0 -0
- adam/utils_k8s/app_clusters.py +33 -0
- adam/utils_k8s/app_pods.py +31 -0
- adam/{k8s_utils → utils_k8s}/cassandra_clusters.py +6 -21
- adam/{k8s_utils → utils_k8s}/cassandra_nodes.py +12 -5
- adam/{k8s_utils → utils_k8s}/deployment.py +2 -2
- adam/{k8s_utils → utils_k8s}/kube_context.py +1 -1
- adam/{k8s_utils → utils_k8s}/pods.py +119 -26
- adam/{k8s_utils → utils_k8s}/secrets.py +4 -0
- adam/{k8s_utils → utils_k8s}/statefulsets.py +5 -4
- adam/utils_net.py +24 -0
- adam/utils_repl/__init__.py +0 -0
- adam/utils_repl/automata_completer.py +48 -0
- adam/utils_repl/repl_completer.py +46 -0
- adam/utils_repl/state_machine.py +173 -0
- adam/utils_sqlite.py +101 -0
- adam/version.py +1 -1
- {kaqing-2.0.14.dist-info → kaqing-2.0.145.dist-info}/METADATA +1 -1
- kaqing-2.0.145.dist-info/RECORD +227 -0
- adam/commands/bash.py +0 -87
- adam/commands/cql_utils.py +0 -53
- adam/commands/devices.py +0 -89
- kaqing-2.0.14.dist-info/RECORD +0 -167
- /adam/{k8s_utils → commands/audit}/__init__.py +0 -0
- /adam/{k8s_utils → utils_k8s}/config_maps.py +0 -0
- /adam/{k8s_utils → utils_k8s}/custom_resources.py +0 -0
- /adam/{k8s_utils → utils_k8s}/ingresses.py +0 -0
- /adam/{k8s_utils → utils_k8s}/jobs.py +0 -0
- /adam/{k8s_utils → utils_k8s}/service_accounts.py +0 -0
- /adam/{k8s_utils → utils_k8s}/services.py +0 -0
- /adam/{k8s_utils → utils_k8s}/volumes.py +0 -0
- {kaqing-2.0.14.dist-info → kaqing-2.0.145.dist-info}/WHEEL +0 -0
- {kaqing-2.0.14.dist-info → kaqing-2.0.145.dist-info}/entry_points.txt +0 -0
- {kaqing-2.0.14.dist-info → kaqing-2.0.145.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.export.exporter import Exporter
|
|
3
|
+
from adam.repl_state import ReplState
|
|
4
|
+
from adam.utils import log2
|
|
5
|
+
from adam.utils_athena import Athena
|
|
6
|
+
from adam.utils_sqlite import SQLite
|
|
7
|
+
|
|
8
|
+
class DropExportDatabase(Command):
|
|
9
|
+
COMMAND = 'drop export database'
|
|
10
|
+
|
|
11
|
+
# the singleton pattern
|
|
12
|
+
def __new__(cls, *args, **kwargs):
|
|
13
|
+
if not hasattr(cls, 'instance'): cls.instance = super(DropExportDatabase, cls).__new__(cls)
|
|
14
|
+
|
|
15
|
+
return cls.instance
|
|
16
|
+
|
|
17
|
+
def __init__(self, successor: Command=None):
|
|
18
|
+
super().__init__(successor)
|
|
19
|
+
|
|
20
|
+
def command(self):
|
|
21
|
+
return DropExportDatabase.COMMAND
|
|
22
|
+
|
|
23
|
+
def required(self):
|
|
24
|
+
return [ReplState.C, ReplState.X]
|
|
25
|
+
|
|
26
|
+
def run(self, cmd: str, state: ReplState):
|
|
27
|
+
if not(args := self.args(cmd)):
|
|
28
|
+
return super().run(cmd, state)
|
|
29
|
+
|
|
30
|
+
state, args = self.apply_state(args, state)
|
|
31
|
+
if not self.validate_state(state):
|
|
32
|
+
return state
|
|
33
|
+
|
|
34
|
+
if not len(args):
|
|
35
|
+
if state.in_repl:
|
|
36
|
+
log2('Database name is required.')
|
|
37
|
+
log2()
|
|
38
|
+
else:
|
|
39
|
+
log2('* Database name is missing.')
|
|
40
|
+
Command.display_help()
|
|
41
|
+
|
|
42
|
+
return 'command-missing'
|
|
43
|
+
|
|
44
|
+
Exporter.drop_databases(state.sts, state.pod, state.namespace, args[0])
|
|
45
|
+
|
|
46
|
+
SQLite.clear_cache()
|
|
47
|
+
Athena.clear_cache()
|
|
48
|
+
|
|
49
|
+
if state.export_session == args[0]:
|
|
50
|
+
state.export_session = None
|
|
51
|
+
|
|
52
|
+
return state
|
|
53
|
+
|
|
54
|
+
def completion(self, _: ReplState):
|
|
55
|
+
return {}
|
|
56
|
+
|
|
57
|
+
def help(self, _: ReplState):
|
|
58
|
+
return f'{DropExportDatabase.COMMAND} <export-database-name>\t drop export database'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.export.exporter import Exporter
|
|
3
|
+
from adam.repl_state import ReplState
|
|
4
|
+
from adam.utils_athena import Athena
|
|
5
|
+
from adam.utils_sqlite import SQLite
|
|
6
|
+
|
|
7
|
+
class DropExportDatabases(Command):
|
|
8
|
+
COMMAND = 'drop all export databases'
|
|
9
|
+
|
|
10
|
+
# the singleton pattern
|
|
11
|
+
def __new__(cls, *args, **kwargs):
|
|
12
|
+
if not hasattr(cls, 'instance'): cls.instance = super(DropExportDatabases, cls).__new__(cls)
|
|
13
|
+
|
|
14
|
+
return cls.instance
|
|
15
|
+
|
|
16
|
+
def __init__(self, successor: Command=None):
|
|
17
|
+
super().__init__(successor)
|
|
18
|
+
|
|
19
|
+
def command(self):
|
|
20
|
+
return DropExportDatabases.COMMAND
|
|
21
|
+
|
|
22
|
+
def required(self):
|
|
23
|
+
return [ReplState.C, ReplState.X]
|
|
24
|
+
|
|
25
|
+
def run(self, cmd: str, state: ReplState):
|
|
26
|
+
if not(args := self.args(cmd)):
|
|
27
|
+
return super().run(cmd, state)
|
|
28
|
+
|
|
29
|
+
state, args = self.apply_state(args, state)
|
|
30
|
+
if not self.validate_state(state):
|
|
31
|
+
return state
|
|
32
|
+
|
|
33
|
+
Exporter.drop_databases(state.sts, state.pod, state.namespace)
|
|
34
|
+
|
|
35
|
+
SQLite.clear_cache()
|
|
36
|
+
Athena.clear_cache()
|
|
37
|
+
|
|
38
|
+
state.export_session = None
|
|
39
|
+
|
|
40
|
+
return state
|
|
41
|
+
|
|
42
|
+
def completion(self, _: ReplState):
|
|
43
|
+
return {}
|
|
44
|
+
|
|
45
|
+
def help(self, _: ReplState):
|
|
46
|
+
return f'{DropExportDatabases.COMMAND}\t drop all export databases'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.cql.cql_utils import cassandra_keyspaces, cassandra_table_names
|
|
3
|
+
from adam.commands.export.export_databases import ExportDatabases
|
|
4
|
+
from adam.commands.export.exporter import Exporter
|
|
5
|
+
from adam.commands.export.utils_export import ExportSpec
|
|
6
|
+
from adam.repl_state import ReplState, RequiredState
|
|
7
|
+
from adam.sql.sql_completer import SqlCompleter, SqlVariant
|
|
8
|
+
from adam.utils import log
|
|
9
|
+
from adam.utils_k8s.statefulsets import StatefulSets
|
|
10
|
+
|
|
11
|
+
class ExportTables(Command):
|
|
12
|
+
COMMAND = 'export'
|
|
13
|
+
|
|
14
|
+
# the singleton pattern
|
|
15
|
+
def __new__(cls, *args, **kwargs):
|
|
16
|
+
if not hasattr(cls, 'instance'): cls.instance = super(ExportTables, cls).__new__(cls)
|
|
17
|
+
|
|
18
|
+
return cls.instance
|
|
19
|
+
|
|
20
|
+
def __init__(self, successor: Command=None):
|
|
21
|
+
super().__init__(successor)
|
|
22
|
+
|
|
23
|
+
def command(self):
|
|
24
|
+
return ExportTables.COMMAND
|
|
25
|
+
|
|
26
|
+
def required(self):
|
|
27
|
+
return RequiredState.CLUSTER_OR_POD
|
|
28
|
+
|
|
29
|
+
def run(self, cmd: str, state: ReplState):
|
|
30
|
+
if not(args := self.args(cmd)):
|
|
31
|
+
return super().run(cmd, state)
|
|
32
|
+
|
|
33
|
+
state, args = self.apply_state(args, state)
|
|
34
|
+
if not self.validate_state(state):
|
|
35
|
+
return state
|
|
36
|
+
|
|
37
|
+
# --export-only for testing only
|
|
38
|
+
args, export_only = Command.extract_options(args, ['--export-only'])
|
|
39
|
+
|
|
40
|
+
if not state.pod:
|
|
41
|
+
state.push()
|
|
42
|
+
state.pod = StatefulSets.pod_names(state.sts, state.namespace)[0]
|
|
43
|
+
|
|
44
|
+
export_session = state.export_session
|
|
45
|
+
spec: ExportSpec = None
|
|
46
|
+
try:
|
|
47
|
+
statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
|
|
48
|
+
if not statuses:
|
|
49
|
+
return state
|
|
50
|
+
|
|
51
|
+
Exporter.clear_export_session_cache()
|
|
52
|
+
|
|
53
|
+
if spec.importer == 'csv' or export_only:
|
|
54
|
+
ExportDatabases.disply_export_session(state.sts, state.pod, state.namespace, spec.session)
|
|
55
|
+
else:
|
|
56
|
+
log()
|
|
57
|
+
ExportDatabases.display_export_db(state.export_session)
|
|
58
|
+
|
|
59
|
+
finally:
|
|
60
|
+
state.pop()
|
|
61
|
+
|
|
62
|
+
# if exporting to csv, do not bind the new session id to repl state
|
|
63
|
+
if spec and spec.importer == 'csv':
|
|
64
|
+
state.export_session = export_session
|
|
65
|
+
|
|
66
|
+
return state
|
|
67
|
+
|
|
68
|
+
def completion(self, state: ReplState):
|
|
69
|
+
def sc():
|
|
70
|
+
return SqlCompleter(lambda: cassandra_table_names(state), expandables={
|
|
71
|
+
'dml':'export',
|
|
72
|
+
'columns': lambda table: ['id', '*'],
|
|
73
|
+
'keyspaces': lambda: cassandra_keyspaces(state),
|
|
74
|
+
'export-dbs': lambda: ExportDatabases.database_names(),
|
|
75
|
+
}, variant=SqlVariant.CQL)
|
|
76
|
+
|
|
77
|
+
if state.sts:
|
|
78
|
+
return {f'@{p}': {ExportTables.COMMAND: sc()} for p in StatefulSets.pod_names(state.sts, state.namespace)}
|
|
79
|
+
|
|
80
|
+
return {}
|
|
81
|
+
|
|
82
|
+
def help(self, _: ReplState):
|
|
83
|
+
return f'{ExportTables.COMMAND} [* [in KEYSPACE]] | [TABLE] [as target-name] [with consistency <level>]\t export tables to Sqlite, Athena or CSV file'
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import boto3
|
|
3
|
+
|
|
4
|
+
from adam.commands.export.utils_export import ExportTableStatus
|
|
5
|
+
from adam.config import Config
|
|
6
|
+
from adam.utils import lines_to_tabular, log, log2, ing
|
|
7
|
+
from adam.utils_athena import Athena
|
|
8
|
+
from adam.utils_k8s.statefulsets import StatefulSets
|
|
9
|
+
from adam.utils_sqlite import SQLite
|
|
10
|
+
|
|
11
|
+
LIKE = 'e%_%'
|
|
12
|
+
|
|
13
|
+
class ExportDatabases:
|
|
14
|
+
def run_query(query: str, database: str, show_query=False) -> int:
|
|
15
|
+
cnt: int = 0
|
|
16
|
+
|
|
17
|
+
if show_query:
|
|
18
|
+
log2(query)
|
|
19
|
+
|
|
20
|
+
if database.startswith('s'):
|
|
21
|
+
cnt += SQLite.run_query(query, database=database)
|
|
22
|
+
else:
|
|
23
|
+
cnt += Athena.run_query(query, database=database)
|
|
24
|
+
|
|
25
|
+
return cnt
|
|
26
|
+
|
|
27
|
+
def sessions_from_dbs(dbs: list[str]):
|
|
28
|
+
|
|
29
|
+
sessions = set()
|
|
30
|
+
|
|
31
|
+
for db in dbs:
|
|
32
|
+
sessions.add(db.split('_')[0])
|
|
33
|
+
|
|
34
|
+
return list(sessions)
|
|
35
|
+
|
|
36
|
+
def drop_export_dbs(db: str = None):
|
|
37
|
+
dbs: list[str] = []
|
|
38
|
+
|
|
39
|
+
if not db or db.startswith('s'):
|
|
40
|
+
dbs.extend(ExportDatabases.drop_sqlite_dbs(db))
|
|
41
|
+
if not db or db.startswith('e'):
|
|
42
|
+
dbs.extend(ExportDatabases.drop_athena_dbs(db))
|
|
43
|
+
|
|
44
|
+
return dbs
|
|
45
|
+
|
|
46
|
+
def drop_sqlite_dbs(db: str = None):
|
|
47
|
+
dbs = SQLite.database_names(db)
|
|
48
|
+
if dbs:
|
|
49
|
+
with ing(f'Droping {len(dbs)} SQLite databases'):
|
|
50
|
+
try:
|
|
51
|
+
for db in dbs:
|
|
52
|
+
file_path = f'{SQLite.local_db_dir()}/{db}'
|
|
53
|
+
try:
|
|
54
|
+
os.remove(file_path)
|
|
55
|
+
except OSError as e:
|
|
56
|
+
pass
|
|
57
|
+
except:
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
return dbs
|
|
61
|
+
|
|
62
|
+
def drop_athena_dbs(db: str = None):
|
|
63
|
+
dbs = Athena.database_names(f'{db}_%' if db else LIKE)
|
|
64
|
+
if dbs:
|
|
65
|
+
with ing(f'Droping {len(dbs)} Athena databases'):
|
|
66
|
+
for db in dbs:
|
|
67
|
+
query = f'DROP DATABASE {db} CASCADE'
|
|
68
|
+
if Config().is_debug():
|
|
69
|
+
log2(query)
|
|
70
|
+
Athena.query(query)
|
|
71
|
+
|
|
72
|
+
with ing(f'Deleting s3 folder: export'):
|
|
73
|
+
try:
|
|
74
|
+
if not db:
|
|
75
|
+
db = ''
|
|
76
|
+
|
|
77
|
+
s3 = boto3.resource('s3')
|
|
78
|
+
bucket = s3.Bucket(Config().get('export.bucket', 'c3.ops--qing'))
|
|
79
|
+
bucket.objects.filter(Prefix=f'export/{db}').delete()
|
|
80
|
+
except:
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
return dbs
|
|
84
|
+
|
|
85
|
+
def display_export_db(export_session: str):
|
|
86
|
+
if not export_session:
|
|
87
|
+
return
|
|
88
|
+
|
|
89
|
+
Athena.clear_cache()
|
|
90
|
+
|
|
91
|
+
keyspaces = {}
|
|
92
|
+
for table in ExportDatabases.table_names(export_session):
|
|
93
|
+
keyspace = table.split('.')[0]
|
|
94
|
+
if keyspace in keyspaces:
|
|
95
|
+
keyspaces[keyspace] += 1
|
|
96
|
+
else:
|
|
97
|
+
keyspaces[keyspace] = 1
|
|
98
|
+
|
|
99
|
+
log(lines_to_tabular([f'{k},{v}' for k, v in keyspaces.items()], header='SCHEMA,# of TABLES', separator=','))
|
|
100
|
+
|
|
101
|
+
def disply_export_session(sts: str, pod: str, namespace: str, session: str):
|
|
102
|
+
if not pod:
|
|
103
|
+
pod = StatefulSets.pod_names(sts, namespace)[0]
|
|
104
|
+
|
|
105
|
+
if not pod:
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
tables, _ = ExportTableStatus.from_session(sts, pod, namespace, session)
|
|
109
|
+
log()
|
|
110
|
+
log(lines_to_tabular([f'{table.keyspace}\t{table.table}\t{table.target_table}\t{"export_completed_pending_import" if table.status == "pending_import" else table.status}' for table in tables], header='KEYSPACE\tTABLE\tTARGET_TABLE\tSTATUS', separator='\t'))
|
|
111
|
+
|
|
112
|
+
def database_names():
|
|
113
|
+
return ExportDatabases.copy_database_names() + ExportDatabases.export_database_names()
|
|
114
|
+
|
|
115
|
+
def copy_database_names():
|
|
116
|
+
return list({n.split('_')[0] for n in SQLite.database_names()})
|
|
117
|
+
|
|
118
|
+
def export_database_names():
|
|
119
|
+
return list({n.split('_')[0] for n in Athena.database_names(LIKE)})
|
|
120
|
+
|
|
121
|
+
def database_names_with_keyspace_cnt(importer: str = None):
|
|
122
|
+
r = {}
|
|
123
|
+
|
|
124
|
+
names = []
|
|
125
|
+
if not importer:
|
|
126
|
+
names = SQLite.database_names() + Athena.database_names(LIKE)
|
|
127
|
+
elif importer == 'athena':
|
|
128
|
+
names = Athena.database_names(LIKE)
|
|
129
|
+
else:
|
|
130
|
+
names = SQLite.database_names()
|
|
131
|
+
|
|
132
|
+
for n in names:
|
|
133
|
+
tokens = n.split('_')
|
|
134
|
+
name = tokens[0]
|
|
135
|
+
keyspace = None
|
|
136
|
+
if len(tokens) > 1:
|
|
137
|
+
keyspace = tokens[1].replace('.db', '')
|
|
138
|
+
|
|
139
|
+
if keyspace == 'root':
|
|
140
|
+
continue
|
|
141
|
+
|
|
142
|
+
if name in r:
|
|
143
|
+
r[name] += 1
|
|
144
|
+
else:
|
|
145
|
+
r[name] = 1
|
|
146
|
+
|
|
147
|
+
return r
|
|
148
|
+
|
|
149
|
+
def table_names(session: str):
|
|
150
|
+
tables = []
|
|
151
|
+
|
|
152
|
+
for session in ExportDatabases._session_database_names(session):
|
|
153
|
+
if session.startswith('s'):
|
|
154
|
+
for table in SQLite.table_names(database=session):
|
|
155
|
+
tables.append(f'{SQLite.keyspace(session)}.{table}')
|
|
156
|
+
else:
|
|
157
|
+
for table in Athena.table_names(database=session, function='export'):
|
|
158
|
+
tables.append(f'{session}.{table}')
|
|
159
|
+
|
|
160
|
+
return tables
|
|
161
|
+
|
|
162
|
+
def _session_database_names(db: str):
|
|
163
|
+
eprefix = db
|
|
164
|
+
if '_' in db:
|
|
165
|
+
eprefix = db.split('_')[0]
|
|
166
|
+
|
|
167
|
+
if db.startswith('s'):
|
|
168
|
+
return SQLite.database_names(prefix=f'{eprefix}_')
|
|
169
|
+
else:
|
|
170
|
+
return Athena.database_names(like=f'{eprefix}_%')
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.export.export_databases import ExportDatabases
|
|
3
|
+
from adam.repl_state import ReplState, RequiredState
|
|
4
|
+
from adam.sql.sql_completer import SqlCompleter, SqlVariant
|
|
5
|
+
from adam.utils import log2
|
|
6
|
+
from adam.utils_athena import Athena
|
|
7
|
+
from adam.utils_sqlite import SQLite
|
|
8
|
+
|
|
9
|
+
class ExportSelect(Command):
|
|
10
|
+
COMMAND = '.select'
|
|
11
|
+
|
|
12
|
+
# the singleton pattern
|
|
13
|
+
def __new__(cls, *args, **kwargs):
|
|
14
|
+
if not hasattr(cls, 'instance'): cls.instance = super(ExportSelect, cls).__new__(cls)
|
|
15
|
+
|
|
16
|
+
return cls.instance
|
|
17
|
+
|
|
18
|
+
def __init__(self, successor: Command=None):
|
|
19
|
+
super().__init__(successor)
|
|
20
|
+
|
|
21
|
+
def command(self):
|
|
22
|
+
return ExportSelect.COMMAND
|
|
23
|
+
|
|
24
|
+
def required(self):
|
|
25
|
+
return RequiredState.EXPORT_DB
|
|
26
|
+
|
|
27
|
+
def run(self, cmd: str, state: ReplState):
|
|
28
|
+
if not(args := self.args(cmd)):
|
|
29
|
+
return super().run(cmd, state)
|
|
30
|
+
|
|
31
|
+
state, args = self.apply_state(args, state)
|
|
32
|
+
if not self.validate_state(state):
|
|
33
|
+
return state
|
|
34
|
+
|
|
35
|
+
if not state.export_session:
|
|
36
|
+
if state.in_repl:
|
|
37
|
+
if state.device == ReplState.C:
|
|
38
|
+
log2("Select an export database first with 'use' command.")
|
|
39
|
+
else:
|
|
40
|
+
log2('cd to an export database first.')
|
|
41
|
+
else:
|
|
42
|
+
log2('* export database is missing.')
|
|
43
|
+
|
|
44
|
+
Command.display_help()
|
|
45
|
+
|
|
46
|
+
return 'command-missing'
|
|
47
|
+
|
|
48
|
+
if not args:
|
|
49
|
+
if state.in_repl:
|
|
50
|
+
log2('Use a SQL statement.')
|
|
51
|
+
else:
|
|
52
|
+
log2('* SQL statement is missing.')
|
|
53
|
+
|
|
54
|
+
Command.display_help()
|
|
55
|
+
|
|
56
|
+
return 'command-missing'
|
|
57
|
+
|
|
58
|
+
query = ' '.join(args)
|
|
59
|
+
|
|
60
|
+
ExportDatabases.run_query(f'select {query}', database=state.export_session)
|
|
61
|
+
|
|
62
|
+
return state
|
|
63
|
+
|
|
64
|
+
def completion(self, state: ReplState):
|
|
65
|
+
if not state.export_session:
|
|
66
|
+
return {}
|
|
67
|
+
|
|
68
|
+
db = state.export_session
|
|
69
|
+
|
|
70
|
+
# warm up the caches first time when x: drive is accessed
|
|
71
|
+
ExportDatabases.table_names(db)
|
|
72
|
+
Athena.column_names(database=db, function='export')
|
|
73
|
+
Athena.column_names(partition_cols_only=True, database=db, function='export')
|
|
74
|
+
|
|
75
|
+
return {ExportSelect.COMMAND: SqlCompleter(
|
|
76
|
+
lambda: ExportDatabases.table_names(db),
|
|
77
|
+
dml='select',
|
|
78
|
+
expandables={
|
|
79
|
+
'columns':lambda table: Athena.column_names(database=db, function='export'),
|
|
80
|
+
},
|
|
81
|
+
variant=SqlVariant.ATHENA
|
|
82
|
+
)}
|
|
83
|
+
|
|
84
|
+
def help(self, _: ReplState):
|
|
85
|
+
return f'.<sql-select-statements>\t run queries on export database'
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.export.export_databases import ExportDatabases
|
|
3
|
+
from adam.repl_state import ReplState, RequiredState
|
|
4
|
+
from adam.sql.sql_completer import SqlCompleter, SqlVariant
|
|
5
|
+
from adam.utils_athena import Athena
|
|
6
|
+
|
|
7
|
+
# No action body, only for a help entry and auto-completion
|
|
8
|
+
class ExportSelectX(Command):
|
|
9
|
+
COMMAND = 'select_on_x'
|
|
10
|
+
|
|
11
|
+
# the singleton pattern
|
|
12
|
+
def __new__(cls, *args, **kwargs):
|
|
13
|
+
if not hasattr(cls, 'instance'): cls.instance = super(ExportSelectX, cls).__new__(cls)
|
|
14
|
+
|
|
15
|
+
return cls.instance
|
|
16
|
+
|
|
17
|
+
def __init__(self, successor: Command=None):
|
|
18
|
+
super().__init__(successor)
|
|
19
|
+
|
|
20
|
+
def command(self):
|
|
21
|
+
return ExportSelectX.COMMAND
|
|
22
|
+
|
|
23
|
+
def required(self):
|
|
24
|
+
return RequiredState.EXPORT_DB
|
|
25
|
+
|
|
26
|
+
def completion(self, state: ReplState):
|
|
27
|
+
completions = {}
|
|
28
|
+
|
|
29
|
+
if state.device == ReplState.X:
|
|
30
|
+
completions = {'drop': SqlCompleter(
|
|
31
|
+
lambda: ExportDatabases.table_names(state.export_session),
|
|
32
|
+
dml='drop',
|
|
33
|
+
expandables={
|
|
34
|
+
'export-dbs': lambda: ExportDatabases.database_names(),
|
|
35
|
+
'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
|
|
36
|
+
},
|
|
37
|
+
variant=SqlVariant.ATHENA
|
|
38
|
+
)}
|
|
39
|
+
|
|
40
|
+
if state.export_session:
|
|
41
|
+
completions |= {'select': SqlCompleter(
|
|
42
|
+
lambda: ExportDatabases.table_names(state.export_session),
|
|
43
|
+
dml='select',
|
|
44
|
+
expandables={
|
|
45
|
+
'export-dbs': lambda: ExportDatabases.database_names(),
|
|
46
|
+
'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
|
|
47
|
+
},
|
|
48
|
+
variant=SqlVariant.ATHENA
|
|
49
|
+
)}
|
|
50
|
+
|
|
51
|
+
return completions
|
|
52
|
+
|
|
53
|
+
def help(self, _: ReplState):
|
|
54
|
+
return f'<sql-select-statements>\t run queries on export database'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from adam.commands.command import Command
|
|
2
|
+
from adam.commands.export.export_databases import ExportDatabases
|
|
3
|
+
from adam.repl_state import ReplState
|
|
4
|
+
from adam.utils import log2
|
|
5
|
+
from adam.utils_athena import Athena
|
|
6
|
+
from adam.utils_sqlite import SQLite
|
|
7
|
+
|
|
8
|
+
class ExportUse(Command):
|
|
9
|
+
COMMAND = 'use'
|
|
10
|
+
|
|
11
|
+
# the singleton pattern
|
|
12
|
+
def __new__(cls, *args, **kwargs):
|
|
13
|
+
if not hasattr(cls, 'instance'): cls.instance = super(ExportUse, cls).__new__(cls)
|
|
14
|
+
|
|
15
|
+
return cls.instance
|
|
16
|
+
|
|
17
|
+
def __init__(self, successor: Command=None):
|
|
18
|
+
super().__init__(successor)
|
|
19
|
+
|
|
20
|
+
def command(self):
|
|
21
|
+
return ExportUse.COMMAND
|
|
22
|
+
|
|
23
|
+
def required(self):
|
|
24
|
+
return [ReplState.C, ReplState.X]
|
|
25
|
+
|
|
26
|
+
def run(self, cmd: str, state: ReplState):
|
|
27
|
+
if not(args := self.args(cmd)):
|
|
28
|
+
return super().run(cmd, state)
|
|
29
|
+
|
|
30
|
+
state, args = self.apply_state(args, state)
|
|
31
|
+
if not self.validate_state(state):
|
|
32
|
+
return state
|
|
33
|
+
|
|
34
|
+
if not args:
|
|
35
|
+
state.export_session = None
|
|
36
|
+
|
|
37
|
+
log2('Export database is unset.')
|
|
38
|
+
|
|
39
|
+
return state
|
|
40
|
+
|
|
41
|
+
state.export_session = args[0]
|
|
42
|
+
if state.export_session.startswith('e'):
|
|
43
|
+
Athena.clear_cache()
|
|
44
|
+
else:
|
|
45
|
+
SQLite.clear_cache()
|
|
46
|
+
|
|
47
|
+
ExportDatabases.display_export_db(state.export_session)
|
|
48
|
+
|
|
49
|
+
return state
|
|
50
|
+
|
|
51
|
+
def completion(self, state: ReplState):
|
|
52
|
+
return super().completion(state, {n: None for n in ExportDatabases.database_names()})
|
|
53
|
+
|
|
54
|
+
def help(self, _: ReplState):
|
|
55
|
+
return f'{ExportUse.COMMAND} <export-database-name>\t use export database'
|