fix(db): add FK constraints to convergence_trajectories and worktrees#95
Open
polishfreak wants to merge 1 commit intoodgrim:mainfrom
Open
fix(db): add FK constraints to convergence_trajectories and worktrees#95polishfreak wants to merge 1 commit intoodgrim:mainfrom
polishfreak wants to merge 1 commit intoodgrim:mainfrom
Conversation
Fixes odgrim#61 Adds migration 008 that recreates convergence_trajectories with ON DELETE CASCADE and worktrees with ON DELETE SET NULL, both referencing tasks(id). Since SQLite doesn't support ALTER TABLE for FK constraints, the tables are recreated and data is preserved via INSERT...SELECT (orphaned rows are dropped during the copy). Also cleans up existing orphaned agent_instances by NULL-ing out current_task_id references to nonexistent tasks. SET NULL is used for worktrees because on-disk cleanup should be handled explicitly rather than auto-deleting the record silently. submitted by Polish's bot :)
odgrim
requested changes
Feb 28, 2026
Owner
odgrim
left a comment
There was a problem hiding this comment.
Review: FK constraints migration — needs test fixes
Issue
The migration itself is well-written — the FK constraint additions, orphan cleanup, and SET NULL strategy for worktrees are all correct. However, the PR introduces 18 test failures:
- All 15
trajectory_repositorytests fail withFOREIGN KEY constraint failed - All 3
worktree_repositorytests fail with the same
Root cause
The test setup for these repositories inserts rows with task_id values that don't exist in the tasks table. With the new FK constraints enforced, SQLite rejects these inserts.
Fix needed
Each test that inserts into convergence_trajectories or worktrees must first insert a corresponding task row into the tasks table. For example, in the trajectory tests, before calling repo.save(&trajectory).await, ensure a task with id = trajectory.task_id exists.
A helper function like insert_test_task(pool, task_id) would keep the fix clean.
Everything else looks good
- Migration structure (CREATE new → INSERT...SELECT with JOIN → DROP old → RENAME) is the correct SQLite pattern
- Indexes are properly recreated
- SET NULL for worktrees is the right choice (disk cleanup should be explicit)
- CASCADE for convergence_trajectories is correct (no external resources)
- agent_instances orphan cleanup is a nice bonus
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.
Fixes #61
Adds migration 008 that recreates convergence_trajectories with ON DELETE CASCADE and worktrees with ON DELETE SET NULL, both referencing tasks(id). Since SQLite doesn't support ALTER TABLE for FK constraints, the tables are recreated and data is preserved via INSERT...SELECT (orphaned rows are dropped during the copy).
Also cleans up existing orphaned agent_instances by NULL-ing out current_task_id references to nonexistent tasks.
SET NULL is used for worktrees because on-disk cleanup should be handled explicitly rather than auto-deleting the record silently.
submitted by Polish's bot :)