0
0
Ruby on Railsframework~10 mins

Running and rolling back migrations in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running and rolling back migrations
Start: Migration files exist
Run migration command
Rails applies migrations in order
Database schema updated
If rollback needed?
NoEnd
Yes
Run rollback command
Rails reverts last migration
Database schema reverted
End
This flow shows how Rails applies migrations to update the database schema and how it can revert changes by rolling back the last migration.
Execution Sample
Ruby on Rails
rails db:migrate
rails db:rollback
Run migrations to update the database, then rollback the last migration to revert changes.
Execution Table
StepCommandActionMigration StatusDatabase Schema State
1rails db:migrateApply pending migrationsMigration 001 appliedSchema updated with migration 001 changes
2rails db:migrateApply next pending migrationMigration 002 appliedSchema updated with migration 002 changes
3rails db:rollbackRevert last migrationMigration 002 rolled backSchema reverted to state after migration 001
4rails db:rollbackRevert last migrationMigration 001 rolled backSchema reverted to initial state
5rails db:rollbackNo migrations left to rollbackNo changeSchema remains at initial state
💡 No more migrations to rollback, database schema is at initial state
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Migration StatusNone applied001 applied002 applied001 appliedNone appliedNone applied
Database Schema StateInitialUpdated with 001Updated with 002Updated with 001InitialInitial
Key Moments - 2 Insights
Why does rolling back twice revert the schema to the initial state?
Because each rollback reverses the last applied migration step-by-step, so rolling back twice removes both migrations, returning the schema to its original state as shown in execution_table rows 3 and 4.
What happens if you run rollback when no migrations are applied?
No changes occur because there are no migrations to revert, as shown in execution_table row 5 where the schema stays at the initial state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the migration status after step 2?
AMigration 001 rolled back
BMigration 002 applied
CNo migrations applied
DMigration 002 rolled back
💡 Hint
Check the 'Migration Status' column at step 2 in the execution_table.
At which step does the database schema revert to the initial state?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Database Schema State' column in execution_table rows for when it shows 'Initial'.
If you run 'rails db:migrate' after step 4, what will happen?
AMigrations 001 and 002 will be applied again
BNo migrations will run because all are applied
COnly migration 002 will be applied
DOnly migration 001 will be applied
💡 Hint
After step 4, no migrations are applied, so running migrate applies all pending migrations again.
Concept Snapshot
rails db:migrate: Applies all pending migrations in order to update the database schema.
rails db:rollback: Reverts the last applied migration, stepping back the schema.
Multiple rollbacks undo migrations one by one.
If no migrations applied, rollback does nothing.
Use these commands to manage schema changes safely.
Full Transcript
This visual execution shows how Rails runs and rolls back migrations. First, running 'rails db:migrate' applies migrations in order, updating the database schema step-by-step. Each migration changes the schema, tracked in the migration status. If you need to undo changes, 'rails db:rollback' reverts the last migration, restoring the schema to the previous state. Rolling back multiple times removes migrations one by one until the schema returns to its initial state. If no migrations are applied, rollback does nothing. This process helps manage database changes safely and predictably.