Skip to main content

A sqlalchemy/alembic extension for migrating entities like functions, triggers, views, materialized views, and check constraints.

Project description

Alembic Utils Extended

Test Status Pre-commit Status

License PyPI version Codestyle Black Download count

Python version PostgreSQL version

Autogenerate Support for PostgreSQL Functions, Views, Materialized Views, Triggers, Policies, and Check Constraints

This is a fork of the much more popular alembic_utils package to extend the capabilities of Alembic, which adds support for autogenerating a larger number of PostgreSQL entity types, including functions, views, materialized views, triggers, and policies.

This repo adds additional support for defining indices for materialized views and autogenerating check constraints.

Quickstart

Visit the quickstart guide for usage instructions.

Entity Registration

# migrations/env.py

from alembic_utils_extended.pg_view import PGView
from alembic_utils_extended.replaceable_entity import register_entities

view = PGView(schema="public", signature="view", definition="SELECT 1")
register_entities([view])

Monitor Check Constraints

Check constraints defined in SQLAlchemy models can also be autogenerated. Note that check constraints must be named. Add to your env.py:

# migrations/env.py
from alembic import context

context.configure(
    # ... other configurations ...
    compare_check_constraints=True,
)

Monitor Expression Indexes

Alembic's built-in autogenerate silently skips expression indexes (Index("ix_foo", func.lower(table.c.x))) because it can't reliably reflect functional-index definitions from pg_indexes. With compare_expression_indexes=True, alembic_utils_extended detects new and removed expression indexes by name and emits the corresponding op.create_index / op.drop_index. Expression indexes must be named.

# migrations/env.py
from alembic import context

context.configure(
    # ... other configurations ...
    compare_expression_indexes=True,
)

Content changes under a stable name are not detected. Comparison is identity-only ((table_name, index_name) set diff). If the same index name exists in both the model and the database, autogenerate raises rather than silently miss an expression change — to evolve the expression, rename the index (which produces a drop + create).

Common pitfalls the comparator catches at autogenerate time:

  • func.X("col_name") anti-pattern — bare strings inside func.X(...) are treated as bound-parameter literal values, not column references. The resulting index is on the constant string, not the column. Use func.X(table.c.col_name) or func.X(literal_column("col_name")) instead. The comparator raises with a remediation hint when it detects this.

Autogeneration

The next time you autogenerate a revision, Alembic will detect if your entities are new, updated, or removed and populate the migration script.

alembic revision --autogenerate -m 'message'

Contributing

If you have any issues with contributing, please reach out to justin@joincandidhealth.com so that we can work out any issues you are having! This is mostly just forked directly from alembic_utils, so it's possible something is misconfigured.

Testing

poetry install
poetry run pre-commit run --all-files
poetry run pytest

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_utils_extended-1.2.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

alembic_utils_extended-1.2.0-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file alembic_utils_extended-1.2.0.tar.gz.

File metadata

  • Download URL: alembic_utils_extended-1.2.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for alembic_utils_extended-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2056df574bddefee52d9ec7ed949444088a83487251a49b2f87680a2f5abd069
MD5 d9d76b9316a16421583e0251824ad9df
BLAKE2b-256 5d59d729c0519b9f8a13b0cfac138d6c5f022b53ec0f22129da0028db7db8233

See more details on using hashes here.

Provenance

The following attestation bundles were made for alembic_utils_extended-1.2.0.tar.gz:

Publisher: publish.yml on candidhealth/alembic-utils-extended

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file alembic_utils_extended-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alembic_utils_extended-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70a104b9646a0759b7cc290147c6facdd0de270eb6d406c41c24c093e3064bb3
MD5 7411c89e19ff2fa154ff752f04713202
BLAKE2b-256 b8cc4ecc8e7a738946ef135d808a3ccc302c45bef66c431aacb8cade89ea5667

See more details on using hashes here.

Provenance

The following attestation bundles were made for alembic_utils_extended-1.2.0-py3-none-any.whl:

Publisher: publish.yml on candidhealth/alembic-utils-extended

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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