diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2013-12-23 20:15:52 +0100 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2013-12-23 20:15:52 +0100 |
commit | c390e60811b2e11bfd5d79b15bfb43690c1a1339 (patch) | |
tree | acac77d1a71e5e2091b6abdd3a1c7146a6832960 /activerecord/lib/active_record/connection_adapters | |
parent | d22a359a18d4ff0e31cb96293f03687e5db719ba (diff) | |
download | rails-c390e60811b2e11bfd5d79b15bfb43690c1a1339.tar.gz rails-c390e60811b2e11bfd5d79b15bfb43690c1a1339.tar.bz2 rails-c390e60811b2e11bfd5d79b15bfb43690c1a1339.zip |
Guarantee the connection resolver handles string values
This commit also cleans up the rake tasks that were checking
for DATABASE_URL in different places.
In fact, it would be nice to deprecate DATABASE_URL usage in the long
term, considering the direction we are moving of allowing those in .yml
files.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/connection_specification.rb | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb index 66d7f04fc3..a87eed5243 100644 --- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb @@ -16,34 +16,43 @@ module ActiveRecord ## # Builds a ConnectionSpecification from user input class Resolver # :nodoc: - attr_reader :config, :klass, :configurations + attr_reader :configurations - def initialize(config, configurations) - @config = config + def initialize(configurations) @configurations = configurations end - def spec - case config - when nil - raise AdapterNotSpecified unless defined?(Rails.env) - resolve_string_connection Rails.env - when Symbol, String - resolve_string_connection config.to_s - when Hash - resolve_hash_connection config + def resolve(config) + if config + resolve_connection config + elsif defined?(Rails.env) + resolve_env_connection Rails.env + else + raise AdapterNotSpecified end end private - def resolve_string_connection(spec) # :nodoc: - hash = configurations.fetch(spec) do |k| - connection_url_to_hash(k) - end - raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash + def resolve_connection(spec) #:nodoc: + case spec + when Symbol, String + resolve_env_connection spec.to_s + when Hash + resolve_hash_connection spec + end + end - resolve_hash_connection hash + def resolve_env_connection(spec) # :nodoc: + # Rails has historically accepted a string to mean either + # an environment key or a url spec. So we support both for + # now but it would be nice to limit the environment key only + # for symbols. + spec = configurations.fetch(spec.to_s) do + resolve_string_connection(spec) if spec.is_a?(String) + end + raise(AdapterNotSpecified, "#{spec} database is not configured") unless spec + resolve_connection spec end def resolve_hash_connection(spec) # :nodoc: @@ -65,8 +74,8 @@ module ActiveRecord ConnectionSpecification.new(spec, adapter_method) end - def connection_url_to_hash(url) # :nodoc: - config = URI.parse url + def resolve_string_connection(spec) # :nodoc: + config = URI.parse spec adapter = config.scheme adapter = "postgresql" if adapter == "postgres" |