Skip to content

Fix/database deletes#767

Open
jason-ford-codecov wants to merge 6 commits intomainfrom
fix/database_deletes
Open

Fix/database deletes#767
jason-ford-codecov wants to merge 6 commits intomainfrom
fix/database_deletes

Conversation

@jason-ford-codecov
Copy link
Contributor

@jason-ford-codecov jason-ford-codecov commented Mar 12, 2026

This adds more logging to the deletion process

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

Low Risk
Primarily adds logging and exception handling around existing _raw_delete calls; low functional risk, but log volume could increase during large cleanups.

Overview
Adds detailed instrumentation around database deletions in cleanup_queryset, logging the model name and a truncated SQL query before deletes and the affected row count after.

Wraps _raw_delete to explicitly log and re-raise SoftTimeLimitExceeded and OperationalError (connection drops), and safely handles EmptyResultSet when rendering the query string.

Written by Cursor Bugbot for commit 8a8170b. This will update automatically on new commits. Configure here.

Comment on lines +46 to +54
try:
cleaned_models = query._raw_delete(query.db)
except SoftTimeLimitExceeded:
log.warning(
"cleanup_queryset: soft time limit hit, rolling back delete of %s",
model.__name__,
exc_info=True,
)
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: The _raw_delete call can raise EmptyResultSet for empty querysets, but this exception is not caught, which will cause the cleanup task to fail unnecessarily.
Severity: MEDIUM

Suggested Fix

Wrap the _raw_delete(query) call in a try...except EmptyResultSet block. Inside the except block, you can simply pass or log that no rows were deleted, as this is an expected outcome for an empty queryset.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: apps/worker/services/cleanup/cleanup.py#L46-L54

Potential issue: The `_raw_delete` function in `cleanup_queryset` is not wrapped in a
`try...except` block that handles `django.db.models.query.EmptyResultSet`. This
exception can be raised when processing an empty queryset, which is a normal scenario
when there are no records to delete. While the code correctly handles this exception
when generating a query string for logging, it fails to do so for the actual delete
operation. This inconsistency will cause the entire cleanup task to fail when it
encounters an empty set of records, treating a valid "0 rows deleted" case as an error.

@sentry
Copy link
Contributor

sentry bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 64.70588% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.25%. Comparing base (b685b72) to head (8a8170b).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
apps/worker/services/cleanup/cleanup.py 64.70% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #767      +/-   ##
==========================================
- Coverage   92.26%   92.25%   -0.01%     
==========================================
  Files        1304     1304              
  Lines       47921    47937      +16     
  Branches     1628     1628              
==========================================
+ Hits        44216    44226      +10     
- Misses       3396     3402       +6     
  Partials      309      309              
Flag Coverage Δ
workerintegration 58.52% <17.64%> (-0.04%) ⬇️
workerunit 90.36% <64.70%> (-0.03%) ⬇️

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

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

@codecov-notifications
Copy link

Codecov Report

❌ Patch coverage is 64.70588% with 6 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
apps/worker/services/cleanup/cleanup.py 64.70% 6 Missing ⚠️

❌ Your patch check has failed because the patch coverage (64.70%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

📢 Thoughts on this report? Let us know!

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.

1 participant