1
0
mirror of https://github.com/fspc/BikeShed-1.git synced 2025-02-28 16:53:23 -05:00
BikeShed-1/db/migrate/20131019023429_change_bike_shop_id.rb

23 lines
623 B
Ruby
Raw Normal View History

2013-10-19 10:03:02 -04:00
class ChangeBikeShopId < ActiveRecord::Migration
# We don't use change_column because we have arbitrary strings we
# need to strip the characters from to make the integer values
def up
bikes = Bike.all
remove_column :bikes, :shop_id
add_column :bikes, :shop_id, :integer, :unique => true
# deconflict shop ids
bikes.each do |bike|
new_id = bike.shop_id.gsub(/[^\d]/, '').to_i rescue 1
while bikes.map(&:shop_id).include? new_id
new_id += 1
end
2013-11-02 15:12:55 -04:00
bike.update_attribute(:shop_id, new_id)
2013-10-19 10:03:02 -04:00
end
end
def down
change_column :bikes, :shop_id, :string
end
end