The Flask-Migrate plugin allows using an shell interface for Alembic
With Alembic you can manage changes in the database model providing a version control tool and easing the recreation of the database on any design change
First steps
On the project setup we execute
$ python manage.py db init
Or using the scaffold fabfile:
$ fab dbcmm:init
This will create the migration folder migrate and all the scripts that will be used in the migrations
The first migration will get the structure in the file models.py and create a database:
$ python manage.py db migrate
$ fab dbcmm:migrate
It will create the first migration script and an empty database
All the migration scripts can be found in the folder versions and ALWAYS is worth reviewing its contents after the creation.
When we run the migration script it recreates the database structure in the freshly created database
$ python manage.py db upgrade
$ fab dbcmm:upgrade
This script doesn't insert data per se. It can be arranged so Alembic inserts bulk data, please refer to the bulk_insert section of the documentation.
But Miguel Grinberg doesn't recommends it, in his opinion Alembic should only be used to maintain the database structure and recommends seeding the data using an specific Flask-Shell command for that purpose
$ python manage.py seed
Everything toghether
Theres an specific Fabric task for executing the first steps automatically.
$ fab initdb
Creating a new version
With the changes already implemented in models.py
The migration script can be generated with:
$ python manage.py db migrate -m 'Version comments'
It is recommended to review the newly generated script and then apply the changes
$ python manage.py db upgrade