Repository navigation
fix(chart): keep dataset_uuid in exports when the dataset is deleted - #44347
Conversation
Deleting a dataset soft-deletes it, so the global visibility filter hides the row from `Slice.table` while the chart itself survives. Exporting such a chart then produced a bundle with neither `dataset_uuid` on the chart YAML nor a `datasets/` file, and `ImportV1ChartSchema` — which has required `dataset_uuid` since the chart importer was added in apache#11743 — rejected Superset's own export with: {'dataset_uuid': ['Missing data for required field.']} Dashboards hit the same error because `ExportDashboardsCommand` exports its charts through `ExportChartsCommand`. Resolve the chart's dataset through the DAO with the visibility filter bypassed so a trashed dataset still reaches the bundle and the chart keeps its only link to it. The DAO base filter still applies, so a dataset the user cannot read stays unreachable. A chart whose dataset was hard-deleted has no row to resolve and still exports without the field, as before. Refs apache#44309 Deliberately not a closing keyword: this fixes the chart and dashboard half. The dataset GENERIC_COMMAND_ERROR reported in the same issue did not reproduce on master and is not addressed here, so the issue should stay open until that half is understood.
Code Review Agent Run #30ecd9Actionable Suggestions - 0Additional Suggestions - 2
Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| contents = _export(chart) | ||
|
|
||
| ImportChartsCommand(contents, overwrite=True).run() | ||
| db.session.flush() | ||
|
|
||
| restored = db.session.query(SqlaTable).filter_by(uuid=dataset.uuid).one() |
There was a problem hiding this comment.
Suggestion: This test blesses a hard-deleted chart bundle without dataset_uuid, although the importer requires that field for every chart, so it treats an unusable export as valid. [api mismatch]
Assessment: 🟠 Major · 🔁 Occurrence: Rarely
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** tests/unit_tests/commands/chart/export_test.py
**Line:** 141:146
**Comment:**
*Api Mismatch: This test blesses a hard-deleted chart bundle without `dataset_uuid`, although the importer requires that field for every chart, so it treats an unusable export as valid.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fixThere was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of af91468.
The test now imports the export made after deleting the dataset, then verifies the dataset is restored and the chart is reattached to it.
If that's not right, unresolve this thread and CodeAnt will leave it open.
There was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of e81fe80.
The export test now asserts that the chart payload contains the expected dataset_uuid and that the bundled dataset has the matching UUID.
If that's not right, unresolve this thread and CodeAnt will leave it open.
There was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of e832160.
The export test now asserts that the chart payload contains the expected dataset_uuid and that the bundled dataset has the matching UUID.
If that's not right, unresolve this thread and CodeAnt will leave it open.
There was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 572dece.
The test now imports the export after deleting the dataset and asserts that both the dataset and chart are restored, including the chart’s link to the restored dataset.
If that's not right, unresolve this thread and CodeAnt will leave it open.
There was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 3bfea04.
The test now imports the export after deleting the dataset and asserts that both the dataset and chart are restored, including the chart’s link to the restored dataset.
If that's not right, unresolve this thread and CodeAnt will leave it open.
There was a problem hiding this comment.
✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of 4013704.
The test now imports the export after deleting the dataset and asserts that both the dataset and chart are restored, including the chart’s link to the restored dataset.
If that's not right, unresolve this thread and CodeAnt will leave it open.
|
The flagged issue is not correct. The test tests/unit_tests/commands/chart/export_test.py |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #44347 +/- ##
===========================================
+ Coverage 65.16% 83.12% +17.95%
===========================================
Files 3048 3032 -16
Lines 199661 198132 -1529
Branches 46583 45591 -992
===========================================
+ Hits 130116 164703 +34587
+ Misses 66618 30194 -36424
- Partials 2927 3235 +308
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Good catch on the soft-delete relationship gap, this closes a real import-breaking hole. One thing before this merges though: sadpandajoe's right that all three new tests mock Could you add a case where the caller can't access the dataset and assert the bundle doesn't leak it? Also curious about codeant's point on the hard-delete test, since the importer still requires |
…d dataset The three existing tests run with can_access_all_datasources patched to True, so a lookup that also skipped the permission filter would still pass them. Add a chart editor with no grant on the dataset: - the chart export gives them the chart alone, with no dataset_uuid and no datasets/ or databases/ file, with and without export_related; - the nested dataset export still raises DatasetNotFoundError for them when include_deleted lifts the soft-delete filter. Signed-off-by: L4XB <[email protected]>
|
@rusackas Both done. Access. Added in 768bf00: a chart editor with no role and no grant on the dataset, so
To check that they guard the bypass, I widened each lookup to skip the permission filter as well:
The file passes 6/6 as pushed, and Hard delete. I measured it. After I'd treat it as accepted for this PR. The chart no longer has any datasource, so the importer can't recreate it, and refusing is correct. The weak part is the message, which doesn't say the dataset is gone. If you want it handled, a follow-up could either have the export skip such a chart with a warning, or have the importer name the chart and the missing dataset. I'd lean towards the importer, since that keeps the chart's params in the bundle. |
Code Review Agent Run #4808d3Actionable Suggestions - 0Additional Suggestions - 1
Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
…port-round-trip # Conflicts: # superset/commands/chart/export.py
|
Merged master to resolve the conflict in
Checked on the merged tree:
|
… the trash ExportDashboardsCommand looked up the datasets of native filters and display controls without the trash, so a filter on a soft-deleted dataset lost its datasetId without gaining a datasetUuid, and the bundle did not carry the dataset. Look the datasets up with skip_visibility_filter and export them with include_deleted, as the chart export does for a chart's own dataset. ExportModelsCommand only passes skip_visibility_filter when include_deleted is set, so other callers keep the plain lookup.
The chart round trip passed even if ImportChartsCommand skipped the chart, since the original row kept its name and dataset id. Rename the chart and clear its dataset id before importing, so both have to come back from the bundle. Add the dashboard case: a native filter on a soft-deleted dataset keeps its datasetUuid through export, and import points it at the restored dataset. The filter is cleared before importing, so it has to come back from the bundle too.
CodeAnt PR Risk: Medium Risk
Assessed commit: |
The filter in the round-trip test targeted the chart's own dataset, which ExportChartsCommand bundles anyway, so the test stayed green with the trash-inclusive lookup in ExportDashboardsCommand._export reverted. The filter now targets a second dataset in the trash that no chart on the dashboard uses, and the mocked dashboard tests check that both lookups pass skip_visibility_filter=True. Reverting either lookup now fails the round trip and one mocked test.
Two behaviours the export fix relies on had no test that would fail without them. ExportDatasetsCommand keeps a readable dataset in the trash out unless include_deleted=True, so the dataset export endpoint and other callers do not start exporting trashed datasets. And find_chart_dataset looks a dataset up by datasource_id only for a table chart, so exporting a semantic-view chart with its related assets does not bundle an unrelated table that has the same numeric id. Dropping the flag check, or the datasource_type guard, now fails one test each.
ExportAssetsCommand exports datasets from the visibility-filtered find_all(), which leaves the trash out, while the chart files now write dataset_uuid for a dataset in the trash and native filters their datasetUuid. ImportAssetsCommand looks those up in the bundle, so it raised a KeyError for the missing dataset instead of the schema error it gave before. The assets export now also exports the datasets in the trash that the exported charts and native filters or display controls name, with the DAO base filter still applied. The dataset ids a dashboard's filters target are read by a shared dashboard_dataset_ids() helper.
Review of apache#44347: - A chart's dataset came from Slice.table, which is loaded without the DAO base filter, so a live dataset the user cannot read reached ExportDatasetsCommand and failed the whole export. Chart and filter ids now share one filtered lookup. - A dashboard whose json_metadata is null or a list parsed, then raised AttributeError and stopped every export. Such metadata names no targets. - dashboard_dataset_ids read a semantic-view target's datasetId as a table id, so an unrelated trashed table sharing the number was bundled. It now reads table targets only.
SUMMARY
Exporting a chart whose dataset has been deleted produces a ZIP that Superset's
own importer rejects. This is the chart and dashboard half of #44309.
ExportChartsCommandreads the chart's dataset off theSlice.tablerelationship:
superset/commands/chart/export.py:77-78Deleting a dataset soft-deletes it (
SOFT_DELETEdefaults toTrue,superset/config.py:715), and the global visibility listener_add_soft_delete_filterdeliberately covers relationship loads(
superset/models/helpers.py:1501).Slice.table(
superset/models/slice.py:169) therefore resolves toNonewhile the chartrow itself survives — the "Missing dataset. The dataset linked to this chart
may have been deleted." state.
The same
model.tableguard also gates the nested dataset export(
superset/commands/chart/export.py:130-132), so the bundle loses both thedataset_uuidkey and thedatasets/*.yamlfile.ImportV1ChartSchemadeclares
dataset_uuid = fields.UUID(required=True)(
superset/charts/schemas.py:1998), and validation fails with exactly theerror in the report:
Dashboards fail identically because
ExportDashboardsCommandexports itscharts through
ExportChartsCommand(
superset/commands/dashboard/export.py:435).This is an exporter defect, not an importer one.
dataset_uuidhas beenrequired=Truesince #11743, the commit that introduced the chart importer,and the
if model.table:guard has been there since #11349 introduced ZIPexport — both in 2020. Neither side changed; soft delete made the gap
reachable from the ordinary Delete button.
The fix
Resolve the chart's dataset through the DAO with the soft-delete visibility
filter bypassed, so a trashed dataset still reaches the bundle and the chart
keeps its only link to it:
skip_visibility_filtersuppresses only the soft-delete predicate; the DAObase filter still runs, so a dataset the user is not permitted to read stays
unreachable here exactly as it is through the relationship. Restoring the
dataset on import is the existing, already-tested behaviour of
import_dataset, which treats a re-imported soft-deleted UUID as an implicitrestore-with-update.
Nested exports need the same bypass one level down, so
ExportModelsCommand.__init__gains aninclude_deletedkeyword (defaultFalse, so every other caller is unchanged) thatvalidate()forwards tofind_by_ids. This is an internal command constructor argument, not a RESTAPI change.
Why the importer is not also relaxed
Making
dataset_uuidoptional would trade a loud failure for a silent one.ImportChartsCommand._importonly imports a chart when its dataset is presentin the bundle:
superset/commands/chart/importers/v1/__init__.py:102A chart with no
dataset_uuidwould fall through that condition and beskipped without an error, so the user would be told the import succeeded while
the chart was never created.
This also means already-exported ZIPs cannot be repaired by any import-side
change: they are missing the
datasets/*.yamlfile itself, not just thepointer to it, so the information needed to restore the chart is not in the
archive. Re-exporting after this fix produces a complete bundle. That is worth
saying plainly rather than implying existing backups are covered.
Dashboard filters (added after review)
A dashboard's native filters and display controls name datasets too.
ExportDashboardsCommandlooked those up without the trash, so a filter on asoft-deleted dataset lost its
datasetIdwithout gaining adatasetUuid, andthe bundle did not carry the dataset. Both lookups now include the trash, and
the datasets are exported with
include_deleted=True. A re-imported filterthen points at the restored dataset.
Scope
The reported dataset symptom is a separate defect and is not addressed
here. Datasets fail with
GENERIC_COMMAND_ERROR, whichImportModelsCommand.runraises for any unexpected exception, so it carries nodiagnostic. A dataset export → delete → re-import round trip succeeds on
masterin the unit tier (the dataset is restored), so I could not reproduceit and have not guessed at a cause. #44309 should stay open for it after this
merges.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Backend-only; the observable change is the contents of the exported ZIP.
Before — chart whose dataset was deleted, exported by
ExportChartsCommand:After — same chart, same command:
TESTING INSTRUCTIONS
Manually, against a running instance:
dataset").
charts/*.yamlcarriesdataset_uuidand adatasets/*.yamlis present.Automated — three unit tests are added in
tests/unit_tests/commands/chart/export_test.py:Two of the three are discriminating. On the merge base (
5dd63ef95b), with thenew test file copied in and the source untouched:
with the failure being the reported error verbatim:
On this branch:
The third test is a regression guard rather than a reproduction: it pins that a
hard-deleted dataset (no row to resolve) still exports the chart file without
dataset_uuidinstead of raising, which is why it also passes on the base.Every unit test file naming a symbol this touches was run together, plus the
chart, dashboard and dataset importer suites — 315 passed:
The surrounding tree was run on the merge base and on this branch to separate
this change from any pre-existing failure.
tests/unit_tests/commands/goesfrom 1232 passed on the base to 1235 passed here — a difference of
exactly the three added tests, with no failures on either side. For
tests/unit_tests/{charts,datasets,dashboards,daos,models,utils}/both treesgive an identical 2280 passed, 2 xfailed.
pre-commit run --filesover the three changed files passes, includingmypy (main),ruff,ruff-formatandpylint with custom Superset plugins.This is the unit tier only; the integration tier needs a provisioned metadata
database that I did not have available.
ADDITIONAL INFORMATION
GENERIC_COMMAND_ERRORin that issue is a separate defect and is notfixed here)
SOFT_DELETE(defaults toTrue) is what makesthe bug reachable through the Delete button; the export gap itself
predates it and also applies to a hard-deleted dataset
File and line references are against
master@5dd63ef95b2c3a2dd913371492c28598ed27930a.