Rails Migrations Basics

Techie     March 2023

Introduction

Rails Migrations are a powerful tool that allows developers to manage database changes and schema updates in a version-controlled and consistent way. Migrations help developers to easily modify the structure of their application’s database, and they are a key component of the Ruby on Rails framework.

In this section, we’ll cover the basics of how to create, modify, and manage migrations in a Rails application.


Creating a Migration

To create a new migration in a Rails application, you can use the rails generate command followed by the migration name and the list of attributes that you want to add to the database table. For example, to create a migration that adds a new column to the users table, you can run the following command:


$ rails generate migration add_email_to_users email:string


This will create a new migration file in the db/migrate directory with a name that looks something like 20210312132235_add_email_to_users.rb. The name of the migration file includes a timestamp to ensure that each migration is executed in the correct order.

The migration file will contain two methods: up and down. The up method is used to define the changes that need to be made to the database, and the down method is used to define how to revert those changes.

Here’s an example of what the up method might look like for the add_email_to_users migration:


def up
  add_column :users, :email, :string
end


This code will add a new column named email to the users table with a data type of string.

The down method is used to undo the changes made by the up method. Here’s an example of what the down method might look like for the add_email_to_users migration:


def down
  remove_column :users, :email
end


This code will remove the email column from the users table.


Running Migrations

Once you’ve created a migration, you can run it by running the following command:


$ rails db:migrate


This will execute all the pending migrations in your application. Rails keeps track of which migrations have already been run, so running rails db:migrate multiple times will not apply the same migration more than once.

You can also roll back a migration by running the following command:

$ rails db:rollback


This will undo the last migration that was applied. You can roll back multiple migrations by specifying the number of migrations to roll back:


$ rails db:rollback STEP=2


This will roll back the last two migrations that were applied.


Modifying a Migration

If you need to modify an existing migration, you can edit the migration file directly. However, you should be aware that once a migration has been run, it should not be modified unless absolutely necessary. Modifying a migration that has already been applied can cause issues with the consistency of your database schema.

If you need to make a change to a migration that has already been run, you should create a new migration that modifies the existing schema. For example, if you need to rename a column that was added in a previous migration, you could create a new migration that renames the column:


$ rails generate migration rename_email_column_in_users


This will create a new migration file that you can use to rename the column. Here’s an example of what the up method might look like:


def up
  rename_column :users, :email, :new_email
end


This code will rename the email column to new_email.


Adding Indexes

Adding indexes to your database tables can help to improve query performance. You can create an index on a column in a table using a migration. Here’s an example of how to create an index on the email column in the users table:


def change
  add_index :users, :email
end


This code will create an index on the email column in the users table. Rails will automatically generate a name for the index based on the table and column names.


Dropping Tables

If you need to remove a table from your database, you can do so using a migration. Here’s an example of how to remove the users table:


def change
  drop_table :users
end


This code will remove the users table from your database.


Conclusion

Migrations are a powerful tool that allow you to manage database changes and schema updates in a version-controlled and consistent way. In this tutorial, we covered the basics of how to create, modify, and manage migrations in a Rails application.

Remember that once a migration has been applied, it should not be modified unless absolutely necessary. If you need to make a change to a migration that has already been applied, create a new migration that modifies the existing schema.

With Rails Migrations, you can easily modify the structure of your application’s database and keep your schema in sync with your application’s code.


Thanks for reading, see you in the next one!