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


##########
superset/versioning/restore.py:
##########
@@ -135,6 +136,97 @@ def __init__(self, model_name: str, detail: str) -> None:
         )
 
 
+class MissingDatasourceError(Exception):
+    """The chart version's datasource no longer exists.
+
+    A version snapshot records the chart's datasource by type and id, but
+    restoring a snapshot does not restore the datasource itself. Restoring
+    onto a deleted one would leave the chart resolving no datasource while
+    reporting success, so the restore refuses instead. The message is
+    user-facing.
+    """
+
+    def __init__(self, datasource_label: str) -> None:
+        message: str = (
+            _(
+                "This chart version can't be restored because the semantic 
view "
+                "it used has been permanently deleted. Restore a version that "
+                "uses an existing semantic view or dataset, or recreate the 
chart."
+            )
+            if datasource_label == "semantic view"
+            else _(
+                "This chart version can't be restored because the dataset it "
+                "used has been permanently deleted. Restore a version that 
uses "
+                "an existing dataset, or recreate the chart."
+            )
+        )
+        super().__init__(message)
+
+
+def _lock_chart_datasource(model_cls: type, target_version: Any) -> Any | None:
+    """Lock and return the datasource a chart snapshot names, or refuse.
+
+    Restoring a snapshot does not restore its datasource, so the restore
+    refuses when that datasource no longer exists. The row is locked in the
+    restore's transaction, so it cannot be deleted between this check and
+    the chart write; SQLite drops ``FOR UPDATE``, so its write lock is
+    reserved before the lookup instead.
+
+    A soft-deleted dataset still counts as existing: it is restorable from
+    the trash with its permissions intact, so the restored chart works again
+    once the dataset is restored. A snapshot without a datasource id has no
+    dependency to check and returns ``None``, as does any non-chart model.
+    """
+    # pylint: disable=import-outside-toplevel
+    from superset.daos.datasource import Datasource, DatasourceDAO
+    from superset.models.helpers import skip_visibility_filter
+    from superset.utils.core import DatasourceType
+
+    if model_cls.__name__ != "Slice":
+        return None
+    datasource_id: int | None = target_version.datasource_id
+    if not datasource_id:
+        return None
+    datasource_type: str = target_version.datasource_type
+    label: str = (
+        "semantic view"
+        if datasource_type == DatasourceType.SEMANTIC_VIEW.value
+        else "dataset"
+    )
+    src_class: type[Datasource] | None = 
DatasourceDAO.sources.get(datasource_type)
+    if src_class is None:
+        raise MissingDatasourceError(label)
+    _reserve_sqlite_write_lock()
+    with skip_visibility_filter(db.session, src_class):
+        # ``populate_existing`` re-reads a row the session already holds, so a
+        # rename committed before the lock is not hidden by a stale copy.
+        datasource: Datasource | None = (
+            db.session.query(src_class)
+            .populate_existing()
+            .enable_eagerloads(False)
+            .filter_by(id=datasource_id)
+            .with_for_update()
+            .one_or_none()
+        )
+    if datasource is None:
+        raise MissingDatasourceError(label)
+    return datasource
+
+
+def _sync_chart_perms(chart: Any, datasource: Any | None) -> None:
+    """Copy the restored datasource's permission fields onto the chart.
+
+    The chart's own listener resolves its datasource through the soft-delete
+    visibility filter, so for a soft-deleted dataset it would keep the
+    previous datasource's permission fields.
+    """
+    if datasource is None:
+        return
+    chart.perm = datasource.perm
+    chart.catalog_perm = datasource.catalog_perm
+    chart.schema_perm = datasource.schema_perm

Review Comment:
   **Suggestion:** A legacy `query` datasource has no `catalog_perm`, and 
`SavedQuery` has no `perm`; restoring either raises `AttributeError` instead of 
completing.
   
   **Assessment:** ๐ŸŸ  `Major` ยท ๐Ÿ” `Occurrence: Rarely` ยท ๐Ÿท๏ธ `Type error`
   
   [![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=a4f6fd729db04a6c815312cd74c3376b&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=a4f6fd729db04a6c815312cd74c3376b&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/versioning/restore.py
   **Line:** 225:227
   **Comment:**
        *Type Error: A legacy `query` datasource has no `catalog_perm`, and 
`SavedQuery` has no `perm`; restoring either raises `AttributeError` instead of 
completing.
   
   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%2F44925&comment_hash=aca109af33c9284902ad3f65e7fcee89c3d7b42e47994263bc6f713097b75e0a&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44925&comment_hash=aca109af33c9284902ad3f65e7fcee89c3d7b42e47994263bc6f713097b75e0a&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