diff options
author | Arthur Neves <arthurnn@gmail.com> | 2016-05-07 12:25:37 -0500 |
---|---|---|
committer | Arthur Neves <arthurnn@gmail.com> | 2016-05-24 22:03:00 -0400 |
commit | 5167b8f7ffa66c8772c455916b8418844e3b4674 (patch) | |
tree | 75443d0cf8311d52e4eb52d39ced3038b23d1f1d /activerecord/lib/active_record | |
parent | bf219714dce494f5b69cb7dd9b8c43a68a4988da (diff) | |
download | rails-5167b8f7ffa66c8772c455916b8418844e3b4674.tar.gz rails-5167b8f7ffa66c8772c455916b8418844e3b4674.tar.bz2 rails-5167b8f7ffa66c8772c455916b8418844e3b4674.zip |
Move establish_connection to handler
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 18 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index f437dafec2..c124f1c4b5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -837,7 +837,15 @@ module ActiveRecord end 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) end diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb index 901c98b22b..7636de067c 100644 --- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb @@ -180,6 +180,10 @@ module ActiveRecord adapter_method = "#{spec[:adapter]}_connection" + unless ActiveRecord::Base.respond_to?(adapter_method) + raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter" + end + name ||= if config.is_a?(Symbol) config.to_s diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index f932deb18d..b3fd837abe 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -44,21 +44,13 @@ module ActiveRecord # # The exceptions AdapterNotSpecified, AdapterNotFound and +ArgumentError+ # may be returned on an error. - def establish_connection(spec = nil) + def establish_connection(config = nil) raise "Anonymous class is not allowed." unless name - spec ||= DEFAULT_ENV.call.to_sym - resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new configurations - # TODO: uses name on establish_connection, for backwards compatibility - spec = resolver.spec(spec, self == Base ? "primary" : 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 + config ||= DEFAULT_ENV.call.to_sym + spec_name = self == Base ? "primary" : name + self.connection_specification_name = spec_name + connection_handler.establish_connection(config, name: spec_name) end class MergeAndResolveDefaultUrlConfig # :nodoc: |