Bird
Raised Fist0
Flaskframework~10 mins

Database migration in deployment in Flask - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize Flask-Migrate with your Flask app and SQLAlchemy database.

Flask
from flask_migrate import [1]
migrate = [1](app, db)
Drag options to blanks, or click blank then click option'
AMigrateApp
BMigration
CFlaskMigrate
DMigrate
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like Migration or FlaskMigrate.
Forgetting to pass both app and db to Migrate.
2fill in blank
medium

Complete the command to create a new migration script using Flask-Migrate.

Flask
flask db [1] -m "Initial migration"
Drag options to blanks, or click blank then click option'
Ainit
Bupgrade
Cmigrate
Drevision
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upgrade' instead of 'migrate' to create migration scripts.
Using 'init' which initializes the migration folder only once.
3fill in blank
hard

Fix the error in the command to apply migrations to the database.

Flask
flask db [1]
Drag options to blanks, or click blank then click option'
Aupgrade
Bdowngrade
Cmigrate
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'migrate' which only creates migration scripts.
Using 'downgrade' which reverts migrations.
4fill in blank
hard

Fill both blanks to import and initialize the Flask app and database for migration.

Flask
from [1] import create_app
from [2] import db
Drag options to blanks, or click blank then click option'
Aapp
Bflask
Cmodels
Dmigrations
Attempts:
3 left
💡 Hint
Common Mistakes
Importing db from flask or migrations modules.
Importing create_app from incorrect modules.
5fill in blank
hard

Fill both blanks to write a command that downgrades the database by one migration step.

Flask
flask db [1] [2]
Drag options to blanks, or click blank then click option'
Aupgrade
Bdowngrade
C-1
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upgrade' instead of 'downgrade'.
Omitting the step count.

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

  1. Step 1: Understand database migration purpose

    Database migration updates the database schema to match app changes without losing data.
  2. 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.
  3. Final Answer:

    To safely update the database structure as the app changes -> Option C
  4. 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

  1. Step 1: Identify command for starting migrations

    The flask db init command creates the migration folder and scripts for the first time.
  2. Step 2: Check other commands

    migrate creates migration files, upgrade applies them, create is invalid.
  3. Final Answer:

    flask db init -> Option D
  4. 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

  1. Step 1: Understand each command effect

    init sets up migration scripts, migrate creates migration files, upgrade applies changes to the database.
  2. Step 2: Identify final effect

    After upgrade, the database schema updates to include new tables or changes.
  3. Final Answer:

    The database schema updates with the new user table -> Option A
  4. 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

  1. Step 1: Understand migrate command behavior

    flask db migrate creates migration files only if there are schema changes detected.
  2. 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.
  3. Final Answer:

    Your database is already up to date with no changes -> Option B
  4. 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

  1. Step 1: Understand correct migration order

    First, create migration files with flask db migrate describing changes, then apply them with flask db upgrade.
  2. Step 2: Check options for correct sequence

    flask db migrate -m "Add column" -> flask db upgrade follows this order; others reverse or misuse commands.
  3. Final Answer:

    flask db migrate -m "Add column" -> flask db upgrade -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard