Repository navigation
fix(backend): add self-reference guard, label/webhook_log FKs, retention index - #167
Merged
Merged
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughChangesThe database migration list now includes migration 0002. The migration rebuilds Schema constraint migration
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
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.
What
Migration
0002_schema_constraints.sql, addressing the 4 schema-hardening items CodeRabbit flagged on #166 (against schema content that PR only renamed, didn't modify):item_dependencies:CHECK (item_id != depends_on_item_id)— there was no guard anywhere (app or db) against an item depending on itself.item::add_dependencydoes a rawINSERT OR IGNORE.labels.project_idnowREFERENCES projects(id)(still nullable — NULL means a workspace-only label).webhook_logs.webhook_idnowREFERENCES webhooks(id).webhook_logs: added an index oncreated_atto make a future retention/purge query cheap. Raw request/response bodies here can carry secrets/PII forwarded through webhooks with no TTL today — actually scheduled purging is a separate, not-yet-requested piece of work; this just removes the cost of adding it later.Implementation notes
SQLite has no
ALTER TABLE ADD CONSTRAINT, so adding aCHECKorFOREIGN KEYto an existing table means rebuilding it. Each rebuild here creates the replacement under a_newname, copies data across, drops the original, then renames_newinto the original's name — deliberately in that order. Doing it the other way (rename original to_oldfirst) hits a real SQLite gotcha: renaming a table away from its name silently rewrites other tables' existing FK definitions that reference it by that name (item_labels.label_id REFERENCES labels(id)becameREFERENCES labels_old(id)when I first tried it, breaking label assignment oncelabels_oldwas dropped). Going new-name-first sidesteps this since nothing references the_newname. Caught by a fullcargo test -p agentflare-backendrun before pushing — two pre-existing label tests failed until this was fixed.The
item_dependenciesself-reference CHECK: any pre-existing violating row is silently dropped during the copy rather than failing the migration (there's no way for a self-referencing row to have been intentional, and failing the migration over it would just brick startup for whoever has one).Testing
Added to
crates/agentflare-backend/src/db.rs:migration_0002_preserves_data_and_drops_bad_self_reference— seeds a v1-only db (only migration0001applied, matching real installedbackend.dbstate before this PR) with valid data plus one self-referencingitem_dependenciesrow, opens it throughopen_db, and asserts: the bad row is gone, all other data survived,user_versionreflects both migrations applied.migration_0002_enforces_new_constraints— on a fresh db, asserts inserting a self-referencing dependency, a label with a bogusproject_id, and a webhook_log with a boguswebhook_idall now fail with a constraint violation.Also ran:
cargo test -p agentflare-backend— 59/59 passcargo build --workspacecargo clippy --locked --workspace --all-targets --all-features -- -D warnings -A unsafe_code -A clippy::pedantic(CI's exact flags)cargo fmt --checkcargo deny checkSummary by CodeRabbit