Alembic
Alembic is a migration tool for SQLAlchemy. It is a flexible tool for managing database migrations. Below, we’ll walk through the process of setting up Alembic in a project and running your first migration.
We’ll start with an example project that has Alembic already set up, and you can follow along with the steps below. Alternatively, you can run these steps in your own project.
Running an Example Migration
Clone the example project
Set up the environment and install dependencies
Create a Migration Script
In the example project, we have a migration script already created.
You can find it in the ./alembic/versions
directory.
Here’s how to create a new migration script:
This will generate a new file: ./alembic/versions/13379be60997_create_account_table.py
. Now we want to edit the file and add the migration script to it:
Connect to Database
To connect Alembic to the right database, edit alembic.ini
and edit the following:
You can hard-code your connection string, but the example project also supports using an
environment variable. You can create a .env
file and add the connection string there:
You can get your connection string from the Nile database home page.
Run the Migration
Thats it! You can connect to your database and see the new table.
Next Steps
This is the most basic use-case of Alembic. There is a lot more to it, which you can learn from the official Alembic documentation.
Was this page helpful?