Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created March 15, 2012 14:27
Show Gist options
  • Save wrburgess/2044461 to your computer and use it in GitHub Desktop.
Save wrburgess/2044461 to your computer and use it in GitHub Desktop.
Rails Migrations #rails #ruby #migrations #activerecord

##References

##Create Empty Migration command:

rails generate migration MyNewMigration

##Empty Migration Example


  class TenderloveMigration < ActiveRecord::Migration
    def change
      create_table(:horses) do |t|
        t.column :content, :text
        t.column :remind_at, :datetime
      end
    end
  end

##Basic Instructions

  • create_table(name, options)
  • drop_table(name)
  • rename_table(old_name, new_name)
  • add_column(table_name, column_name, type, options):
    • types: :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.
    • A default value can be specified by passing an options hash like { :default => 11 }. Other options include :limit and :null (e.g. { :limit => 50, :null => false })
  • rename_column(table_name, column_name, new_column_name)
  • change_column(table_name, column_name, type, options)
  • remove_column(table_name, column_names)

##Adding Indexes

  • add_index(table_name, column_names, options): Adds a new index with the name of the column. Other options include :name, :unique (e.g. { :name => "users_name_index", :unique => true }) and :order (e.g. { :order => {:name => :desc} })
  • remove_index(table_name, :column => column_name)
  • remove_index(table_name, :name => index_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment