codeant-ai-for-open-source[bot] commented on code in PR #35542:
URL: https://github.com/apache/superset/pull/35542#discussion_r3408401991


##########
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:
   **🔴 Architect Review — CRITICAL**
   
   The migration hardcodes the old unique constraint name as "tag_name_key", 
but the original tag table was created in migration c82ee8a39623 via 
Column(..., unique=True) on a bare declarative_base() (no naming convention), 
so the constraint name is dialect-dependent; on non-Postgres backends the 
unique index will not be called "tag_name_key", and 
utils.drop_unique_constraint(op, "tag_name_key", "tag") will emit a DROP 
CONSTRAINT for a non-existent constraint and fail.
   
   **Suggestion:** Before dropping, inspect existing unique constraints on the 
"tag" table and drop the one whose column set is ["name"] (using Inspector or 
by extending migration_utils) instead of assuming the name "tag_name_key", so 
the migration works across all supported dialects (Postgres, MySQL, etc.).
   
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=7c440293eaf04f67a817c53f6d37c960&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=7c440293eaf04f67a817c53f6d37c960&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 an **Architect / Logical Review** comment left during a code review. 
These reviews are first-class, important findings — not optional suggestions. 
Do NOT dismiss this as a 'big architectural change' just because the title says 
architect review; most of these can be resolved with a small, localized fix 
once the intent is understood.
   
   **Path:** 
superset/migrations/versions/2025-10-06_16-05_b54f3bd8e69_update_tag_unique_constraint.py
   **Line:** 56:97
   **Comment:**
        *CRITICAL: The migration hardcodes the old unique constraint name as 
"tag_name_key", but the original tag table was created in migration 
c82ee8a39623 via Column(..., unique=True) on a bare declarative_base() (no 
naming convention), so the constraint name is dialect-dependent; on 
non-Postgres backends the unique index will not be called "tag_name_key", and 
utils.drop_unique_constraint(op, "tag_name_key", "tag") will emit a DROP 
CONSTRAINT for a non-existent constraint and fail.
   
   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.
   If a suggested approach is provided above, use it as the authoritative 
instruction. If no explicit code suggestion is given, you MUST still draft and 
apply your own minimal, localized fix — do not punt back with 'no suggestion 
provided, review manually'. Keep the change as small as possible: add a guard 
clause, gate on a loading state, reorder an await, wrap in a conditional, etc. 
Do not refactor surrounding code or expand scope beyond the finding.
   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>



##########
superset/tags/models.py:
##########
@@ -104,6 +104,8 @@ class Tag(CoreTag, AuditMixinNullable):
         security_manager.user_model, secondary=user_favorite_tag_table
     )
 
+    __table_args__ = (UniqueConstraint("name", "type", 
name="uix_tag_name_type"),)

Review Comment:
   **🟠 Architect Review — HIGH**
   
   After changing the unique constraint to (name, type), TagDAO and tag 
commands still resolve tags by name only (e.g., 
TagDAO.find_by_name/find_by_names and 
DeleteTagsCommand/DeleteTaggedObjectCommand), so flows that previously relied 
on name being globally unique can now target the wrong tag row when the same 
name exists across different TagType values.
   
   **Suggestion:** Make all name-based tag operations type-aware (or use tag 
IDs) before relying on (name, type) uniqueness—for example, add a type filter 
to TagDAO.find_by_name/find_by_names and update 
DeleteTagsCommand/DeleteTaggedObjectCommand and 
TagDAO.delete_tags/delete_tagged_object to work with (name, type) or tag IDs 
instead of bare names.
   
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6d162ccc3936416c9ccb2c4d9a509d92&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=6d162ccc3936416c9ccb2c4d9a509d92&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 an **Architect / Logical Review** comment left during a code review. 
These reviews are first-class, important findings — not optional suggestions. 
Do NOT dismiss this as a 'big architectural change' just because the title says 
architect review; most of these can be resolved with a small, localized fix 
once the intent is understood.
   
   **Path:** superset/tags/models.py
   **Line:** 95:107
   **Comment:**
        *HIGH: After changing the unique constraint to (name, type), TagDAO and 
tag commands still resolve tags by name only (e.g., 
TagDAO.find_by_name/find_by_names and 
DeleteTagsCommand/DeleteTaggedObjectCommand), so flows that previously relied 
on name being globally unique can now target the wrong tag row when the same 
name exists across different TagType values.
   
   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.
   If a suggested approach is provided above, use it as the authoritative 
instruction. If no explicit code suggestion is given, you MUST still draft and 
apply your own minimal, localized fix — do not punt back with 'no suggestion 
provided, review manually'. Keep the change as small as possible: add a guard 
clause, gate on a loading state, reorder an await, wrap in a conditional, etc. 
Do not refactor surrounding code or expand scope beyond the finding.
   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>



-- 
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]

Reply via email to