aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/connection_specification.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2013-12-24 10:02:07 +0100
committerJosé Valim <jose.valim@plataformatec.com.br>2013-12-24 10:02:07 +0100
commitd2ed433b0af948da78e971bf342c506b27f6072f (patch)
tree56c97a391d79ff47ff0b03782f84cd7b9ebf5e1a /activerecord/lib/active_record/connection_adapters/connection_specification.rb
parentd8336cab32d0d8e8c2877cac26111cbecb5ac872 (diff)
downloadrails-d2ed433b0af948da78e971bf342c506b27f6072f.tar.gz
rails-d2ed433b0af948da78e971bf342c506b27f6072f.tar.bz2
rails-d2ed433b0af948da78e971bf342c506b27f6072f.zip
Only build a ConnectionSpecification if required
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/connection_specification.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/connection_specification.rb54
1 files changed, 28 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
index 5f50ca6aae..a3c645c53c 100644
--- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb
+++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
@@ -32,6 +32,24 @@ module ActiveRecord
end
end
+ def spec(config)
+ spec = resolve(config).symbolize_keys
+
+ raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
+
+ path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
+ begin
+ require path_to_adapter
+ rescue Gem::LoadError => e
+ raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
+ rescue LoadError => e
+ raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
+ end
+
+ adapter_method = "#{spec[:adapter]}_connection"
+ ConnectionSpecification.new(spec, adapter_method)
+ end
+
private
def resolve_connection(spec) #:nodoc:
@@ -52,30 +70,15 @@ module ActiveRecord
resolve_string_connection(spec) if spec.is_a?(String)
end
raise(AdapterNotSpecified, "#{spec} database is not configured") unless config
- resolve_connection config
+ resolve_connection(config)
end
def resolve_hash_connection(spec) # :nodoc:
- spec = spec.symbolize_keys
-
- raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
-
- path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
- begin
- require path_to_adapter
- rescue Gem::LoadError => e
- raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
- rescue LoadError => e
- raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
- end
-
- adapter_method = "#{spec[:adapter]}_connection"
-
- ConnectionSpecification.new(spec, adapter_method)
+ spec
end
def resolve_string_connection(spec) # :nodoc:
- config = URI.parse spec
+ config = URI.parse spec
adapter = config.scheme
adapter = "postgresql" if adapter == "postgres"
@@ -89,12 +92,12 @@ module ActiveRecord
config.path.sub(%r{^/},"")
end
- spec = { :adapter => adapter,
- :username => config.user,
- :password => config.password,
- :port => config.port,
- :database => database,
- :host => config.host }
+ spec = { "adapter" => adapter,
+ "username" => config.user,
+ "password" => config.password,
+ "port" => config.port,
+ "database" => database,
+ "host" => config.host }
spec.reject!{ |_,value| value.blank? }
@@ -103,8 +106,7 @@ module ActiveRecord
spec.map { |key,value| spec[key] = uri_parser.unescape(value) if value.is_a?(String) }
if config.query
- options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
-
+ options = Hash[config.query.split("&").map{ |pair| pair.split("=") }]
spec.merge!(options)
end