Alembic autogenerate extension for PostgreSQL-specific objects
Project description
alembic-pg-autogen
Alembic autogenerate extension for PostgreSQL. Extends Alembic's --autogenerate to detect and emit migrations for
PostgreSQL functions and triggers that Alembic doesn't handle out of the box.
How it works
You declare your desired functions and triggers as DDL strings. When you run alembic revision --autogenerate, the
extension:
- Inspects the current database catalog (
pg_proc,pg_trigger) - Canonicalizes your DDL by executing it in a savepoint and reading back the catalog (then rolling back)
- Diffs current vs. desired state, matching objects by identity
- Emits
CREATE,DROP, orCREATE OR REPLACEoperations in dependency-safe order (drop triggers before functions, create functions before triggers)
Installation
pip install alembic-pg-autogen
Requires Python 3.10+ and SQLAlchemy 2.x. You provide your own PostgreSQL driver (psycopg, psycopg2, asyncpg, etc.).
Usage
In your env.py, import the extension and pass your DDL via process_revision_directives options:
import alembic_pg_autogen # noqa: F401 # registers the comparator plugin
# Define your functions and triggers as DDL strings
PG_FUNCTIONS = [
"""
CREATE OR REPLACE FUNCTION audit_trigger_func()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$
""",
]
PG_TRIGGERS = [
"""
CREATE TRIGGER set_updated_at
BEFORE UPDATE ON my_table
FOR EACH ROW EXECUTE FUNCTION audit_trigger_func()
""",
]
Then in your run_migrations_online() function, pass them as context options:
context.configure(
connection=connection,
target_metadata=target_metadata,
opts={
"pg_functions": PG_FUNCTIONS,
"pg_triggers": PG_TRIGGERS,
},
)
Run autogenerate as usual:
alembic revision --autogenerate -m "add audit trigger"
The generated migration will contain op.execute() calls with the appropriate CREATE, DROP, or CREATE OR REPLACE
statements.
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
MIT
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.2.tar.gz.
File metadata
- Download URL: alembic_pg_autogen-0.0.2.tar.gz
- Upload date:
- Size: 198.5 kB
- 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 |
f4157cabe11bc438d93a119ee1fb9e8c85e28059d1ef1ccd4f804845d235457a
|
|
| MD5 |
85833d25da7faa653d13059cc7d0ea4e
|
|
| BLAKE2b-256 |
f7132b5bac40e54c162530b1e2835f0bcb21e3f1e8f3af5b40d6d2c2b9ae27dc
|
File details
Details for the file alembic_pg_autogen-0.0.2-py3-none-any.whl.
File metadata
- Download URL: alembic_pg_autogen-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.4 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 |
c0b1ebe8277e7d6342c8e400b5ef454bb34b777bf9d75545ea2f1cc89c1cf07a
|
|
| MD5 |
65aed0b8e92e2594bb22367020eacb48
|
|
| BLAKE2b-256 |
bff34ca6a6504841491cc65a30f678c8c65fc9c8c2175552fe34ca07483c58ef
|