How to Run Flask Database Migration.

By | 2018-06-03

Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. Database operations are made available through the Flask command-line interface or through the Flask-Script extension.

Full list of migration commands and flask-migrate documents can be found here.

Initialise migration repository

$ flask db init

Create migration

$ flask db migrate -m "enter migration message here"

This command creates a python script that describe what changes will be applied to our database.

Apply migration:

$ flask db upgrade

Rollback migration (downgrade)

$ flask db downgrade

All migration commands preserve data in the database.

List migration history

$ flask db history