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


##########
superset/migrations/versions/2026-08-07_09-00_da0e3f0081bf_enforce_oauth2_token_uniqueness.py:
##########
@@ -0,0 +1,75 @@
+# 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.
+"""Enforce one OAuth2 token per (user_id, database_id).
+
+`OAuth2StoreTokenCommand` always deletes any existing token for a
+user+database pair before storing a new one, so the table is only ever
+meant to carry a single live row per pair -- but that invariant was only
+enforced in application code, via a plain (non-unique) lookup index. A
+race between two concurrent OAuth2 callbacks for the same user+database
+can leave duplicate rows behind, and nothing downstream picks a
+deterministic one of them. Flagged as a follow-up during review of #42211
+(which fixed an unrelated `purge_oauth2_tokens` filter bug on this same
+table).
+
+Pre-flight: deletes any pre-existing duplicate rows, keeping the
+highest-id row per (user_id, database_id) pair, since
+`OAuth2StoreTokenCommand` always deletes-then-inserts and a higher id is
+therefore the more recently issued token.
+
+Revision ID: da0e3f0081bf
+Revises: b8d2f4a6c901
+Create Date: 2026-08-07 09:00:00.000000
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.sql import func, select
+
+from superset.migrations.shared.utils import create_index, drop_index
+
+# revision identifiers, used by Alembic.
+revision: str = "da0e3f0081bf"
+down_revision: str = "b8d2f4a6c901"
+
+TABLE_NAME = "database_user_oauth2_tokens"
+INDEX_NAME = "idx_user_id_database_id"
+
+
+def upgrade() -> None:
+    bind = op.get_bind()
+    metadata = sa.MetaData()
+    table = sa.Table(TABLE_NAME, metadata, autoload_with=bind)
+
+    max_ids = (
+        select(func.max(table.c.id).label("max_id"))
+        .group_by(table.c.user_id, table.c.database_id)
+        .alias("max_ids")
+    )
+    
bind.execute(table.delete().where(table.c.id.notin_(select(max_ids.c.max_id))))

Review Comment:
   **Suggestion:** MySQL rejects a DELETE whose subquery reads from the same 
target table (error 1093). On MySQL, this cleanup fails before the old index is 
dropped or the unique index is created, preventing the migration from 
completing. Read the duplicate rows separately or use a 
derived-table/Python-side deduplication strategy that is portable across 
supported databases. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Critical 🚨</summary>
   
   ```mdx
   - ❌ MySQL schema upgrades fail during duplicate cleanup.
   - ❌ The unique OAuth2-token index is never created.
   - ⚠️ Deployments remain blocked until manual cleanup.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e1f0954e310648d984c9d9e7b503617d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e1f0954e310648d984c9d9e7b503617d&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/2026-08-07_09-00_da0e3f0081bf_enforce_oauth2_token_uniqueness.py
   **Line:** 59:64
   **Comment:**
        *Api Mismatch: MySQL rejects a DELETE whose subquery reads from the 
same target table (error 1093). On MySQL, this cleanup fails before the old 
index is dropped or the unique index is created, preventing the migration from 
completing. Read the duplicate rows separately or use a 
derived-table/Python-side deduplication strategy that is portable across 
supported databases.
   
   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%2F42897&comment_hash=eb7cae282a0b3fd33ae636692e848c581c4e96bfa84d8a2e1024671324a17890&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42897&comment_hash=eb7cae282a0b3fd33ae636692e848c581c4e96bfa84d8a2e1024671324a17890&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]

Reply via email to