cheat sheets.

$ cheat migrations
Methods:
  create_table(name, options)
  drop_table(name)
  rename_table(old_name, new_name)
  add_column(table_name, column_name, type, options)
  rename_column(table_name, column_name, new_column_name)
  change_column(table_name, column_name, type, options)
  remove_column(table_name, column_name)
  add_index(table_name, column_name, index_type)
  remove_index(table_name, column_name)

Available Colum Types:
  * integer
  * float
  * datetime
  * date
  * timestamp
  * time
  * text
  * string
  * binary
  * boolean

Valid Column Options:
  * limit
  * null (i.e. ” :null => false” implies NOT NULL)
  * default (to specify default values)
  
Rake Tasks:
  rake db:schema:dump: run after you create a model to capture the schema.rb
  rake db:schema:import: import the schema file into the current database (on
  error, check if your schema.rb has ”:force => true” on the create table
  statements
  ./script/generate migration MigrationName: generate a new migration with a new
  ‘highest’ version (run ’./script/generate migration’ for this info at
  your fingertips)
  rake db:migrate: migrate your current database to the most recent version
  rake db:migrate VERSION=5: migrate your current database to a specific version
  (in this case, version 5)

SQL:
  Queries can be executed directly:
  execute 'ALTER TABLE researchers ADD CONSTRAINT fk_researchers_departments
  FOREIGN KEY ( department_id ) REFERENCES departments( id )'
  
Example Migration:
  class UpdateUsersAndCreateProducts < ActiveRecord::Migration
    def self.up
      rename_column "users", "password", "hashed_password" 
      remove_column "users", "email" 

      create_table "products", :force => true do |t|
          t.column "name", :text
          t.column "description", :text
      end
    end

    def self.down
      rename_column "users", "hashed_password", "password" 
      add_column "users", "email" 
      drop_table "products" 
    end
  end
Version 156, updated 819 days ago.
. o 0 ( | previous | history | revert to | current | diff )
( add new | see all )