perf(bundle_analysis): Batch delete bundles to avoid N+1 queries#875
perf(bundle_analysis): Batch delete bundles to avoid N+1 queries#875sentry[bot] wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 328e0f0. Configure here.
| if len(sessions_to_delete) != len(bundles_to_delete): | ||
| raise Exception( | ||
| "Data integrity error - cannot have Bundles without Sessions" | ||
| ) |
There was a problem hiding this comment.
Aggregate count check weakens data integrity validation
Medium Severity
The integrity check len(sessions_to_delete) != len(bundles_to_delete) only compares aggregate counts, unlike the original one_or_none() which validated each bundle individually. If one bundle has two sessions and another has zero, the counts balance out (e.g., 2 sessions == 2 bundles) and the check passes — silently masking a real data integrity issue where a bundle exists without a session. The original per-bundle one_or_none() would have caught both the missing-session and the multiple-sessions cases independently.
Reviewed by Cursor Bugbot for commit 328e0f0. Configure here.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #875 +/- ##
==========================================
- Coverage 92.25% 92.22% -0.04%
==========================================
Files 1307 1307
Lines 48017 48037 +20
Branches 1636 1642 +6
==========================================
+ Hits 44299 44300 +1
- Misses 3407 3426 +19
Partials 311 311
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |


Fixes WORKER-Y98. The issue was that: Bundle deletion iterates, querying each non-cached bundle individually, causing N+1 database queries.
delete_bundles_by_namestoBundleReportfor efficient batch deletion of multiple bundles and their associated sessions, assets, chunks, and modules._process_bundle_reportmethod in the worker service to collect bundle names for deletion and then use the new batch deletion method.This fix was generated by Seer in Sentry, triggered automatically. 👁️ Run ID: 13573979
Not quite right? Click here to continue debugging with Seer.
Legal Boilerplate
Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.
Note
Medium Risk
Changes bundle-analysis deletion logic to perform multi-row deletes in one DB session, which reduces query load but increases blast radius if the bundle name filter or session linkage is wrong. Data integrity assumptions are enforced via a new exception when bundles and sessions don’t match 1:1.
Overview
Speeds up bundle-analysis report carry-forward by batch deleting bundles that no longer have caching enabled, instead of deleting each bundle individually.
Adds
BundleAnalysisReport.delete_bundles_by_names()to delete matchingBundles and their associatedSession/Asset/Chunk/Modulerows in a single database session, and updates the worker’s_attempt_init_from_previous_reportto collect bundle names and call this new method.Reviewed by Cursor Bugbot for commit 328e0f0. Bugbot is set up for automated code reviews on this repo. Configure here.