0
0
Ruby on Railsframework~10 mins

Database folder and migrations in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database folder and migrations
Create Migration File
Write Schema Changes
Run Migration Command
Rails Updates Schema
Database Structure Changes
Migration Status Updated
Shows how a migration file is created, written, run, and applied to update the database schema.
Execution Sample
Ruby on Rails
class CreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end
This migration creates a users table with a name column and timestamps.
Execution Table
StepActionFile/CommandEffectDatabase State
1Generate migration filerails generate migration CreateUsersCreates db/migrate/xxxx_create_users.rbNo users table yet
2Write migration codedb/migrate/xxxx_create_users.rbDefines users table with name and timestampsNo change yet
3Run migrationrails db:migrateRails runs migration codeUsers table created with columns: id, name, created_at, updated_at
4Update schema.rbAutomaticSchema file updated to reflect new tableSchema matches database
5Migration statusrails db:migrate:statusShows migration as upMigration applied successfully
6Exit-No more migrations to runDatabase schema up to date
💡 All migrations applied, database schema is current
Variable Tracker
VariableStartAfter Step 1After Step 3Final
users_tabledoes not existmigration file createdtable created in DBtable exists with columns
Key Moments - 2 Insights
Why doesn't the database change immediately after creating the migration file?
Creating the migration file only prepares the instructions. The database changes only after running 'rails db:migrate' as shown in step 3 of the execution_table.
What does the schema.rb file represent?
schema.rb is an auto-generated file that reflects the current database structure after migrations run, as seen in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the users table actually created in the database?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Effect' and 'Database State' columns for when the table appears
According to variable_tracker, what is the state of users_table after step 1?
AMigration file created but table does not exist
BTable exists with columns
CTable created in DB
DNo users table yet
💡 Hint
Look at the 'After Step 1' column for users_table
If you skip running 'rails db:migrate', what will happen to the database?
ADatabase schema updates automatically
BDatabase remains unchanged
CSchema.rb updates but DB does not
DMigration status shows as up
💡 Hint
Refer to step 3 and the exit_note in execution_table
Concept Snapshot
Database folder holds migration files.
Migrations describe schema changes.
Run 'rails db:migrate' to apply changes.
Schema.rb reflects current DB structure.
Migration status shows applied migrations.
Full Transcript
In Rails, the database folder contains migration files that describe changes to the database structure. First, you generate a migration file which is just a set of instructions. Then you write the schema changes inside it, like creating tables or adding columns. When you run 'rails db:migrate', Rails executes these instructions and updates the actual database. After migration, Rails updates the schema.rb file to reflect the current database structure. You can check migration status to see which migrations have been applied. This process ensures your database matches your app's needs step-by-step.