diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 01b3be9590..97319c5e33 100644 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -1279,34 +1279,36 @@ def trim_database(context, dry_run, format, no_backup, yes=False): information_schema = frappe.qb.Schema("information_schema") table_name = frappe.qb.Field("table_name").as_("name") - queried_result = ( + database_tables: list[str] = ( frappe.qb.from_(information_schema.tables) .select(table_name) .where(information_schema.tables.table_schema == frappe.conf.db_name) .where(information_schema.tables.table_type == "BASE TABLE") - .run() + .run(pluck=True) ) - - database_tables = [x[0] for x in queried_result] doctype_tables = frappe.get_all("DocType", pluck="name") - for x in database_tables: - if not x.startswith("tab"): + for table_name in database_tables: + if not table_name.startswith("tab"): continue - doctype = x.replace("tab", "", 1) - if not (doctype in doctype_tables or x.startswith("__") or x in STANDARD_TABLES): - TABLES_TO_DROP.append(x) + if not (table_name.replace("tab", "", 1) in doctype_tables or table_name in STANDARD_TABLES): + TABLES_TO_DROP.append(table_name) if not TABLES_TO_DROP: if format == "text": - click.secho(f"No ghost tables found in {frappe.local.site}...Great!", fg="green") + click.secho(f"{site}: No ghost tables", fg="green") else: - if not yes: - print("Following tables will be dropped:") + if format == "text": + print(f"{site}: Following tables will be dropped:") print("\n".join(f"* {dt}" for dt in TABLES_TO_DROP)) + + if dry_run: + continue + + if not yes: click.confirm("Do you want to continue?", abort=True) - if not (no_backup or dry_run): + if not no_backup: if format == "text": print(f"Backing Up Tables: {', '.join(TABLES_TO_DROP)}") @@ -1323,8 +1325,7 @@ def trim_database(context, dry_run, format, no_backup, yes=False): for table in TABLES_TO_DROP: if format == "text": print(f"* Dropping Table '{table}'...") - if not dry_run: - frappe.db.sql_ddl(f"drop table `{table}`") + frappe.db.sql_ddl(f"drop table `{table}`") ALL_DATA[frappe.local.site] = TABLES_TO_DROP frappe.destroy()