Migrations
Evolving the schema.
Migrations is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
endColumn Types
Common column types map to database types:
stringshort text.textlong text.integer,float,decimalnumbers.booleantrue/false.datetime,datetime values.
create_table :products do |t|
t.string :title
t.text :description
t.decimal :price
t.boolean :active
t.timestamps
endRunning 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
endRolling 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
endIndexes
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
endMigrations 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, andadd_indexshape tables.db:migrateapplies;db:rollbackreverses.- Use
up/downfor irreversible changes. schema.rbis the current snapshot.
Next we connect models together with associations.
Frequently asked questions
Is the “Migrations” lesson free?
Yes — the full text of “Migrations” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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.