Repository navigation
feat(migrations): support conditional AddField on PostgreSQL - #2414
Open
DavidOprea wants to merge 2 commits into
Open
DavidOprea wants to merge 2 commits into
DavidOprea wants to merge 2 commits into
Conversation
Merging this PR will not alter performance1 warning
Comparing |
This branch has not been deployed
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.
Description
This PR adds an optional
if_not_existsparameter to theAddFieldmigration operation, allowing PostgreSQL migrations to skip adding a column if it already exists.Changes include:
if_not_exists=FalsetoAddField, preserving existing behavior by default.ADD COLUMN IF NOT EXISTSSQL.This PR focuses only on
AddField. Support forRemoveFieldand index operations is outside its scope.Motivation and Context
Partially addresses #2406.
When database schemas diverge from their migration history, adding a column that already exists causes a migration to fail.
The new
if_not_existsoption allows PostgreSQL migrations to handle this situation gracefully without requiring manual database changes.The option is disabled by default to preserve backward compatibility.
How Has This Been Tested?
Environment: macOS, Python with
uv, PostgreSQL usingasyncpg, and SQLite.The following checks passed:
make checkuv run pytest tests/migrations/ -vmake testAddFieldbehaviorThe PostgreSQL integration tests verify that:
if_not_exists=Truesucceeds.if_not_exists=Truesucceeds without creating a duplicate.Full PostgreSQL suite:
make test_postgres_asyncpgcompleted with:The six failures were reproduced on the unmodified
developbranch and are unrelated to this PR.Checklist: