Repository navigation
fix(cameras): cascade camera deletion to its history and stored images - #744
Merged
MateoLostanlen merged 4 commits intoNov 19, 2026
Merged
Conversation
Deleting a camera with poses, sequences or detections failed on a foreign key (500). The foreign keys to cameras, and those of occlusion masks and alert links, now cascade. The route recomputes the alerts that held a sequence of the camera (deleting those left empty) in the same transaction, then deletes the stored images and crops that no row references anymore. Part of pyronear#541. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #744 +/- ##
==========================================
+ Coverage 95.73% 95.78% +0.04%
==========================================
Files 65 65
Lines 3732 3768 +36
==========================================
+ Hits 3573 3609 +36
Misses 159 159
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The fixture frames all share a key that camera 2 keeps, so the failure test never reached the patched deletion. Camera 1 now gets a key of its own, and the test asserts the deletion is attempted. Also cover the batching and error logging of S3Bucket.delete_files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MateoLostanlen
requested changes
Nov 18, 2026
MateoLostanlen
left a comment
Member
There was a problem hiding this comment.
Hi @Majyles, thanks for the PR! Clean approach and nice tests.
- Please wrap the DB part of
delete_camera(reading alert ids through the commit) in_organization_alert_lock(camera.organization_id), likelabel_sequencedoes. Otherwise a validation run can attach a sequence of this camera to a new alert in between, and that alert ends up empty forever. - Please add an index on
alerts_sequences.sequence_id(model + migration). The PK starts withalert_id, so each cascaded sequence delete scans the whole table, which gets slow for old cameras. - (nit) Bucket keys start with the camera id and ids are never reused, so the "still referenced" check could be dropped. At least crops only need checking against
crop_bucket_key, which has no index. - (nit) Worth noting in the description that deleting a pose now also deletes its occlusion masks.
Happy to merge once the first two points are fixed.
…eletion Address review: - run the DB part of the deletion under the organization alert lock, so a validation run cannot attach a sequence of the camera to a new alert between the read of its alerts and the commit (that alert would stay empty) - index alerts_sequences.sequence_id: the primary key starts with alert_id and cannot serve the cascade's lookups by sequence - delete the camera's objects without checking references: every key starts with the camera id, and ids are never reused - rebase the migration onto a9c2e5f7b1d3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Thanks for the review! All four points are addressed:
I also merged |
MateoLostanlen
approved these changes
Nov 19, 2026
MateoLostanlen
left a comment
Member
There was a problem hiding this comment.
Thanks @Majyles, all four points look good. Approving!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #541 (camera deletion; organization deletion will follow in a separate PR).
Problem
DELETE /cameras/{id}returned a 500 as soon as the camera had poses, sequences or detections (poses_camera_id_fkeyviolation).Changes
94a223eae6ac):poses,sequencesanddetections→cameras,occlusion_masks→poses,alerts_sequences→sequences. The downgrade restores the previous constraints.refresh_alert_stateon each one, in the same transaction. Alerts left empty are deleted (with their shares, via the existing cascade); the others get their bounds and location recomputed from the remaining cameras.delete_objectsin batches of 1000 (newS3Bucket.delete_files). A storage failure is logged and does not fail the request, since the rows are already gone.occlusion_masks.pose_idcascades). A pose with detections or sequences still cannot be deleted, see the question below.Tests
The three tests fail on
mainwith the foreign key error. Locally: full suite passes, migration upgrade/downgrade/upgrade checked (pg_constraint.confdeltype),alembic checkclean.Notes for review
detectionstable. If needed, the constraints could be added asNOT VALIDand then validated separately.DELETE /poses/{id}still fails when the pose has detections or sequences (detections.pose_idis non-nullable). Should the history be deleted, detached, or should poses with history only be deactivated throughactive(returning a 409 instead of a 500)? Happy to handle it in a follow-up.Generated with Claude Code