I am a full-stack developer student currently doing the Microverse program, and I've been developing things for months until now. This article is for helping newcomers to Rails to understand Rails migrations by showing some examples and explaining them the best I can.
If you are a newbie with Rails, you may have thought that migrations are complicated, as always, it can be, but most of the time is not. First of all, let's demystify the concept of migration, this is just a regular file, that's right, a plain simple ruby file placed under db/migrate used by Rails to change the database schema incrementally.
What means that changes are made incrementally? Putting it simple, something like creating a table could be one migration (remember, a file), then, other thing like renaming, adding, or indexing a column afterwards could be another migration, is not that complicated right?
So, how do you create a migration? Rails expects that migrations be named in the following way: YYYYMMDDHHMMSS_my_migration.rb, obviously we don't want to write the UTC timestamp every time we create a migration, Rails have us covered, just type this in your terminal (obviously you must be in the Rails application directory).
rails generate migration CreateProducts title:string description:text price:decimal image_url:string
What's that gibberish? That's how you'll usually generate a migration, this specific migration defines the creation of a table named products, with columns: title of type string, description of type text, price of type decimal that has a precision of 8 significant figures and 2 digits after the point, and image_url of type string.
Alright then, is that all? Well, if you wanted to add something extra you can go and edit the migration, let's take a look at the generated file, remember that it should be called something like 20191205104706_create_products.rb and be placed under the directory
db/migrate
of your Rails application.class CreateProducts < ActiveRecord::Migration[6.0]
def change
create_table :products do |t|
t.title :string
t.description :text
t.price :decimal, precision: 8, scale: 2
t.image_url :string
# I forgot about suppliers
t.references :supplier, foreign_key: true
# This only creates columns created_at and updated_at, is a default
t.timestamps
end
end
end
Now, let's see the current state of our migrations.
rails db:migrate:status
You should see something like the following (this is the migrations status of one of my rails experiments).
If you haven't applied the migrations yet (more on that in a moment), some migrations should have a status of
down
, to apply pending migrations run the db:migrate
task.rails db:migrate
Now, if you take a look at the status of your migrations all of them should be
up
.A good feature about migrations is the ability of going back in time, this is achieved by the
db:rollback
task.rails db:rollback
This will revert the latest migration, of course, you can revert several migrations at once.
rails db:rollback STEP=3
This will revert the last 3 migrations.
Normally, you'll want to do little things like adding a column to an existing table.
rails generate migration add_sales_to_products sales:bigint
class AddSalesToProduct < ActiveRecord::Migration[6.0]
def change
add_column :products, :sales, :bigint
end
end
And sometimes, you want to do several changes to an existing table.
rails generate migration change_products
class ChangeProducts < ActiveRecord::Migration[6.0]
def change
change_table :products do |t|
t.remove :image_url, :description
t.index :title
end
end
end
There are a lot of more complicated things you can do with migrations, but for a beginner this will do for now. If you want to know more about migrations, the guys that work on Rails provide a lot of useful information on the subject. But never forget.
The only way of learning some activity is by actually performing such activity over and over until it becomes natural to you.
Hope this article helped a bit to your grasp of Rails migrations, you can reach me out in LinkedIn, Github, Twitter, and Facebook.