Chapter 12. Building Migrations

In Chapter 11, we initialized and configured the Alembic migration environment to prepare for adding data classes to our application and to create migrations to add them to our database. We’re going to explore how to use autogenerate for adding tables, and how to handcraft migrations to accomplish things that autogenerate cannot do. It’s always a good idea to start with an empty migration, so let’s begin there as it gives us a clean starting point for our migrations.

Generating a Base Empty Migration

To create the base empty migration, make sure you are in the CH12/ folder of the example code for this book. We’ll create an empty migration using this command:

# alembic revision -m "Empty Init" 1
   Generating ch12/alembic/versions/8a8a9d067_empty_init.py ... done
1

Run the alembic revision command and add the message (-m) "Empty Init" to the migration

This will create a migration file in the alembic/versions/ subfolder. The filenames will always begin with a hash that represents the revision ID and then whatever message you supply. Let’s look inside this file:

"""Empty Init 1

Revision ID: 8a8a9d067
Revises:
Create Date: 2015-09-13 20:10:05.486995

"""

Get Essential SQLAlchemy, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.