Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error raised when handler doesn't exist #35042

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1006,7 +1006,16 @@ def flush_idle_connections!
# for (not necessarily the current class).
def retrieve_connection(spec_name) #:nodoc:
pool = retrieve_connection_pool(spec_name)
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found." unless pool

unless pool
# multiple database application
if ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
else
raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found."
end
end

pool.connection
end

Expand Down
4 changes: 0 additions & 4 deletions activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -158,10 +158,6 @@ def lookup_connection_handler(handler_key) # :nodoc:
end

def with_handler(handler_key, &blk) # :nodoc:
unless ActiveRecord::Base.connection_handlers.keys.include?(handler_key)
raise ArgumentError, "The #{handler_key} role does not exist. Add it by establishing a connection with `connects_to` or use an existing role (#{ActiveRecord::Base.connection_handlers.keys.join(", ")})."
end

handler = lookup_connection_handler(handler_key)
swap_connection_handler(handler, &blk)
end
Expand Down
Expand Up @@ -336,13 +336,13 @@ def test_connection_handlers_swapping_connections_in_fiber
end

def test_calling_connected_to_on_a_non_existent_handler_raises
error = assert_raises ArgumentError do
error = assert_raises ActiveRecord::ConnectionNotEstablished do
ActiveRecord::Base.connected_to(role: :reading) do
yield
Person.first
end
end

assert_equal "The reading role does not exist. Add it by establishing a connection with `connects_to` or use an existing role (writing).", error.message
assert_equal "No connection pool with 'primary' found for the 'reading' role.", error.message
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/unconnected_test.rb
Expand Up @@ -29,6 +29,14 @@ def test_connection_no_longer_established
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
Expand Down