Link Search Menu Expand Document

DB Migration

Table of contents

Column data types

# Standard

# Extra native data types for Postgres:
:json / :jsonb

# and more
# @see

Change existing table

add_column :products, :price, :decimal, precision: 5, scale: 2
change_column :products, :quantity, :integer
# change column type from string to integer with Postgres
change_column :products, :price, 'integer USING CAST(price AS integer)'
remove_column :products, :photo, :string
rename_column :products, :image, :photo
change_column_default :advertisements, :enable, from: true, to: false
drop_table :products

Apply bulk: true when changing multiple columns in an existing table

# before
def up
  add_column :table, :useful_foreign_key, :integer
  add_index  :table, :useful_foreign_key
  add_column :table, :summary, :string

# after
def up
  change_table :table, bulk: true do |t|
    t.integer :useful_foreign_key
    t.index   :useful_foreign_key
    t.string  :summary


add_index :products, :category_id
add_index :users, :email, unique: true
# partial index with Postgres
add_index :users, :is_active, where: "is_active = true"

remove_index :products, :category_id

Migrate and Rollback

# Rails 3/4
rake db:migrate
rake db:rollback

# Rails 5+
rails db:migrate
rails db:rollback

# migrate/rollback a specific version
rails db:migrate VERSION=version
rails db:rollback VERSION=version

# rollback several migrations
rails db:rollback STEP=3

# rollback and rerun migrations
rails db:migrate:redo STEP=3

Reset PK sequence for Postgres:

# single table

# all tables
ActiveRecord::Base.connection.tables.each do |t|