From 2b817cde622e625f08a638d909f3e9b12f0b3066 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Tue, 8 Apr 2014 03:54:23 +0930 Subject: Only apply DATABASE_URL for Rails.env As we like ENV vars, also support DATABASE_URL_#{env}, for more obscure use cases. --- .../lib/active_record/connection_handling.rb | 42 ++++++++-------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index bbb866cedf..5a83797ee5 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -58,9 +58,8 @@ module ActiveRecord end class MergeAndResolveDefaultUrlConfig # :nodoc: - def initialize(raw_configurations, url = ENV['DATABASE_URL']) + def initialize(raw_configurations) @raw_config = raw_configurations.dup - @url = url end # Returns fully resolved connection hashes. @@ -71,35 +70,22 @@ module ActiveRecord private def config - if @url - raw_merged_into_default - else - @raw_config + cfg = Hash.new do |hash, key| + entry = @raw_config[key] + env_url = nil + if key.to_s == DEFAULT_ENV.call.to_s + env_url = ENV["DATABASE_URL"] + end + env_url ||= ENV["DATABASE_URL_#{key.upcase}"] + entry ||= {} if env_url + entry.merge!("url" => env_url) { |h, v1, v2| v1 || v2 } if entry.is_a?(Hash) + hash[key] = entry if entry end - end - def raw_merged_into_default - default = default_url_hash + @raw_config.keys.each {|k| cfg[k] } + cfg[DEFAULT_ENV.call.to_s] - @raw_config.each do |env, values| - default[env] = values || {} - default[env].merge!("url" => @url) { |h, v1, v2| v1 || v2 } if default[env].is_a?(Hash) - end - default - end - - # When the raw configuration is not present and ENV['DATABASE_URL'] - # is available we return a hash with the connection information in - # the connection URL. This hash responds to any string key with - # resolved connection information. - def default_url_hash - Hash.new do |hash, key| - hash[key] = if key.is_a? String - ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(@url).to_hash - else - nil - end - end + cfg end end -- cgit v1.2.3