From 0413349cc8d7af44b8355ff2564eeb46e9dfc849 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 14 Sep 2021 21:53:02 +0530 Subject: [PATCH] fix: Table view for bench trim-tables --- frappe/commands/site.py | 15 ++++++++++----- frappe/model/meta.py | 6 +++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 055a407f87..4b87118539 100755 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -813,7 +813,7 @@ def get_standard_tables(): @click.command('trim-tables') @click.option('--dry-run', is_flag=True, default=False, help='Show what would be deleted') -@click.option('--format', default='table', type=click.Choice(['json', 'table']), help='Output format') +@click.option('--format', '-f', default='table', type=click.Choice(['json', 'table']), help='Output format') @click.option('--no-backup', is_flag=True, default=False, help='Do not backup the site') @pass_context def trim_tables(context, dry_run, format, no_backup): @@ -833,7 +833,11 @@ def trim_tables(context, dry_run, format, no_backup): odb.print_summary() try: - trimmed_data = trim_tables(dry_run=dry_run) + trimmed_data = trim_tables(dry_run=dry_run, quiet=format == 'json') + + if format == 'table' and not dry_run: + click.secho(f"The following data have been removed from {frappe.local.site}", fg='green') + handle_data(trimmed_data, format=format) finally: frappe.destroy() @@ -843,9 +847,10 @@ def handle_data(data: dict, format='json'): import json print(json.dumps({frappe.local.site: data}, indent=1, sort_keys=True)) else: - click.secho(f"Site {frappe.local.site}", fg='green') - for table, columns in data.items(): - print(f"{table}: {', '.join(columns)}") + from frappe.utils.commands import render_table + data = [["DocType", "Fields"]] + [[table, ", ".join(columns)] for table, columns in data.items()] + render_table(data) + commands = [ add_system_manager, diff --git a/frappe/model/meta.py b/frappe/model/meta.py index 2c92d2debe..cd0d8e0f3a 100644 --- a/frappe/model/meta.py +++ b/frappe/model/meta.py @@ -659,7 +659,7 @@ def get_default_df(fieldname): fieldtype = "Data" ) -def trim_tables(doctype=None, dry_run=False): +def trim_tables(doctype=None, dry_run=False, quiet=False): """ Removes database fields that don't exist in the doctype (json or custom field). This may be needed as maintenance since removing a field in a DocType doesn't automatically @@ -676,9 +676,13 @@ def trim_tables(doctype=None, dry_run=False): if dropped_columns: UPDATED_TABLES[doctype] = dropped_columns except frappe.db.TableMissingError: + if quiet: + continue click.secho(f"Ignoring missing table for DocType: {doctype}", fg="yellow", err=True) click.secho(f"Consider removing record in the DocType table for {doctype}", fg="yellow", err=True) except Exception as e: + if quiet: + continue click.echo(e, err=True) return UPDATED_TABLES