Skip to main content

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:

  1. Inspects the current database catalog (pg_proc, pg_trigger)
  2. Canonicalizes your DDL by executing it in a savepoint and reading back the catalog (then rolling back)
  3. Diffs current vs. desired state, matching objects by identity
  4. Emits CREATE, DROP, or CREATE OR REPLACE operations 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

alembic_pg_autogen-0.0.1.tar.gz (186.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alembic_pg_autogen-0.0.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file alembic_pg_autogen-0.0.1.tar.gz.

File metadata

  • Download URL: alembic_pg_autogen-0.0.1.tar.gz
  • Upload date:
  • Size: 186.7 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

Hashes for alembic_pg_autogen-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e2f2caec00d0e9e549922bdd41b5049a859ec6020dbffc364c1b51b23470d995
MD5 6d6cef64897a22b0b6c52e2aedcd9f9b
BLAKE2b-256 c38de8d4e4c9ed111c79458f3ceccadf62e48c292f114f07897112dff9811cae

See more details on using hashes here.

File details

Details for the file alembic_pg_autogen-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: alembic_pg_autogen-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 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

Hashes for alembic_pg_autogen-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac12a8d6408804498b507520297b15a9a94216c0417990d5d8306a8b28f67fe9
MD5 fca0b780c3af0023fe982eae4872fd64
BLAKE2b-256 5b90657b4fb15b04c0b4d952aa13a25a8726799ef7da478bb9f15e9b4296aeaf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page