TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial…

Follow publication

Member-only story

Safely Test and Apply Changes to Your Database: Getting Started with Alembic

Mike Huls
TDS Archive
Published in
7 min readNov 8, 2021

--

Migrations are like blueprints; they are instructions for building your database (image by JESHOOTS on Pexels)

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

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Mike Huls
Mike Huls

Written by Mike Huls

I write about interesting programming-related things: techniques, system architecture, software design and how to apply them in the best way. — mikehuls.com

Responses (3)

Write a response