In Rails 3.1+, instead of defining a separate up
and down
method you can define a single method change
:
class AddComparisonFieldsToReport < ActiveRecord::Migration
def change
add_column :reports, :compare, :boolean
update "UPDATE reports SET compare = #{quoted_false}"
add_column :reports, :compare_start_date, :date
add_column :reports, :compare_end_date, :date
end
end
Migrating up works as expected:
b rake db:migrate
== AddComparisonFieldsToReport: migrating ====================================
-- add_column(:reports, :compare, :boolean)
-> 0.0274s
-- quoted_false()
-> 0.0000s
-- update("UPDATE reports SET compare = 0")
-> 0.0018s
-> 2 rows
-- add_column(:reports, :compare_start_date, :date)
-> 0.0610s
-- add_column(:reports, :compare_end_date, :date)
-> 0.0451s
== AddComparisonFieldsToReport: migrated (0.1358s) ===========================
Migrating down also works as expected. Note how destructive commands that cannot be reverted (update
) are simply skipped:
b rake db:rollback
== AddComparisonFieldsToReport: reverting ====================================
-- remove_column("reports", :compare_end_date)
-> 0.0699s
-- remove_column("reports", :compare_start_date)
-> 0.0448s
-- remove_column("reports", :compare)
-> 0.0294s
== AddComparisonFieldsToReport: reverted (0.1444s) ===========================
Posted by Henning Koch to makandra dev (2012-07-09 09:31)