Running an Example Migration
1
Clone the example project
2
Set up the environment and install dependencies
3
Create a Migration Script
In the example project, we have a migration script already created.
You can find it in the This will generate a new file:
./alembic/versions
directory.Here’s how to create a new migration script:./alembic/versions/13379be60997_create_account_table.py
. Now we want to edit the file and add the migration script to it:4
Connect to Database
To connect Alembic to the right database, create a You can get your connection string from the Nile database home page.
.env
file and add the connection string there:5
Run the Migration
If you see an error that the table already exists, double check that you didn’t accidentally create a second
migration with the
accounts
table on top of the existing one. If this happened to you, you can delete the new migration file (or alternatively, modify the table name).Generating Migrations
Alembic can also generate migrations for you. This is useful if you want to create a migration for a new table.1
Create a new migration
The example project has a
models.py
file that defines two models: Todo
and Tenant
.To generate a migration for these models, you can use the following command:Alembic autogeneration compares your database state with models.py and will drop any tables not defined there.
Since Nile has a built-in
Tenants
table that can’t be dropped, we include the Tenant
model in models.py to prevent this.2
Run the generated migration file
You can then edit the generated migration file (if needed) and run it like as we did before: