ref: master
db/migrate/20140807134625_change_category_display_color_to_string.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class ChangeCategoryDisplayColorToString < ActiveRecord::Migration COLORS = ['ffa500', '00FF00', 'a020f0', 'ff0000', '006400', '191970', '0000ff', 'a52a2a', '32cd32', 'add8e6', '483d8b', 'b8e9ee', 'f5f5dc', 'ffff00', 'f4a460'] def self.up change_table :categories do |t| t.string :display_color_tmp, :limit => 6 end COLORS.each_with_index do |color, i| Category.where(display_color: i+1).update_all display_color_tmp: color end change_table :categories do |t| t.remove :display_color t.rename :display_color_tmp, :display_color end end def self.down puts "WARNING: only old defined colors will be reverted" change_table :categories do |t| t.integer :display_color_tmp end COLORS.each_with_index do |color, i| Category.where(display_color: color).update_all display_color_tmp: i+1 end change_table :categories do |t| t.remove :display_color t.rename :display_color_tmp, :display_color end end end |