aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorEileen Uchitelle <eileencodes@gmail.com>2018-12-19 15:11:22 -0500
committerEileen Uchitelle <eileencodes@gmail.com>2018-12-21 11:10:10 -0500
commit22a1265828d9f743b8348fe53424fffb60d6d824 (patch)
treed47480ea6f007bd20e2441d16b25693974cc79f6 /activerecord
parentabae9d0e0a0f1628cbf608765db09c1db3303732 (diff)
downloadrails-22a1265828d9f743b8348fe53424fffb60d6d824.tar.gz
rails-22a1265828d9f743b8348fe53424fffb60d6d824.tar.bz2
rails-22a1265828d9f743b8348fe53424fffb60d6d824.zip
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.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_handling.rb4
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb10
-rw-r--r--activerecord/test/cases/query_cache_test.rb5
3 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb
index 558cdeccf2..4a941055d1 100644
--- a/activerecord/lib/active_record/connection_handling.rb
+++ b/activerecord/lib/active_record/connection_handling.rb
@@ -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
diff --git a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
index 79c2c4d827..0b3fb82e12 100644
--- a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
@@ -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
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index 02ead8d914..0256a6da61 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -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