Ruby Academy · Lesson

Migrations

Evolving the schema.

Lesson 2 of 413 steps

Migrations is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

What Is a Migration?

A migration is a versioned Ruby file that describes a change to your database schema, such as creating a table or adding a column.

  • Migrations are code, so they live in version control.
  • Everyone on the team applies the same changes in the same order.
  • You never edit the database by hand.

Generating a Migration

Rails generates migration files for you. For example rails generate migration CreateUsers creates a timestamped file in db/migrate/. The timestamp fixes the order in which migrations run.

Creating a Table

A migration defines a change method. create_table builds a table; t.timestamps adds created_at and updated_at automatically.

class CreateUsers < ActiveRecord::Migration[7.1]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.timestamps
    end
  end
end

Column Types

Common column types map to database types:

  • string short text.
  • text long text.
  • integer, float, decimal numbers.
  • boolean true/false.
  • datetime, date time values.
create_table :products do |t|
  t.string :title
  t.text :description
  t.decimal :price
  t.boolean :active
  t.timestamps
end

Running Migrations

rails db:migrate applies all pending migrations and updates schema.rb. Rails records which migrations have run in a schema_migrations table so each runs only once.

Adding a Column

To extend an existing table, generate a migration and use add_column. You specify the table, column name, type, and options.

class AddAgeToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :age, :integer, default: 0
  end
end

Rolling Back

rails db:rollback undoes the last migration. Rails reverses a change method automatically when the operations are reversible (like add_column or create_table).

Irreversible Changes

Some operations cannot be auto-reversed, like running raw SQL or removing a column with data. For these, define explicit up and down methods so Rails knows how to undo the change.

class ChangeData < ActiveRecord::Migration[7.1]
  def up
    execute "UPDATE users SET active = TRUE"
  end

  def down
    execute "UPDATE users SET active = FALSE"
  end
end

Indexes

Add an index to speed up lookups on a column you query often, such as a foreign key or an email used for login. Use unique: true to also enforce uniqueness at the database level.

class AddIndexToUsersEmail < ActiveRecord::Migration[7.1]
  def change
    add_index :users, :email, unique: true
  end
end

Migrations vs schema.rb

Migrations are the history of changes; schema.rb is the current snapshot. New developers can run rails db:schema:load to build the database instantly from the snapshot instead of replaying every migration.

Best Practices

Keep migrations safe:

  • Never edit a migration that has run on shared environments; write a new one.
  • Keep each migration focused on one change.
  • Add database-level constraints (null: false, indexes) for integrity.

Quick Check

Test your understanding of migrations.

Recap: Migrations

You learned to evolve the schema safely:

  • Migrations are versioned, committed schema changes.
  • create_table, add_column, and add_index shape tables.
  • db:migrate applies; db:rollback reverses.
  • Use up/down for irreversible changes.
  • schema.rb is the current snapshot.

Next we connect models together with associations.

Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Migrations” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Migrations”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Migrations”?

Evolving the schema. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Migrations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Active Record Basics
  2. Migrations
  3. Associations
  4. Validations and Queries
← Back to Ruby Academy