alembic-pg-autogen
Status: Beta — the core pipeline is stable and tested against real PostgreSQL. The API may still evolve before 1.0, but the library is suitable for production use.
Alembic autogenerate extension for PostgreSQL functions and triggers. Declare your DDL strings and let
alembic revision --autogenerate figure out the CREATE, DROP, and CREATE OR REPLACE for you.
Background
alembic_utils pioneered autogenerate support for PostgreSQL objects and has been hugely helpful to the community. This project takes a different approach aimed at faster performance on large schemas with many functions and triggers.
How it works
You declare your desired functions and triggers as plain DDL strings. At autogenerate time, the extension inspects the live database catalog, canonicalizes your DDL via a temporary savepoint, diffs current vs. desired state, and emits migration ops in dependency-safe order.
Quick example
import alembic_pg_autogen # noqa: F401 # registers the comparator plugin
PG_FUNCTIONS = [
"""
CREATE OR REPLACE FUNCTION set_updated_at()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$
""",
]
PG_TRIGGERS = [
"""
CREATE TRIGGER set_updated_at_on_update
BEFORE UPDATE ON my_table
FOR EACH ROW EXECUTE FUNCTION set_updated_at()
""",
]
# in run_migrations_online():
context.configure(
connection=connection,
target_metadata=target_metadata,
pg_functions=PG_FUNCTIONS,
pg_triggers=PG_TRIGGERS,
)
alembic revision --autogenerate -m "add audit function and trigger"
Installation
pip install alembic-pg-autogen
Requires Python 3.10+ and SQLAlchemy 2.x. Bring your own PostgreSQL driver (psycopg, psycopg2, asyncpg, etc.).
This package depends on postgast for DDL parsing, which requires
protobuf >= 5.27.
Documentation
Full documentation is available at alembic-pg-autogen.readthedocs.io, including a quick-start guide, migration instructions for alembic_utils users, and API reference.
Development
make install # Install dependencies (uses uv)
make lint # Format (mdformat, codespell, ruff) then type-check (basedpyright)
make test # Run full test suite (requires Docker for integration tests)
make test-unit # Run unit tests only (no Docker needed)
License
Release files for alembic-pg-autogen 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| alembic_pg_autogen-0.1.0.tar.gz | 1.0 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| alembic_pg_autogen-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.0 MB
Release files / alembic_pg_autogen-0.1.0.tar.gz
| Download URL | alembic_pg_autogen-0.1.0.tar.gz |
|---|---|
| Size | 1.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3219f6e7a940d0569227a701ac2ca44dfdc7d53b938058ececf44b33d7547e41
|
|
BLAKE2b-256 checksum How to use checksums |
f1272e7df6855428a0ae658cdeaf1d8c30791c775b3f58b5f7b7a214c44d523e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / alembic_pg_autogen-0.1.0-py3-none-any.whl
| Download URL | alembic_pg_autogen-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ce6cd10dda5dcf9dd53616e5d1e91ddbb5c92a9854116a71ca9ccf7d849b6d2
|
|
BLAKE2b-256 checksum How to use checksums |
9b83be8d37739391484e51ad977f9433a230d5764ea2ce541af9c7be02e6d278
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|