What if you could fix database mistakes with a single command instead of hours of manual work?
Why Running and rolling back migrations in Ruby on Rails? - Purpose & Use Cases
Imagine you have to add a new column to your database table by writing raw SQL every time you update your app.
Then, you realize you made a mistake and need to undo that change manually.
Manually changing the database is risky and slow.
You might forget the exact commands to undo changes, causing errors or data loss.
It's hard to keep track of what changes were made and when.
Rails migrations let you write simple Ruby code to change your database structure.
You can easily run these changes or roll them back with simple commands.
This keeps your database in sync with your app code safely and clearly.
ALTER TABLE users ADD COLUMN age INTEGER; -- To undo: ALTER TABLE users DROP COLUMN age;
rails generate migration AddAgeToUsers age:integer rails db:migrate rails db:rollback
You can safely evolve your database step-by-step as your app grows, with full control to undo mistakes.
When adding a new feature that needs extra data, you create a migration to add columns instead of manually editing the database.
If the feature isn't ready, you roll back the migration to keep the database clean.
Manual database changes are error-prone and hard to track.
Migrations automate applying and undoing database changes safely.
This keeps your app and database in sync and easy to manage.
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
