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..72d08e71091c7 100644 --- a/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py +++ b/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py @@ -27,21 +27,24 @@ revision = "b4456560d4f3" down_revision = "bb51420eaf83" +conv = { + "uq": "uq_%(table_name)s_%(column_0_name)s", +} + def upgrade(): try: - # Trying since sqlite doesn't like constraints - op.drop_constraint("tables_table_name_key", "tables", type_="unique") - op.create_unique_constraint( - "_customer_location_uc", "tables", ["database_id", "schema", "table_name"] - ) + 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", naming_convention=conv) as batch_op: + batch_op.create_unique_constraint( + "_customer_location_uc", ["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("_customer_location_uc", "tables", type_="unique")