0
0
Ruby on Railsframework~3 mins

Why migrations version the database in Ruby on Rails - The Real Reasons

Choose your learning style9 modes available
The Big Idea

Discover how a simple version number can save your database from chaos!

The Scenario

Imagine you have a team working on a website, and everyone changes the database structure by hand on their own computers.

One person adds a column, another removes a table, but no one keeps track of who did what or when.

It quickly becomes a mess, and the database on each computer looks different.

The Problem

Manually changing the database is confusing and risky.

You can easily forget what changes were made or accidentally overwrite someone else's work.

This causes bugs, lost data, and wasted time fixing problems.

The Solution

Migrations version the database by keeping a clear, ordered list of changes.

This way, everyone applies the same steps in the same order, making the database consistent across all computers.

It also allows easy rollback if something goes wrong.

Before vs After
Before
ALTER TABLE users ADD COLUMN age INTEGER;
-- No record of this change or order
After
class AddAgeToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :age, :integer
  end
end
What It Enables

It enables smooth teamwork and safe, trackable database updates that everyone can follow.

Real Life Example

A team building an online store can add new features like product reviews or discounts without breaking the database for others.

Key Takeaways

Migrations keep database changes organized and versioned.

This prevents conflicts and errors in team projects.

It makes updating and fixing the database safer and easier.