- Notifications
You must be signed in to change notification settings - Fork54
An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
License
olirice/alembic_utils
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
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.
—— ——
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