Skip to content

Commit 7988759

Browse files
committed
reestablish previous connection after creating all databases
creating all databases mutates the connection pool. This patch restores the connection pool to the connection spec established before creating all databases. Fixes #23279
1 parent b2c2d32 commit 7988759

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ def create(*arguments)
116116
end
117117

118118
def create_all
119+
old_pool = ActiveRecord::Base.connection_handler.retrieve_connection_pool(ActiveRecord::Base)
119120
each_local_configuration { |configuration| create configuration }
121+
if old_pool
122+
ActiveRecord::Base.connection_handler.establish_connection(ActiveRecord::Base, old_pool.spec)
123+
end
120124
end
121125

122126
def create_current(environment = env)

railties/test/application/test_runner_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,19 @@ def test_pass_TEST_env_on_rake_test
502502
assert_match '1 runs, 1 assertions', output
503503
end
504504

505+
def test_rails_db_create_all_restores_db_connection
506+
create_test_file :models, 'account'
507+
output = Dir.chdir(app_path) { `bin/rails db:create:all db:migrate && echo ".tables" | rails dbconsole` }
508+
assert_match "ar_internal_metadata", output, "tables should be dumped"
509+
end
510+
511+
def test_rails_db_create_all_restores_db_connection_after_drop
512+
create_test_file :models, 'account'
513+
Dir.chdir(app_path) { `bin/rails db:create:all` } # create all to avoid warnings
514+
output = Dir.chdir(app_path) { `bin/rails db:drop:all db:create:all db:migrate && echo ".tables" | rails dbconsole` }
515+
assert_match "ar_internal_metadata", output, "tables should be dumped"
516+
end
517+
505518
def test_rake_passes_TESTOPTS_to_minitest
506519
create_test_file :models, 'account'
507520
output = Dir.chdir(app_path) { `bin/rake test TESTOPTS=-v` }

0 commit comments

Comments
 (0)