Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies

License

NotificationsYou must be signed in to change notification settings

olirice/alembic_utils

Repository files navigation

Test StatusPre-commit Status

LicensePyPI versionCodestyle BlackDownload count

Python versionPostgreSQL 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 withSQLAlchemy. 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 ofPostgreSQL entity types, includingfunctions,views,materialized views,triggers, andpolicies.

TL;DR

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

# migrations/env.pyfromalembic_utils.pg_functionimportPGFunctionfromalembic_utils.replaceable_entityimportregister_entitiesto_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'supgrade anddowngrade sections automatically.

For example:

"""create to_upperRevision ID: 8efi0da3a4Revises:Create Date: 2020-04-22 09:24:25.556995"""fromalembicimportopimportsqlalchemyassafromalembic_utils.pg_functionimportPGFunction# revision identifiers, used by Alembic.revision='8efi0da3a4'down_revision=Nonebranch_labels=Nonedepends_on=Nonedefupgrade():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)defdowngrade():public_to_upper_6fa0de=PGFunction(schema="public",signature="to_upper(some_text text)",definition="# Not Used"    )op.drop_entity(public_to_upper_6fa0de)

Visit thequickstart guide for usage instructions.

—— ——

Contributing

To run the tests

# install pip dependenciespip install wheel && pip install -e ".[dev]"# run the testspytest src/test

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

pip install pre-commitpre-commit install# manually runpre-commit run --all

About

An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp