I make sure all the extra code is located after any database changes I want to make and then wrap the whole shebang in a begin-rescue block:
class FixCategoriesOrder < ActiveRecord::Migration
def self.up
begin
add_column :categories, :sort_order, :integer
# ... other database changes
# Code
[["Immunology", 1],
["Biochemistry", 2],
["Biotechnology", 3],
["Cell Biology", 4],
["Genomics", 5],
["Plant Biology", 6],
["Developmental Biology", 7],
["Ecology", 8],
["Other", 100]].each do |ar|
puts "Processing #{ar[0]} - #{ar[1]}"
Category.find_by_name(ar[0]).update_attribute(:sort_order, ar[1])
end
rescue
self.down
raise $!
end
end
def self.down
remove_column :categories, :sort_order
end
end
Njoy
No comments:
Post a Comment