mikebridge commented on code in PR #42469: URL: https://github.com/apache/superset/pull/42469#discussion_r3674049420
########## superset/commands/version_restore.py: ########## @@ -0,0 +1,152 @@ +# 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. +"""Shared base for the per-entity restore-version commands. + +The three concrete commands (:mod:`superset.commands.chart.restore_version`, +:mod:`superset.commands.dashboard.restore_version`, +:mod:`superset.commands.dataset.restore_version`) differ only in: + +* the model class they operate on +* the per-entity ``NotFoundError`` / ``ForbiddenError`` / ``UpdateFailedError`` + triplet they raise + +Everything else — capture gate, lookup, editorship check, version-uuid +resolution, action-kind stamping, restore dispatch, transactional +boundary — lives here. Subclasses are pure declarations: the base builds +the ``@transaction`` wrapper at call time from the ``failed_exc`` +ClassVar (mirroring ``BaseRestoreCommand``), so a new entity rollout +cannot forget the decorator. +""" + +from __future__ import annotations + +from functools import partial +from typing import Any, ClassVar +from uuid import UUID + +from superset import security_manager +from superset.commands.base import BaseCommand +from superset.exceptions import SupersetSecurityException +from superset.extensions import db +from superset.utils.decorators import on_error, transaction +from superset.versioning.queries import find_active_by_uuid, resolve_version +from superset.versioning.restore import restore_version, RestoreResult +from superset.versioning.utils import capture_enabled + + +class BaseRestoreVersionCommand(BaseCommand): + """Workflow for a non-destructive version restore on one entity. + + Subclasses declare the model class plus the three entity-specific + exception ClassVars; the base owns the workflow and the transactional + boundary. + """ + + #: Subclass overrides — the versioned model class (``Slice`` / + #: ``Dashboard`` / ``SqlaTable``). + model_cls: ClassVar[type] + + #: Subclass overrides — exception classes raised on the matching + #: failure modes. ``not_found_exc`` covers "no such entity", + #: "version_uuid not on this entity", and "capture disabled" (the + #: route is inert under the kill-switch); the API handler maps each + #: to HTTP 404. ``forbidden_exc`` covers the row-level editorship + #: denial (HTTP 403). ``failed_exc`` wraps unexpected failures inside + #: the transaction (HTTP 422). + not_found_exc: ClassVar[type[Exception]] + forbidden_exc: ClassVar[type[Exception]] + failed_exc: ClassVar[type[Exception]] + + def __init__(self, entity_uuid: UUID, version_uuid: UUID) -> None: + self._uuid = entity_uuid + self._version_uuid = version_uuid + + def run(self) -> RestoreResult: + # Build the transactional wrapper at call time so ``on_error`` can + # reference ``self.failed_exc`` — a per-subclass ClassVar that + # isn't available when this method is defined on the base (same + # pattern and rationale as ``BaseRestoreCommand.run``). + @transaction(on_error=partial(on_error, reraise=self.failed_exc)) + def _perform() -> RestoreResult: + return self._do_restore() + + return _perform() + + def _do_restore(self) -> RestoreResult: + entity = self.validate() + resolved = resolve_version( + self.model_cls, self._uuid, self._version_uuid, entity=entity + ) + if resolved is None: + raise self.not_found_exc() + version_number, transaction_id = resolved Review Comment: This one is real — thanks. Filed as a follow-up rather than fixed here. Confirmed there is no row lock and no optimistic check: `run()` wraps `_perform()` in `@transaction()`, but `validate()` loads the entity, `resolve_version()` resolves the target, and `revert()` is applied to that same instance with nothing verifying it hasn't moved. Under READ COMMITTED a concurrent edit committed in that window is overwritten. Two things keep it off the blocking list: 1. Overwriting current state is what restore *is* — the narrower issue is that a concurrent edit is overwritten without telling anyone. 2. Nothing is permanently lost. Any entity with version history has capture on, so the overwritten edit is itself recorded as a version and can be restored. The failure mode is confusion and a redo, not data loss. Between the two remedies you suggest, the optimistic one looks better: capture the entity's current transaction id in `validate()`, re-check immediately before `revert()`, and return 409 if it moved. That avoids serialising restores against saves, and it composes with the version-history UI (#41551), which already knows which version the user was looking at and could surface "this changed while you were viewing it" rather than a generic error. That UI coupling is the main reason it belongs in its own change. -- 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]
