Permalink
Browse files

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.
  • Loading branch information...
1 parent 0ec0475 commit de9f2f63b8548e2a8950c1727f0a1a6893713505 @matthewd matthewd committed Apr 8, 2014
Showing with 16 additions and 19 deletions.
  1. +16 −19 activerecord/lib/active_record/connection_handling.rb
@@ -60,6 +60,7 @@ def establish_connection(spec = nil)
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 @@ def resolve
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

0 comments on commit de9f2f6

Please sign in to comment.