mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
1284f826cc
While working on another feature for multiple databases (auto-switching) I observed that in development the first request won't autoload the application record connection for the primary database and may not yet know about the replica connection. In my test application this caused the application to thrown an error if I tried to send the first request to the replica before the replica was connected. This wouldn't be an issue in production because the application is preloaded. In order to fix this I decided to leave the original error message and delete the new error message. I updated the original error message to include the `role` to make it a bit clearer that the connection isn't established for that particular role. The error now reads: ``` No connection pool with 'primary' found for the 'reading' role. ``` A single database application will continue uisng the original error message: ``` No connection pool with 'primary' found. ```
43 lines
1 KiB
Ruby
43 lines
1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "cases/helper"
|
|
|
|
class TestRecord < ActiveRecord::Base
|
|
end
|
|
|
|
class TestUnconnectedAdapter < ActiveRecord::TestCase
|
|
self.use_transactional_tests = false
|
|
|
|
def setup
|
|
@underlying = ActiveRecord::Base.connection
|
|
@specification = ActiveRecord::Base.remove_connection
|
|
end
|
|
|
|
teardown do
|
|
@underlying = nil
|
|
ActiveRecord::Base.establish_connection(@specification)
|
|
load_schema if in_memory_db?
|
|
end
|
|
|
|
def test_connection_no_longer_established
|
|
assert_raise(ActiveRecord::ConnectionNotEstablished) do
|
|
TestRecord.find(1)
|
|
end
|
|
|
|
assert_raise(ActiveRecord::ConnectionNotEstablished) do
|
|
TestRecord.new.save
|
|
end
|
|
end
|
|
|
|
def test_error_message_when_connection_not_established
|
|
error = assert_raise(ActiveRecord::ConnectionNotEstablished) do
|
|
TestRecord.find(1)
|
|
end
|
|
|
|
assert_equal "No connection pool with 'primary' found.", error.message
|
|
end
|
|
|
|
def test_underlying_adapter_no_longer_active
|
|
assert_not @underlying.active?, "Removed adapter should no longer be active"
|
|
end
|
|
end
|