mirror of
https://github.com/fspc/BikeShed-1.git
synced 2025-02-28 08:43:23 -05:00
Added migration file
This commit is contained in:
parent
6cbe0f8b57
commit
5e042c2577
23
db/migrate/20131019023429_change_bike_shop_id.rb
Normal file
23
db/migrate/20131019023429_change_bike_shop_id.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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
|
||||||
|
bike.shop_id = new_id
|
||||||
|
end
|
||||||
|
bikes.each(&:save)
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
change_column :bikes, :shop_id, :string
|
||||||
|
end
|
||||||
|
end
|
@ -16,8 +16,10 @@ end
|
|||||||
if BikeBrand.all.empty? and BikeModel.all.empty?
|
if BikeBrand.all.empty? and BikeModel.all.empty?
|
||||||
# Need to use DEFAULT instead of explicit IDs that are used in the sql file,
|
# Need to use DEFAULT instead of explicit IDs that are used in the sql file,
|
||||||
# so that the PG table ID sequence is incremented
|
# so that the PG table ID sequence is incremented
|
||||||
|
#
|
||||||
|
# Note the drop(1) which assumes we have a junk PRAGMA line at the top
|
||||||
load_statements = File.readlines(File.join(Rails.root, 'db', 'seed', 'sql', 'bike_brands_and_models.sql')).drop(1).map do |statement|
|
load_statements = File.readlines(File.join(Rails.root, 'db', 'seed', 'sql', 'bike_brands_and_models.sql')).drop(1).map do |statement|
|
||||||
statement.sub(/VALUES\(\d+,/, 'VALUES(DEFAULT,').tap {|x| puts x }
|
statement.sub(/VALUES\(\d+,/, 'VALUES(DEFAULT,')
|
||||||
end
|
end
|
||||||
ActiveRecord::Base.connection.execute(load_statements.join)
|
ActiveRecord::Base.connection.execute(load_statements.join)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user