Ruby on Rails Migrations – Quick Reference

by @ralfebert · updated October 10, 2021

Create a new table

bin/rails g model Supplier name:string
bin/rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary

Resulting migrations:

class CreateSuppliers < ActiveRecord::Migration[6.1]
  def change
    create_table :suppliers do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateProducts < ActiveRecord::Migration[6.1]
  def change
    create_table :products do |t|
      t.string :name
      t.string :sku, limit: 10
      t.integer :count
      t.text :description
      t.references :supplier, null: false, foreign_key: true
      t.float :popularity
      t.decimal :price, precision: 10, scale: 2
      t.boolean :available
      t.datetime :availableSince
      t.binary :image

      t.timestamps
    end
    add_index :products, :name
    add_index :products, :sku, unique: true
  end
end

Add a column

bin/rails g migration AddKeywordsSizeToProduct keywords:string size:string

Resulting migration:

class AddKeywordsSizeToProduct < ActiveRecord::Migration[6.1]
  def change
    add_column :products, :keywords, :string
    add_column :products, :size, :string
  end
end

Remove a column

bin/rails g migration RemoveKeywordsFromProduct keywords

Resulting migration:

class RemoveKeywordsFromProduct < ActiveRecord::Migration[6.1]
  def change
    remove_column :products, :keywords, :string
  end
end

Rename a column

bin/rails g migration RenameProductPopularityToRanking

You need to add the rename_column command manually to the resulting migration:

class RenameProductPopularityToRanking < ActiveRecord::Migration[6.1]
  def change
    rename_column :products, :popularity, :ranking
  end
end

Change a column type

bin/rails g migration ChangeProductPopularity

You need to add the change_column command manually to the resulting migration:

class ChangeProductPopularity < ActiveRecord::Migration[6.1]
  def change
      change_column :products, :ranking, :decimal, precision: 10, scale: 2
  end
end

Running migrations

bin/rake db:migrate

In production:

bin/rake db:migrate RAILS_ENV="production" 

More information