-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix change_column_null when raising an error in safe mode
#271
Open
chaadow
wants to merge
1
commit into
ankane:master
Choose a base branch
from
chaadow:fix_unstable_migrations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
When changing a column null value from true to false (namely, the column becomes NOT NULL) and `safe mode` is enabled: - we add a NOT VALID check constraint - then commit the transaction - and finally validate the check constraint in another migration. The issue arises when the validation of check constraint fails. This leaves an invalid check constraint in the database and it is NOT dropped in a cleanup phase. After making sure that all values are NOT NULL), we then retry to run `rails db:migrate`. However this fails, because, since the check constraint already exists, it will result in an error raised stating that said check constraint **already** exists. This commit fixes the issue in both ways: - Firstly, if we're already in this unstable state of a NOT VALID check constraint already existing in the database, we avoid the recreation of the constraint and simply ignore this step - Secondly, in case the check validation fails, we rescue the exception and execute the drop of the check constraint to avoid leaving the database in an unstable state.
chaadow
force-pushed
the
fix_unstable_migrations
branch
from
September 1, 2024 13:44
e7c79d3
to
21f9048
Compare
chaadow
added a commit
to chaadow/strong_migrations
that referenced
this pull request
Sep 1, 2024
Followup to ankane#271 When adding a CHECK constraint and safe mode is enabled: - we add a NOT VALID check constraint - then commit the transaction - and finally validate the check constraint in another migration. If the validation step fails, this leaves a NOT VALID check constraint in the database. If we retry after having cleaned up the database, the creation of the NOT VALID check constraint will fail because it will already be in the DB. This commit makes the migration safely retriable by checking if said check constraint already exists, and if so, skips its creation again.
chaadow
added a commit
to chaadow/strong_migrations
that referenced
this pull request
Sep 1, 2024
Followup to ankane#271 When adding a CHECK constraint and safe mode is enabled: - we add a NOT VALID check constraint - then commit the transaction - and finally validate the check constraint in another migration. If the validation step fails, this leaves a NOT VALID check constraint in the database. If we retry after having cleaned up the database, the creation of the NOT VALID check constraint will fail because it will already be in the DB. This commit makes the migration safely retriable by checking if said check constraint already exists, and if so, skips its creation again.
chaadow
added a commit
to chaadow/strong_migrations
that referenced
this pull request
Sep 1, 2024
Followup to ankane#271 When adding a CHECK constraint and safe mode is enabled: - we add a NOT VALID check constraint - then commit the transaction - and finally validate the check constraint in another migration. If the validation step fails, this leaves a NOT VALID check constraint in the database. If we retry after having cleaned up the database, the creation of the NOT VALID check constraint will fail because it will already be in the DB. This commit makes the migration safely retriable by checking if said check constraint already exists, and if so, skips its creation again.
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.
When changing a column null value from true to false (namely, the column becomes NOT NULL) and
safe mode
is enabled:The issue arises when the validation of check constraint fails. This leaves an invalid check constraint in the database and it is NOT dropped in a cleanup phase.
After making sure that all values are NOT NULL), we then retry to run
rails db:migrate
. However this fails, because, since the check constraint already exists, it will result in an error raised stating that said check constraint already exists.This commit fixes the issue in both ways: