Migration with Alembic
As discussed earlier, Alembic is a migration tool that makes the tracking of changes in a database a less problematic operation for Flask developers. Since we expect data models to change, we need a tool that can keep track of these changes and ensure they are updated in the database.
This is similar to how we do version control of source code using Git
. The same applies to database schema change management, where we keep incremental and reversible changes in the database structure. Working with a database table, you will want to add or remove columns, thus altering the schema in a Python model class.
Once this is done, you need an automatic process to ensure your database table and the state of the data model schema are in sync. Alembic graciously handles the schema migration and ensures that the data model in a Python file is the same as the database structure.
Let’s examine how you can implement migration in a Flask application. We will add...