Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is Alembic used for in FastAPI projects?
Alembic helps manage database schema changes by creating and applying migrations. It keeps the database structure in sync with your code changes.
Click to reveal answer
beginner
What command initializes Alembic in your project?
The command alembic init alembic creates the Alembic folder and configuration files needed to start migrations.
Click to reveal answer
beginner
How do you create a new migration script with Alembic?
Use alembic revision -m "message" to create a new migration file where you define schema changes.
Click to reveal answer
beginner
What does the command alembic upgrade head do?
It applies all pending migrations to update the database schema to the latest version.
Click to reveal answer
intermediate
Why is it important to keep migration scripts under version control?
Migration scripts track database changes over time. Keeping them in version control helps teams share and apply consistent database updates safely.
Click to reveal answer
Which Alembic command creates the initial setup files?
Aalembic migrate
Balembic upgrade head
Calembic revision -m "init"
Dalembic init alembic
✗ Incorrect
The alembic init alembic command sets up the Alembic environment with necessary files.
What does alembic revision -m "add users table" do?
AApplies the migration to the database
BCreates a new migration script with the message
CDeletes old migration scripts
DRolls back the last migration
✗ Incorrect
This command creates a new migration file where you can define schema changes.
How do you apply all migrations to update your database?
Aalembic upgrade head
Balembic downgrade base
Calembic create all
Dalembic migrate
✗ Incorrect
The alembic upgrade head command applies all migrations to bring the database to the latest version.
Where do Alembic migration scripts live by default?
AIn the root project folder
BIn the database folder
CIn the alembic/versions folder
DIn the migrations folder
✗ Incorrect
Alembic stores migration scripts inside the alembic/versions directory by default.
Why should you not edit migration scripts after applying them to production?
AIt can cause database inconsistencies
BBecause they are read-only files
CAlembic will delete them automatically
DThey are encrypted
✗ Incorrect
Changing applied migrations can cause the database to get out of sync and lead to errors.
Explain the steps to create and apply a new Alembic migration in a FastAPI project.
Think about setup, writing changes, and applying them.
You got /4 concepts.
Describe why Alembic migrations are important for managing database changes in web applications.
Consider teamwork and database consistency.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Alembic migrations in a FastAPI project?
easy
A. To manage and apply database schema changes safely over time
B. To handle HTTP requests and responses
C. To serve static files like images and CSS
D. To create user interface components
Solution
Step 1: Understand Alembic's role
Alembic is a tool designed to manage database schema changes, not web server tasks.
Step 2: Identify the correct purpose
It helps developers apply, track, and revert database changes safely during development and deployment.
Final Answer:
To manage and apply database schema changes safely over time -> Option A
Quick Check:
Alembic = database migrations [OK]
Hint: Alembic is for database schema changes, not web or UI tasks [OK]
Common Mistakes:
Confusing Alembic with FastAPI routing
Thinking Alembic serves static files
Assuming Alembic builds UI components
2. Which Alembic command creates a new migration script file?
easy
A. alembic upgrade head
B. alembic init migrations
C. alembic downgrade -1
D. alembic revision -m "message"
Solution
Step 1: Identify commands for migration scripts
The command to create a new migration script is revision with a message describing the change.
Step 2: Match the command
alembic revision -m "message" creates a new migration file with the given message.
Final Answer:
alembic revision -m "message" -> Option D
Quick Check:
revision = create migration script [OK]
Hint: Use 'revision' with -m to create migration scripts [OK]
Common Mistakes:
Using 'upgrade' to create scripts instead of apply them
Confusing 'downgrade' with script creation
Using 'init' after project setup
3. Given this Alembic command sequence: alembic revision -m "add users table" alembic upgrade head What happens after running these commands?
medium
A. The database schema is reset to the initial state
B. The migration script is deleted and no changes are applied
C. A new migration script is created and the database schema is updated to include the users table
D. The database schema is downgraded by one version
Solution
Step 1: Understand the revision command
alembic revision -m "add users table" creates a new migration script file describing the addition of the users table.
Step 2: Understand the upgrade command
alembic upgrade head applies all migrations up to the latest, updating the database schema accordingly.
Final Answer:
A new migration script is created and the database schema is updated to include the users table -> Option C
Quick Check:
revision + upgrade = new migration applied [OK]
Hint: Revision creates script; upgrade applies it to DB [OK]
Common Mistakes:
Thinking upgrade resets or deletes migrations
Confusing downgrade with upgrade
Assuming revision applies changes immediately
4. You run alembic upgrade head but get an error about missing dependencies in your migration script. What is the best way to fix this?
medium
A. Edit the migration script to add missing imports or fix syntax errors
B. Delete all migration scripts and start over
C. Run alembic downgrade base without fixing scripts
D. Ignore the error and rerun the command
Solution
Step 1: Identify cause of error
Missing dependencies or syntax errors in migration scripts cause upgrade failures.
Step 2: Fix the migration script
Editing the script to add missing imports or correct syntax resolves the error and allows upgrade to succeed.
Final Answer:
Edit the migration script to add missing imports or fix syntax errors -> Option A
Quick Check:
Fix script errors before upgrading [OK]
Hint: Fix migration script errors before upgrading [OK]
Common Mistakes:
Deleting scripts unnecessarily
Ignoring errors and retrying blindly
Downgrading without fixing root cause
5. You want to add a new column to an existing table using Alembic migrations. Which sequence correctly applies this change without losing existing data?
hard
A. Delete the database and recreate it with the new column
B. Create a new revision script adding the column, then run alembic upgrade head
C. Modify the existing migration script that created the table and rerun alembic upgrade head
D. Run alembic downgrade base then create a new revision script
Solution
Step 1: Create a new migration script
Adding a new column requires a new migration script describing the change to preserve history and data.
Step 2: Apply the migration
Running alembic upgrade head applies the new migration safely without deleting existing data.
Final Answer:
Create a new revision script adding the column, then run alembic upgrade head -> Option B
Quick Check:
New revision + upgrade = safe schema update [OK]
Hint: Always create new revision for schema changes, then upgrade [OK]