Skip to content

Commit

Permalink
Fixed handling of --on-cluster option in "table delete" and "database…
Browse files Browse the repository at this point in the history
… delete" commands
  • Loading branch information
Alex-Burmak committed Sep 27, 2023
1 parent 4549510 commit 5237759
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
15 changes: 13 additions & 2 deletions ch_tools/chadmin/cli/database_group.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -47,18 +48,28 @@ 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",
is_flag=True,
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"]:
Expand Down
21 changes: 19 additions & 2 deletions ch_tools/chadmin/cli/table_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,29 @@ 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",
is_flag=True,
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.
"""
Expand All @@ -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
):
Expand All @@ -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,
)
Expand Down
7 changes: 6 additions & 1 deletion ch_tools/chadmin/internal/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -158,14 +160,17 @@ 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,
query,
database=database,
table=table,
cluster=cluster,
sync_mode=sync_mode,
echo=echo,
dry_run=dry_run,
format_=None,
Expand Down

0 comments on commit 5237759

Please sign in to comment.