fix: Table view for bench trim-tables

This commit is contained in:
Gavin D'souza 2021-09-14 21:53:02 +05:30
parent cb6010f1cf
commit 0413349cc8
2 changed files with 15 additions and 6 deletions

View file

@ -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,

View file

@ -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