From de9f2f63b8548e2a8950c1727f0a1a6893713505 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Tue, 8 Apr 2014 14:49:03 +0930 Subject: Rearrange the config merger some more This seems to simplify the operative part. Most importantly, by pre-loading all the configs supplied in ENV, we ensure the list is complete: if the developer specifies an unknown config, the exception includes a list of valid ones. --- .../lib/active_record/connection_handling.rb | 35 ++++++++++------------ 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index 1c8fbcb625..f83829bb26 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -60,6 +60,7 @@ module ActiveRecord class MergeAndResolveDefaultUrlConfig # :nodoc: def initialize(raw_configurations) @raw_config = raw_configurations.dup + @env = DEFAULT_ENV.call.to_s end # Returns fully resolved connection hashes. @@ -70,30 +71,26 @@ module ActiveRecord private def config - env = DEFAULT_ENV.call.to_s - - cfg = Hash.new do |hash, key| - entry = @raw_config[key] - env_url = nil - - if key.to_s == env - env_url = ENV["DATABASE_URL"] + @raw_config.dup.tap do |cfg| + urls_in_environment.each do |key, url| + cfg[key] ||= {} + cfg[key]["url"] ||= url end + end + end - env_url ||= ENV["DATABASE_URL_#{key.upcase}"] - - if env_url - entry ||= {} - entry.merge!("url" => env_url) { |h, v1, v2| v1 || v2 } + def urls_in_environment + {}.tap do |mapping| + ENV.each do |k, v| + if k =~ /\ADATABASE_URL_(.*)/ + mapping[$1.downcase] = v + end end - hash[key] = entry if entry + # Check for this last, because it is prioritised over the + # longer "DATABASE_URL_#{@env}" spelling + mapping[@env] = ENV['DATABASE_URL'] if ENV['DATABASE_URL'] end - - @raw_config.keys.each {|k| cfg[k] } - cfg[env] - - cfg end end -- cgit v1.2.3