Before you begin
Before you begin the tutorial, .Step 1. Start a cluster and create a database
-
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.
-
Create the
bankdatabase:
Step 2. Get the application code
-
Open a new terminal, and clone the
example-app-python-sqlalchemyGitHub repository:
Step 3. Install and initialize Alembic
-
Navigate to the
example-app-python-sqlalchemyproject directory, and run the following commands to create and start a virtual environment: -
Install the
alembic,sqlalchemy-cockroachdb, andpsycopg2modules to the virtual environment:Thesqlalchemy-cockroachdbandpsycopg2-binarymodules are required to use the CockroachDB adapter that the app uses to run transactions against a CockroachDB cluster.alembicincludes thesqlalchemymodule, which is a primary dependency of theexample-app-python-sqlalchemysample app. Thealembicinstall also includes thealembiccommand line tool, which we use throughout the tutorial to manage migrations. -
Use the
alembiccommand-line tool to initialize Alembic for the project:This command creates a migrations directory calledalembic. This directory will contain the files that specify the schema migrations for the app. The command also creates a properties file calledalembic.iniat the top of the project directory. -
Open
alembic.iniand update thesqlalchemy.urlproperty 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
-
Use the
alembiccommand-line tool to create the first migration script: -
Open the newly-created migration file (
alembic/versions/ad72c7ec8b22_create_accounts_table.py, in this case), and edit theupgrade()anddowngrade()functions to read as follows:Running this migration creates theaccountstable, with anidcolumn and abalancecolumn. Note that this file also specifies an operation for “downgrading” the migration. In this case, downgrading will drop theaccountstable, effectively reversing the schema changes of the migration. -
Use the
alembictool to run this first migration:Specifyingheadruns the latest migration. This migration will create theaccountstable. It will also create a table calledalembic_version, which tracks the current migration version of the database.
Step 5. Verify the migration
-
Open the terminal with the SQL shell to your demo cluster, and verify that the table was successfully created:
-
In a different terminal, set the
DATABASE_URLenvironment variable to the connection string for your cluster:The sample app reads inDATABASE_URLas the connection string to the database. -
Run the app to insert, update, and delete rows of data:
Step 6. Add additional migrations
Suppose you want to add a new to theaccounts table that tracks which accounts are overdrawn.
-
Create a new migration with the
alembictool: -
Open the migration file (
alembic/versions/fd88c68af7b5_add_overdrawn_column.py), update the imports, and edit theupgrade()anddowngrade()functions: -
Use the
alembictool to run the migration. Because this is the latest migration, you can specifyhead, or you can use the migration’s ID (fd88c68af7b5): -
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_versiontable.
Execute Raw SQL with Alembic
While Alembic supports most SQL operations, you can always execute raw SQL using theexecute() operation.
For example, the raw SQL for the second migration would look something like this:
alembic/versions/fd88c68af7b5_add_overdrawn_column.py, and edit the upgrade() function to use execute() instead of the operation-specific function:
overdrawn column has been dropped from 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 exampleoverdrawn computed column from above.
-
Downgrade the
fd88c68af7b5migration: -
Verify the columns using a
SHOW COLUMNSstatement: -
Delete the old migration file:
-
Open the
models.pyfile in the app’s project, and add theoverdrawncolumn to theAccountclass definition: -
Open the
alembic/env.pyfile, and add the following import to the top of the file: -
Update the variable
target_metadatato read as follows:These two lines import the database model metadata from the app. -
Use the
alembiccommand-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. -
Run the migration:
-
Verify that the new column exists in the
accountstable:
Report Issues with Alembic and CockroachDB
If you run into problems, please file an issue in thealembic repository, including the following details about the environment where you encountered the issue:
- CockroachDB version ( )
- Alembic version
- Operating system
- Steps to reproduce the behavior

