mikebridge commented on code in PR #44925:
URL: https://github.com/apache/superset/pull/44925#discussion_r4209371955
##########
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:
Good catch, thanks. The restore tests now include an actual snapshot with an
unsupported datasource type, restored through `RestoreChartVersionCommand`. It
has to raise `MissingDatasourceError` and leave the working chart unchanged.
With the raise replaced by `return None`, the new test fails because the
command reports a successful restore, and it passes with the guard restored.
Added in
https://github.com/apache/superset/commit/78b38da04218018eafa9394ef2c01a2c5b94ae79.
##########
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:
Good catch, thanks. Both the restore copy and the chart's flush listener
(`Slice.set_related_perm`) now read `perm`, `catalog_perm` and `schema_perm`
with `getattr(..., None)`, so fields a source type doesn't have are cleared
instead of copied from the previous source. The listener change was needed as
well because it reads those fields independently during flush. As a result,
saving query and saved-query charts works again (they previously failed with an
`AttributeError`), with no loosening of permissions. New command-level Query
and SavedQuery restore cases failed on the original code and pass now, and they
assert the restored source identity and every denormalized permission field.
Added in
https://github.com/apache/superset/commit/78b38da04218018eafa9394ef2c01a2c5b94ae79.
--
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]