Skip to content

Commit 22a1265

Browse files
committed
Raise helpful error when role doesn't exist
If you try to call `connected_to` with a role that doesn't have an established connection you used to get an error that said: ``` >> ActiveRecord::Base.connected_to(role: :i_dont_exist) { Home.first } ActiveRecord::ConnectionNotEstablished Exception: No connection pool with 'primary' found. ``` This is confusing because the connection could be established but we spelled the role wrong. I've changed this to raise if the `role` used in `connected_to` doesn't have an associated handler. Users who encounter this should either check that the role is spelled correctly (writin -> writing), establish a connection to that role in the model with connects_to, or use the `database` keyword for the `role`. I think this will provide a less confusing error message for those starting out with multiple databases.
1 parent abae9d0 commit 22a1265

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

activerecord/lib/active_record/connection_handling.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def lookup_connection_handler(handler_key) # :nodoc:
158158
end
159159

160160
def with_handler(handler_key, &blk) # :nodoc:
161+
unless ActiveRecord::Base.connection_handlers.keys.include?(handler_key)
162+
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(", ")})."
163+
end
164+
161165
handler = lookup_connection_handler(handler_key)
162166
swap_connection_handler(handler, &blk)
163167
end

activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,16 @@ def test_connection_handlers_swapping_connections_in_fiber
328328
ensure
329329
ActiveRecord::Base.connection_handlers = original_handlers
330330
end
331+
332+
def test_calling_connected_to_on_a_non_existent_handler_raises
333+
error = assert_raises ArgumentError do
334+
ActiveRecord::Base.connected_to(role: :reading) do
335+
yield
336+
end
337+
end
338+
339+
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
340+
end
331341
end
332342
end
333343
end

activerecord/test/cases/query_cache_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def test_exceptional_middleware_clears_and_disables_cache_on_error
5656
end
5757

5858
def test_query_cache_is_applied_to_connections_in_all_handlers
59+
ActiveRecord::Base.connection_handlers = {
60+
writing: ActiveRecord::Base.default_connection_handler,
61+
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
62+
}
63+
5964
ActiveRecord::Base.connected_to(role: :reading) do
6065
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["arunit"])
6166
end

0 commit comments

Comments
 (0)