michael-s-molina commented on code in PR #33072: URL: https://github.com/apache/superset/pull/33072#discussion_r2037117513
########## superset/migrations/migration_utils.py: ########## @@ -15,21 +15,32 @@ # specific language governing permissions and limitations # under the License. +import logging + from alembic.operations import Operations 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: - with op.batch_alter_table( - table_name, naming_convention=naming_convention - ) as batch_op: - batch_op.create_unique_constraint(index_id, uix_columns) + # 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) Review Comment: @eschutho Can we check if the constraint already exists like we do for other functions? ``` def create_unique_constraint( op: Operations, index_id: str, table_name: str, uix_columns: list[str] ) -> None: # Get the database connection and inspector conn = op.get_bind() inspector = inspect(conn) # 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) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org