mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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. Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
This commit is contained in:
parent
22d9b3e83e
commit
b7b26fabee
3 changed files with 16 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue