Alembic autogenerate extension for PostgreSQL-specific objects
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file alembic_pg_autogen-0.0.4.tar.gz.
File metadata
- Download URL: alembic_pg_autogen-0.0.4.tar.gz
- Upload date:
- Size: 1.0 MB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
220c8986ff1fa3dabbe1015cb11f2aeee59a60253cc0a50b89523b481838b610
|
|
| MD5 |
b81f92112a950e8c4fd859a5e6944e60
|
|
| BLAKE2b-256 |
886e667f772a7bdb2eb7f97d7da6c4a3b37e331da9c81a28b411fda5ef8a3687
|
File details
Details for the file alembic_pg_autogen-0.0.4-py3-none-any.whl.
File metadata
- Download URL: alembic_pg_autogen-0.0.4-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
599e6efa3670bfce741dce6beb34acbb9bf6810cb485b334c074a54fdfe0012c
|
|
| MD5 |
6e247669bcf1db788956119187d26d7c
|
|
| BLAKE2b-256 |
557409cfd13239a0d9c0e48bab8390498d8752ac4e94151a29d201a3f5b9ba37
|