fix: properly drop temporary NOT NULL constraint#166
Merged
tianzhou merged 2 commits intopgplex:mainfrom Nov 23, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where temporary CHECK constraints used during NOT NULL column migrations were not being properly cleaned up, causing constraint name collisions on subsequent migrations.
Key Changes:
- Added a 4th step to the NOT NULL migration workflow that drops the temporary CHECK constraint after setting the column to NOT NULL
- Updated test data to reflect the new cleanup step
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/plan/rewrite.go | Added logic to generate and execute DROP CONSTRAINT statement for temporary CHECK constraint cleanup |
| testdata/diff/online/add_not_null/plan.txt | Updated expected migration plan output to include constraint cleanup |
| testdata/diff/online/add_not_null/plan.sql | Updated expected SQL output to include DROP CONSTRAINT statement |
| testdata/diff/online/add_not_null/plan.json | Updated expected JSON plan to include new cleanup step |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tianzhou
approved these changes
Nov 23, 2025
Contributor
tianzhou
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the fix.
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.
Problem Statement
When adding and removing NOT NULL constraints, pgschema fails to properly clean up temporary constraints. This leads to constraint name collisions and migration failures when attempting to re-add the constraint.
Error Message
Root Cause
pgschema creates named constraints when
NOT NULLis added to a column. When the constraint is removed in a subsequent migration, the temporary constraint object is not being properly dropped from the database. This orphaned constraint prevents future migrations from creating a new constraint with the same name.Reproduction Steps
The issue can be reproduced with the following sequence of schema changes:
Expected Result: Step 4 should succeed
Actual Result: Error thrown due to constraint name collision