From 52377591934976287a16765b83b15759e4456aba Mon Sep 17 00:00:00 2001 From: Alexander Burmak Date: Wed, 27 Sep 2023 17:33:47 +0300 Subject: [PATCH] Fixed handling of --on-cluster option in "table delete" and "database delete" commands --- ch_tools/chadmin/cli/database_group.py | 15 +++++++++++++-- ch_tools/chadmin/cli/table_group.py | 21 +++++++++++++++++++-- ch_tools/chadmin/internal/table.py | 7 ++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ch_tools/chadmin/cli/database_group.py b/ch_tools/chadmin/cli/database_group.py index b22c4f77..5adb4d52 100644 --- a/ch_tools/chadmin/cli/database_group.py +++ b/ch_tools/chadmin/cli/database_group.py @@ -1,5 +1,6 @@ from click import argument, group, option, pass_context +from ch_tools.chadmin.cli import get_cluster_name from ch_tools.chadmin.internal.utils import execute_query @@ -47,7 +48,13 @@ def list_databases_command(ctx, **kwargs): @option("-d", "--database") @option("--exclude-database") @option("-a", "--all", "all_", is_flag=True, help="Delete all databases.") -@option("--cluster") +@option( + "--cluster", + "--on-cluster", + "on_cluster", + is_flag=True, + help="Delete databases on all hosts of the cluster.", +) @option( "-n", "--dry-run", @@ -55,10 +62,14 @@ def list_databases_command(ctx, **kwargs): default=False, help="Enable dry run mode and do not perform any modifying actions.", ) -def delete_databases_command(ctx, dry_run, all_, database, exclude_database, cluster): +def delete_databases_command( + ctx, dry_run, all_, database, exclude_database, on_cluster +): if not any((all_, database)): ctx.fail("At least one of --all, --database options must be specified.") + cluster = get_cluster_name(ctx) if on_cluster else None + for d in get_databases( ctx, database=database, exclude_database=exclude_database, format_="JSON" )["data"]: diff --git a/ch_tools/chadmin/cli/table_group.py b/ch_tools/chadmin/cli/table_group.py index 639be479..7d684b2d 100644 --- a/ch_tools/chadmin/cli/table_group.py +++ b/ch_tools/chadmin/cli/table_group.py @@ -120,7 +120,19 @@ def columns_command(ctx, database, table): "--exclude-table", help="Filter out tables to delete by the specified table name." ) @option("-a", "--all", "all_", is_flag=True, help="Delete all tables.") -@option("--cluster") +@option( + "--cluster", + "--on-cluster", + "on_cluster", + is_flag=True, + help="Delete tables on all hosts of the cluster.", +) +@option( + "--sync/--async", + "sync_mode", + default=True, + help="Enable/Disable synchronous query execution.", +) @option( "-n", "--dry-run", @@ -128,7 +140,9 @@ def columns_command(ctx, database, table): default=False, help="Enable dry run mode and do not perform any modifying actions.", ) -def delete_command(ctx, dry_run, all_, database, table, exclude_table, cluster): +def delete_command( + ctx, all_, database, table, exclude_table, on_cluster, sync_mode, dry_run +): """ Delete one or several tables. """ @@ -137,6 +151,8 @@ def delete_command(ctx, dry_run, all_, database, table, exclude_table, cluster): "At least one of --all, --database, --table options must be specified." ) + cluster = get_cluster_name(ctx) if on_cluster else None + for t in list_tables( ctx, database=database, table=table, exclude_table=exclude_table ): @@ -145,6 +161,7 @@ def delete_command(ctx, dry_run, all_, database, table, exclude_table, cluster): database=t["database"], table=t["table"], cluster=cluster, + sync_mode=sync_mode, echo=True, dry_run=dry_run, ) diff --git a/ch_tools/chadmin/internal/table.py b/ch_tools/chadmin/internal/table.py index 86567fd2..b38b6b77 100644 --- a/ch_tools/chadmin/internal/table.py +++ b/ch_tools/chadmin/internal/table.py @@ -149,7 +149,9 @@ def attach_table(ctx, database, table, *, cluster=None, echo=False, dry_run=Fals ) -def delete_table(ctx, database, table, *, cluster=None, echo=False, dry_run=False): +def delete_table( + ctx, database, table, *, cluster=None, echo=False, sync_mode=True, dry_run=False +): """ Perform "DROP TABLE" for the specified table. """ @@ -158,7 +160,9 @@ def delete_table(ctx, database, table, *, cluster=None, echo=False, dry_run=Fals {%- if cluster %} ON CLUSTER '{{ cluster }}' {%- endif %} + {%- if sync_mode %} NO DELAY + {%- endif %} """ execute_query( ctx, @@ -166,6 +170,7 @@ def delete_table(ctx, database, table, *, cluster=None, echo=False, dry_run=Fals database=database, table=table, cluster=cluster, + sync_mode=sync_mode, echo=echo, dry_run=dry_run, format_=None,