mikebridge commented on code in PR #41176:
URL: https://github.com/apache/superset/pull/41176#discussion_r3555074678
##########
superset/dashboards/api.py:
##########
@@ -2364,3 +2437,109 @@ def copy_dash(self, original_dash: Dashboard) ->
Response:
).timestamp(),
},
)
+
+ @expose("/<uuid_str>/versions/", methods=("GET",))
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs:
f"{self.__class__.__name__}.list_versions",
+ log_to_statsd=False,
+ )
+ def list_versions(self, uuid_str: str) -> Response:
+ """List version history for a dashboard.
+ ---
+ get:
+ summary: Return the version history for a dashboard
+ parameters:
+ - in: path
+ schema:
+ type: string
+ format: uuid
+ name: uuid_str
+ description: Dashboard UUID
+ responses:
+ 200:
+ description: Version history ordered by oldest first
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ result:
+ type: array
+ items:
+ $ref: '#/components/schemas/VersionListItemSchema'
+ count:
+ type: integer
+ 400:
+ $ref: '#/components/responses/400'
+ 401:
+ $ref: '#/components/responses/401'
+ 403:
+ $ref: '#/components/responses/403'
+ 404:
+ $ref: '#/components/responses/404'
+ """
+ return list_versions_endpoint(
+ self, Dashboard, uuid_str, access_kwarg="dashboard"
+ )
+
+ @expose(
+ "/<uuid_str>/versions/<version_uuid_str>/",
+ methods=("GET",),
Review Comment:
Yep — `list_versions` and `get_version` are both mapped to `read` in
`MODEL_API_RW_METHOD_PERMISSION_MAP` (which `method_permission_name` spreads
in), so they resolve to the existing `can_read_Dashboard` permission — no new
perm is minted. The per-object `raise_for_access(dashboard=…)` inside
`list_versions_endpoint` adds the object-level gate on top.
_(Replying via Claude on my behalf.)_
##########
superset/datasets/api.py:
##########
@@ -489,17 +552,69 @@ def put(self, pk: int) -> Response:
# This validates custom Schema with custom validations
except ValidationError as error:
return self.response_400(message=error.messages)
+
+ # Live version identifiers before the update (empty + query-free when
+ # ``ENABLE_VERSIONING_CAPTURE`` is off).
+ old_info = current_entity_version_info(SqlaTable, pk)
+
try:
+ # Two commands, two commits, two Continuum transactions for an
+ # ``override_columns`` save — deliberately NOT merged into one
+ # transaction. A single-transaction design was attempted and
+ # reverted: ``DBEventLogger`` writes request logs through the
+ # SHARED scoped session and calls ``commit()`` /
+ # ``rollback()`` on it mid-request (superset/utils/log.py),
+ # so any save held uncommitted across a logged sub-action can
+ # be committed half-done (Postgres/MySQL) or rolled back
+ # entirely on a transient logger failure (SQLite's
+ # "database is locked"). Until the event logger gets its own
+ # session, per-command commit boundaries are the only shape
+ # whose failure modes are honest. Consequence the
+ # version-history UI must tolerate: one logical save can
+ # surface as two version transactions stamped the same second.
changed_model = UpdateDatasetCommand(pk, item,
override_columns).run()
+ # Capture the post-update identifiers BEFORE the refresh:
+ # RefreshDatasetCommand commits its own transaction, so reading
+ # afterwards would attribute the refresh's version to the
+ # user's update (and old→new would span two transactions).
+ new_info = current_entity_version_info(
+ SqlaTable, changed_model.id, changed_model.uuid
+ )
+ etag_version_uuid = new_info.version_uuid
if override_columns:
RefreshDatasetCommand(pk).run()
- response = self.response(200, id=changed_model.id, result=item)
+ # The ETag must reflect the entity's *current live* version,
+ # which after the refresh is the refresh's transaction —
+ # re-read it rather than reusing the pre-refresh uuid.
+ etag_version_uuid = current_entity_etag_uuid(
+ SqlaTable, changed_model.id, changed_model.uuid
+ )
+ response = self.response(
+ 200,
+ id=changed_model.id,
+ result=item,
+ old_version=old_info.version,
+ new_version=new_info.version,
+ old_transaction_id=old_info.transaction_id,
+ new_transaction_id=new_info.transaction_id,
+ old_version_uuid=old_info.version_uuid,
+ new_version_uuid=new_info.version_uuid,
+ )
+ set_version_etag(response, etag_version_uuid)
except DatasetNotFoundError:
response = self.response_404()
except DatasetForbiddenError:
response = self.response_403()
except DatasetInvalidError as ex:
response = self.response_422(message=ex.normalized_messages())
+ except DatasetRefreshFailedError as ex:
+ logger.error(
+ "Error refreshing dataset during update %s: %s",
+ self.__class__.__name__,
+ str(ex),
+ exc_info=True,
Review Comment:
Good call — done in 38bb2b0. `logger.exception` keeps the ERROR-level
traceback without the explicit `exc_info`.
_(Replying via Claude on my behalf.)_
##########
superset/versioning/baseline/children.py:
##########
@@ -0,0 +1,212 @@
+# 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.
+"""Per-entity child-baseline handlers.
+
+After a parent baseline row lands in :mod:`.insertion`, this module's
+handlers write the parent's child baselines under the same transaction
+id. The dispatch table :data:`CHILD_BASELINE_HANDLERS` is keyed on
+the parent class name (avoids an import-cycle with the entity modules,
+which can't be loaded at app-init time).
+
+The dataset handler baselines :class:`TableColumn` and
+:class:`SqlMetric` children. The dashboard handler baselines the
+``dashboard_slices`` M2M membership *and* synthesizes
+``operation_type=0`` rows in ``slices_version`` for attached slices
+that have no prior shadow — without those slice-side baselines,
+Continuum's M2M revert query returns empty.
+
+Leaf-level helpers (:func:`_insert_child_baseline_rows`,
+:func:`_baseline_attached_slices`,
+:func:`_insert_synthetic_slice_baseline`) live here too — they're
+shared between the two parent-specific handlers.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Callable
+from typing import Any
+
+import sqlalchemy as sa
+from sqlalchemy.orm import Session
+
+from superset.versioning.baseline.shadow import insert_baseline_shadow_row
+
+
+def _baseline_dataset_children(session: Session, dataset: Any, tx_id: int) ->
None:
+ """Baseline a dataset's ``TableColumn`` and ``SqlMetric`` children
+ under the dataset's baseline tx.
+ """
+ # pylint: disable=import-outside-toplevel
+ from sqlalchemy_continuum import version_class
+
+ from superset.connectors.sqla.models import SqlMetric, TableColumn
+
+ for child_cls in (TableColumn, SqlMetric):
+ _insert_child_baseline_rows(
+ session,
+ dataset,
+ child_cls.__table__,
+ version_class(child_cls).__table__,
+ "table_id",
+ tx_id,
+ )
+
+
+def _baseline_dashboard_children(session: Session, dashboard: Any, tx_id: int)
-> None:
+ """Baseline a dashboard's ``dashboard_slices`` M2M plus synthesize
+ ``operation_type=0`` rows in ``slices_version`` for attached slices
+ with no prior shadow.
+
+ Continuum's M2M version-side relationship for ``Dashboard.slices``
+ joins through both ``dashboard_slices_version`` AND
+ ``slices_version``: the second exists clause filters slices by
+ "latest slices_version row with tx <= dashboard.tx". If a slice
+ has no slices_version rows at all, that join produces no match
+ and ``version_obj.slices`` returns empty — leaving the dashboard
+ restore with no slices to append. The synthetic slice baseline at
+ this dashboard's tx gives the M2M query a slice version it can match.
+
+ Doesn't try to be clever about slices shared across dashboards: a
+ slice is baselined at this dashboard's tx_id only when it has no
+ shadow rows at all. If a later dashboard baseline references the
+ same slice, this baseline (now at lower tx) is still found by
+ that dashboard's restore. The reverse — a dashboard baselined
+ AFTER the slice was first baselined under another dashboard at
+ a higher tx — is a residual gap deferred to a future fix.
+ """
+ metadata = type(dashboard).__table__.metadata
+ live_tbl = metadata.tables.get("dashboard_slices")
+ shadow_tbl = metadata.tables.get("dashboard_slices_version")
+ if live_tbl is None or shadow_tbl is None:
+ return
+
+ _insert_child_baseline_rows(
+ session, dashboard, live_tbl, shadow_tbl, "dashboard_id", tx_id
+ )
+ _baseline_attached_slices(session, dashboard, live_tbl, tx_id)
+
+
+# Dispatch table keyed by parent CLASS NAME rather than class, to avoid
+# the import-cycle between baseline.py (loaded at app init) and the
+# entity modules. The class-name string is set once at app start by
+# the model definitions — typo-prone if extended. Declared after the
+# handlers it references because module-level dict literals evaluate
+# at import time and need the names already bound.
+_ChildBaselineHandler = Callable[[Session, Any, int], None]
+CHILD_BASELINE_HANDLERS: dict[str, _ChildBaselineHandler] = {
+ "SqlaTable": _baseline_dataset_children,
+ "Dashboard": _baseline_dashboard_children,
+}
+
+
+def _insert_child_baseline_rows(
+ session: Session,
+ parent_obj: Any,
+ child_table: sa.Table,
+ child_version_table: sa.Table,
+ fk_column_name: str,
+ tx_id: int,
+) -> None:
+ """Synthesize ``operation_type=0`` shadow rows for every live child of
+ *parent_obj* under transaction id *tx_id*.
+
+ Parallels
:func:`~superset.versioning.baseline.insertion._insert_baseline_row`
+ but iterates over child rows. Used to give Continuum's ``Reverter``
+ baseline data for children of pre-existing parents (children that
+ predate this commit have no shadow rows otherwise, so Reverter
+ would treat them as "deleted at the target tx" and try to remove
+ them on revert — the ADR-004 Failure 1 reproduction scenario).
+
+ :param child_table: the live child SQLAlchemy ``Table`` (e.g.
+ ``TableColumn.__table__`` or the bare ``dashboard_slices`` association)
+ :param child_version_table: the corresponding Continuum shadow ``Table``
+ :param fk_column_name: column on *child_table* that points to the parent
+ (e.g. ``"table_id"`` for ``TableColumn``, ``"dashboard_id"`` for
+ ``dashboard_slices``)
+ """
+ conn = session.connection()
+ fk_col = getattr(child_table.c, fk_column_name)
+
+ rows = (
+ conn.execute(sa.select(child_table).where(fk_col == parent_obj.id))
Review Comment:
These are SQLAlchemy Core (`conn.execute(sa.select(...))`) rather than raw
`text()` — parameterized, no string SQL. They run on `session.connection()`
instead of the ORM `session.query` deliberately: baseline capture fires inside
`before_flush`, and an ORM query there would trigger an autoflush of
Continuum's in-progress transaction mid-flush. Reading straight off the
connection avoids that re-entrancy. Happy to add an inline comment making that
explicit if it'd help.
_(Replying via Claude on my behalf.)_
##########
superset/versioning/baseline/children.py:
##########
@@ -0,0 +1,212 @@
+# 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.
+"""Per-entity child-baseline handlers.
+
+After a parent baseline row lands in :mod:`.insertion`, this module's
+handlers write the parent's child baselines under the same transaction
+id. The dispatch table :data:`CHILD_BASELINE_HANDLERS` is keyed on
+the parent class name (avoids an import-cycle with the entity modules,
+which can't be loaded at app-init time).
+
+The dataset handler baselines :class:`TableColumn` and
+:class:`SqlMetric` children. The dashboard handler baselines the
+``dashboard_slices`` M2M membership *and* synthesizes
+``operation_type=0`` rows in ``slices_version`` for attached slices
+that have no prior shadow — without those slice-side baselines,
+Continuum's M2M revert query returns empty.
+
+Leaf-level helpers (:func:`_insert_child_baseline_rows`,
+:func:`_baseline_attached_slices`,
+:func:`_insert_synthetic_slice_baseline`) live here too — they're
+shared between the two parent-specific handlers.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Callable
+from typing import Any
+
+import sqlalchemy as sa
+from sqlalchemy.orm import Session
+
+from superset.versioning.baseline.shadow import insert_baseline_shadow_row
+
+
+def _baseline_dataset_children(session: Session, dataset: Any, tx_id: int) ->
None:
+ """Baseline a dataset's ``TableColumn`` and ``SqlMetric`` children
+ under the dataset's baseline tx.
+ """
+ # pylint: disable=import-outside-toplevel
+ from sqlalchemy_continuum import version_class
+
+ from superset.connectors.sqla.models import SqlMetric, TableColumn
+
+ for child_cls in (TableColumn, SqlMetric):
+ _insert_child_baseline_rows(
+ session,
+ dataset,
+ child_cls.__table__,
+ version_class(child_cls).__table__,
+ "table_id",
+ tx_id,
+ )
+
+
+def _baseline_dashboard_children(session: Session, dashboard: Any, tx_id: int)
-> None:
+ """Baseline a dashboard's ``dashboard_slices`` M2M plus synthesize
+ ``operation_type=0`` rows in ``slices_version`` for attached slices
+ with no prior shadow.
+
+ Continuum's M2M version-side relationship for ``Dashboard.slices``
+ joins through both ``dashboard_slices_version`` AND
+ ``slices_version``: the second exists clause filters slices by
+ "latest slices_version row with tx <= dashboard.tx". If a slice
+ has no slices_version rows at all, that join produces no match
+ and ``version_obj.slices`` returns empty — leaving the dashboard
+ restore with no slices to append. The synthetic slice baseline at
+ this dashboard's tx gives the M2M query a slice version it can match.
+
+ Doesn't try to be clever about slices shared across dashboards: a
+ slice is baselined at this dashboard's tx_id only when it has no
+ shadow rows at all. If a later dashboard baseline references the
+ same slice, this baseline (now at lower tx) is still found by
+ that dashboard's restore. The reverse — a dashboard baselined
+ AFTER the slice was first baselined under another dashboard at
+ a higher tx — is a residual gap deferred to a future fix.
+ """
+ metadata = type(dashboard).__table__.metadata
+ live_tbl = metadata.tables.get("dashboard_slices")
+ shadow_tbl = metadata.tables.get("dashboard_slices_version")
+ if live_tbl is None or shadow_tbl is None:
+ return
+
+ _insert_child_baseline_rows(
+ session, dashboard, live_tbl, shadow_tbl, "dashboard_id", tx_id
+ )
+ _baseline_attached_slices(session, dashboard, live_tbl, tx_id)
+
+
+# Dispatch table keyed by parent CLASS NAME rather than class, to avoid
+# the import-cycle between baseline.py (loaded at app init) and the
+# entity modules. The class-name string is set once at app start by
+# the model definitions — typo-prone if extended. Declared after the
+# handlers it references because module-level dict literals evaluate
+# at import time and need the names already bound.
+_ChildBaselineHandler = Callable[[Session, Any, int], None]
+CHILD_BASELINE_HANDLERS: dict[str, _ChildBaselineHandler] = {
+ "SqlaTable": _baseline_dataset_children,
+ "Dashboard": _baseline_dashboard_children,
+}
+
+
+def _insert_child_baseline_rows(
+ session: Session,
+ parent_obj: Any,
+ child_table: sa.Table,
+ child_version_table: sa.Table,
+ fk_column_name: str,
+ tx_id: int,
+) -> None:
+ """Synthesize ``operation_type=0`` shadow rows for every live child of
+ *parent_obj* under transaction id *tx_id*.
+
+ Parallels
:func:`~superset.versioning.baseline.insertion._insert_baseline_row`
+ but iterates over child rows. Used to give Continuum's ``Reverter``
+ baseline data for children of pre-existing parents (children that
+ predate this commit have no shadow rows otherwise, so Reverter
+ would treat them as "deleted at the target tx" and try to remove
+ them on revert — the ADR-004 Failure 1 reproduction scenario).
+
+ :param child_table: the live child SQLAlchemy ``Table`` (e.g.
+ ``TableColumn.__table__`` or the bare ``dashboard_slices`` association)
+ :param child_version_table: the corresponding Continuum shadow ``Table``
+ :param fk_column_name: column on *child_table* that points to the parent
+ (e.g. ``"table_id"`` for ``TableColumn``, ``"dashboard_id"`` for
+ ``dashboard_slices``)
+ """
+ conn = session.connection()
+ fk_col = getattr(child_table.c, fk_column_name)
+
+ rows = (
+ conn.execute(sa.select(child_table).where(fk_col == parent_obj.id))
+ .mappings()
+ .all()
+ )
+ if not rows:
+ return
+
+ for row in rows:
+ insert_baseline_shadow_row(conn, child_version_table, row, tx_id)
+
+
+def _baseline_attached_slices(
+ session: Session, dashboard: Any, live_tbl: sa.Table, tx_id: int
+) -> None:
+ """Insert ``operation_type=0`` rows in ``slices_version`` for each
+ slice attached to *dashboard* that has no shadow row yet.
+
+ Batched: one membership SELECT, one existing-shadow SELECT, one live
+ SELECT for the missing slices. Per-slice work happens only on
+ ``_insert_synthetic_slice_baseline``. The previous per-slice
+ ``COUNT(*)`` + ``SELECT`` pattern was O(N) round-trips and surfaced
+ as a measurable first-save hotspot on dashboards with many charts.
+ """
+ # pylint: disable=import-outside-toplevel
+ from sqlalchemy_continuum import version_class
+
+ from superset.models.slice import Slice
+
+ slice_ver_table = version_class(Slice).__table__
+ slice_table = Slice.__table__
+ conn = session.connection()
+
+ attached_slice_ids = [
+ r.slice_id
+ for r in conn.execute(
Review Comment:
Same here — Core `sa.select` on the connection, not raw SQL; see the note on
the select just above (avoids an autoflush re-entrancy inside `before_flush`).
_(Replying via Claude on my behalf.)_
##########
superset/versioning/baseline/collection.py:
##########
@@ -0,0 +1,153 @@
+# 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.
+"""Discovery: figure out which parents need a baseline row.
+
+Three helpers cooperate on the listener's "should I baseline" decision:
+
+* :func:`collect_parents_to_baseline` — walks ``session.dirty`` /
+ ``new`` / ``deleted`` and returns the unique parent entities to
+ consider (directly-dirty versioned parents + parents reachable from
+ dirty children via :func:`child_to_parent_registry`).
+* :func:`version_table_for` — resolves a Continuum shadow Table for
+ one parent object.
+* :func:`shadow_row_count` — counts existing shadow rows for the
+ parent's id; ``0`` is the signal to insert a baseline.
+
+:func:`child_to_parent_registry` is also exposed because
+:mod:`superset.versioning.factory` consumes it via inline import.
+
+**Inline imports.** ``versioning.baseline`` is imported during
+``init_versioning()`` before all SQLAlchemy mappers are configured;
+the lazy imports defer Continuum + model resolution until call time.
+"""
+
+from __future__ import annotations
+
+import functools
+import logging
+from typing import Any
+
+import sqlalchemy as sa
+from sqlalchemy.exc import OperationalError, ProgrammingError
+from sqlalchemy.orm import Session
+
+# Populated at app startup (superset/initialization/__init__.py) before
+# register_baseline_listener() is called.
+VERSIONED_MODELS: list[type] = []
+
+logger = logging.getLogger(__name__)
+
+
+def collect_parents_to_baseline(session: Session) -> dict[int, Any]:
+ """Return parents-to-baseline as ``{id(obj): obj}`` keyed by Python
+ object identity to dedupe across ``session.dirty + new + deleted``.
+
+ Includes both directly-dirty versioned parents and parents reachable
+ from dirty/new/deleted children via the child→parent registry.
+ """
+ parents: dict[int, Any] = {}
+ child_map = child_to_parent_registry()
+ for obj in list(session.dirty) + list(session.new) + list(session.deleted):
+ if type(obj) in VERSIONED_MODELS:
+ parents[id(obj)] = obj
Review Comment:
Just to make sure I'm on the right line — on current HEAD,
`collection.py:66` is `parents[id(obj)] = obj` (a dict assignment), and the
`child_map` lookup just below already uses `.get()`. Could you point me at the
exact access you'd like switched? The anchor may have drifted after the recent
master rebase.
_(Replying via Claude on my behalf.)_
--
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]