Thursday, July 28, 2011

#change

Do you now about change?
In Rails 3.1 ActiveRecord::Migration we can define only one method
change
It`s 'reversible' migrations. You just implement 'up' version and Migration system generate for you methods to create revers migration.
For example:
class CreateWagons < ActiveRecord::Migration
  def change
    create_table :wagons do |t|
      t.integer :number
      t.integer :mileage
      t.datetime :build_date
    end
  end
end
When you run migration 'up' it create a new table, if 'down' - drop dable. But you must be careful using this magick, because some methods can`t be resolved and you get ActiveRecord::IrreversibleMigration
Rails known how to revert this commands:
add_column
add_index
add_timestamp
create_table
remove_timestamps
rename_column
rename_index
rename_table
There for you can create 'change' migration. Try to run it 'up' and 'down', and if you migration raised, you just implement 'down' version.
I think it`s one more beautiful issues that presents to us Rails platform.