fix(db-postgres): keep generated down migrations idempotent#16767
Open
youcefzemmar wants to merge 1 commit into
Open
fix(db-postgres): keep generated down migrations idempotent#16767youcefzemmar wants to merge 1 commit into
youcefzemmar wants to merge 1 commit into
Conversation
Generated migrations sequence the dependent drops drizzle-kit emits, but Postgres also drops foreign keys when its referenced table is dropped with CASCADE. Subsequent ALTER TABLE … DROP CONSTRAINT / DROP INDEX statements in the same script then fail because the objects are already gone. Rewrite those drops to use IF EXISTS in db-postgres and db-vercel-postgres before the SQL is written to the migration file. Fixes payloadcms#14800.
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.
What
Auto-generated migrations on Postgres can fail on the down step (and sometimes on the up step when a collection is dropped) because drizzle-kit emits explicit
DROP CONSTRAINT/DROP INDEXstatements that follow aDROP TABLE … CASCADE. By the time those explicit drops run, Postgres has already removed the dependent objects, so each subsequent drop errors out withconstraint … does not exist/index … does not exist.This PR rewrites the generated statements so each
DROP CONSTRAINTandDROP INDEXcarriesIF EXISTS. The rewrite happens once, at migration-generation time insidesanitizeStatements, so existing user migrations are not touched — only newly created files.Why
DROP TABLE "x" CASCADE;followed byALTER TABLE "x_rels" DROP CONSTRAINT "x_rels_x_fk";is a common shape in down migrations that delete a collection. The CASCADE has already removedx_rels_x_fkas a side effect, so the explicit drop fails and the whole migration aborts mid-way.IF EXISTSlets drizzle-kit's ordering survive Postgres's dependency-resolution behavior without changing the generator upstream.The same pattern already lives in the v2→v3 predefined migration (
groupUpSQLStatements.ts:156), which convertsDROP CONSTRAINTtoDROP CONSTRAINT IF EXISTSfor the same reason. This PR generalizes that fix to every Postgres migration the adapter writes.Fix
packages/drizzle/src/utilities/makePostgresDropsIdempotent.ts— pure transform overstring[]of generated SQL. Anchored on\bDROP CONSTRAINT\b/\bDROP INDEX\bwith a negative lookahead forIF EXISTS, so it's safe to run more than once and won't touchCREATE INDEXor quoted strings that don't contain those tokens.@payloadcms/drizzle/postgres.sanitizeStatementsinpackages/db-postgres/src/index.tsandpackages/db-vercel-postgres/src/index.ts. SQLite adapters are unaffected — SQLite doesn't supportALTER TABLE … DROP CONSTRAINTand doesn't exhibit the regression.Testing
packages/drizzle/src/utilities/makePostgresDropsIdempotent.spec.tscovering: single-statement rewrites for bothDROP CONSTRAINTandDROP INDEX, idempotency whenIF EXISTSis already present, no-ops onCREATE INDEX/DROP TABLE/ADD COLUMN, and multi-dropALTER TABLEstatements.nodeagainst the same five cases — all pass.pnpm installis broken in my local sandbox (unable to open database filefrom pnpm's content-addressable store), so I couldn't run the project's vitest/eslint locally; the spec runs in CI under theunitproject (vitest.config.ts:52).Notes
test/database/up-down-migration/int.spec.tsis gated onPAYLOAD_DATABASE=postgresand won't exercise this branch in default CI. Left untouched on purpose — the unit test of the transform is the load-bearing test for this fix, and the int spec would need a multi-config setup (create collection, migrate up, drop collection, migrate up/down) to reproduce end-to-end. Happy to follow up on that scenario if you'd like it in this PR.Fixes #14800.