delete chart config from dashboard , when we delete the chart#1409
delete chart config from dashboard , when we delete the chart#1409himanshudube97 wants to merge 1 commit into
Conversation
WalkthroughThis PR implements cascade cleanup for chart deletion. When a chart is deleted (single or bulk), the operation now automatically removes all references to that chart from dashboards in the organization, then deletes the chart itself—all within a single database transaction to prevent dangling chart tiles if deletion fails mid-operation. ChangesChart cascade cleanup on deletion
Sequence DiagramsequenceDiagram
participant Client
participant ChartService
participant Transaction
participant Dashboard
participant Chart
Client->>ChartService: delete_chart or bulk_delete_charts
ChartService->>Transaction: atomic() context
ChartService->>ChartService: _remove_chart_from_dashboards(chart_ids, org)
ChartService->>Dashboard: iterate dashboards
ChartService->>Dashboard: remove chart components from tabs
ChartService->>Dashboard: update layout_config
Dashboard->>Dashboard: save only modified dashboards
ChartService->>Chart: delete chart record(s)
Transaction->>Transaction: commit or rollback
🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
ddpui/tests/services/test_chart_service.py (1)
286-464: 💤 Low valueConsider adding a multi-tab test case.
All existing tests use single-tab dashboards. The service implementation loops through
dashboard.tabs, so adding one test that embeds the same chart in multiple tabs would provide more thorough coverage of that loop.Example multi-tab test structure
def test_delete_chart_removes_from_all_tabs(self, orguser, org, seed_db): """Deleting a chart removes it from all tabs that embed it.""" chart = Chart.objects.create( title="Multi-tab Chart", chart_type="bar", schema_name="public", table_name="users", extra_config={}, created_by=orguser, last_modified_by=orguser, org=org, ) dashboard = Dashboard.objects.create( title="Multi-tab Dashboard", dashboard_type="native", grid_columns=12, tabs=[ { "id": "tab-1", "title": "Tab 1", "layout_config": [{"i": "c1", "x": 0, "y": 0, "w": 6, "h": 4}], "components": { "c1": { "type": DashboardComponentType.CHART.value, "config": {"chartId": chart.id}, } }, }, { "id": "tab-2", "title": "Tab 2", "layout_config": [{"i": "c2", "x": 0, "y": 0, "w": 6, "h": 4}], "components": { "c2": { "type": DashboardComponentType.CHART.value, "config": {"chartId": chart.id}, } }, }, ], created_by=orguser, org=org, ) ChartService.delete_chart(chart.id, org, orguser) dashboard.refresh_from_db() assert "c1" not in dashboard.tabs[0]["components"] assert "c2" not in dashboard.tabs[1]["components"] dashboard.delete()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ddpui/tests/services/test_chart_service.py` around lines 286 - 464, Add a multi-tab test to TestDeleteChartCascadeCleanup to exercise the loop over dashboard.tabs: create a chart and a Dashboard with at least two tabs each embedding that chart (use Dashboard.objects.create in the test), call ChartService.delete_chart(chart.id, org, orguser), refresh the dashboard and assert that the chart component key is removed from components and layout_config in every tab (e.g. add a new test named test_delete_chart_removes_from_all_tabs that mirrors existing tests' structure and cleanup).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@ddpui/tests/services/test_chart_service.py`:
- Around line 286-464: Add a multi-tab test to TestDeleteChartCascadeCleanup to
exercise the loop over dashboard.tabs: create a chart and a Dashboard with at
least two tabs each embedding that chart (use Dashboard.objects.create in the
test), call ChartService.delete_chart(chart.id, org, orguser), refresh the
dashboard and assert that the chart component key is removed from components and
layout_config in every tab (e.g. add a new test named
test_delete_chart_removes_from_all_tabs that mirrors existing tests' structure
and cleanup).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 264d58fd-5b86-4918-b483-e078e2ffb1f0
📒 Files selected for processing (2)
ddpui/services/chart_service.pyddpui/tests/services/test_chart_service.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1409 +/- ##
==========================================
+ Coverage 59.86% 59.91% +0.05%
==========================================
Files 138 138
Lines 16560 16583 +23
==========================================
+ Hits 9913 9936 +23
Misses 6647 6647 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| raise ChartPermissionError("You can only delete charts you created.") | ||
|
|
||
| chart_title = chart.title | ||
| chart.delete() |
There was a problem hiding this comment.
@himanshudube97 do we really need to do this ?
We purposely didn't do it in the first go. It was because we didn't want the dashboard layout to look weird once they delete the chart (with a missing chart). We said (if i remember correctly), they can go and remove it from the dashboard first & then delete the chart.
Thats why there is a message saying which dashboard(s) the chart is part of
https://linear.app/dalgo/issue/DALGO-1418/when-a-chart-is-deleted-it-leaves-this-empty-block-with-an-error
Summary by CodeRabbit
Bug Fixes
Tests