Member-only story
Safely Test and Apply Changes to Your Database: Getting Started with Alembic
Version control your database with this simple Python tool
If you are not working with migrations in your database you’re missing out. Database migrations establish the structure and history of changes of our database in code and provide us with the ability to safely apply and revert these changes to our database.
In this article we’ll take a look at a Alembic; a Python tool that makes working with migrations very easy. With it we’ll create migrations that are database-agnostic (meaning you can execute them on any database. These migrations are defined in code which means that we can apply Git-like version control; tracking changes, having a single source of the truth and the ability to collaborate with multiple developers. When you’ve read this article you’ll be able to:
- Define changes to the database structure in code
- version control the changes
- Apply and revert the migrations
- Apply the migrations to multiple databases (e.g. testing on a dev database and then migrating the definitive structure to a production database)
- Not only create your required tables but also add indices and associations