Move establish_connection to handler

上级 bf219714
...@@ -837,7 +837,15 @@ def connection_pool_list ...@@ -837,7 +837,15 @@ def connection_pool_list
end end
alias :connection_pools :connection_pool_list alias :connection_pools :connection_pool_list
def establish_connection(spec) def establish_connection(spec_or_config, name: "primary")
if spec_or_config.is_a?(ConnectionSpecification)
spec = spec_or_config
else
resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
spec = resolver.spec(spec_or_config, name)
end
remove_connection(spec.name)
owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec) owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
end end
......
...@@ -180,6 +180,10 @@ def spec(config, name = nil) ...@@ -180,6 +180,10 @@ def spec(config, name = nil)
adapter_method = "#{spec[:adapter]}_connection" adapter_method = "#{spec[:adapter]}_connection"
unless ActiveRecord::Base.respond_to?(adapter_method)
raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
end
name ||= name ||=
if config.is_a?(Symbol) if config.is_a?(Symbol)
config.to_s config.to_s
......
...@@ -44,21 +44,13 @@ module ConnectionHandling ...@@ -44,21 +44,13 @@ module ConnectionHandling
# #
# The exceptions AdapterNotSpecified, AdapterNotFound and +ArgumentError+ # The exceptions AdapterNotSpecified, AdapterNotFound and +ArgumentError+
# may be returned on an error. # may be returned on an error.
def establish_connection(spec = nil) def establish_connection(config = nil)
raise "Anonymous class is not allowed." unless name raise "Anonymous class is not allowed." unless name
spec ||= DEFAULT_ENV.call.to_sym config ||= DEFAULT_ENV.call.to_sym
resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new configurations spec_name = self == Base ? "primary" : name
# TODO: uses name on establish_connection, for backwards compatibility self.connection_specification_name = spec_name
spec = resolver.spec(spec, self == Base ? "primary" : name) connection_handler.establish_connection(config, name: spec_name)
unless respond_to?(spec.adapter_method)
raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
end
remove_connection(spec.name)
self.connection_specification_name = spec.name
connection_handler.establish_connection spec
end end
class MergeAndResolveDefaultUrlConfig # :nodoc: class MergeAndResolveDefaultUrlConfig # :nodoc:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册