aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_handling.rb35
1 files changed, 16 insertions, 19 deletions
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