From cd148be07298e298bb84f5df7b973f2b188830ee Mon Sep 17 00:00:00 2001
From: eileencodes <eileencodes@gmail.com>
Date: Wed, 31 Jul 2019 12:22:01 -0400
Subject: Fix db:seed

The `rake db:seed` command was broken for the primary environment if the
application is using multiple databases. We never implemented `rake
db:seed` for other databases (coming soon), but that shouldn't break the
default case.

The reason this was broken was because `abort_if_pending_migrations`
would loop through the configs for all databases and check for
migrations but would leave the last established connection. So `db:seed`
was looking in the wrong database for the table to seed.

This PR doesn't fix the fact that `db:seed` doesn't work for multiple
databases but does fix the default case.

Fixes #36817

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
---
 railties/test/application/rake/multi_dbs_test.rb | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

(limited to 'railties')

diff --git a/railties/test/application/rake/multi_dbs_test.rb b/railties/test/application/rake/multi_dbs_test.rb
index 2606e64424..c5f8904f4e 100644
--- a/railties/test/application/rake/multi_dbs_test.rb
+++ b/railties/test/application/rake/multi_dbs_test.rb
@@ -349,6 +349,25 @@ module ApplicationTests
           rails "db:drop" rescue nil
         end
       end
+
+      test "db:seed uses primary database connection" do
+        @old_rails_env = ENV["RAILS_ENV"]
+        @old_rack_env = ENV["RACK_ENV"]
+        ENV.delete "RAILS_ENV"
+        ENV.delete "RACK_ENV"
+
+        db_migrate_and_schema_dump_and_load "schema"
+
+        app_file "db/seeds.rb", <<-RUBY
+          print Book.connection.pool.spec.config[:database]
+        RUBY
+
+        output = rails("db:seed")
+        assert_equal output, "db/development.sqlite3"
+      ensure
+        ENV["RAILS_ENV"] = @old_rails_env
+        ENV["RACK_ENV"] = @old_rack_env
+      end
     end
   end
 end
-- 
cgit v1.2.3