Bird
Raised Fist0
Flaskframework~20 mins

Database migration in deployment in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Database Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Flask-Migrate's role in deployment

What is the primary purpose of Flask-Migrate during the deployment of a Flask application?

ATo automatically update the database schema to match the current models without losing data
BTo create a backup of the database before deployment
CTo optimize SQL queries for faster performance
DTo generate HTML forms for database models
Attempts:
2 left
💡 Hint

Think about how your database structure changes when you update your code models.

❓ component_behavior
intermediate
2:00remaining
Behavior of migration commands in Flask

What happens when you run flask db migrate followed by flask db upgrade during deployment?

A<code>flask db migrate</code> applies migrations; <code>flask db upgrade</code> rolls back changes
B<code>flask db migrate</code> generates migration scripts; <code>flask db upgrade</code> applies them to the database
C<code>flask db migrate</code> deletes old migrations; <code>flask db upgrade</code> creates new tables
D<code>flask db migrate</code> backs up the database; <code>flask db upgrade</code> restores it
Attempts:
2 left
💡 Hint

One command prepares changes, the other applies them.

🔧 Debug
advanced
3:00remaining
Fixing migration script conflicts

You deployed a Flask app and ran migrations. Later, a teammate also created migrations on their branch. When merging, you get a conflict in migration scripts. What is the best way to resolve this?

AIgnore the conflict and run <code>flask db upgrade</code> anyway
BDelete all migration files and recreate them from scratch
CManually merge the conflicting migration files and then run <code>flask db upgrade</code>
DRevert the database to the previous state and avoid merging branches
Attempts:
2 left
💡 Hint

Migration files are code and can be merged carefully.

❓ state_output
advanced
2:30remaining
Database state after partial migration

After running flask db migrate and partially applying flask db upgrade, the deployment crashes. What is the likely state of the database?

AThe database schema is partially updated, causing inconsistencies and errors
BThe database schema is fully updated and consistent
CThe database schema remains unchanged from before migration
DThe database is completely reset to an empty state
Attempts:
2 left
💡 Hint

Think about what happens if migration scripts are interrupted.

📝 Syntax
expert
3:00remaining
Correct Alembic migration script snippet

Which option shows the correct way to add a new column age of type Integer to the users table in an Alembic migration script?

Aop.add_column('users', Column('age', Integer))
Bop.add_column('users', sa.Column('age', sa.Int(), nullable=True))
Cadd_column('users', sa.Column('age', Integer, nullable=False))
Dop.add_column('users', sa.Column('age', sa.Integer(), nullable=True))
Attempts:
2 left
💡 Hint

Remember to use the correct Alembic and SQLAlchemy syntax with proper imports.

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