From 93b8e3b0551d1c5be3f426074c1a399320a90cd0 Mon Sep 17 00:00:00 2001 From: Ron Warholic Date: Fri, 18 Oct 2013 21:24:44 -0400 Subject: [PATCH 1/2] Fixed the seeder loading the sql incorrectly; now stubs the index out so it generates the sequence number. --- Gemfile.lock | 2 -- db/seeds.rb | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0765f43..faf0b50 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -162,7 +162,6 @@ GEM rdoc (~> 3.4) thor (>= 0.14.6, < 2.0) rake (10.0.4) - rb-fsevent (0.9.3) rdoc (3.12.2) json (~> 1.4) rspec (2.8.0) @@ -242,7 +241,6 @@ DEPENDENCIES pg pry (~> 0.9.8) rails (= 3.2.13) - rb-fsevent rspec-rails (~> 2.8.1) shoulda-matchers (~> 1.0.0) spork diff --git a/db/seeds.rb b/db/seeds.rb index f78c7ea..4704bf4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -14,10 +14,10 @@ end #Load bike brands and models from sql if BikeBrand.all.empty? and BikeModel.all.empty? - sql_path = File.join(Rails.root, 'db', 'seed', 'sql', 'bike_brands_and_models.sql') - db_config = Rails.application.config.database_configuration[Rails.env] - cmd = "psql #{db_config["database"]} < #{sql_path}" - system cmd + 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 } + end + ActiveRecord::Base.connection.execute(load_statements.join) end if Rails.env.development? From 8d8058dea0de6fbad4068a8cefcb8b5543b57861 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Fri, 18 Oct 2013 22:57:10 -0400 Subject: [PATCH 2/2] Comment for bike brands sql insert issue --- db/seeds.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 4704bf4..2d77169 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -14,6 +14,8 @@ end #Load bike brands and models from sql if BikeBrand.all.empty? and BikeModel.all.empty? + # Need to use DEFAULT instead of explicit IDs that are used in the sql file, + # so that the PG table ID sequence is incremented 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 } end