From 5f51fc5815e9621f963f28b23150d30606cb820d Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Thu, 21 May 2020 20:55:17 -0700 Subject: [PATCH 1/3] fix: old unique constraint remnance --- superset/connectors/sqla/models.py | 6 +++++- superset/migrations/versions/4e6a06bad7a8_init.py | 4 +++- .../b4456560d4f3_change_table_unique_constraint.py | 14 +++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index ad45474c33b35..84b2ffe0104c7 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -373,7 +373,11 @@ class SqlaTable(Model, BaseDatasource): owner_class = security_manager.user_model __tablename__ = "tables" - __table_args__ = (UniqueConstraint("database_id", "table_name"),) + __table_args__ = ( + UniqueConstraint( + "database_id", "schema", "table_name", name="_customer_location_uc", + ), + ) table_name = Column(String(250), nullable=False) main_dttm_col = Column(String(250)) diff --git a/superset/migrations/versions/4e6a06bad7a8_init.py b/superset/migrations/versions/4e6a06bad7a8_init.py index 31d46b8798ff3..a6956805433ca 100644 --- a/superset/migrations/versions/4e6a06bad7a8_init.py +++ b/superset/migrations/versions/4e6a06bad7a8_init.py @@ -127,7 +127,9 @@ def upgrade(): "changed_by_fk", sa.Integer(), sa.ForeignKey("ab_user.id"), nullable=True ), sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("table_name"), + # Commenting out spring 2020 to avoid flimsy delete in + # subsequent migration b4456560d4f3_change_table_unique_constraint.py + # sa.UniqueConstraint("table_name"), ) op.create_table( "columns", diff --git a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py index d22f72cf03c7d..e4abf43bd475b 100644 --- a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py +++ b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py @@ -30,18 +30,14 @@ def upgrade(): try: - # Trying since sqlite doesn't like constraints + # Trying to drop the constraint if it exists op.drop_constraint("tables_table_name_key", "tables", type_="unique") - op.create_unique_constraint( - "_customer_location_uc", "tables", ["database_id", "schema", "table_name"] - ) except Exception: pass + op.create_unique_constraint( + "_customer_location_uc", "tables", ["database_id", "schema", "table_name"] + ) def downgrade(): - try: - # Trying since sqlite doesn't like constraints - op.drop_constraint(u"_customer_location_uc", "tables", type_="unique") - except Exception: - pass + op.drop_constraint(u"_customer_location_uc", "tables", type_="unique") From 19df6eab4b2f14339a6e625a4a4fcf683a4f4595 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Thu, 21 May 2020 21:48:45 -0700 Subject: [PATCH 2/3] Using batch_op --- .../b4456560d4f3_change_table_unique_constraint.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py index e4abf43bd475b..df61ca540e181 100644 --- a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py +++ b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py @@ -30,13 +30,15 @@ def upgrade(): try: - # Trying to drop the constraint if it exists - op.drop_constraint("tables_table_name_key", "tables", type_="unique") + with op.batch_alter_table("tables") as batch_op: + # Trying to drop the constraint if it exists + batch_op.drop_constraint("tables_table_name_key", type_="unique") except Exception: pass - op.create_unique_constraint( - "_customer_location_uc", "tables", ["database_id", "schema", "table_name"] - ) + with op.batch_alter_table("tables") as batch_op: + batch_op.create_unique_constraint( + "_customer_location_uc", ["database_id", "schema", "table_name"], + ) def downgrade(): From 69833db2a65a3a43abe56601a90d75c12f8f8433 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Thu, 21 May 2020 22:34:29 -0700 Subject: [PATCH 3/3] Wrap in transaciton --- ...b4456560d4f3_change_table_unique_constraint.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py index df61ca540e181..72d08e71091c7 100644 --- a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py +++ b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py @@ -27,19 +27,24 @@ revision = "b4456560d4f3" down_revision = "bb51420eaf83" +conv = { + "uq": "uq_%(table_name)s_%(column_0_name)s", +} + def upgrade(): try: - with op.batch_alter_table("tables") as batch_op: - # Trying to drop the constraint if it exists - batch_op.drop_constraint("tables_table_name_key", type_="unique") + with op.get_context().autocommit_block(): + with op.batch_alter_table("tables", naming_convention=conv) as batch_op: + # Trying to drop the constraint if it exists + batch_op.drop_constraint("tables_table_name_key", type_="unique") except Exception: pass - with op.batch_alter_table("tables") as batch_op: + with op.batch_alter_table("tables", naming_convention=conv) as batch_op: batch_op.create_unique_constraint( "_customer_location_uc", ["database_id", "schema", "table_name"], ) def downgrade(): - op.drop_constraint(u"_customer_location_uc", "tables", type_="unique") + op.drop_constraint("_customer_location_uc", "tables", type_="unique")