This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a commit to branch elizabeth/use-create-table-util
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to
refs/heads/elizabeth/use-create-table-util by this push:
new 51cda4cb4f update contstraint logic and test
51cda4cb4f is described below
commit 51cda4cb4f4ca725f28aef5565c559dc4f66907d
Author: Elizabeth Thompson <[email protected]>
AuthorDate: Thu Apr 10 13:29:29 2025 -0700
update contstraint logic and test
---
superset/migrations/migration_utils.py | 32 ++++++++++++++++--------------
tests/integration_tests/dashboard_tests.py | 2 +-
2 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/superset/migrations/migration_utils.py
b/superset/migrations/migration_utils.py
index c4f48c599e..1dc0e08383 100644
--- a/superset/migrations/migration_utils.py
+++ b/superset/migrations/migration_utils.py
@@ -15,32 +15,34 @@
# specific language governing permissions and limitations
# under the License.
-import logging
-
from alembic.operations import Operations
+from sqlalchemy.engine.reflection import Inspector
naming_convention = {
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
}
-logger = logging.getLogger(__name__)
-
def create_unique_constraint(
op: Operations, index_id: str, table_name: str, uix_columns: list[str]
) -> None:
- # Run the DDL command in an autocommit block so a failure here
- # won't abort the outer transaction.
- try:
- with op.get_context().autocommit_block():
- with op.batch_alter_table(
- table_name, naming_convention=naming_convention
- ) as batch_op:
- batch_op.create_unique_constraint(index_id, uix_columns)
- except Exception as e:
- # Likely the constraint already exists, or another DDL error occurred.
- logger.warning("Failed to create unique constraint %s: %s", index_id,
e)
+ # Get the database connection and inspector
+ bind = op.get_bind()
+ inspector = Inspector.from_engine(bind)
+
+ # Check if the unique constraint already exists
+ existing_constraints = inspector.get_unique_constraints(table_name)
+ for constraint in existing_constraints:
+ if constraint["name"] == index_id:
+ # Constraint already exists, no need to create it
+ return
+
+ # Create the unique constraint if it doesn't exist
+ with op.batch_alter_table(
+ table_name, naming_convention=naming_convention
+ ) as batch_op:
+ batch_op.create_unique_constraint(index_id, uix_columns)
def drop_unique_constraint(op: Operations, index_id: str, table_name: str) ->
None:
diff --git a/tests/integration_tests/dashboard_tests.py
b/tests/integration_tests/dashboard_tests.py
index 78218744b3..c0ab28794e 100644
--- a/tests/integration_tests/dashboard_tests.py
+++ b/tests/integration_tests/dashboard_tests.py
@@ -119,7 +119,7 @@ class TestDashboard(SupersetTestCase):
dash_count_after =
db.session.query(func.count(Dashboard.id)).first()[0]
assert dash_count_before + 1 == dash_count_after
group = re.match(
- r"\/superset\/dashboard\/([0-9]*)\/\?edit=true",
+ r"\/superset\/dashboard\/([0-9]*)\/\?edit=True",
response.headers["Location"],
)
assert group is not None