Skip to content

Let migrations apply the schema's own delete rules instead of refusing to start - #1494

Merged
dylanplayer merged 2 commits into
mainfrom
codevisor/raclette
Nov 26, 2026
Merged

dylanplayer merged 2 commits into
mainfrom
codevisor/raclette

Conversation

@dylanplayer

Copy link
Copy Markdown
Member

Why

Migrations run with foreign_keys = OFF (SQLite needs that for table rebuilds), so a migration that deletes a parent row skips the schema's ON DELETE rules. #1479's migration 115 deleted empty harnessless chats, left their session_state and session_events rows orphaned, and the server refused to start. #1490 patched 115 by hand.

What changed

  • orphaned-rows.ts (new): after the migrations and inside their transaction, the runner resolves every row pragma foreign_key_check reports, using that key's own rule:
    • CASCADE deletes the row, repeating until a pass orphans nothing new.
    • SET NULL and SET DEFAULT reset the columns.
    • Only orphans on a key with no rule (or RESTRICT) still refuse to start. The error names the table, column, parent and rule, and the migrations roll back.
    • It acts only on the reported rowids. When nothing is orphaned it runs one check and changes nothing, the same as before. What it resolved is logged after the commit.
  • migration-chain.test.ts + schema-fixture.ts (new): before every migration, from the first to the latest, the test seeds linked rows into every table. The rows come from the schema's pragmas: one generation across all tables, plus one per table with only its ancestors. Then the real runner opens the database. New tables are covered without editing the test.
  • migrations-projects.test.ts: a v4 chat whose workspace was already gone used to block the migration. It is now deleted by its own on delete cascade rule, and the test asserts that.
  • Migration 115 keeps Let the server start again by deleting a dropped chat's rows before the chat #1490's deletes. Every key that references sessions is CASCADE (bots is excluded by 115's own filter), so on real data they produce the same database the runner's cascade does. Rewriting a shipped migration would change nothing.

Verification

🤖 Generated with Claude Code

dylanplayer and others added 2 commits November 26, 2026 13:17
…g to start

Migrations run with foreign keys off, so a migration that deletes a parent row
skips the schema's ON DELETE rules and leaves children pointing at nothing.
The runner then refused to start the server (#1479, patched by hand in #1490).

After the migrations, the runner now resolves every row pragma
foreign_key_check reports by that foreign key's own rule: cascade deletes the
row (repeating until nothing new is orphaned), set null and set default reset
the columns. Only orphans on a key with no rule (or restrict) still refuse to
start. It runs inside the migration transaction, touches only reported rows,
does one check and nothing else when nothing is orphaned, and logs what it
resolved after the commit.

A new test seeds linked rows into every table, derived from the schema, before
every migration from the first to the latest, then opens the database through
the real runner. It fails on the old runner with #1479's migration 115.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dylanplayer
dylanplayer merged commit 82bc7dd into main Nov 26, 2026
@dylanplayer
dylanplayer deleted the codevisor/raclette branch November 26, 2026 21:23
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.

1 participant