Skip to content

fix(dashboard): copy a layout that omits the source's current charts - #45472

Merged
aminghadersohi merged 3 commits into
apache:masterfrom
mikebridge:sc-128205-open-as-new-dashboard
Oct 20, 2026
Merged

aminghadersohi merged 3 commits into
apache:masterfrom
mikebridge:sc-128205-open-as-new-dashboard

Conversation

@mikebridge

@mikebridge mikebridge commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

SUMMARY

TL;DR: Copying a dashboard with a layout that leaves out some of the source dashboard's current charts failed with a generic 400 Arguments are not correct, and nothing was logged. This PR fixes the copy and logs the cause when a copy fails.

What users saw. In a dashboard's version history, choosing an older version and clicking "Open as new dashboard" showed "Failed to create new dashboard from this version: arguments are not correct", and no new dashboard was created. It happened whenever the dashboard had gained a chart since that older version, which is true of most dashboards that are still being worked on, so most older versions couldn't be opened as a new dashboard. The message was misleading (nothing was wrong with the request), and the server logged nothing, so the failure was invisible to operators too.

Why, in plain terms. Making the copy took two steps: first the new dashboard was given all of the original dashboard's charts as they are today, then it was trimmed down to the charts in the old version's layout. A chart that was added after the old version was therefore added and immediately removed again. The database layer skipped the add but still tried to carry out the remove, found nothing to delete, and aborted the whole copy. That internal error was turned into the generic message above and not logged.

What this changes. When a layout is supplied, the copy now takes its charts from that layout only, so there's no add-then-remove. And if a copy does fail for a database reason, the real error is now logged.

Background. In version history, "Open as new dashboard" forks an older version by posting that version's layout to POST /api/v1/dashboard/<id>/copy/ (without duplicate_slices). The copy endpoint rebuilds the new dashboard's charts from json_metadata.positions.

Problem. DashboardDAO.copy_dashboard first linked all of the source's current charts to the new dashboard (dash.slices = original_dash.slices). Then set_dash_metadata replaced that list with the charts in the layout. For each chart the layout dropped, the chart's unloaded Slice.dashboards backref collection was left holding a queued append followed by a remove. On flush, SQLAlchemy issued DELETE FROM dashboard_slices for a row that was never inserted. That raised StaleDataError, which the command's transaction handler turned into DashboardCopyError, and the API returned the generic 400. on_error logs only exceptions that carry an exception attribute, so nothing was logged.

Any older version that doesn't contain every chart the dashboard has today hits this. It also hits an empty layout. It happens on PostgreSQL and SQLite alike.

Changes.

  • DashboardDAO.copy_dashboard links the source's charts only when no layout is sent. With a layout, set_dash_metadata remains the only place that sets membership. The duplicate_slices path is unchanged, and the integration test covers it with partial and empty layouts too.
  • copy_dash logs DashboardCopyError with exc_info, so the underlying database error appears in the logs. The response is unchanged. The log sits in the REST route so the shared on_error helper, which every command's transaction uses, keeps its behavior. The MCP duplicate tool still reports the error without logging its cause.
  • Restoring a version in place ("Restore this version") doesn't have this problem: it sets the dashboard's charts once, from the version. A new integration test pins that for a chart gained since the version, one lost since, both at once, and the empty first version. It checks the dashboard's charts, the restored layout and the chart-membership history.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A (API behavior).

Before: POST /api/v1/dashboard/<id>/copy/ with a layout that omits a current chart returns 400 {"message": "Arguments are not correct"}. After: it returns 200, and the new dashboard has exactly the charts in the layout. The source dashboard is unchanged.

TESTING INSTRUCTIONS

  • Unit (SQLite): pytest tests/unit_tests/dao/test_copy_dashboard_legacy_ids.py -k dropped_from_layout. It fails with StaleDataError before the fix.
  • Integration (run on PostgreSQL): pytest tests/integration_tests/dashboards/api_tests.py -k "copy_dashboard_layout_without_current_charts or copy_dashboard_logs_database_error". Both fail before the fix.
  • Restore regression (no code change): pytest tests/integration_tests/dashboards/version_restore_tests.py -k TestDashboardRestoreLayoutMembership.
  • Manually: with version history on, add a chart to a dashboard, then open an earlier version in version history and choose "Open as new dashboard". The new dashboard opens with that version's charts.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Opening a dashboard version as a new dashboard sends that version's layout
to the copy endpoint. When the layout omitted a chart the source dashboard
has today, the copy failed with a generic 400 and nothing was logged.

copy_dashboard linked the source's current charts to the new dashboard
before set_dash_metadata rebuilt membership from the layout. Dropping a
chart then left a pending removal on that chart's unloaded dashboards
collection, so the flush deleted a dashboard_slices row that was never
inserted and raised StaleDataError. Link the source's charts only when no
layout is sent, and log the cause when a copy fails.
@github-actions github-actions Bot added the api Related to the REST API label Oct 19, 2026
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.60%. Comparing base (88e9266) to head (5481b9c).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #45472   +/-   ##
=======================================
  Coverage   83.59%   83.60%           
=======================================
  Files        3050     3050           
  Lines      200206   200206           
  Branches    46682    46682           
=======================================
+ Hits       167368   167373    +5     
+ Misses      29595    29589    -6     
- Partials     3243     3244    +1     
Flag Coverage Δ
hive 34.92% <0.00%> (-0.01%) ⬇️
mysql 52.90% <100.00%> (+<0.01%) ⬆️
postgres 52.91% <100.00%> (+<0.01%) ⬆️
presto 36.67% <0.00%> (-0.01%) ⬇️
python 87.16% <100.00%> (+<0.01%) ⬆️
sqlite 52.67% <100.00%> (+<0.01%) ⬆️
unit 81.28% <33.33%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mikebridge
mikebridge marked this pull request as ready for review October 19, 2026 19:18
@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt PR Risk: Low Risk

  • The PR appears safe to merge; the change is narrowly scoped to dashboard-copy membership handling and error logging.
  • Added tests cover copies with omitted charts and restoring versioned layouts with changed chart membership.

Assessed commit: 5481b9c50203

Restoring a dashboard version whose layout lacks a chart the dashboard
gained since, or has one it lost since, sets the dashboard's charts once
from the version. It doesn't add and then drop charts the way the copy
did. Pin that for both directions, for both at once and for the empty
baseline, checking the live membership, the restored layout and the
membership history.

@fitzee fitzee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, no blocking issues. The root cause is right: linking original_dash.slices and then letting set_dash_metadata replace the collection queues an append and a remove on the unloaded Slice.dashboards backref, and the flush then issues a DELETE for a dashboard_slices row that was never inserted. Membership ends up the same as before for every input that used to succeed, because set_dash_metadata already rebuilt it wholesale from positions. The fix just removes the redundant first link. Reverting only superset/daos/dashboard.py to master makes test_copy_without_charts_dropped_from_layout fail with StaleDataError: DELETE statement on table 'dashboard_slices' expected to delete 2 row(s); Only 0 were matched. With the fix, the whole unit file passes (12/12). CI on 5481b9c is green: test-postgres, test-mysql, test-sqlite and the unit tests pass, and the skipped jobs are frontend/docs only.

Three non-blocking follow-ups are inline. All of them predate this PR, but the version-fork flow makes them easier to hit.

)
value["meta"]["chartId"] = new_id
else:
elif metadata.get("positions") is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking, pre-existing. With this change the posted layout is the only thing that decides the copy's membership. Nothing on the server checks that the layout's chart IDs belong to the source or that the caller can access them. A non-duplicate copy whose layout references a chart from an unrelated dashboard links that chart to the new dashboard (confirmed in a SQLite DAO harness). createDashboardFromSnapshot filters unreachable charts through fetchReachableChartIds, but that runs in the browser and isn't access control. PUT on a dashboard you own has the same gap, so this PR doesn't widen it. That matters because dashboard membership feeds the dashboardId datasource grant in raise_for_access (viewer-promiscuous / embedded paths). It's worth a follow-up that limits layout chart IDs to the source's members, plus charts from version history the caller can access.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. This predates the PR (PUT on a dashboard you own has the same gap), so I've kept it out of this fix and it's tracked as a follow-up: limit the layout's chart IDs to the source's members, plus charts from version history the caller can access, on the server.

# flush delete dashboard_slices rows that were never inserted.
dash.slices = original_dash.slices

dash.params = original_dash.params

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking, pre-existing. set_dash_metadata starts from dash.params, which is the source's current metadata here, and only overlays the keys in the payload. For a version fork, any key the old snapshot lacks comes from today's dashboard. Keys that are carried through also aren't pruned to the layout's charts. With a layout that keeps only chart A, chart_configuration, timed_refresh_immune_slices and expanded_slices entries for the omitted chart B survive in the copy's json_metadata. Only filter_scopes and default_filters are filtered by slice_ids. The frontend seems to tolerate dangling cross-filter entries, but the fork isn't a faithful copy of the version.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, the copy starts from the source's current params and only overlays the posted keys, so a version fork inherits today's keys and per-chart entries for charts the layout dropped. Pre-existing, so not changing it here; tracked as a follow-up to build the fork's metadata from the snapshot and prune per-chart keys to the layout's charts.

if copied_id is not None:
db.session.delete(db.session.get(Dashboard, copied_id))
db.session.delete(dashboard)
# Clones carry the source names, including ones the layout left out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking. This cleanup shows the side effect of the duplicate_slices=True cases. The clone loop clones every non-archived member of the source, whether or not the layout references it. Charts the layout leaves out become orphan clones: owned by the caller, on no dashboard, and recorded as added-then-removed in the new dashboard's association history. The test only checks copied.slices, so it passes while leaving those orphans behind. Skipping clones whose original ID isn't in the layout's chart IDs would fix this. The remap and filter_scopes mapping already only use IDs from the layout. At minimum, assert the orphan count so a later change to this behavior is deliberate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. The clone loop has always cloned every non-archived member regardless of the layout, so that's pre-existing too. Tracked as a follow-up to skip clones whose original isn't in the layout and assert the orphan count in this test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Related to the REST API size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants