Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dashboard-export): Fixes datasetId is not replaced with datasetUuid in Dashboard export in 4.1.x #30425

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 143 in superset/commands/dashboard/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dashboard/export.py#L138-L143

Added lines #L138 - L143 were not covered by tests

# 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 @@
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the risk of introducing yet more regressions, I feel it would be cleaner to do the following:

if export_related:
    for target in native_filter...

The point being, no sense in iterating over all datasets, if export_related is False, as we're not doing anything else here (look at how charts are exported above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Since this is now only to invoke the dataset export, it is fine to have the condition before iterating.

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:

Check warning on line 204 in superset/commands/dashboard/export.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/dashboard/export.py#L200-L204

Added lines #L200 - L204 were not covered by tests
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
Loading