Skip to main content
This page guides you through a series of simple database schema changes using the Alembic schema migration module with a simple Python application built on SQLAlchemy and CockroachDB. For a detailed tutorial about using Alembic, see the Alembic documentation site. For information about specific migration tasks, see Alembic’s Cookbook.

Before you begin

Before you begin the tutorial, .

Step 1. Start a cluster and create a database

  1. Start a :
    This command creates a virtual cluster and opens a SQL shell to that cluster.
Leave this terminal window open for the duration of the tutorial. Closing the window will destroy the cluster and erase all data in it.
  1. Create the bank database:

Step 2. Get the application code

  1. Open a new terminal, and clone the example-app-python-sqlalchemy GitHub repository:

Step 3. Install and initialize Alembic

  1. Navigate to the example-app-python-sqlalchemy project directory, and run the following commands to create and start a virtual environment:
  2. Install the alembic, sqlalchemy-cockroachdb, and psycopg2 modules to the virtual environment:
    The sqlalchemy-cockroachdb and psycopg2-binary modules are required to use the CockroachDB adapter that the app uses to run transactions against a CockroachDB cluster. alembic includes the sqlalchemy module, which is a primary dependency of the example-app-python-sqlalchemy sample app. The alembic install also includes the alembic command line tool, which we use throughout the tutorial to manage migrations.
  3. Use the alembic command-line tool to initialize Alembic for the project:
    This command creates a migrations directory called alembic. This directory will contain the files that specify the schema migrations for the app. The command also creates a properties file called alembic.ini at the top of the project directory.
  4. Open alembic.ini and update the sqlalchemy.url property to specify the correct connection string to your database: For example:
You must use the cockroachdb:// prefix in the connection string for SQLAlchemy to make sure the CockroachDB dialect is used. Using the postgresql:// URL prefix to connect to your CockroachDB cluster will not work.

Step 4. Create and run a migration script

  1. Use the alembic command-line tool to create the first migration script:
  2. Open the newly-created migration file (alembic/versions/ad72c7ec8b22_create_accounts_table.py, in this case), and edit the upgrade() and downgrade() functions to read as follows:
    Running this migration creates the accounts table, with an id column and a balance column. Note that this file also specifies an operation for “downgrading” the migration. In this case, downgrading will drop the accounts table, effectively reversing the schema changes of the migration.
  3. Use the alembic tool to run this first migration:
    Specifying head runs the latest migration. This migration will create the accounts table. It will also create a table called alembic_version, which tracks the current migration version of the database.

Step 5. Verify the migration

  1. Open the terminal with the SQL shell to your demo cluster, and verify that the table was successfully created:
  2. In a different terminal, set the DATABASE_URL environment variable to the connection string for your cluster:
    The sample app reads in DATABASE_URL as the connection string to the database.
  3. Run the app to insert, update, and delete rows of data:

Step 6. Add additional migrations

Suppose you want to add a new to the accounts table that tracks which accounts are overdrawn.
  1. Create a new migration with the alembic tool:
  2. Open the migration file (alembic/versions/fd88c68af7b5_add_overdrawn_column.py), update the imports, and edit the upgrade() and downgrade() functions:
  3. Use the alembic tool to run the migration. Because this is the latest migration, you can specify head, or you can use the migration’s ID (fd88c68af7b5):
  4. In the terminal with the SQL shell to your demo cluster, verify that the column was successfully created:
    The changes will also be reflected in the alembic_version table.

Execute Raw SQL with Alembic

While Alembic supports most SQL operations, you can always execute raw SQL using the execute() operation.
Executing DDL statements as raw SQL can be particularly helpful when using SQL syntax for DDL statements specific to CockroachDB, like or statements.
For example, the raw SQL for the second migration would look something like this:
To make the second migration use raw SQL instead of Alembic operations, open alembic/versions/fd88c68af7b5_add_overdrawn_column.py, and edit the upgrade() function to use execute() instead of the operation-specific function:
Before running this migration, downgrade the original migration:
Then, in the SQL shell to the demo cluster, verify that the overdrawn column has been dropped from the table:
Now, run the updated migration script:
And verify that the column has been added to the table:

Auto-generate a Migration

Alembic can automatically generate migrations, based on changes to the models in your application source code. Let’s use the same example overdrawn computed column from above.
  1. Downgrade the fd88c68af7b5 migration:
  2. Verify the columns using a SHOW COLUMNS statement:
  3. Delete the old migration file:
  4. Open the models.py file in the app’s project, and add the overdrawn column to the Account class definition:
  5. Open the alembic/env.py file, and add the following import to the top of the file:
  6. Update the variable target_metadata to read as follows:
    These two lines import the database model metadata from the app.
  7. Use the alembic command-line tool to auto-generate the migration from the models defined in the app:
    Alembic creates a new migration file (44fa7043e441_add_overdrawn_column.py, in this case). If you open this file, you’ll see that it looks very similar to the one you manually created earlier in the tutorial.
  8. Run the migration:
  9. Verify that the new column exists in the accounts table:

Report Issues with Alembic and CockroachDB

If you run into problems, please file an issue in the alembic repository, including the following details about the environment where you encountered the issue:
  • CockroachDB version ( )
  • Alembic version
  • Operating system
  • Steps to reproduce the behavior

See Also