refactor: move drop index util to db utils file
This commit is contained in:
parent
01d2e24f4a
commit
de7d1abcf4
4 changed files with 20 additions and 19 deletions
|
|
@ -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():
|
||||
|
|
|
|||
|
|
@ -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}")
|
||||
|
|
|
|||
|
|
@ -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}")
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue