mikebridge commented on code in PR #40129:
URL: https://github.com/apache/superset/pull/40129#discussion_r3510306611
##########
superset/daos/dashboard.py:
##########
@@ -276,8 +277,19 @@ def set_dash_metadata(
if isinstance(value, dict)
]
+ # Bypass the soft-delete visibility filter when resolving the
+ # incoming chart ids: a dashboard's ``position_json`` may still
+ # reference a chart that is currently soft-deleted, and this
+ # assignment REBUILDS ``dashboard.slices`` wholesale. With the
+ # filter active, the hidden member would be silently dropped —
+ # deleting its ``dashboard_slices`` junction row (breaking the
+ # documented restore-reattach contract) and writing
+ # ``uuid: None`` into its position slot via ``uuid_map`` below.
current_slices = (
- db.session.query(Slice).filter(Slice.id.in_(slice_ids)).all()
+ db.session.query(Slice)
+ .execution_options(**{SKIP_VISIBILITY_FILTER_CLASSES: {Slice}})
+ .filter(Slice.id.in_(slice_ids))
+ .all()
)
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
tests/integration_tests/charts/commands_tests.py:
##########
@@ -721,7 +721,7 @@ def test_fave_unfave_chart_command(self):
def test_fave_unfave_chart_command_not_found(self):
"""Test that faving / unfaving a non-existing chart raises an
exception"""
with self.client.application.test_request_context():
- example_chart_id = 1234
+ example_chart_id = 0
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
tests/unit_tests/commands/chart/restore_test.py:
##########
@@ -0,0 +1,90 @@
+# 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.
+"""Unit tests for RestoreChartCommand."""
+
+from __future__ import annotations
+
+from datetime import datetime, timezone
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from superset.commands.chart.exceptions import (
+ ChartForbiddenError,
+ ChartNotFoundError,
+)
+from superset.commands.chart.restore import RestoreChartCommand
+from superset.exceptions import SupersetSecurityException
+
+
+def test_restore_chart_clears_deleted_at(app_context: None) -> None:
+ """RestoreChartCommand.run() restores a soft-deleted chart."""
+ chart = MagicMock()
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
tests/unit_tests/commands/chart/restore_test.py:
##########
@@ -0,0 +1,90 @@
+# 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.
+"""Unit tests for RestoreChartCommand."""
+
+from __future__ import annotations
+
+from datetime import datetime, timezone
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from superset.commands.chart.exceptions import (
+ ChartForbiddenError,
+ ChartNotFoundError,
+)
+from superset.commands.chart.restore import RestoreChartCommand
+from superset.exceptions import SupersetSecurityException
+
+
+def test_restore_chart_clears_deleted_at(app_context: None) -> None:
+ """RestoreChartCommand.run() restores a soft-deleted chart."""
+ chart = MagicMock()
+ chart.deleted_at = datetime(2026, 1, 1, tzinfo=timezone.utc)
+ chart.id = 1
+
+ with (
+ patch(
+ "superset.daos.chart.ChartDAO.find_by_id", return_value=chart
+ ) as mock_find,
+ patch("superset.commands.restore.security_manager") as mock_sec,
+ ):
+ mock_sec.raise_for_ownership.return_value = None
+
+ cmd = RestoreChartCommand("1")
+ cmd.run()
+
+ mock_find.assert_called_once()
+ chart.restore.assert_called_once()
+
+
+def test_restore_chart_not_found_raises(app_context: None) -> None:
+ """RestoreChartCommand raises ChartNotFoundError for missing chart."""
+ with patch("superset.daos.chart.ChartDAO.find_by_id", return_value=None):
+ cmd = RestoreChartCommand("999")
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
tests/unit_tests/commands/chart/restore_test.py:
##########
@@ -0,0 +1,90 @@
+# 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.
+"""Unit tests for RestoreChartCommand."""
+
+from __future__ import annotations
+
+from datetime import datetime, timezone
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from superset.commands.chart.exceptions import (
+ ChartForbiddenError,
+ ChartNotFoundError,
+)
+from superset.commands.chart.restore import RestoreChartCommand
+from superset.exceptions import SupersetSecurityException
+
+
+def test_restore_chart_clears_deleted_at(app_context: None) -> None:
+ """RestoreChartCommand.run() restores a soft-deleted chart."""
+ chart = MagicMock()
+ chart.deleted_at = datetime(2026, 1, 1, tzinfo=timezone.utc)
+ chart.id = 1
+
+ with (
+ patch(
+ "superset.daos.chart.ChartDAO.find_by_id", return_value=chart
+ ) as mock_find,
+ patch("superset.commands.restore.security_manager") as mock_sec,
+ ):
+ mock_sec.raise_for_ownership.return_value = None
+
+ cmd = RestoreChartCommand("1")
+ cmd.run()
+
+ mock_find.assert_called_once()
+ chart.restore.assert_called_once()
+
+
+def test_restore_chart_not_found_raises(app_context: None) -> None:
+ """RestoreChartCommand raises ChartNotFoundError for missing chart."""
+ with patch("superset.daos.chart.ChartDAO.find_by_id", return_value=None):
+ cmd = RestoreChartCommand("999")
+ with pytest.raises(ChartNotFoundError):
+ cmd.run()
+
+
+def test_restore_active_chart_raises_not_found(app_context: None) -> None:
+ """RestoreChartCommand raises ChartNotFoundError for non-deleted chart."""
+ chart = MagicMock()
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
tests/unit_tests/migrations/test_add_deleted_at_to_slices.py:
##########
@@ -0,0 +1,186 @@
+# 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.
+"""Tests for migration ``7c4a8d09ca37_add_deleted_at_to_slices``.
+
+Runs the migration's ``upgrade()`` and ``downgrade()`` against an
+in-memory SQLite engine with a real Alembic ``Operations`` context.
+The behaviour being pinned is the operator-facing contract documented
+in ``UPDATING.md``: ``downgrade()`` reverses the schema but does not
+hard-delete or otherwise mutate rows that were soft-deleted before
+the migration was reversed — those rows survive the downgrade and
+become visible to any code path that no longer applies the
+soft-delete visibility filter.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from importlib import import_module
+
+import pytest
+from alembic.migration import MigrationContext
+from alembic.operations import Operations
+from sqlalchemy import (
+ Column,
+ create_engine,
+ insert,
+ inspect,
+ Integer,
+ MetaData,
+ select,
+ String,
+ Table,
+)
+from sqlalchemy.engine import Engine
+
+migration = import_module(
+ "superset.migrations.versions."
+ "2026-05-08_12-00_7c4a8d09ca37_add_deleted_at_to_slices"
+)
+
+TABLE_NAME = migration.TABLE_NAME # "slices"
+INDEX_NAME = migration.INDEX_NAME # "ix_slices_deleted_at"
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
##########
superset/charts/api.py:
##########
@@ -134,7 +142,17 @@ def ensure_thumbnails_enabled(self) -> Optional[Response]:
"warm_up_cache",
}
class_permission_name = "Chart"
- method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+ # Custom methods (``restore``) need an explicit entry; FAB's @protect()
+ # decorator falls back to ``can_<method>_<class>`` (i.e.
+ # ``can_restore_Chart``) when the mapping is missing, which standard
+ # roles don't carry. Mirrors the permission model documented for
+ # ``DELETE`` / ``bulk_delete``: endpoint-level ``can_write`` plus
+ # resource-level ``raise_for_ownership``. See themes/api.py for the
+ # established pattern.
+ method_permission_name = {
+ **MODEL_API_RW_METHOD_PERMISSION_MAP,
+ "restore": "write",
+ }
Review Comment:
Following the surrounding file's conventions — local/test-scaffolding
variables whose types are unambiguous from context. Declining as style.
--
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]