L4XB commented on code in PR #44347:
URL: https://github.com/apache/superset/pull/44347#discussion_r4302345346
##########
superset/commands/chart/export.py:
##########
@@ -259,9 +282,12 @@ def _export(
lambda: ExportChartsCommand._file_content(model),
)
- if model.table and export_related:
+ if export_related and (dataset := find_chart_dataset(model)):
Review Comment:
Yes, it should. Thanks for tracing it through the importer. Fixed in
06b820ee89. Both dataset lookups in `ExportDashboardsCommand` now use
`skip_visibility_filter=True`: the one in `_file_content` that maps
native-filter and display-control `datasetId`s to `datasetUuid`, and the one in
`_export` that decides which datasets travel with the bundle. Those datasets
are exported with `include_deleted=True`, so a filter on a trashed dataset
keeps its `datasetUuid`, and the bundle carries the dataset for the importer to
rebuild `datasetId` from.
`test_dashboard_filter_keeps_a_soft_deleted_dataset_through_import` covers
exactly your scenario: a chart and a native filter on D, D deleted, export,
then re-import with overwrite. It checks that the exported target is
`{"datasetUuid": <D>, "column": ...}`, that the bundle holds one `datasets/`
file, and that the imported filter points at the restored D. Without the
change, the exported target is `{"column": ...}` and the test fails there.
Two existing tests mocked these calls without the new arguments, so I
updated their expectations:
`test_export_yields_dataset_files_for_display_controls` and
`test_orphaned_dashboard_target_does_not_abort_mixed_export`.
`ExportModelsCommand.validate` now passes `skip_visibility_filter` only when
`include_deleted` is set, so every other caller keeps the plain
`find_by_ids(ids)` call.
##########
tests/unit_tests/commands/chart/export_test.py:
##########
@@ -0,0 +1,272 @@
+# 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 ExportChartsCommand."""
+
+from __future__ import annotations
+
+from collections.abc import Generator
+
+import pytest
+import yaml
+from flask_appbuilder.security.sqla.models import User
+from pytest_mock import MockerFixture
+from sqlalchemy.orm.session import Session
+
+from superset import db, security_manager
+from superset.commands.chart.export import ExportChartsCommand
+from superset.commands.chart.importers.v1 import ImportChartsCommand
+from superset.commands.dataset.exceptions import DatasetNotFoundError
+from superset.commands.dataset.export import ExportDatasetsCommand
+from superset.connectors.sqla.models import Database, SqlaTable
+from superset.daos.dataset import DatasetDAO
+from superset.models.slice import Slice
+from superset.subjects.models import Subject
+from superset.subjects.types import SubjectType
+from superset.utils.core import override_user
+
+
[email protected]
+def chart_on_dataset(session: Session) -> Generator[Slice, None, None]:
+ """A chart backed by a dataset, both alive."""
+ engine = session.get_bind()
+ SqlaTable.metadata.create_all(engine) # pylint: disable=no-member
+
+ dataset = SqlaTable(
+ table_name="my_table",
+ metrics=[],
+ main_dttm_col=None,
+ database=Database(database_name="my_database",
sqlalchemy_uri="sqlite://"),
+ )
+ session.add(dataset)
+ session.flush()
+
+ chart = Slice(
+ slice_name="my_chart",
+ viz_type="table",
+ datasource_id=dataset.id,
+ datasource_type="table",
+ datasource_name="my_table",
+ params="{}",
+ )
+ session.add(chart)
+ session.flush()
+
+ yield chart
+ session.rollback()
+
+
[email protected]
+def permissive_security(mocker: MockerFixture) -> None:
+ """Grant every read/write check the export and import paths consult."""
+ mocker.patch.object(
+ security_manager, "can_access_all_datasources", return_value=True
+ )
+ mocker.patch.object(security_manager, "can_access", return_value=True)
+ mocker.patch.object(security_manager, "is_editor", return_value=True)
+ mocker.patch.object(security_manager, "is_admin", return_value=True)
+
+
[email protected]
+def chart_editor_without_dataset_access(
+ chart_on_dataset: Slice,
+) -> Generator[User, None, None]:
+ """Log in a user who edits the chart but holds no grant on its dataset.
+
+ The user has no role, so no ``all_datasource_access``, ``database_access``,
+ ``datasource_access``, ``schema_access`` or ``catalog_access``, and is not
+ an editor of the dataset, so ``DatasourceFilter`` hides the dataset from
+ them in the trash as well. Editing the chart is what lets ``ChartFilter``
+ hand them the chart.
+ """
+ user = User(
+ first_name="chart",
+ last_name="editor",
+ username="chart_editor",
+ email="[email protected]",
+ active=True,
+ )
+ db.session.add(user)
+ db.session.flush()
+ chart_on_dataset.editors = [
+ Subject(label="chart_editor", type=SubjectType.USER, user_id=user.id)
+ ]
+ db.session.flush()
+
+ with override_user(user):
+ yield user
+
+
+def _export(chart: Slice, export_related: bool = True) -> dict[str, str]:
+ return {
+ file_name: file_content()
+ for file_name, file_content in ExportChartsCommand(
+ [chart.id], export_related=export_related
+ ).run()
+ }
+
+
+def _next_request() -> None:
+ """Drop everything loaded in this session.
+
+ ``Slice.table`` is ``lazy="subquery"``, so a relationship loaded before the
+ delete stays warm in the identity map and would mask the visibility filter.
+ Export runs in its own request in a deployment; expiring models that.
+ """
+ db.session.expire_all()
+
+
[email protected]("permissive_security")
+def test_export_chart_bundle_is_importable_when_dataset_is_soft_deleted(
+ app_context: None,
+ chart_on_dataset: Slice,
+) -> None:
+ """A chart whose dataset was deleted still exports a complete bundle.
+
+ Deleting a dataset soft-deletes it, which hides it from ``Slice.table``
+ while the chart itself survives. The export must still name the dataset,
+ otherwise ``charts/*.yaml`` carries no ``dataset_uuid`` and the bundle is
+ rejected on import with ``Missing data for required field``.
+ """
+ chart = chart_on_dataset
+ dataset = chart.table
+ dataset_uuid = str(dataset.uuid)
+
+ DatasetDAO.delete([dataset])
+ db.session.flush()
+ _next_request()
+ assert chart.table is None, "dataset should be hidden from the
relationship"
+
+ contents = _export(chart)
+
+ chart_files = [name for name in contents if name.startswith("charts/")]
+ dataset_files = [name for name in contents if name.startswith("datasets/")]
+ assert len(chart_files) == 1
+ assert len(dataset_files) == 1, "the dataset must travel with the chart"
+
+ payload = yaml.safe_load(contents[chart_files[0]])
+ assert payload["dataset_uuid"] == dataset_uuid
+ assert yaml.safe_load(contents[dataset_files[0]])["uuid"] == dataset_uuid
+
+
[email protected]("permissive_security")
+def test_export_import_round_trip_restores_chart_and_dataset(
+ app_context: None,
+ chart_on_dataset: Slice,
+) -> None:
+ """Export → delete → import restores both assets (issue #44309)."""
+ chart = chart_on_dataset
+ dataset = chart.table
+ dataset_uuid = str(dataset.uuid)
+
+ DatasetDAO.delete([dataset])
+ db.session.flush()
+ _next_request()
+
+ contents = _export(chart)
+
+ ImportChartsCommand(contents, overwrite=True).run()
+ db.session.flush()
+
+ restored = db.session.query(SqlaTable).filter_by(uuid=dataset.uuid).one()
+ assert restored.deleted_at is None, "the dataset must come back out of the
trash"
+ assert str(restored.uuid) == dataset_uuid
+
+ reimported = db.session.query(Slice).filter_by(uuid=chart.uuid).one()
+ assert reimported.datasource_id == restored.id
Review Comment:
Right, the original row made it vacuous. Fixed in af91468fcd. Before
importing, the test renames the chart and sets its `datasource_id` to `None`,
and it now asserts that both come back: `slice_name == "my_chart"` and
`datasource_id == restored.id`. I checked it against the silent-skip path by
making `ImportChartsCommand` drop the chart (`config.get("dataset_uuid") in
{}`). The test then fails with `'renamed after the export' == 'my_chart'`. The
new dashboard test follows the same idea and clears the native filter before
importing.
`pytest tests/unit_tests/commands tests/unit_tests/semantic_layers
tests/unit_tests/dao`: 2433 passed. `ruff format --check` and `ruff check` are
clean.
--
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]