Skip to content

fix(notes): delete an object's note history in one batch - #17050

Merged
Maffooch merged 2 commits into
devfrom
fix/notes-delete-history-in-bulk
Nov 21, 2026
Merged

Maffooch merged 2 commits into
devfrom
fix/notes-delete-history-in-bulk

Conversation

@Maffooch

Copy link
Copy Markdown
Contributor

Description

Deleting a finding (or an engagement, test, risk acceptance or benchmark) deletes its notes through delete_related_notes. Each note's edit history was deleted by the per-note pre_delete receiver in dojo/notes/signals.py, about four queries per note, so the cost grew with the number of notes: a single-finding DELETE with 50 notes ran 312 queries, with 5 notes 131.

delete_related_notes now reads the notes' history ids in one query, deletes the notes with that receiver standing down (a ContextVar, so it is thread and async safe), and deletes the history rows in one query. The rows deleted are the same; only the number of statements changes. A note deleted on its own (the note delete view, or any other Notes.delete()) still takes its history through the receiver. The debug log line no longer runs a COUNT(*) on every call.

Test results

  • New unittests/test_notes_delete_in_bulk.py: the query count of delete_related_notes is the same for 1 note and for 10 (17 vs 53 before the change), every note and history row is gone, another object's notes and history are kept, an object with no notes costs one query, and a note deleted on its own still takes its history. The two query-count tests fail on dev without the change.
  • Measured through DELETE /api/v2/findings/<id>/ (medians of 7 runs): 50 notes 312 to 115 queries, 0.108 s to 0.054 s of SQL; 5 notes 131 to 115 queries.
  • Also ran the existing suites that create, read or delete notes (test_api_notes_nplusone, test_apiv2_delete_preview, test_bulk_delete_findings_m2m, test_copy_model, test_risk_acceptance, test_risk_acceptance_api, test_note_ui_private_authz, test_apiv2_note_visibility, test_jira_helper, test_legacy_table_fk_removal, test_deduplication_logic): the same results with and without the change.
  • ruff check (ruff 0.16.10, ruff.toml) passes on dojo/notes/.

Documentation

No user-visible change; no documentation needed.

Checklist

  • Make sure to rebase your PR against the very latest dev.
  • Submit all PRs, features and bug fixes alike, against the dev branch.
  • Give a meaningful name to your PR, as it may end up being used in the release notes.
  • Your code is Ruff compliant (see ruff.toml).
  • Your code is python 3.13 compliant.
  • If this is a new feature and not a bug fix, you've included the proper documentation in the docs at https://github.com/DefectDojo/django-DefectDojo/tree/dev/docs as part of this PR.
  • Model changes must include the necessary migrations in the dojo/db_migrations folder.
  • Add applicable tests to the unit tests.
  • Add the proper label to categorize your PR.

🤖 Generated with Claude Code

delete_related_notes deleted each note's edit history from the per-note
pre_delete receiver, about four queries per note, so deleting a finding
with a hundred notes cost some 500 queries. It now reads the history ids
once, deletes the notes with that receiver standing down, and deletes the
history rows in one query. A note deleted on its own still takes its
history through the receiver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Maffooch Maffooch added this to the 3.5.300 milestone Nov 21, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Maffooch
Maffooch added this pull request to the merge queue Nov 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 21, 2026
@Maffooch
Maffooch added this pull request to the merge queue Nov 21, 2026
Merged via the queue into dev with commit da47f99 Nov 21, 2026
49 checks passed
@Maffooch
Maffooch deleted the fix/notes-delete-history-in-bulk branch November 21, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants