Skip to main content

Alembic Utils

Test Status Pre-commit Status

License PyPI version Codestyle Black Download count

Python version PostgreSQL version


Documentation: https://olirice.github.io/alembic_utils

Source Code: https://github.com/olirice/alembic_utils


Autogenerate Support for PostgreSQL Functions, Views, Materialized View, Triggers, and Policies

Alembic is the defacto migration tool for use with SQLAlchemy. Without extensions, alembic can detect local changes to SQLAlchemy models and autogenerate a database migration or "revision" script. That revision can be applied to update the database's schema to match the SQLAlchemy model definitions.

Alembic Utils is an extension to alembic that adds support for autogenerating a larger number of PostgreSQL entity types, including functions, views, materialized views, triggers, and policies.

TL;DR

Update alembic's env.py to register a function or view:

# migrations/env.py
from alembic_utils.pg_function import PGFunction
from alembic_utils.replaceable_entity import register_entities


to_upper = PGFunction(
  schema='public',
  signature='to_upper(some_text text)',
  definition="""
  RETURNS text as
  $$
    SELECT upper(some_text)
  $$ language SQL;
  """
)

register_entities([to_upper])

You're done!

The next time you autogenerate a revision with

alembic revision --autogenerate -m 'create to_upper'

Alembic will detect if your entities are new, updated, or removed & populate the revison's upgrade and downgrade sections automatically.

For example:

"""create to_upper

Revision ID: 8efi0da3a4
Revises:
Create Date: 2020-04-22 09:24:25.556995
"""
from alembic import op
import sqlalchemy as sa
from alembic_utils.pg_function import PGFunction

# revision identifiers, used by Alembic.
revision = '8efi0da3a4'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    public_to_upper_6fa0de = PGFunction(
        schema="public",
        signature="to_upper(some_text text)",
        definition="""
        returns text
        as
        $$ select upper(some_text) $$ language SQL;
        """
    )

    op.create_entity(public_to_upper_6fa0de)


def downgrade():
    public_to_upper_6fa0de = PGFunction(
        schema="public",
        signature="to_upper(some_text text)",
        definition="# Not Used"
    )

    op.drop_entity(public_to_upper_6fa0de)

Visit the quickstart guide for usage instructions.

—— ——

Contributing

To run the tests

# install pip dependencies
pip install wheel && pip install -e ".[dev]"

# run the tests
pytest src/test

To invoke the linter automated formatting and generally make use of precommit checks:

pip install pre-commit
pre-commit install

# manually run
pre-commit run --all

Metadata

Release files for alembic_utils 0.8.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alembic_utils 0.8.7
File Size Uploaded Provenance Actions
alembic_utils-0.8.7.tar.gz 21.6 kB Without provenance Details for alembic_utils-0.8.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for alembic_utils 0.8.7
File Interpreter ABI Platform Actions
alembic_utils-0.8.7-py3-none-any.whl Python 3 none any Details for alembic_utils-0.8.7-py3-none-any.whl

Total release size: 52.6 kB

Release files / alembic_utils-0.8.7.tar.gz

Download URL alembic_utils-0.8.7.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b8561a35ff96664994595a78867794c8d4f4bf9c16852c84b7a1bea1de617d1c
BLAKE2b-256 checksum
How to use checksums
24033eb3b2362f914b065172228b0298ce93558b985fba380a943cc824686e72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.9.19 Darwin/24.0.0

Release files / alembic_utils-0.8.7-py3-none-any.whl

Download URL alembic_utils-0.8.7-py3-none-any.whl
Size 31.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b6ac96b26b6898c27737a895f98ddd1cfce4ed331190050214144b475488225
BLAKE2b-256 checksum
How to use checksums
c00b9d2726fde772502f6867c21830346781278ea82f965ae6315b345e159dff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.9.19 Darwin/24.0.0

Security for alembic_utils 0.8.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.7 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page