Integrate Alembic with Flask.
Maintained. Last release 8 weeks ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install flask-alembicFlask-Alembic
Flask-Alembic is a Flask extension that provides a configurable Alembic database migration environment. Supports Flask-SQLAlchemy, Flask-SQLAlchemy-Lite, or plain SQLAlchemy. Supports Alembic's single and multiple database templates.
Pallets Community Ecosystem
[!IMPORTANT]
This project is part of the Pallets Community Ecosystem. Pallets is the open source organization that maintains Flask; Pallets-Eco enables community maintenance of Flask extensions. If you are interested in helping maintain this project, please reach out on the Pallets Discord server.
Basic Usage
You've created a Flask application and some models with Flask-SQLAlchemy. Now start using Flask-Alembic:
from flask_alembic import Alembic
# Initialize the extension
alembic = Alembic()
alembic.init_app(app)
Commands are added to the flask CLI:
$ flask db --help
$ flask db revision "making changes"
$ flask db upgrade
You can also access Alembic's functionality from Python:
with app.app_context():
# Auto-generate a migration
alembic.revision("making changes")
# Upgrade the database
alembic.upgrade()
# Access the internals
environment_context = alembic.env
Differences from Alembic
- Configuration is taken from
Flask.configinstead ofalembic.iniandenv.py. - Engines and model/table metadata are taken from Flask-SQLAlchemy(-Lite) if available, or can be configured manually.
- The migrations are stored directly in the
migrationsfolder instead of theversionsfolder. - Provides the migration environment instead of
env.pyand exposes Alembic's internal API objects. - Differentiates between CLI commands and Python functions. The functions return revision objects and don't print to stdout.
- Allows operating Alembic at any API level while the app is running, through the exposed objects and functions.