Browse Source

fix: Table view for bench trim-tables

version-14
Gavin D'souza 3 years ago
parent
commit
0413349cc8
2 changed files with 15 additions and 6 deletions
  1. +10
    -5
      frappe/commands/site.py
  2. +5
    -1
      frappe/model/meta.py

+ 10
- 5
frappe/commands/site.py View File

@@ -813,7 +813,7 @@ def get_standard_tables():


@click.command('trim-tables') @click.command('trim-tables')
@click.option('--dry-run', is_flag=True, default=False, help='Show what would be deleted') @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') @click.option('--no-backup', is_flag=True, default=False, help='Do not backup the site')
@pass_context @pass_context
def trim_tables(context, dry_run, format, no_backup): 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() odb.print_summary()


try: 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) handle_data(trimmed_data, format=format)
finally: finally:
frappe.destroy() frappe.destroy()
@@ -843,9 +847,10 @@ def handle_data(data: dict, format='json'):
import json import json
print(json.dumps({frappe.local.site: data}, indent=1, sort_keys=True)) print(json.dumps({frappe.local.site: data}, indent=1, sort_keys=True))
else: 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 = [ commands = [
add_system_manager, add_system_manager,


+ 5
- 1
frappe/model/meta.py View File

@@ -659,7 +659,7 @@ def get_default_df(fieldname):
fieldtype = "Data" 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 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 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: if dropped_columns:
UPDATED_TABLES[doctype] = dropped_columns UPDATED_TABLES[doctype] = dropped_columns
except frappe.db.TableMissingError: except frappe.db.TableMissingError:
if quiet:
continue
click.secho(f"Ignoring missing table for DocType: {doctype}", fg="yellow", err=True) 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) click.secho(f"Consider removing record in the DocType table for {doctype}", fg="yellow", err=True)
except Exception as e: except Exception as e:
if quiet:
continue
click.echo(e, err=True) click.echo(e, err=True)


return UPDATED_TABLES return UPDATED_TABLES


Loading…
Cancel
Save