Registry / database / alembic-autogenerate-enums

alembic-autogenerate-enums

JSON →
library0.1.2pypypiunverified

alembic-autogenerate-enums is a Python library that provides an Alembic hook to automatically handle the upgrading and downgrading of enum values in database migrations. It simplifies managing changes to SQLAlchemy `Enum` types, particularly when adding, removing, or reordering enum members. The current version is 0.1.2, and it appears to have a low but steady release cadence, focusing on stability and compatibility with older SQLAlchemy versions.

pip install alembic-autogenerate-enums
INSTALL
IMPORT
SIG · ALEMBIC-AUTOGENERA
A
alembic-autogenerate-enums
databasepythonv0.1.2
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ColumnEnum
from alembic_autogenerate_enums import ColumnEnum
from alembic_autogenerate_enums import ColumnEnum

To enable automatic enum migration, you need to integrate `create_api()` into your `alembic/env.py` file by passing the `api` instance to `context.configure()`. Additionally, ensure that your SQLAlchemy `Enum` columns in your models use `alembic_autogenerate_enums.ColumnEnum` for the library to track changes effectively. The `values_callable` argument is crucial for `ColumnEnum` to correctly extract enum values.

from alembic import context from alembic_autogenerate_enums import create_api from sqlalchemy import create_engine, MetaData # Assuming these are defined elsewhere in your env.py # target_metadata = Base.metadata or some other SQLAlchemy MetaData object # config = context.config # Alembic configuration object # Placeholder for database connection and target_metadata DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///:memory:') connectable = create_engine(DATABASE_URL) target_metadata = MetaData() # Replace with your actual target_metadata # --- The key integration part in env.py --- # 1. Create the API instance api = create_api() # 2. Integrate the API into context.configure def run_migrations_online(): with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, api=api, # Pass the API instance here! # include_object=include_object, # Optional, if you have one # process_revision_directives=process_revision_directives # Optional, if you have one ) with context.begin_transaction(): context.run_migrations() # Call the function if running online # if context.is_offline_mode(): # run_migrations_offline() # else: # run_migrations_online() # Example model using ColumnEnum import enum from sqlalchemy.orm import declarative_base, Mapped, mapped_column from sqlalchemy import String Base = declarative_base() class UserStatus(enum.Enum): ACTIVE = 'active' INACTIVE = 'inactive' PENDING = 'pending' class User(Base): __tablename__ = 'users' id: Mapped[int] = mapped_column(primary_key=True) status: Mapped[UserStatus] = mapped_column(ColumnEnum(UserStatus, values_callable=lambda x: [m.value for m in x])) print("Alembic Autogenerate Enums setup snippet ready for integration into env.py.")
Debug
Known issues
gotchaThe autogenerate hook will not function unless `api=api` is explicitly passed to `alembic.context.configure()` in your `env.py` file. Forgetting this step is a common setup error.
fix
Ensure your `env.py` has `api = create_api()` and then `context.configure(..., api=api, ...)`.
affects: 0.1.x
gotchaThe specific enum value detection and migration capabilities are tied to using the library's `ColumnEnum` type in your SQLAlchemy models. Standard `SQLAlchemy.Enum` types might not be fully supported by the autogeneration hook for value changes.
fix
Replace `SQLAlchemy.Enum` with `alembic_autogenerate_enums.ColumnEnum` in your models for enums you want to manage automatically.
affects: 0.1.x
gotchaIf you have existing custom logic in `process_revision_directives` within `env.py`, ensure the `alembic-autogenerate-enums` API is called correctly and doesn't interfere with or override your custom processing.
fix
Review the order of operations and ensure the `api` hook's `process_revision_directives` method is applied either before or after your custom directives as appropriate, or integrated by wrapping the existing `process_revision_directives` with the `api.process_revision_directives`.
affects: 0.1.x
Upgrade
Version history
0.1.2latest on PyPI · released Jul 19, 2023
Audit
Dependencies
alembicrequiredCore dependency for Alembic migration functionality. Requires >=1.4.0.
SQLAlchemyrequiredCore dependency for database ORM and enum types. Requires >=1.3.0,<2.0.0.
Agent activity
30 hits · last 30 days
node
24
OpenAI (training)
1
Resources
alembic-autogenerate-enums — pip install alembic-autogenerate-enums · libregistry