mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #34753 from eileencodes/raise-less-confusing-error-if-handler-doesnt-exist
Raise helpful error when role doesn't exist
This commit is contained in:
commit
b00e46bd6d
3 changed files with 19 additions and 0 deletions
|
@ -158,6 +158,10 @@ module ActiveRecord
|
|||
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
|
||||
|
|
|
@ -328,6 +328,16 @@ module ActiveRecord
|
|||
ensure
|
||||
ActiveRecord::Base.connection_handlers = original_handlers
|
||||
end
|
||||
|
||||
def test_calling_connected_to_on_a_non_existent_handler_raises
|
||||
error = assert_raises ArgumentError do
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
yield
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -56,6 +56,11 @@ class QueryCacheTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_query_cache_is_applied_to_connections_in_all_handlers
|
||||
ActiveRecord::Base.connection_handlers = {
|
||||
writing: ActiveRecord::Base.default_connection_handler,
|
||||
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
|
||||
}
|
||||
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["arunit"])
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue