Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/olirice/alembic_utils
An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
https://github.com/olirice/alembic_utils
alembic migration postgres postgresql sqlalchemy
Last synced: 2 days ago
JSON representation
An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
- Host: GitHub
- URL: https://github.com/olirice/alembic_utils
- Owner: olirice
- License: mit
- Created: 2020-04-21T13:28:03.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-12T17:55:20.000Z (about 1 month ago)
- Last Synced: 2024-12-16T00:02:28.792Z (9 days ago)
- Topics: alembic, migration, postgres, postgresql, sqlalchemy
- Language: Python
- Homepage: https://olirice.github.io/alembic_utils
- Size: 5.56 MB
- Stars: 228
- Watchers: 6
- Forks: 48
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Alembic Utils
---
**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](https://alembic.sqlalchemy.org/en/latest/) is the defacto migration tool for use with [SQLAlchemy](https://www.sqlalchemy.org/). 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](https://www.postgresql.org/) entity types, including [functions](https://www.postgresql.org/docs/current/sql-createfunction.html), [views](https://www.postgresql.org/docs/current/sql-createview.html), [materialized views](https://www.postgresql.org/docs/current/sql-creatematerializedview.html), [triggers](https://www.postgresql.org/docs/current/sql-createtrigger.html), and [policies](https://www.postgresql.org/docs/current/sql-createpolicy.html).
### TL;DR
Update alembic's `env.py` to register a function or view:
```python
# migrations/env.py
from alembic_utils.pg_function import PGFunction
from alembic_utils.replaceable_entity import register_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
```shell
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:
```python
"""create to_upperRevision 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 = Nonedef 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](https://olirice.github.io/alembic_utils/quickstart/) 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
```