diff --git a/frappe/core/doctype/communication/patches/drop_ref_dt_dn_index.py b/frappe/core/doctype/communication/patches/drop_ref_dt_dn_index.py index c1c3fa995b..2afe5e9f7c 100644 --- a/frappe/core/doctype/communication/patches/drop_ref_dt_dn_index.py +++ b/frappe/core/doctype/communication/patches/drop_ref_dt_dn_index.py @@ -1,5 +1,5 @@ import frappe -from frappe.patches.v14_0.drop_unused_indexes import drop_index_if_exists +from frappe.database.utils import drop_index_if_exists def execute(): diff --git a/frappe/database/utils.py b/frappe/database/utils.py index 0ceb9bd440..3165046faf 100644 --- a/frappe/database/utils.py +++ b/frappe/database/utils.py @@ -183,3 +183,20 @@ def commit_after_response(func): request.after_response.add(callback_manager.run) callback_manager.add(func) + + +def drop_index_if_exists(table: str, index: str): + import click + + if not frappe.db.has_index(table, index): + click.echo(f"- Skipped {index} index for {table} because it doesn't exist") + return + + try: + frappe.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`") + except Exception as e: + frappe.log_error("Failed to drop index") + click.secho(f"x Failed to drop index {index} from {table}\n {e!s}", fg="red") + return + + click.echo(f"✓ dropped {index} index from {table}") diff --git a/frappe/patches/v14_0/drop_unused_indexes.py b/frappe/patches/v14_0/drop_unused_indexes.py index a2b3f468c2..711ba39f15 100644 --- a/frappe/patches/v14_0/drop_unused_indexes.py +++ b/frappe/patches/v14_0/drop_unused_indexes.py @@ -2,9 +2,8 @@ This patch just drops some known indexes which aren't being used anymore or never were used. """ -import click - import frappe +from frappe.database.utils import drop_index_if_exists UNUSED_INDEXES = [ ("Comment", ["link_doctype", "link_name"]), @@ -39,18 +38,3 @@ def execute(): if table not in db_tables: continue drop_index_if_exists(table, index_name) - - -def drop_index_if_exists(table: str, index: str): - if not frappe.db.has_index(table, index): - click.echo(f"- Skipped {index} index for {table} because it doesn't exist") - return - - try: - frappe.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`") - except Exception as e: - frappe.log_error("Failed to drop index") - click.secho(f"x Failed to drop index {index} from {table}\n {e!s}", fg="red") - return - - click.echo(f"✓ dropped {index} index from {table}") diff --git a/frappe/patches/v15_0/drop_modified_index.py b/frappe/patches/v15_0/drop_modified_index.py index 8cdcf12ece..e8e3590896 100644 --- a/frappe/patches/v15_0/drop_modified_index.py +++ b/frappe/patches/v15_0/drop_modified_index.py @@ -1,5 +1,5 @@ import frappe -from frappe.patches.v14_0.drop_unused_indexes import drop_index_if_exists +from frappe.database.utils import drop_index_if_exists def execute():