feat(alembic): auto-enable PostgreSQL enum migration detection - #774
feat(alembic): auto-enable PostgreSQL enum migration detection#774hasansezertasan wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
f8a3f4b to
66925ef
Compare
There was a problem hiding this comment.
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_enumto thesyncandasyncioAlembicenv.pytemplates to enable enum diff detection via import side effects. - Introduce a new public
postgresqloptional 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.pycontains 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.
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.
3e21188 to
bc1b9b2
Compare
|
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. |
Summary
Closes #492.
Alembic's autogenerate cannot detect changes to PostgreSQL native
ENUMtypes on its own (for example, adding a value to an existingStrEnum). Those changes are silently skipped, leaving the schema out of sync. The established fix is thealembic-postgresql-enumpackage, which registers the missing autogenerate comparators purely as an import side effect.Since Advanced Alchemy ships its own migration
env.pytemplates, this PR wires that support in directly so it works out of the box.Changes
alembic/templates/{sync,asyncio}/env.py— Guardedimport alembic_postgresql_enumin atry/except ImportError. It activates only when the package is installed and is a no-op otherwise, so no existing users break.pyproject.toml— New publicpostgresqlextra (pip install "advanced-alchemy[postgresql]") and the package added to the devpostgresgroup so CI exercises it.docs/usage/cli.rst— Documents the behavior undermake-migrations, including how existing projects can enable it by adding the import to an already-generatedenv.py.tests/integration/test_alembic_commands.py— Asserts the generatedenv.pyincludes the conditional import.Notes
The generated template intentionally keeps an explicit
try/except(rather thancontextlib.suppress) since users read and edit these files;SIM105is ignored for the template path accordingly.🤖 Generated with Claude Code
📚 Documentation preview: https://litestar-org.github.io/advanced-alchemy-docs-preview/774