Skip to main content

A sqlalchemy/alembic extension for migrating procedures and views

Project description

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/candidhealth/alembic-utils-extended


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_extended.pg_function import PGFunction
from alembic_utils_extended.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_extended.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

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-0.0.0.tar.gz (21.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_utils_extended-0.0.0-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for alembic_utils_extended-0.0.0.tar.gz
Algorithm Hash digest
SHA256 a7c3b0b5e0e90ff27a6db88cf5579604b155f577427c4ac32f07ab9c6bda2b51
MD5 5ce63a5e9a649d0e089c32482b449d1d
BLAKE2b-256 411b389cbd8e9ba33a24d491fe692d141e60cee4c6b552ef7a3cf22222f94a89

See more details on using hashes here.

Provenance

The following attestation bundles were made for alembic_utils_extended-0.0.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-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alembic_utils_extended-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bf57f5b5aa433eb9948f04208ec4baf091454b38f286156d64454c03c339898
MD5 174e1832dbb1f003bf0180d61fd2997b
BLAKE2b-256 df3cd88b57172a0f0917d93809042442fe09d4b6be516419dd8d9e77ac052230

See more details on using hashes here.

Provenance

The following attestation bundles were made for alembic_utils_extended-0.0.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