0
0
Ruby on Railsframework~10 mins

Creating migrations in Ruby on Rails - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating migrations
Generate migration file
Edit migration file
Run migration command
Database schema updated
Migration status checked
This flow shows how to create a migration file, edit it, run it to update the database schema, and then check the migration status.
Execution Sample
Ruby on Rails
rails generate migration AddAgeToUsers age:integer
rails db:migrate
rails db:migrate:status
This code generates a migration to add an age column to users, runs the migration, and checks migration status.
Execution Table
StepCommand/ActionEffectResult/Output
1rails generate migration AddAgeToUsers age:integerCreates migration fileFile db/migrate/20240610123456_add_age_to_users.rb created
2Edit migration file (optional)Modify change method if neededMigration file ready with add_column :users, :age, :integer
3rails db:migrateRuns migration== 20240610123456 AddAgeToUsers: migrating ====== -- add_column(:users, :age, :integer) -> 0.0012s == 20240610123456 AddAgeToUsers: migrated (0.0013s) ==
4rails db:migrate:statusShows migration statusup 20240610123456 AddAgeToUsers
5ExitAll migrations up to dateNo pending migrations
💡 All migrations have been applied; no pending migrations remain.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
Migration fileNoneCreated with add_age_to_users.rbExists and readyApplied to database schema
Database schemaOriginal schemaUnchangedUpdated with age column in users tableUpdated schema with new column
Key Moments - 2 Insights
Why do we need to run 'rails db:migrate' after generating a migration file?
Generating a migration file only creates the instructions. Running 'rails db:migrate' actually applies those instructions to update the database schema, as shown in execution_table step 3.
What does 'rails db:migrate:status' tell us?
It shows which migrations have been applied (up) and which are pending (down). In execution_table step 4, it confirms the new migration is applied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 1?
AMigration status is checked
BThe database schema is updated
CA migration file is created
DThe migration is rolled back
💡 Hint
Check the 'Effect' column in step 1 of the execution_table.
At which step does the database schema get updated?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Result/Output' column describing the migration running.
If you skip running 'rails db:migrate', what will 'rails db:migrate:status' show?
AAll migrations up to date
BThe new migration marked as down (pending)
CNo migrations found
DAn error message
💡 Hint
Refer to the meaning of 'up' and 'down' in migration status from execution_table step 4.
Concept Snapshot
Creating migrations in Rails:
1. Generate migration file with 'rails generate migration Name'
2. Edit migration file if needed
3. Run 'rails db:migrate' to apply changes
4. Check status with 'rails db:migrate:status'
Migrations update database schema safely and track changes.
Full Transcript
Creating migrations in Rails involves generating a migration file that contains instructions to change the database schema. After generating, you can edit the file to specify the exact changes. Running 'rails db:migrate' applies these changes to the database. Finally, 'rails db:migrate:status' shows which migrations have been applied or are pending. This process helps keep the database structure organized and version controlled.