Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Database migration using Alembic and Flask-Migrate


Now, let's say we want to update our models to have a new field called company in our Product model. One way is to drop the database and then create a new one using db.drop_all() and db.create_all(). However, this approach cannot be followed for applications in production or even in staging. We would want to migrate our database to match the newly updated model with all the data intact.

For this, we have Alembic, which is a Python-based tool to manage database migrations and uses SQLAlchemy as the underlying engine. Alembic provides automatic migrations to a great extent with some limitations (of course, we cannot expect any tool to be seamless). To act as the icing on the cake, we have a Flask extension called Flask-Migrate, which eases the process of migrations even more.

Getting ready

First of all, we will install Flask-Migrate:

$ pip install Flask-Migrate

This will also install Flask-Script and Alembic, among some other dependencies. Flask...