Skip to content

Commit

Permalink
fix(dashboard-export): Fixes datasetId is not replaced with datasetUu…
Browse files Browse the repository at this point in the history
…id in Dashboard export in 4.1.x (#30425)
  • Loading branch information
fmannhardt authored Oct 9, 2024
1 parent 7a8e8f8 commit 211564a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
34 changes: 22 additions & 12 deletions superset/commands/dashboard/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ def _file_content(model: Dashboard) -> str:
logger.info("Unable to decode `%s` field: %s", key, value)
payload[new_name] = {}

# Extract all native filter datasets and replace native
# filter dataset references with uuid
for native_filter in payload.get("metadata", {}).get(
"native_filter_configuration", []
):
for target in native_filter.get("targets", []):
dataset_id = target.pop("datasetId", None)
if dataset_id is not None:
dataset = DatasetDAO.find_by_id(dataset_id)
if dataset:
target["datasetUuid"] = str(dataset.uuid)

# the mapping between dashboard -> charts is inferred from the position
# attribute, so if it's not present we need to add a default config
if not payload.get("position"):
Expand Down Expand Up @@ -180,16 +192,14 @@ def _export(
logger.info("Unable to decode `%s` field: %s", key, value)
payload[new_name] = {}

# Extract all native filter datasets and replace native
# filter dataset references with uuid
for native_filter in payload.get("metadata", {}).get(
"native_filter_configuration", []
):
for target in native_filter.get("targets", []):
dataset_id = target.pop("datasetId", None)
if dataset_id is not None:
dataset = DatasetDAO.find_by_id(dataset_id)
if dataset:
target["datasetUuid"] = str(dataset.uuid)
if export_related:
if export_related:
# Extract all native filter datasets and export referenced datasets
for native_filter in payload.get("metadata", {}).get(
"native_filter_configuration", []
):
for target in native_filter.get("targets", []):
dataset_id = target.pop("datasetId", None)
if dataset_id is not None:
dataset = DatasetDAO.find_by_id(dataset_id)
if dataset:
yield from ExportDatasetsCommand([dataset_id]).run()
45 changes: 45 additions & 0 deletions tests/integration_tests/dashboards/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,51 @@ def test_export_dashboard_command(self, mock_g1, mock_g2):
"version": "1.0.0",
}

# @pytest.mark.usefixtures("load_covid_dashboard")
@pytest.mark.skip(reason="missing covid fixture")
@patch("superset.security.manager.g")
@patch("superset.views.base.g")
def test_export_dashboard_command_dataset_references(self, mock_g1, mock_g2):
mock_g1.user = security_manager.find_user("admin")
mock_g2.user = security_manager.find_user("admin")

example_dashboard = (
db.session.query(Dashboard)
.filter_by(uuid="f4065089-110a-41fa-8dd7-9ce98a65e250")
.one()
)
command = ExportDashboardsCommand([example_dashboard.id])
contents = dict(command.run())

expected_paths = {
"metadata.yaml",
f"dashboards/COVID_Vaccine_Dashboard_{example_dashboard.id}.yaml",
"datasets/examples/covid_vaccines.yaml", # referenced dataset needs to be exported
"databases/examples.yaml",
}
for chart in example_dashboard.slices:
chart_slug = secure_filename(chart.slice_name)
expected_paths.add(f"charts/{chart_slug}_{chart.id}.yaml")
assert expected_paths == set(contents.keys())

metadata = yaml.safe_load(
contents[f"dashboards/World_Banks_Data_{example_dashboard.id}.yaml"]()
)

# find the dataset references in native filter and check if they are correct
assert "native_filter_configuration" in metadata["metadata"]

for filter_config in metadata["metadata"][
"native_filter_configuration"
].values():
assert "targets" in filter_config
targets = filter_config["targets"]

for column in targets:
# we need to find the correct datasetUuid (not datasetId)
assert "datasetUuid" in column
assert column["datasetUuid"] == "974b7a1c-22ea-49cb-9214-97b7dbd511e0"

@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices")
@patch("superset.security.manager.g")
@patch("superset.views.base.g")
Expand Down

0 comments on commit 211564a

Please sign in to comment.