Bird
Raised Fist0
Ruby on Railsframework~20 mins

Running and rolling back migrations in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Migration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens after running rails db:migrate?
You run rails db:migrate in your Rails project. What is the immediate effect on your database?
AAll migrations are deleted from the migrations folder to prevent rerunning.
BThe database is reset to its initial empty state, removing all tables.
CThe Rails server automatically restarts to apply migration changes.
DThe database schema is updated to include all pending migrations, and a record is added to the schema_migrations table.
Attempts:
2 left
💡 Hint
Think about what migrations do to the database structure and how Rails tracks them.
❓ state_output
intermediate
2:00remaining
What is the state of the database after rails db:rollback?
After running rails db:migrate to apply three migrations, you run rails db:rollback. What is the state of the database schema?
AThe last applied migration is reversed, so the database schema reflects only the first two migrations.
BAll migrations are reversed, and the database schema is empty.
CThe database schema remains unchanged because rollback only affects migration files.
DThe first migration is reversed, but the last two remain applied.
Attempts:
2 left
💡 Hint
Rollback undoes migrations one step at a time, starting from the most recent.
📝 Syntax
advanced
2:00remaining
Which command rolls back the last two migrations?
You want to undo the last two migrations you applied. Which command correctly does this?
Arails db:migrate:down VERSION=2
Brails db:rollback --count=2
Crails db:rollback STEP=2
Drails db:reset STEP=2
Attempts:
2 left
💡 Hint
Look for the option that specifies how many steps to rollback.
🔧 Debug
advanced
2:00remaining
Why does rails db:rollback fail with 'No migrations to rollback'?
You run rails db:rollback but get the error 'No migrations to rollback'. What is the most likely reason?
AThere are no migrations applied yet, so nothing to rollback.
BThe migration files are missing from the project folder.
CThe database connection is not established.
DThe Rails version does not support rollback commands.
Attempts:
2 left
💡 Hint
Rollback only works if migrations have been applied before.
🧠 Conceptual
expert
3:00remaining
What is the effect of rails db:migrate:redo?
You run rails db:migrate:redo. What does this command do to your database schema?
AIt deletes the last migration file and updates the schema accordingly.
BIt rolls back the last migration and then reapplies it, effectively refreshing the last migration.
CIt applies all pending migrations without rolling back any.
DIt resets the entire database and reapplies all migrations from scratch.
Attempts:
2 left
💡 Hint
Think about what 'redo' means in terms of undoing and reapplying.

Practice

(1/5)
1. What does running a migration in Rails do?
easy
A. Applies changes to the database schema step-by-step
B. Deletes the entire database
C. Creates a backup of the database
D. Restarts the Rails server

Solution

  1. Step 1: Understand migration purpose

    Migrations are designed to change the database schema in a controlled way.
  2. Step 2: Identify what running migration does

    Running a migration applies these schema changes step-by-step to the database.
  3. Final Answer:

    Applies changes to the database schema step-by-step -> Option A
  4. Quick Check:

    Running migration = apply schema changes [OK]
Hint: Running migration updates database schema stepwise [OK]
Common Mistakes:
  • Thinking it deletes the database
  • Confusing migration with backup
  • Assuming it restarts the server
2. Which command correctly rolls back the last migration in Rails?
easy
A. rails db:reset
B. rails db:rollback
C. rails db:migrate:down
D. rails db:rollback STEP=2

Solution

  1. Step 1: Recall rollback command syntax

    The basic command to undo the last migration is rails db:rollback.
  2. Step 2: Analyze other options

    rails db:migrate:down requires specifying a version, rails db:reset resets the whole database, and rails db:rollback STEP=2 rolls back two steps, not one.
  3. Final Answer:

    rails db:rollback -> Option B
  4. Quick Check:

    Rollback last migration = rails db:rollback [OK]
Hint: Rollback last migration uses rails db:rollback [OK]
Common Mistakes:
  • Using db:reset which resets entire DB
  • Using rollback with STEP=2 to undo one migration
  • Confusing migrate:down without version
3. Given these commands run in order:
rails db:migrate
rails db:migrate
rails db:rollback STEP=2
What is the state of the database migrations?
medium
A. Only the first migration is applied
B. All migrations are applied
C. No migrations are applied
D. Only the last migration is applied

Solution

  1. Step 1: Understand the commands run

    Two migrations are applied by running rails db:migrate twice, then rollback with STEP=2 undoes both.
  2. Step 2: Determine final migration state

    Rolling back two steps removes both migrations, so no migrations remain applied.
  3. Final Answer:

    No migrations are applied -> Option C
  4. Quick Check:

    Rollback STEP=2 undoes two migrations [OK]
Hint: Rollback STEP=2 undoes last two migrations [OK]
Common Mistakes:
  • Assuming rollback STEP=2 only undoes one migration
  • Thinking migrations remain applied after rollback
  • Confusing migrate command effects
4. You run rails db:rollback STEP=3 but get an error saying there are not enough migrations to rollback. What is the likely cause?
medium
A. The STEP option is not supported
B. Rollback only works with STEP=1
C. You need to restart the Rails server first
D. There are fewer than 3 migrations applied

Solution

  1. Step 1: Understand rollback STEP behavior

    Rollback with STEP=n tries to undo n migrations if they exist.
  2. Step 2: Analyze error cause

    If fewer than 3 migrations are applied, rollback STEP=3 fails because it can't undo more than exist.
  3. Final Answer:

    There are fewer than 3 migrations applied -> Option D
  4. Quick Check:

    Rollback STEP=n needs at least n migrations applied [OK]
Hint: Rollback STEP=n requires n migrations applied [OK]
Common Mistakes:
  • Thinking STEP option is unsupported
  • Believing server restart fixes rollback errors
  • Assuming rollback only works with STEP=1
5. You have applied 5 migrations. You want to undo the last 3 migrations and then reapply them one by one to test changes. Which sequence of commands achieves this?
hard
A. rails db:rollback STEP=3, then rails db:migrate three times
B. rails db:rollback STEP=3, then rails db:migrate once
C. rails db:migrate:reset, then rails db:migrate
D. rails db:rollback, then rails db:migrate STEP=3

Solution

  1. Step 1: Undo last 3 migrations

    Use rails db:rollback STEP=3 to undo the last three migrations at once.
  2. Step 2: Reapply migrations one by one

    Run rails db:migrate three times to apply each migration individually.
  3. Final Answer:

    rails db:rollback STEP=3, then rails db:migrate three times -> Option A
  4. Quick Check:

    Rollback STEP=3 + migrate x3 reapplies migrations [OK]
Hint: Rollback STEP=3 then migrate repeatedly reapplies changes [OK]
Common Mistakes:
  • Using migrate once after rollback STEP=3
  • Resetting database instead of rolling back
  • Trying migrate with STEP option (not valid)
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