LearnThatStack Ace your next interview
Ruby on Rails · question
Question 5 of 70

Explain database migrations in Rails

beginner
← All Ruby on Rails questions
Re-explain
Visual ↓

A migration is a Ruby class that changes the database schema one step at a time, so the schema evolves in a consistent way. You describe the change in a Ruby domain-specific language (DSL) instead of SQL. A migration written only in this DSL works on any database Rails supports.

  • Each file name starts with a timestamp, and Rails records the ones it ran in the schema_migrations table. That gives the schema version control.
  • Teammates get new migrations with the code and run bin/rails db:migrate to catch up.
  • Rollback with bin/rails db:rollback reverts the last migration. Rails can reverse a change method by itself when every command in it is reversible, like create_table or add_column. Raw SQL and other irreversible steps need separate up and down methods.
class CreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      t.string :email
      t.timestamps
    end
  end
end

Each run updates db/schema.rb, the schema snapshot used to build a fresh database.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
Migration files and the versions a database ran
Migration filesin the repo
20260105093000create_users
20260212093000create_posts
20260320093000create_tags
Applied versionsthis database's schema_migrations
2026010509300020260212093000
After each commandtables and db/schema.rb
tablesusers, posts
db/schema.rbversion 20260212093000
Console
1step

A file's timestamp is its version. The schema_migrations table records each version this database ran. Only two are there, so create_tags has not run.

1 / 3

Rails runs only the migration files whose versions are missing from schema_migrations, and rollback removes the newest version but never the file.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Ruby on Rails cheatsheet.

← Back to all Ruby on Rails questions
Pro · $10/mo

61 of 70 Ruby on Rails answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime