Repository navigation
Let migrations apply the schema's own delete rules instead of refusing to start - #1494
Merged
Merged
Conversation
…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]>
…uard Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Why
Migrations run with
foreign_keys = OFF(SQLite needs that for table rebuilds), so a migration that deletes a parent row skips the schema'sON DELETErules. #1479's migration 115 deleted empty harnessless chats, left theirsession_stateandsession_eventsrows 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 rowpragma foreign_key_checkreports, using that key's own rule:CASCADEdeletes the row, repeating until a pass orphans nothing new.SET NULLandSET DEFAULTreset the columns.RESTRICT) still refuse to start. The error names the table, column, parent and rule, and the migrations roll back.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 ownon delete cascaderule, and the test asserts that.sessionsisCASCADE(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
Migration left foreign key violations(its own test, the new chain test, and a real database copy)./v1/healthok, database ready). The log showed 10session_events, 10session_stateand 5session_attentionrows deleted. There were no foreign key violations.bots.workspace_idhas no rule) fails the chain test.🤖 Generated with Claude Code