0
0
FastAPIframework~10 mins

Alembic migrations in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Alembic migrations
Create migration script
Edit migration script
Run alembic upgrade
Database schema updated
Check migration history
Repeat for new changes
This flow shows how you create, edit, and apply database schema changes step-by-step using Alembic migrations.
Execution Sample
FastAPI
alembic revision -m "add users table"
# edit migration script
alembic upgrade head
Create a new migration script, edit it to define schema changes, then apply those changes to the database.
Execution Table
StepCommand/ActionEffectDatabase StateNotes
1alembic revision -m "add users table"Creates new migration script fileSchema unchangedMigration script ready for edits
2Edit migration scriptDefines new table 'users'Schema unchangedScript now contains SQL commands for new table
3alembic upgrade headRuns migration scriptUsers table addedDatabase schema updated
4alembic historyShows applied migrationsUsers table presentConfirms migration applied
5alembic revision -m "add email column"Creates new migration scriptUsers table presentPrepare next schema change
6Edit migration scriptAdds 'email' column to usersUsers table presentScript ready for next upgrade
7alembic upgrade headRuns migration scriptUsers table with email columnSchema updated again
8alembic downgrade -1Reverts last migrationUsers table without email columnSchema rolled back one step
9alembic upgrade headReapplies last migrationUsers table with email columnSchema restored
10ExitNo more commandsFinal schema stateMigration process complete
💡 No more migration commands; database schema is up to date
Variable Tracker
VariableStartAfter Step 3After Step 7After Step 8After Step 9
Database SchemaEmptyUsers table addedUsers table + email columnUsers table without email columnUsers table + email column
Key Moments - 3 Insights
Why does the database schema not change immediately after creating a migration script?
Creating a migration script only prepares the changes; the schema updates only happen after running 'alembic upgrade' as shown in steps 1-3.
What happens when you run 'alembic downgrade -1'?
It reverts the last applied migration, rolling back the schema to the previous state, as seen in step 8.
Why do we need to edit the migration script after creating it?
Alembic generates an empty script; you must add the specific schema changes manually before applying them, shown in steps 1-2 and 5-6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database state after step 3?
AUsers table with email column
BEmpty schema
CUsers table added
DUsers table without email column
💡 Hint
Check the 'Database State' column in row for step 3
At which step does the 'email' column get removed from the users table?
AStep 8
BStep 9
CStep 7
DStep 5
💡 Hint
Look for the step where the schema rolls back the last migration
If you skip editing the migration script after creating it, what happens when you run 'alembic upgrade head'?
ASchema updates with default changes
BNo schema changes applied
CError stops migration
DAlembic creates a new migration script automatically
💡 Hint
Refer to steps 1-3 and the note about editing migration scripts
Concept Snapshot
Alembic migrations manage database schema changes.
Create a migration script with 'alembic revision -m'.
Edit the script to define schema updates.
Apply changes using 'alembic upgrade head'.
Use 'alembic downgrade' to revert changes.
Check migration history with 'alembic history'.
Full Transcript
Alembic migrations help update your database schema step-by-step. First, you create a migration script using 'alembic revision -m' with a message describing the change. This script is empty initially, so you edit it to add the specific schema changes like creating tables or adding columns. Then, you apply these changes to the database by running 'alembic upgrade head'. This updates the database schema. You can check which migrations have been applied with 'alembic history'. If needed, you can undo the last migration using 'alembic downgrade -1'. This process allows you to safely and clearly manage database changes over time.