diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a3a7eb38d4..23856eba77 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Disallow calling `connected_to` on subclasses of `ActiveRecord::Base`. + + Behavior has not changed here but the previous API could be misleading to people who thought it would switch connections for only that class. `connected_to` switches the context from which we are getting connections, not the connections themselves. + + *Eileen M. Uchitelle*, *John Crepezzi* + * Add support for horizontal sharding to `connects_to` and `connected_to`. Applications can now connect to multiple shards and switch between their shards in an application. Note that the shard swapping is still a manual process as this change does not include an API for automatic shard swapping. diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index de86d9a759..77183aeed6 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -136,6 +136,8 @@ module ActiveRecord # # The database kwarg is deprecated and will be removed in 6.2.0 without replacement. def connected_to(database: nil, role: nil, shard: nil, prevent_writes: false, &blk) + raise NotImplementedError, "connected_to can only be called on ActiveRecord::Base" unless self == Base + if database ActiveSupport::Deprecation.warn("The database key in `connected_to` is deprecated. It will be removed in Rails 6.2.0 without replacement.") end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9010f348d1..b225be354c 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1591,6 +1591,14 @@ class BasicsTest < ActiveRecord::TestCase end end + test "cannot call connected_to on subclasses of ActiveRecord::Base" do + error = assert_raises(NotImplementedError) do + Bird.connected_to(role: :reading) { } + end + + assert_equal "connected_to can only be called on ActiveRecord::Base", error.message + end + test "preventing writes applies to all connections on a handler" do conn1_error = assert_raises ActiveRecord::ReadOnlyError do ActiveRecord::Base.connection_handler.while_preventing_writes do