SQLAlchemy database migrations for Flask applications using Alembic.
Project description
Flask-Migrate
Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are provided as command-line arguments under the flask db
command.
Installation
Install Flask-Migrate with pip
:
pip install Flask-Migrate
Example
This is an example application that handles database migrations through Flask-Migrate:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
With the above application you can create the database or enable migrations if the database already exists with the following command:
$ flask db init
Note that the FLASK_APP
environment variable must be set according to the Flask documentation for this command to work. This will add a migrations
folder to your application. The contents of this folder need to be added to version control along with your other source files.
You can then generate an initial migration:
$ flask db migrate
The migration script needs to be reviewed and edited, as Alembic currently does not detect every change you make to your models. In particular, Alembic is currently unable to detect indexes. Once finalized, the migration script also needs to be added to version control.
Then you can apply the migration to the database:
$ flask db upgrade
Then each time the database models change repeat the migrate
and upgrade
commands.
To sync the database in another system just refresh the migrations
folder from source control and run the upgrade
command.
To see all the commands that are available run this command:
$ flask db --help
Resources
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-Migrate-4.0.3.tar.gz
.
File metadata
- Download URL: Flask-Migrate-4.0.3.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0062c8d3f32de02847086b46cfc389412f78c71c89a619ebd7097e89d72ea4b |
|
MD5 | 6877bc4295f9bf880194676aade44680 |
|
BLAKE2b-256 | 42a78c0228414545bbaa8f20ba746aff449ac0fe9768c7c5669a773fc2081dbe |
File details
Details for the file Flask_Migrate-4.0.3-py3-none-any.whl
.
File metadata
- Download URL: Flask_Migrate-4.0.3-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8662a9dd391ce36deeaf3265987319c20fdb4c8a45306a32ba4f8224459abed4 |
|
MD5 | 0658fa75b40366b2b4c3df25ce4d91d7 |
|
BLAKE2b-256 | 7fcdea936f728fd0e2d148ad25dc9859d15c2bf62580ffdbfa04ce4f7aca461a |