codeant-ai-for-open-source[bot] commented on code in PR #35542: URL: https://github.com/apache/superset/pull/35542#discussion_r3408404159
########## superset/migrations/versions/2025-10-06_16-05_b54f3bd8e69_update_tag_unique_constraint.py: ########## @@ -0,0 +1,116 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""update_tag_unique_constraint + +Revision ID: b54f3bd8e69 +Revises: c233f5365c9e +Create Date: 2025-10-06 16:05:00.000000 + +""" + +import enum + +import migration_utils as utils +from alembic import op +from sqlalchemy import Column, Enum, Integer, MetaData, String, Table, Text +from sqlalchemy.sql import func, select + +# revision identifiers, used by Alembic. +revision = "b54f3bd8e69" +down_revision = "c233f5365c9e" + + +class TagType(enum.Enum): + # pylint: disable=invalid-name + custom = 1 + type = 2 + owner = 3 + favorited_by = 4 + + +# Define the tag table structure for data operations +metadata = MetaData() +tag_table = Table( + "tag", + metadata, + Column("id", Integer, primary_key=True), + Column("name", String(250)), + Column("type", Enum(TagType)), + Column("description", Text), +) + +old_constraint_name = "tag_name_key" +new_constraint_name = "uix_tag_name_type" +table_name = "tag" +new_constraint_columns = ["name", "type"] + + +def upgrade(): + """ + Change tag unique constraint from name only to (name, type) composite. + This allows the same tag name to exist with different types (e.g., 'type:dashboard' + can be both a system tag with type='type' and a custom tag with type='custom'). + """ + bind = op.get_bind() + + # Reflect the current database state to get existing tables + metadata.reflect(bind=bind) + + # Delete duplicate tags if any, keeping the one with the lowest ID + min_id_subquery = ( + select( + [ + func.min(tag_table.c.id).label("min_id"), + tag_table.c.name, + tag_table.c.type, + ] + ) + .group_by( + tag_table.c.name, + tag_table.c.type, + ) + .alias("min_ids") + ) + + delete_query = tag_table.delete().where( + tag_table.c.id.notin_(select([min_id_subquery.c.min_id])) + ) + + bind.execute(delete_query) + + # Drop the old unique constraint on name only + utils.drop_unique_constraint(op, old_constraint_name, table_name) Review Comment: **Suggestion:** The migration hardcodes the old unique constraint name as `tag_name_key`, but the original `name` uniqueness was created from `unique=True` and its physical name is backend-dependent (for example MySQL commonly uses `name`). Dropping a non-existent constraint name will raise during `superset db upgrade` on those databases. Resolve the existing unique constraint name via inspector (matching column `name`) before dropping, instead of assuming a fixed identifier. [api mismatch] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ DB migrations fail on MySQL metadata backends. - ❌ Docker init scripts running `superset db upgrade` abort. - ⚠️ Tag unique constraint change never applied on affected DBs. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Initialize Superset using a MySQL metadata database and start it via the provided Docker init script `docker/docker-init.sh:46-48`, which runs `superset db upgrade` to apply Alembic migrations. 2. During `superset db upgrade`, Alembic processes migration `b54f3bd8e69_update_tag_unique_constraint` and executes `upgrade()` in `superset/migrations/versions/2025-10-06_16-05_b54f3bd8e69_update_tag_unique_constraint.py:62-101`. 3. The existing `tag` table and its unique index on `name` were originally created in migration `2018-07-26_11-10_c82ee8a39623_add_implicit_tags.py:73-81` with `name = Column(String(250), unique=True)` and no explicit constraint name, leaving the physical unique index name up to the database backend (PostgreSQL uses `tag_name_key`, MySQL typically uses the column name, e.g. `name`). 4. In `upgrade()`, the code calls `utils.drop_unique_constraint(op, old_constraint_name, table_name)` at `2025-10-06_16-05_b54f3bd8e69_update_tag_unique_constraint.py:95-96`, and `drop_unique_constraint` in `superset/migrations/migration_utils.py:48-59` blindly issues a `DROP CONSTRAINT`/`DROP INDEX` for the provided name; on MySQL, where no constraint named `tag_name_key` exists for the `tag` table, this raises a database error and causes `superset db upgrade` (and thus application startup) to fail. ``` </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=af9a34f661304a6291ba49caa55a2c31&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=af9a34f661304a6291ba49caa55a2c31&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/migrations/versions/2025-10-06_16-05_b54f3bd8e69_update_tag_unique_constraint.py **Line:** 56:96 **Comment:** *Api Mismatch: The migration hardcodes the old unique constraint name as `tag_name_key`, but the original `name` uniqueness was created from `unique=True` and its physical name is backend-dependent (for example MySQL commonly uses `name`). Dropping a non-existent constraint name will raise during `superset db upgrade` on those databases. Resolve the existing unique constraint name via inspector (matching column `name`) before dropping, instead of assuming a fixed identifier. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F35542&comment_hash=2a962011ae69c4376751c019c074a33d2bb1cc28b8c603893fb6c11cdfd14990&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F35542&comment_hash=2a962011ae69c4376751c019c074a33d2bb1cc28b8c603893fb6c11cdfd14990&reaction=dislike'>👎</a> -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
