From b7fcafcaf1af22ce8023de899bc89cb96ea69ac2 Mon Sep 17 00:00:00 2001 From: Akhil Narang Date: Wed, 18 Sep 2024 16:30:52 +0530 Subject: [PATCH] chore: add back describe-database-table command Partial revert of b169f8780ab543a21ee091c1d105c10d2c22f076 Signed-off-by: Akhil Narang --- frappe/commands/site.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 00a6a153fa..605a528374 100644 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -590,6 +590,44 @@ def add_db_index(context: CliCtxObj, doctype, column): raise SiteNotSpecifiedError +@click.command("describe-database-table") +@click.option("--doctype", help="DocType to describe") +@click.option( + "--column", + multiple=True, + help="Explicitly fetch accurate cardinality from table data. This can be quite slow on large tables.", +) +@pass_context +def describe_database_table(context, doctype, column): + """Describes various statistics about the table. + This is useful to build integration like + This includes: + 1. Schema + 2. Indexes + 3. stats - total count of records + 4. if column is specified then extra stats are generated for column: + Distinct values count in column + """ + if doctype is None: + raise click.UsageError("--doctype is required") + import json + + from frappe.core.doctype.recorder.recorder import _fetch_table_stats + + for site in context.sites: + frappe.init(site=site) + frappe.connect() + try: + data = _fetch_table_stats(doctype, column) + # NOTE: Do not print anything else in this to avoid clobbering the output. + print(json.dumps(data, indent=2)) + finally: + frappe.destroy() + + if not context.sites: + raise SiteNotSpecifiedError + + @click.command("add-system-manager") @click.argument("email") @click.option("--first-name") @@ -660,7 +698,6 @@ def disable_user(context: CliCtxObj, email): @pass_context def migrate(context: CliCtxObj, skip_failing=False, skip_search_index=False): "Run patches, sync schema and rebuild files/translations" - from traceback_with_variables import activate_by_import from frappe.migrate import SiteMigration @@ -1541,6 +1578,7 @@ commands = [ add_system_manager, add_user_for_sites, add_db_index, + describe_database_table, backup, drop_site, install_app,