Skip to content

Commit

Permalink
fix: migration script can't drop constraint (apache#15807)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored Jul 21, 2021
1 parent 5cc95bb commit 6cb91ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions scripts/benchmark_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from flask_migrate import downgrade, upgrade
from graphlib import TopologicalSorter # pylint: disable=wrong-import-order
from progress.bar import ChargingBar
from sqlalchemy import create_engine, inspect, Table
from sqlalchemy import create_engine, inspect
from sqlalchemy.ext.automap import automap_base

from superset import db
Expand Down Expand Up @@ -172,7 +172,6 @@ def main(
rows = session.query(model).count()
print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})")
model_rows[model] = rows
session.close()

print("Benchmarking migration")
results: Dict[str, float] = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.exc import OperationalError


Expand Down Expand Up @@ -119,13 +120,17 @@ def upgrade():
)


def has_unique_constraint(constraint_name: str, table_name: str) -> bool:
bind = op.get_bind()
inspector = Inspector.from_engine(bind)
unique_constraints = inspector.get_unique_constraints(table_name)
return constraint_name in {constraint["name"] for constraint in unique_constraints}


def downgrade():
op.drop_index(op.f("ix_report_schedule_active"), table_name="report_schedule")
try:
if has_unique_constraint("uq_report_schedule_name", "report_schedule"):
op.drop_constraint("uq_report_schedule_name", "report_schedule", type_="unique")
except Exception:
# Expected to fail on SQLite
pass

op.drop_table("report_execution_log")
op.drop_table("report_recipient")
Expand Down

0 comments on commit 6cb91ee

Please sign in to comment.