What is a database migration in the context of Flask deployment?
A database migration is a way to update the database schema (structure) safely when the application changes, without losing existing data.
Click to reveal answer
beginner
Which Flask extension is commonly used for database migrations?
Flask-Migrate is the common extension that helps manage database migrations using Alembic under the hood.
Click to reveal answer
beginner
What command do you run to create a new migration script after changing your models?
You run flask db migrate -m "message" to generate a migration script reflecting model changes.
Click to reveal answer
beginner
Why should you run flask db upgrade during deployment?
Running flask db upgrade applies the migration scripts to update the database schema to the latest version.
Click to reveal answer
intermediate
What is the role of Alembic in Flask database migrations?
Alembic is the tool that Flask-Migrate uses to track and apply database schema changes safely.
Click to reveal answer
Which Flask extension helps manage database migrations?
AFlask-Mail
BFlask-Login
CFlask-WTF
DFlask-Migrate
✗ Incorrect
Flask-Migrate is designed to handle database migrations in Flask apps.
What command creates a new migration script after model changes?
Aflask db migrate -m "message"
Bflask db init
Cflask run
Dflask db upgrade
✗ Incorrect
The 'flask db migrate' command generates migration scripts based on model changes.
What does flask db upgrade do?
ACreates a new migration script
BApplies migration scripts to update the database
CRuns the Flask app
DInitializes the migration environment
✗ Incorrect
It applies the migration scripts to update the database schema.
Why are database migrations important during deployment?
ATo update database schema without losing data
BTo change the app's UI
CTo restart the server
DTo clear the database
✗ Incorrect
Migrations update the database structure safely without data loss.
Which tool does Flask-Migrate use internally?
ASQLAlchemy
BPip
CAlembic
DJinja2
✗ Incorrect
Alembic is the migration tool Flask-Migrate uses to track schema changes.
Explain the steps to perform a database migration in a Flask deployment.
Think about how you update the database schema safely after changing your code.
You got /5 concepts.
Why is it important to use migrations instead of manually changing the database during deployment?
Consider what could go wrong if you change the database by hand.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of database migration in a Flask application deployment?
easy
A. To speed up the app by removing unused tables
B. To delete all existing data before deploying
C. To safely update the database structure as the app changes
D. To backup the database automatically
Solution
Step 1: Understand database migration purpose
Database migration updates the database schema to match app changes without losing data.
Step 2: Compare options
Only To safely update the database structure as the app changes describes safe schema updates; others describe unrelated or harmful actions.
Final Answer:
To safely update the database structure as the app changes -> Option C
Quick Check:
Migration = safe schema update [OK]
Hint: Migration means changing database structure safely [OK]
Common Mistakes:
Thinking migration deletes data
Confusing migration with backup
Assuming migration speeds up app
2. Which Flask-Migrate command initializes migration scripts in your project?
easy
A. flask db migrate
B. flask db create
C. flask db upgrade
D. flask db init
Solution
Step 1: Identify command for starting migrations
The flask db init command creates the migration folder and scripts for the first time.
Step 2: Check other commands
migrate creates migration files, upgrade applies them, create is invalid.
Final Answer:
flask db init -> Option D
Quick Check:
Init = start migration scripts [OK]
Hint: Init means start or create migration setup [OK]
Common Mistakes:
Using migrate before init
Confusing upgrade with init
Typing create instead of init
3. Given these commands run in order: flask db init flask db migrate -m "Add user table" flask db upgrade What happens after the last command?
medium
A. The database schema updates with the new user table
B. The migration folder is deleted
C. The app restarts automatically
D. The migration message is ignored
Solution
Step 1: Understand each command effect
init sets up migration scripts, migrate creates migration files, upgrade applies changes to the database.
Step 2: Identify final effect
After upgrade, the database schema updates to include new tables or changes.
Final Answer:
The database schema updates with the new user table -> Option A
Quick Check:
Upgrade = apply schema changes [OK]
Hint: Upgrade applies migration changes to database [OK]
Common Mistakes:
Thinking upgrade deletes migration files
Assuming app restarts automatically
Ignoring migration message
4. You run flask db migrate but see no new migration file created. What is the likely cause?
medium
A. You forgot to run flask db init first
B. Your database is already up to date with no changes
C. You ran flask db upgrade instead
D. You need to restart the Flask app
Solution
Step 1: Understand migrate command behavior
flask db migrate creates migration files only if there are schema changes detected.
Step 2: Analyze no new file case
If no migration file appears, it means no schema changes were found, so no new migration is needed.
Final Answer:
Your database is already up to date with no changes -> Option B
Quick Check:
No changes = no migration file [OK]
Hint: No migration file means no schema changes detected [OK]
Common Mistakes:
Skipping init causes errors but not no file
Confusing upgrade with migrate
Thinking app restart affects migration files
5. You deployed a Flask app and added a new column to a model. Which sequence of commands correctly updates the production database without losing data?
hard
A. flask db migrate -m "Add column" -> flask db upgrade
B. flask db upgrade -> flask db migrate -m "Add column"
C. flask db init -> flask db upgrade
D. flask db migrate -m "Add column" -> flask db init
Solution
Step 1: Understand correct migration order
First, create migration files with flask db migrate describing changes, then apply them with flask db upgrade.
Step 2: Check options for correct sequence
flask db migrate -m "Add column" -> flask db upgrade follows this order; others reverse or misuse commands.
Final Answer:
flask db migrate -m "Add column" -> flask db upgrade -> Option A
Quick Check:
Migrate then upgrade = safe update [OK]
Hint: Always migrate before upgrade to apply changes safely [OK]
Common Mistakes:
Running upgrade before migrate
Running init after migrate
Skipping migrate step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.