mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
connected_to
shouldn't be called on the abstract class that not established connection
Fixed: https://github.com/rails/rails/issues/40559#issuecomment-752056106 When abstract class hasn't own connections, calling `AbstractClass.connection` returns parent class's connection. We call `AbstractClass.connection.preventing_writes?` expecting abstract class's state to be returned, but actually it is parent's one. I think that it isn't expected behavior so I prevents call `connected_to` on the abstract class that not established the connection.
This commit is contained in:
parent
1993496d7f
commit
1cd08410a0
2 changed files with 16 additions and 0 deletions
|
@ -143,6 +143,10 @@ module ActiveRecord
|
|||
if self != Base && !abstract_class
|
||||
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
|
||||
end
|
||||
|
||||
if name != connection_specification_name && !primary_class?
|
||||
raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
|
||||
end
|
||||
end
|
||||
|
||||
unless role || shard
|
||||
|
|
|
@ -41,6 +41,10 @@ class SecondAbstractClass < FirstAbstractClass
|
|||
connects_to database: { writing: :arunit, reading: :arunit }
|
||||
end
|
||||
|
||||
class ThirdAbstractClass < SecondAbstractClass
|
||||
self.abstract_class = true
|
||||
end
|
||||
|
||||
class Photo < SecondAbstractClass; end
|
||||
class Smarts < ActiveRecord::Base; end
|
||||
class CreditCard < ActiveRecord::Base
|
||||
|
@ -1658,6 +1662,14 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test "cannot call connected_to on the abstract class that did not establish the connection" do
|
||||
error = assert_raises(NotImplementedError) do
|
||||
ThirdAbstractClass.connected_to(role: :reading) { }
|
||||
end
|
||||
|
||||
assert_equal "calling `connected_to` is only allowed on the abstract class that established the connection.", error.message
|
||||
end
|
||||
|
||||
test "#connecting_to with role" do
|
||||
SecondAbstractClass.connecting_to(role: :reading)
|
||||
|
||||
|
|
Loading…
Reference in a new issue