[ rails ] migration

Migration
use migration to alter table that is exists

$ rails generate migration DoNothingYet

db/migration/XXXX_do_nothing_yet.rb

class DoNothingYet < ActiveRecord::Migration
    def up

    end
    def down

    end
end

XXXX <- timestamp to help migration unique

Generate Model

generate model create migration at the same time
model name singular, table name plural.

$ rails generate model User

options
:limit => size ( size characters )
:default => value
:null => true/false ( false : not null )
:precision => number
:scale => number

If we do not want id(PK, auto increments), put :users, {:id => false}.
db/migration/XXXX_create_users.rb

class CreateUsers &lt; ActiveRecords::Migration
     def up
        create_table :users do |t|
            t.column "first_name", :string, options
            t.string "name", options
            # binary, boolean, date, datetime, decimal, float, integer, string, text, time

            # t.datetime "created_at" # auto-update by rails
            # t.datetime "updated_at" # auto-update by rails
            # timestamps equals to created_at and updated_at above
        	t.timestamps
        end
put "output "
put "step 2"
     end
     def down
put "step 2"
put "output "
     	drop_table :users
     end
end

Run migration

$ rake db:migrate RAILS_ENV=development

Run migration that is not yet been done.

schema_migrations table
version
XXXX <– timestamp will be used as an id
XXXX <– timestamp

running seven migration, there will be seven migration in this table
if run 3 migration, 4 will be run.
no need to change this table
/schema.rb : current db state

$ rake db:migrate:status

status: down ( not done yet )

Migration Command

$ rake db:migrate VERSION=migration_id

Example

$ rake db:migrate:down VERSION=20100905201547

ref : https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration

If migrated #1, #2, #3, then run rake db:migrate VERSION=#1, it will take us migrated down to #1.

$ rake db:migrate:up VERSION=migration_id

only run up of that VERSION

$ rake db:migrate:redo VERSION=migration_id

run down and up of that version

if not provide VERSION, it will assume the last version.

Migration methods

create_table
drop_table(table)
rename_table(table, newtable)

add_column(table, column, type, options) : options :after=>”other_field” ( works for mySQL )
remove_column(table, column)
rename_column(table, column, newname)  : can put in def change : ( however, we use def up: def down:, since not all the method is reversable )
change_column(table, column, type, options)

add_index(table, column, options) # always add for FK, and always used column
remove_index(table, column)

* column = [] array if we want to add multiple indexes
* options :unique => true/false, :name => “table_column_name”

sql setting/legacy table
execute(“any SQL string, specific for particular database i.e. MySQL, ProgresSQL”)

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s