Skip to content

delete chart config from dashboard , when we delete the chart#1409

Open
himanshudube97 wants to merge 1 commit into
mainfrom
delete-chart-deletes-it-from-dashboard-too
Open

delete chart config from dashboard , when we delete the chart#1409
himanshudube97 wants to merge 1 commit into
mainfrom
delete-chart-deletes-it-from-dashboard-too

Conversation

@himanshudube97

@himanshudube97 himanshudube97 commented Jun 11, 2026

Copy link
Copy Markdown
Member

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

    • Charts are now automatically removed from all organization dashboards when deleted.
    • Chart deletion operations use atomic transactions to prevent orphaned chart tiles on dashboards.
  • Tests

    • Added comprehensive tests for chart deletion cascade cleanup across dashboards.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

This 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.

Changes

Chart cascade cleanup on deletion

Layer / File(s) Summary
Cascade cleanup infrastructure
ddpui/services/chart_service.py
Adds django.db.transaction import and a new _remove_chart_from_dashboards(chart_ids, org) helper that iterates through each dashboard's tabs and layout configurations, removes chart components whose chartId matches the provided IDs, updates tabs and layout_config only for modified dashboards, and logs the removals.
Single-chart deletion with cascade
ddpui/services/chart_service.py
delete_chart wraps dashboard cleanup and chart deletion inside transaction.atomic(), calling the helper to strip chart references before deleting the chart record.
Bulk deletion with cascade
ddpui/services/chart_service.py
bulk_delete_charts wraps cascade cleanup for all found chart IDs and the bulk delete call inside a single transaction.atomic() block, ensuring dashboard updates and chart deletions commit together.
Cascade cleanup test coverage
ddpui/tests/services/test_chart_service.py
Adds TestDeleteChartCascadeCleanup class with helper _make_dashboard_with_chart and four test cases verifying: deleting an embedded chart removes its component and layout entry, deleting one chart leaves other components intact, deleting a chart not in any dashboard is a no-op, and bulk deletion removes all referenced charts from dashboards.

Sequence Diagram

sequenceDiagram
  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
Loading

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main objective: removing chart configuration from dashboards when charts are deleted, matching the PR's core intent.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch delete-chart-deletes-it-from-dashboard-too

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@himanshudube97 himanshudube97 self-assigned this Jun 11, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
ddpui/tests/services/test_chart_service.py (1)

286-464: 💤 Low value

Consider 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1cff2fb and 85e59da.

📒 Files selected for processing (2)
  • ddpui/services/chart_service.py
  • ddpui/tests/services/test_chart_service.py

@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.91%. Comparing base (1cff2fb) to head (85e59da).

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.
📢 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.

raise ChartPermissionError("You can only delete charts you created.")

chart_title = chart.title
chart.delete()

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.

@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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants