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


##########
superset/db_engine_specs/databricks.py:
##########
@@ -932,12 +973,22 @@ def adjust_engine_params(
 
         if schema:
             uri = uri.update_query_dict({"schema": schema})
+        elif (
+            "schema" not in uri.query and "schema" not in connect_args and 
uri.database
+        ):
+            # The SQLAlchemy 2 dialect reads schema from the query, not the 
path.
+            uri = uri.update_query_dict({"schema": uri.database})
+
+        # Edits made in the native form take precedence over legacy 
connect_args.
+        for key in ("http_path", "catalog", "schema"):
+            if key in uri.query:
+                connect_args.pop(key, None)

Review Comment:
   **Suggestion:** When a field is cleared on a migrated connection, the URI 
builder omits its query key, leaving the old `connect_args` value active.
   
   **Assessment:** ๐ŸŸ  `Major` ยท ๐Ÿ” `Occurrence: Sometimes` ยท ๐Ÿท๏ธ `Incorrect 
condition logic`
   
   [![Use CodeAnt 
Skill](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/use-codeant-skill-flat-v2.svg)](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
 [![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=f6c19107fa8c4d2ea01e63f7a4cec7ae&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=f6c19107fa8c4d2ea01e63f7a4cec7ae&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/db_engine_specs/databricks.py
   **Line:** 983:985
   **Comment:**
        *Incorrect Condition Logic: When a field is cleared on a migrated 
connection, the URI builder omits its query key, leaving the old `connect_args` 
value active.
   
   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%2F45843&comment_hash=d01318342b1f86a26e71bc79a413f135828976c88aefb8603f6b0431c621a91f&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F45843&comment_hash=d01318342b1f86a26e71bc79a413f135828976c88aefb8603f6b0431c621a91f&reaction=dislike'>๐Ÿ‘Ž</a>



##########
superset/migrations/versions/2026-10-29_00-00_b8f3a2c91d64_migrate_databricks_connector_uri.py:
##########
@@ -0,0 +1,176 @@
+# 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.
+
+"""Use the SQLAlchemy 2 Databricks dialect for saved legacy connections.
+
+Revision ID: b8f3a2c91d64
+Revises: f72b6e9c104a
+
+Only the URI prefix is rewritten. Credentials live in the separate encrypted
+password column, which this migration never reads or writes. The extra marker
+and a migration-owned fingerprint table limit rollback to unchanged converted 
rows.
+"""
+
+from datetime import datetime
+from hashlib import sha256
+from typing import Any
+
+import sqlalchemy as sa
+from alembic import op
+
+from superset.migrations.shared.utils import create_table, drop_table, 
has_table
+from superset.utils import json
+
+revision = "b8f3a2c91d64"
+down_revision = "f72b6e9c104a"
+
+LEGACY_PREFIX = "databricks+connector://"
+NATIVE_PREFIX = "databricks://"
+MARKER = "migrated_from_databricks_connector_uri"
+BATCH_SIZE = 1000
+
+# Deliberately independent of application models and encrypted column types.
+databases = sa.table(
+    "dbs",
+    sa.column("id", sa.Integer),
+    sa.column("sqlalchemy_uri", sa.String(1024)),
+    sa.column("extra", sa.Text),
+    sa.column("changed_on", sa.DateTime),
+)
+
+
+# Fingerprints avoid copying connection settings or credentials into a backup 
table.
+records = sa.table(
+    "databricks_uri_migration",
+    sa.column("id", sa.Integer),
+    sa.column("fingerprint", sa.String(64)),
+    sa.column("extra_marked", sa.Boolean),
+)
+
+
+def _load_extra(value: str | None) -> dict[str, Any] | None:
+    """Return no object for malformed settings, without replacing their 
text."""
+    try:
+        extra = json.loads(value) if value is not None else None
+    except (ValueError, RecursionError):
+        return None
+    return extra if isinstance(extra, dict) else None
+
+
+def _fingerprint(uri: str, extra: str | None, changed_on: datetime | None) -> 
str:
+    """Recognize the migrated state, including edits to other database 
fields."""
+    state = json.dumps([uri, extra, str(changed_on)])
+    return sha256(state.encode("utf-8")).hexdigest()
+
+
+def _query(upgrading: bool, tracked: bool, last_id: int) -> sa.sql.Select:
+    """Find source URIs or rollback candidates, including exported markers."""
+    query = sa.select(databases).where(databases.c.id > last_id)
+    if upgrading:
+        query = 
query.where(databases.c.sqlalchemy_uri.like(f"{LEGACY_PREFIX}%"))
+    elif tracked:
+        query = (
+            query.add_columns(records.c.fingerprint, records.c.extra_marked)
+            .select_from(databases.outerjoin(records, databases.c.id == 
records.c.id))
+            .where(
+                sa.or_(records.c.id.isnot(None), 
databases.c.extra.like(f"%{MARKER}%"))
+            )
+        )
+    else:
+        query = query.add_columns(
+            sa.literal(None).label("fingerprint"),
+            sa.literal(None).label("extra_marked"),
+        ).where(databases.c.extra.like(f"%{MARKER}%"))
+    return query
+
+
+def _migrate(upgrading: bool) -> None:
+    """Use keyset batches so rewriting the filter cannot skip later rows."""
+    bind = op.get_bind()
+    tracked = has_table(records.name)
+    last_id = -1
+    while True:
+        query = _query(upgrading, tracked, last_id)
+        rows = 
bind.execute(query.order_by(databases.c.id).limit(BATCH_SIZE)).fetchall()
+        if not rows:
+            break
+        for row in rows:
+            last_id = row.id
+            extra = _load_extra(row.extra)
+            uri = row.sqlalchemy_uri
+            extra_text = row.extra
+            if upgrading:
+                # LIKE can be case-insensitive on some metadata databases.
+                if not uri.startswith(LEGACY_PREFIX):
+                    continue
+                uri = NATIVE_PREFIX + uri[len(LEGACY_PREFIX) :]
+                extra_marked = extra is not None and (
+                    MARKER not in extra or extra[MARKER] is True
+                )
+                if extra is not None and extra_marked:
+                    extra[MARKER] = True
+                    extra_text = json.dumps(extra)
+                # Preserve conflicting user settings instead of aborting the 
upgrade.
+                bind.execute(records.delete().where(records.c.id == row.id))
+                bind.execute(
+                    records.insert().values(
+                        id=row.id,
+                        fingerprint=_fingerprint(uri, extra_text, 
row.changed_on),
+                        extra_marked=extra_marked,
+                    )
+                )
+            else:
+                unchanged = row.fingerprint == _fingerprint(
+                    uri, extra_text, row.changed_on
+                )
+                imported = (
+                    row.fingerprint is None
+                    and uri.startswith(NATIVE_PREFIX)
+                    and extra is not None
+                    and extra.get(MARKER) is True
+                )
+                if (unchanged or imported) and uri.startswith(NATIVE_PREFIX):
+                    uri = LEGACY_PREFIX + uri[len(NATIVE_PREFIX) :]
+                if (
+                    (row.extra_marked or imported)
+                    and extra is not None
+                    and extra.get(MARKER) is True
+                ):
+                    del extra[MARKER]
+                    extra_text = json.dumps(extra)
+            bind.execute(
+                databases.update()
+                .where(databases.c.id == row.id)
+                .values(sqlalchemy_uri=uri, extra=extra_text)
+            )

Review Comment:
   **Suggestion:** The row is updated using only its ID; an edit after this 
batch read can therefore be overwritten by the stale URI and extra values 
during upgrade.
   
   **Assessment:** ๐ŸŸ  `Major` ยท ๐Ÿ” `Occurrence: Rarely` ยท ๐Ÿท๏ธ `Race condition`
   
   [![Use CodeAnt 
Skill](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/use-codeant-skill-flat-v2.svg)](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
 [![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=0eaf5d620e1a4e48b9cf1a3afda85d2c&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=0eaf5d620e1a4e48b9cf1a3afda85d2c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset/migrations/versions/2026-10-29_00-00_b8f3a2c91d64_migrate_databricks_connector_uri.py
   **Line:** 155:159
   **Comment:**
        *Race Condition: The row is updated using only its ID; an edit after 
this batch read can therefore be overwritten by the stale URI and extra values 
during upgrade.
   
   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%2F45843&comment_hash=b3309b9711f347bc0281e591909d13bc8a1e3323e7d959188bb0fff60e2a24f5&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F45843&comment_hash=b3309b9711f347bc0281e591909d13bc8a1e3323e7d959188bb0fff60e2a24f5&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