fix(postgres): drop_index_if_exists uses DROP INDEX IF EXISTS (#35636)

* fix(postgres): drop_index_if_exists uses DROP INDEX IF EXISTS

* fix(linting): apply pre-commit to code

---------

Co-authored-by: Matt Howard <github.severity519@passmail.net>
Co-authored-by: AarDG10 <aarol.dsouza@gmail.com>
This commit is contained in:
ili.ad 2026-01-05 06:15:41 -05:00 committed by GitHub
parent f71035c6ba
commit 261e78e492
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -193,7 +193,12 @@ def drop_index_if_exists(table: str, index: str):
return
try:
frappe.db.sql_ddl(f"ALTER TABLE `{table}` DROP INDEX `{index}`")
if frappe.db.db_type == "postgres":
# Postgres drops indexes with DROP INDEX, not ALTER TABLE ... DROP INDEX
safe_index = index.replace('"', '""')
frappe.db.sql_ddl(f'DROP INDEX IF EXISTS "{safe_index}"')
else:
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")