feat: support IF EXISTS table guard for dropConstraint#1654
Merged
Shinigami92 merged 4 commits intoJun 22, 2026
Conversation
Shinigami92
requested changes
Jun 21, 2026
Shinigami92
left a comment
Collaborator
There was a problem hiding this comment.
This requires an integration test in test/migrations
Contributor
Author
|
Thanks😀 added a migration case under test/migrations for the missing-table path I also updated the migration fixture counts and snapshots after adding the new migration file. The full test suite now passes locally with 'pnpm test' |
1f86eb1 to
e4ae3d0
Compare
Shinigami92
previously approved these changes
Jun 22, 2026
Collaborator
|
I will regenerate snapshots with |
Shinigami92
approved these changes
Jun 22, 2026
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.
Summary
ifTableExistsoption todropConstraintALTER TABLE IF EXISTSwhen the option is setifExistsbehavior forDROP CONSTRAINT IF EXISTSWhy
dropConstraintalready supportsifExists, but that only applies to the constraint itself:This still fails if the table no longer exists.
The issue describes a retry case for non-transactional migrations, where a previous migration attempt may have already dropped the table but failed before the migration was recorded as complete. In that case, retrying can fail at an earlier
dropConstraintcall.PostgreSQL supports guarding the table separately:
So this PR adds a separate
ifTableExistsoption for that case.Notes
I kept
ifTableExistsseparate fromifExistson purpose.Changing
ifExiststo also emitALTER TABLE IF EXISTSwould be a behavior change. A missing table can still be useful as an error signal, for example when there is a typo, a wrong schema name, or a migration-order issue.ifTableExistsonly guards the table part of the statement. If callers also want to ignore a missing constraint, they still need to passifExists: true.I also avoided adding this option to the shared
DropOptionstype, since that would expose it to unrelated operations such asdropTable. Instead,DropConstraintOptionsnow extendsDropOptionsand adds only this option.addConstraintalready combines forward constraint options with reversedropConstraintoptions, so I kept that existing structure and added a small test for the reverse pass-through path.This does not make non-transactional migrations fully safe. It only lets
dropConstraintemitALTER TABLE IF EXISTSwhen the caller explicitly asks for it.Tests
ifTableExistsifTableExistswithifExistsifTableExistswithifExistsandcascadeaddConstrainttodropConstraintpnpm testpnpm test:unit test/operations/constraints/dropConstraint.spec.ts test/operations/constraints/addConstraint.spec.tspnpm ts-checkpnpm lint(0 errors; existing warnings are unrelated to this change)pnpm format:checkpnpm docs:buildtest/migrationsCloses #1628