Fix setting connection_specification_name on ActiveRecord::Base
Since 2f8b397258b66581409b0e6537f98ea9b56e9f19, `ActiveRecord::Base` and `ApplicationRecord` use the same default `connection_specification_name` so that database connections configured on `ApplicationRecord` also apply to `ActiveRecord::Base`. However, when resolving the connection for `ApplicationRecord` we should continue to fall back to `ActiveRecord::Base` when there's no connection explicitly configured, so that setting `connection_specification_name` on `ActiveRecord::Base` affects `ApplicationRecord` and its descendants in this case as it did in previous versions.
This commit is contained in:
parent
8ab4fd12f1
commit
92d31b1f81
@ -204,7 +204,7 @@ def connection
|
||||
# Return the specification name from the current class or its parent.
|
||||
def connection_specification_name
|
||||
if !defined?(@connection_specification_name) || @connection_specification_name.nil?
|
||||
return primary_class? ? "primary" : superclass.connection_specification_name
|
||||
return self == Base ? "primary" : superclass.connection_specification_name
|
||||
end
|
||||
@connection_specification_name
|
||||
end
|
||||
|
@ -375,6 +375,8 @@ class MyClass < ApplicationRecord
|
||||
end
|
||||
|
||||
def test_connection_specification_name_should_fallback_to_parent
|
||||
Object.send :const_set, :ApplicationRecord, ApplicationRecord
|
||||
|
||||
klassA = Class.new(Base)
|
||||
klassB = Class.new(klassA)
|
||||
klassC = Class.new(MyClass)
|
||||
@ -387,6 +389,12 @@ def test_connection_specification_name_should_fallback_to_parent
|
||||
|
||||
klassA.connection_specification_name = "readonly"
|
||||
assert_equal "readonly", klassB.connection_specification_name
|
||||
|
||||
ActiveRecord::Base.connection_specification_name = "readonly"
|
||||
assert_equal "readonly", klassC.connection_specification_name
|
||||
ensure
|
||||
Object.send :remove_const, :ApplicationRecord
|
||||
ActiveRecord::Base.connection_specification_name = "primary"
|
||||
end
|
||||
|
||||
def test_remove_connection_should_not_remove_parent
|
||||
|
Loading…
Reference in New Issue
Block a user