migrations

class ... < ActiveRecord::Migration
  def change
    add_column :target_table_name, :target_column_name, :decimal, precision: 10, scale: 4
    add_index :target_table_name, :target_column_name, :unique => true
    add_index :target_table_name, [:target_column1_name, :target_column2_name], :name => 'combined_index_name'
    add_reference :target_table_name, :foreign_key_name_without_id, :index => true
    #rails will add _id suffix automaticaly

    change_column :target_table_name, :target_column_name, :decimal, precision: 10, scale: 4, :default => default_value
    change_column_null :target_table_name, :targe_column_name, false
    # NOT NULL Allowed
    change_column_null :target_table_name, :targe_column_name, true
    # NULL Allowed

    rename_column :target_table_name, :target_column_name, :new_name_for_target_column

    remove_index :target_table_name, name: 'target_index_name'
    remove_column :target_table_name, :target_column_name
    drop_table :target_table_name
  end
end
class ... < ActiveRecord::Migration
  def self.up
    rename_table :target_table_name, :new_name_for_target_table
  end

  def self.down
    rename_table :new_name_for_target_table, :target_table_name
  end
end

Add a new column to a model

# rails g migration AddXXXToAnyModel XXX: TYPE
rails g migration AddPartnerToMerchant partner:integer
class AddPartnerToMerchant < ActiveRecord::Migration 
  def change 
    add_column :merchants, :partner, :integer 
  end
end

Something wrong?

rake db:rollback
rake db:rollback STEP=3
#revert the last 3 migrations
rake db:rollback RAILS_ENV=test
# for test environment

Reference

results matching ""

    No results matching ""