Create a Migration in Rails Manually

Shaqqour
3 min readJan 1, 2021
Photo by Alok Sharma on Unsplash

Migration in Rails is responsible for creating and editing database tables. To migrate the database changes in rails, run $ rails db:migrate.
Rails will only read the new migration files that have never been migrated before. So once you run a migration file, you can’t go back and change it and run it again. You will have to create a new migration to edit your database.

In this blog post, I will show you how to manually create a migration file in Rails; whether it is a whole new table or fixing an existing table.

Create a table

You will have to create a new file in your db/migrate folder and you can choose a name such as 01_create_employees.rb. In this file, you will define a method within a class that inherits from Active Record. This method will create the employees' table. Here is a code snippet on how this file will look like:

class CreateEmployees < ActiveRecord::Migration[5.1]
def change
create_table :employees do |t|
t.string :first_name
t.string :last_name
end
end
end

We defined the change method indicating inside that these are the changes I want to do to my database which is creating a new table called employees. We are using the ActiveRecord method create_table to do so by passing a block that contains the attributes for this table and the data type for each one.

For these changes to take effect, as I said before, you will have to run this migration by running the following command:$ rails db:migrate

Add a Column to a table

Here you will create a migration to add a column, such as a salary attribute, to the employees’ table. Let’s create a file in the same directory, db/migrate, and name it 02_add_salary_to_employees.rb. Similar to creating a table, you will define a method within a class that inherits from Active Record. This method will add the salary column to the employees’ table. Here is a code snippet on how this file will look like:

class AddSalaryToEmployees < ActiveRecord::Migration[5.1]
def change
add_column :employees, :salary, :integer
end
end

As before, we defined the change method indicating inside that these are the changes I want to do to my employees’ table which is adding the salary attribute to it. We are using another ActiveRecord method add_column to do so by passing a block that contains the table you want to add the attribute to, the attribute you want to add, and the data type for this attribute. Run $rails db:migrate command for these changes to take effect.

There are many situations that you will need to create a new migration for, such as fixing a data type for a column. Here is a link you can check out for some extra ActiveRecord migration methods you can use to update your database tables.

If you have anything you would like to add, please comment below!

--

--

Shaqqour

Full stack software engineer. Passionate about making people’s lives better and easier through programming. LinkedIn.com/in/shaqqour