Goal

You’ll be able to write and run basic migrations, and understand why migrations exist as a genuinely important practice, not just Rails busywork.

Learn

A migration is a versioned, incremental change to your database schema, written in Ruby rather than raw SQL:

class CreateProducts < ActiveRecord::Migration[7.0]
  def change
    create_table :products do |t|
      t.string :name
      t.decimal :price
      t.timestamps
    end
  end
end

Running rails db:migrate applies any pending migrations, actually changing the database schema to match. t.timestamps is a genuinely useful shortcut adding both created_at and updated_at columns automatically, which ActiveRecord then manages for you without any extra code.

Here’s the genuinely important reason migrations exist, beyond just “the Rails way”: they provide a versioned, shareable history of database changes. Without migrations, coordinating database schema changes across a team (or between your development machine and a production server) would require manually tracking and applying raw SQL changes, with real risk of one environment’s database schema silently drifting out of sync with another’s.

rails generate migration AddStockToProducts stock:integer
rails db:migrate

This generates and applies a migration adding a new stock column to the existing products table — an incremental change layered on top of the original table creation, exactly the versioned-history model migrations are built around.

Decision Task

A team of three developers is working on the same Rails project, each on their own computer. Before reading on: what real, practical problem do migrations specifically solve for this team, compared to each developer manually running SQL commands to update their own local database?

Show Answer

Migrations provide a versioned, shareable, ordered history of every schema change, checked into version control alongside the actual code — every developer can run rails db:migrate to apply the exact same sequence of changes to their own local database, guaranteeing all three databases stay in sync. Manual SQL changes would risk each developer’s database silently drifting out of sync with the others, with no reliable, ordered record of what changed and when.

Common Mistake

Manually editing a database’s schema directly (through a database GUI tool, for example) instead of creating a proper migration for the change. This bypasses the entire versioned-history benefit migrations provide, and specifically creates the exact synchronization problem across team members’ machines (or between development and production) that migrations exist to prevent.

Practice Questions

1. What command applies any pending migrations to the database?

Show Answer

rails db:migrate

2. What does t.timestamps automatically add to a table, and what manages those columns afterward?

Show Answer

It adds created_at and updated_at columns; ActiveRecord automatically manages and updates these without needing extra code.

3. Why do migrations matter for a team working on the same project across different machines?

Show Answer

They provide a versioned, shareable, ordered history of schema changes that every team member can apply identically, keeping every database in sync rather than risking silent drift between different environments.

4. True or False: it’s generally fine to manually edit a production database’s schema directly, bypassing migrations, as long as you remember what you changed.

Show Answer

False — this bypasses the versioned-history and team-synchronization benefits migrations specifically provide, creating real risk of schema drift between environments.

5. Write a command that generates a migration adding a “featured” boolean column to a products table.

Show Answer

rails generate migration AddFeaturedToProducts featured:boolean

Try It Yourself

Without looking back, write a complete migration class that creates a “comments” table with a text column called “body” and a timestamps call.

Show Answer

class CreateComments < ActiveRecord::Migration[7.0]\n def change\n create_table :comments do |t|\n t.text :body\n t.timestamps\n end\n end\nend

Quick Check

1. What is a migration?

Show Answer

A versioned, incremental change to your database schema, written in Ruby.

2. What command applies pending migrations?

Show Answer

rails db:migrate

3. What does t.timestamps add?

Show Answer

created_at and updated_at columns, automatically managed by ActiveRecord.

4. What real team-coordination problem do migrations solve?

Show Answer

They keep every developer’s (and every environment’s) database schema in sync through a shared, versioned, ordered history of changes.

5. What command generates a new migration file?

Show Answer

rails generate migration [MigrationName]

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.