1
0
Fork 0
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:
alpaca-tc 2021-01-06 23:23:30 +09:00 committed by eileencodes
parent 1993496d7f
commit 1cd08410a0
No known key found for this signature in database
GPG key ID: BA5C575120BBE8DF
2 changed files with 16 additions and 0 deletions

View file

@ -143,6 +143,10 @@ module ActiveRecord
if self != Base && !abstract_class if self != Base && !abstract_class
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes." raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
end 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 end
unless role || shard unless role || shard

View file

@ -41,6 +41,10 @@ class SecondAbstractClass < FirstAbstractClass
connects_to database: { writing: :arunit, reading: :arunit } connects_to database: { writing: :arunit, reading: :arunit }
end end
class ThirdAbstractClass < SecondAbstractClass
self.abstract_class = true
end
class Photo < SecondAbstractClass; end class Photo < SecondAbstractClass; end
class Smarts < ActiveRecord::Base; end class Smarts < ActiveRecord::Base; end
class CreditCard < ActiveRecord::Base class CreditCard < ActiveRecord::Base
@ -1658,6 +1662,14 @@ class BasicsTest < ActiveRecord::TestCase
end end
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 test "#connecting_to with role" do
SecondAbstractClass.connecting_to(role: :reading) SecondAbstractClass.connecting_to(role: :reading)