Skip to content

feat(alembic): auto-enable PostgreSQL enum migration detection - #774

Open
hasansezertasan wants to merge 3 commits into
litestar-org:mainfrom
hasansezertasan:feat/postgres-enum-migrations
Open

feat(alembic): auto-enable PostgreSQL enum migration detection#774
hasansezertasan wants to merge 3 commits into
litestar-org:mainfrom
hasansezertasan:feat/postgres-enum-migrations

Conversation

@hasansezertasan

@hasansezertasan hasansezertasan commented Jul 21, 2026

Copy link
Copy Markdown
Member

Summary

Closes #492.

Alembic's autogenerate cannot detect changes to PostgreSQL native ENUM types on its own (for example, adding a value to an existing StrEnum). Those changes are silently skipped, leaving the schema out of sync. The established fix is the alembic-postgresql-enum package, which registers the missing autogenerate comparators purely as an import side effect.

Since Advanced Alchemy ships its own migration env.py templates, this PR wires that support in directly so it works out of the box.

Changes

  • alembic/templates/{sync,asyncio}/env.py — Guarded import alembic_postgresql_enum in a try/except ImportError. It activates only when the package is installed and is a no-op otherwise, so no existing users break.
  • pyproject.toml — New public postgresql extra (pip install "advanced-alchemy[postgresql]") and the package added to the dev postgres group so CI exercises it.
  • docs/usage/cli.rst — Documents the behavior under make-migrations, including how existing projects can enable it by adding the import to an already-generated env.py.
  • tests/integration/test_alembic_commands.py — Asserts the generated env.py includes the conditional import.

Notes

The generated template intentionally keeps an explicit try/except (rather than contextlib.suppress) since users read and edit these files; SIM105 is ignored for the template path accordingly.

🤖 Generated with Claude Code


📚 Documentation preview: https://litestar-org.github.io/advanced-alchemy-docs-preview/774

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.45%. Comparing base (8dc8285) to head (31be229).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #774      +/-   ##
==========================================
+ Coverage   82.43%   82.45%   +0.02%     
==========================================
  Files         105      105              
  Lines        9039     9039              
  Branches     1219     1219              
==========================================
+ Hits         7451     7453       +2     
+ Misses       1262     1260       -2     
  Partials      326      326              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hasansezertasan
hasansezertasan force-pushed the feat/postgres-enum-migrations branch 2 times, most recently from f8a3f4b to 66925ef Compare July 22, 2026 13:29
@hasansezertasan
hasansezertasan requested a review from Copilot July 22, 2026 13:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Advanced Alchemy’s Alembic env.py templates to automatically opt into PostgreSQL native ENUM autogenerate support (via alembic-postgresql-enum) when the optional dependency is installed, addressing enum-value additions not being detected during migration generation.

Changes:

  • Add a guarded import of alembic_postgresql_enum to the sync and asyncio Alembic env.py templates to enable enum diff detection via import side effects.
  • Introduce a new public postgresql optional extra and update lock/dev dependency groups so CI/dev can exercise the integration.
  • Document the behavior in CLI docs and add an integration test asserting the generated env.py contains the conditional import.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
advanced_alchemy/alembic/templates/sync/env.py Adds conditional import to enable PostgreSQL enum autogenerate support in sync migrations.
advanced_alchemy/alembic/templates/asyncio/env.py Adds conditional import to enable PostgreSQL enum autogenerate support in asyncio migrations.
pyproject.toml Adds postgresql extra and updates ruff per-file ignores for template style.
uv.lock Locks alembic-postgresql-enum and exposes the new extra in lock metadata/groups.
docs/usage/cli.rst Documents the new enum detection behavior and installation path.
tests/integration/test_alembic_commands.py Adds a test to ensure generated env.py includes the conditional import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread advanced_alchemy/alembic/templates/sync/env.py
Comment thread advanced_alchemy/alembic/templates/asyncio/env.py
Comment thread tests/integration/test_alembic_commands.py Outdated
hasansezertasan added a commit to hasansezertasan/advanced-alchemy that referenced this pull request Jul 22, 2026
Catching ImportError also swallowed genuine import-time failures (e.g.
version mismatches), silently disabling enum autogenerate support even
when alembic-postgresql-enum is installed. Catch ModuleNotFoundError so
a missing package is handled quietly while real errors surface.

Addresses review feedback on litestar-org#774.
Alembic autogenerate cannot detect changes to PostgreSQL native ENUM
types (e.g. adding a value to an existing StrEnum), silently skipping
those migrations. The generated env.py templates now import
alembic-postgresql-enum inside a guarded try/except, which registers the
required autogenerate comparators as an import side effect. This is a
no-op when the package is not installed.

Adds a public `postgresql` extra so users can install the dependency
with `pip install "advanced-alchemy[postgresql]"`, documents the
behavior in the CLI usage guide, and covers the template rendering with
a test.

Closes litestar-org#492
The dependency was added unpinned in both the postgresql extra and the
postgres group. Pin it to >=1.10.0 (the version resolved in uv.lock) to
match the lower-bound convention used by every other dependency.
Catching ImportError also swallowed genuine import-time failures (e.g.
version mismatches), silently disabling enum autogenerate support even
when alembic-postgresql-enum is installed. Catch ModuleNotFoundError so
a missing package is handled quietly while real errors surface.

Addresses review feedback on litestar-org#774.
@cofin
cofin force-pushed the feat/postgres-enum-migrations branch from 3e21188 to bc1b9b2 Compare July 22, 2026 14:00
@cofin

cofin commented Jul 23, 2026

Copy link
Copy Markdown
Member

I'll take a review of this tonight. I think we have to be careful to ensure this is not a breaking change for the 1.0 branch. This would include the migrations scaffolding as well, which I think is the challenge.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Built-in PostgreSQL Enum Migration Support using alembic-postgresql-enum

4 participants