Skip to content

fix(#255): migrate emits constraint/index drops before drop-column - #257

Merged
dmealing merged 3 commits into
mainfrom
fix/255-migrate-drop-fk-before-drop-column
Aug 2, 2026
Merged

fix(#255): migrate emits constraint/index drops before drop-column#257
dmealing merged 3 commits into
mainfrom
fix/255-migrate-drop-fk-before-drop-column

Conversation

@dmealing

@dmealing dmealing commented Aug 2, 2026

Copy link
Copy Markdown
Member

Closes #255.

npm-only (migrate-ts). The SQL emitter's STAGE_ORDER ran DROP COLUMN (stage 2) before DROP CONSTRAINT (FK/check, stage 5) and DROP INDEX (stage 4), so dropping a column that a still-present foreign key referenced — or that a still-present index backed — produced an un-appliable migration (cannot drop column … because other objects depend on it). Root cause: constraint/index drops shared a stage with their adds but need the opposite order vs column mutation.

Fix (both emit/postgres.ts + emit/sqlite.ts): hoist all drops ahead of column mutation — drop-fk/drop-check at stage 1, drop-index at 1.5 (a foreign key depends on the unique/PK index backing its target, so the FK must drop before that index), columns at 2; adds (add-fk/add-check/add-index) stay late. Only those three stage keys moved; every other stage is unchanged.

Validated against real Postgres 16 (Testcontainers): reproduced the exact apply errors pre-fix, applies cleanly + re-diff-empty post-fix, including the combined FK + backing-index + column-drop scenario. New pg-drop-fk-before-drop-column.test.ts (emit→apply→introspect→re-diff-empty) + unit order-assertions for both dialects.

Byte-identity: any migration with none of drop-fk/drop-check/drop-index is byte-identical (only those keys' positions changed; stable sort). A migration combining one of those drops with a column change now emits the corrected order — CHANGELOG flags this so adopters re-review un-applied migration files.

Reviewed (senior, opus): ready to merge, no Critical/Important; all ordering invariants, byte-identity, real-engine genuineness, down-path, and sqlite recreate-and-copy interaction verified.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HLoJkFSyoticveo5ehMUAr

dmealing and others added 3 commits August 1, 2026 21:59
…erenced-column drops apply

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HLoJkFSyoticveo5ehMUAr
…x cascade class)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HLoJkFSyoticveo5ehMUAr
@dmealing
dmealing merged commit 9094fcf into main Aug 2, 2026
1 check passed
@dmealing
dmealing deleted the fix/255-migrate-drop-fk-before-drop-column branch August 2, 2026 02:22
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.

migrate: drop-column is emitted before drop-fk, so dropping a referenced column always fails

1 participant