0
0
Ruby on Railsframework~3 mins

Why Database folder and migrations in Ruby on Rails? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could change your app's data safely without fear of breaking everything?

The Scenario

Imagine you have a website where users can sign up, but every time you want to add a new feature like a profile picture or change the user data, you have to manually update the database tables by writing complex commands.

The Problem

Manually changing the database structure is risky and confusing. You might forget what changes you made, accidentally break the data, or struggle to keep track of updates across different computers or team members.

The Solution

Database migrations in Rails let you write simple, clear instructions to change your database step-by-step. These instructions are saved in files inside the database folder, so you can easily apply, track, and even undo changes safely.

Before vs After
Before
ALTER TABLE users ADD COLUMN profile_picture VARCHAR(255);
After
class AddProfilePictureToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :profile_picture, :string
  end
end
What It Enables

This makes it easy to evolve your app's data structure over time, share changes with your team, and keep your database organized and reliable.

Real Life Example

When your app grows and you want to add a new feature like storing user addresses, you just create a migration file describing the change, run it, and your database updates automatically without losing existing data.

Key Takeaways

Manual database changes are error-prone and hard to track.

Migrations provide a safe, clear way to update your database step-by-step.

The database folder keeps all migration files organized for easy teamwork and history.