feat: Adds helper functions for migrations (#31303)

This commit is contained in:
Luiz Otavio
2024-12-11 10:50:56 -03:00
committed by GitHub
parent fd57fce977
commit 423a0fefa5
13 changed files with 234 additions and 70 deletions

View File

@@ -19,10 +19,8 @@ from __future__ import annotations
from dataclasses import dataclass
from alembic import op
from sqlalchemy.dialects.sqlite.base import SQLiteDialect # noqa: E402
from sqlalchemy.engine.reflection import Inspector
from superset.migrations.shared.utils import has_table
from superset.utils.core import generic_find_fk_constraint_name
@@ -73,23 +71,3 @@ def redefine(
ondelete=on_delete,
onupdate=on_update,
)
def drop_fks_for_table(table_name: str) -> None:
"""
Drop all foreign key constraints for a table if it exist and the database
is not sqlite.
:param table_name: The table name to drop foreign key constraints for
"""
connection = op.get_bind()
inspector = Inspector.from_engine(connection)
if isinstance(connection.dialect, SQLiteDialect):
return # sqlite doesn't like constraints
if has_table(table_name):
foreign_keys = inspector.get_foreign_keys(table_name)
for fk in foreign_keys:
op.drop_constraint(fk["name"], table_name, type_="foreignkey")