Migrations help you change your database step-by-step. Running migrations applies changes. Rolling back undoes them if needed.
Running and rolling back migrations in Ruby on Rails
rails db:migrate rails db:rollback STEP=n
rails db:migrate runs all pending migrations in order.
rails db:rollback undoes the last migration by default. Use STEP=n to undo multiple steps.
rails db:migrate
rails db:rollback
rails db:rollback STEP=3First, we run all pending migrations to update the database. Then, we undo the last migration. Finally, we undo the last two migrations to go back further.
# Run migrations rails db:migrate # Rollback last migration rails db:rollback # Rollback last 2 migrations rails db:rollback STEP=2
Always check your migration files before running or rolling back to avoid data loss.
Rolling back migrations only affects the database schema, not your application code.
You can use rails db:migrate:status to see which migrations have run.
Running migrations applies database changes step-by-step.
Rolling back undoes migrations to fix or test changes.
Use STEP=n with rollback to undo multiple migrations at once.
Practice
Solution
Step 1: Understand migration purpose
Migrations are designed to change the database schema in a controlled way.Step 2: Identify what running migration does
Running a migration applies these schema changes step-by-step to the database.Final Answer:
Applies changes to the database schema step-by-step -> Option AQuick Check:
Running migration = apply schema changes [OK]
- Thinking it deletes the database
- Confusing migration with backup
- Assuming it restarts the server
Solution
Step 1: Recall rollback command syntax
The basic command to undo the last migration israils db:rollback.Step 2: Analyze other options
rails db:migrate:downrequires specifying a version,rails db:resetresets the whole database, andrails db:rollback STEP=2rolls back two steps, not one.Final Answer:
rails db:rollback -> Option BQuick Check:
Rollback last migration = rails db:rollback [OK]
- Using db:reset which resets entire DB
- Using rollback with STEP=2 to undo one migration
- Confusing migrate:down without version
rails db:migraterails db:migraterails db:rollback STEP=2What is the state of the database migrations?
Solution
Step 1: Understand the commands run
Two migrations are applied by runningrails db:migratetwice, then rollback withSTEP=2undoes both.Step 2: Determine final migration state
Rolling back two steps removes both migrations, so no migrations remain applied.Final Answer:
No migrations are applied -> Option CQuick Check:
Rollback STEP=2 undoes two migrations [OK]
- Assuming rollback STEP=2 only undoes one migration
- Thinking migrations remain applied after rollback
- Confusing migrate command effects
rails db:rollback STEP=3 but get an error saying there are not enough migrations to rollback. What is the likely cause?Solution
Step 1: Understand rollback STEP behavior
Rollback with STEP=n tries to undo n migrations if they exist.Step 2: Analyze error cause
If fewer than 3 migrations are applied, rollback STEP=3 fails because it can't undo more than exist.Final Answer:
There are fewer than 3 migrations applied -> Option DQuick Check:
Rollback STEP=n needs at least n migrations applied [OK]
- Thinking STEP option is unsupported
- Believing server restart fixes rollback errors
- Assuming rollback only works with STEP=1
Solution
Step 1: Undo last 3 migrations
Userails db:rollback STEP=3to undo the last three migrations at once.Step 2: Reapply migrations one by one
Runrails db:migratethree times to apply each migration individually.Final Answer:
rails db:rollback STEP=3, then rails db:migrate three times -> Option AQuick Check:
Rollback STEP=3 + migrate x3 reapplies migrations [OK]
- Using migrate once after rollback STEP=3
- Resetting database instead of rolling back
- Trying migrate with STEP option (not valid)
Ready to prove it?
Pass the test and this pattern moves unknown
