Skip to content

feat(migrations): support conditional AddField on PostgreSQL - #2414

Open
DavidOprea wants to merge 2 commits into
tortoise:developfrom
DavidOprea:feat/2406-conditional-migrations
Open

DavidOprea wants to merge 2 commits into
tortoise:developfrom
DavidOprea:feat/2406-conditional-migrations

Conversation

@DavidOprea

Copy link
Copy Markdown

Description

This PR adds an optional if_not_exists parameter to the AddField migration operation, allowing PostgreSQL migrations to skip adding a column if it already exists.

Changes include:

  • Added if_not_exists=False to AddField, preserving existing behavior by default.
  • Updated the base schema editor to support conditional ADD COLUMN IF NOT EXISTS SQL.
  • Updated SQLite and MySQL schema editors to accept the new parameter and explicitly reject unsupported conditional additions.
  • Added unit tests for SQL generation and backend-specific behavior.
  • Added PostgreSQL integration tests covering new columns, existing columns, and duplicate-column errors.

This PR focuses only on AddField. Support for RemoveField and 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_exists option 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 using asyncpg, and SQLite.

The following checks passed:

  • make check
  • uv run pytest tests/migrations/ -v
  • make test
  • PostgreSQL integration tests for conditional AddField behavior

The PostgreSQL integration tests verify that:

  • Adding a new column with if_not_exists=True succeeds.
  • Adding an existing column with if_not_exists=True succeeds without creating a duplicate.
  • Adding an existing column without the flag raises an error.

Full PostgreSQL suite:

make test_postgres_asyncpg completed with:

  • 2,306 passed
  • 6 failed
  • 74 skipped
  • 1 expected failure

The six failures were reproduced on the unmodified develop branch and are unrelated to this PR.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed

codspeed Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 24 untouched benchmarks


Comparing DavidOprea:feat/2406-conditional-migrations (853c6fe) with develop (eda0850)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant