aminghadersohi commented on code in PR #44925:
URL: https://github.com/apache/superset/pull/44925#discussion_r4203613160
##########
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:
Agree with codeant: `Query` lacks `catalog_perm` and `SavedQuery` lacks
`perm`, so restoring a `query`/`saved_query` chart version raises a bare
`AttributeError` (500) where master returns 422. `getattr` restores the 422;
`set_related_perm` still raises on flush, so these restores won't complete.
```suggestion
chart.perm = getattr(datasource, "perm", None)
chart.catalog_perm = getattr(datasource, "catalog_perm", None)
chart.schema_perm = getattr(datasource, "schema_perm", None)
```
##########
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)
Review Comment:
No test reaches this raise: changing it to `return None` keeps all 40 tests
green, and an unknown-type snapshot then restores as success. A case with
`datasource_type="nonexistent"` would pin it.
--
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]