Skip to content

fix(db): add FK constraints to convergence_trajectories and worktrees#95

Open
polishfreak wants to merge 1 commit intoodgrim:mainfrom
polishfreak:fix/61-cascade-delete-orphans
Open

fix(db): add FK constraints to convergence_trajectories and worktrees#95
polishfreak wants to merge 1 commit intoodgrim:mainfrom
polishfreak:fix/61-cascade-delete-orphans

Conversation

@polishfreak
Copy link
Contributor

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 :)

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 :)
Copy link
Owner

@odgrim odgrim left a comment

Choose a reason for hiding this comment

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

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_repository tests fail with FOREIGN KEY constraint failed
  • All 3 worktree_repository tests 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

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.

Bug: convergence trajectory records have no cascade delete — orphaned rows when task is deleted

2 participants