aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJohn Crepezzi <seejohnrun@github.com>2019-07-25 20:08:10 -0400
committerJohn Crepezzi <seejohnrun@github.com>2019-07-25 20:14:21 -0400
commit5e260574a43d5c2fef8b170138baa9f7e10bfb24 (patch)
tree6b3cf3dceafbb1bad11a651ef83e43c3ab06b199 /activerecord/lib
parentec7aa03c984a0daeead4f71c7189d241139a6770 (diff)
downloadrails-5e260574a43d5c2fef8b170138baa9f7e10bfb24.tar.gz
rails-5e260574a43d5c2fef8b170138baa9f7e10bfb24.tar.bz2
rails-5e260574a43d5c2fef8b170138baa9f7e10bfb24.zip
Only merge DATABASE_URL settings into the current env
This commit fixes a regression where when the `DATABASE_URL` environment variable was set and the current Rails environment had a valid configuration defined in the database config, settings from the environment variable would affect _all_ environments (not just the current one).
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/database_configurations.rb38
1 files changed, 20 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/database_configurations.rb b/activerecord/lib/active_record/database_configurations.rb
index e122628b05..8baa0f5af6 100644
--- a/activerecord/lib/active_record/database_configurations.rb
+++ b/activerecord/lib/active_record/database_configurations.rb
@@ -105,18 +105,20 @@ module ActiveRecord
return configs if configs.is_a?(Array)
db_configs = configs.flat_map do |env_name, config|
- if config.is_a?(Hash) && config.all? { |k, v| v.is_a?(Hash) }
+ if config.is_a?(Hash) && config.all? { |_, v| v.is_a?(Hash) }
walk_configs(env_name.to_s, config)
else
build_db_config_from_raw_config(env_name.to_s, "primary", config)
end
- end.compact
+ end
- if url = ENV["DATABASE_URL"]
- merge_url_with_configs(url, db_configs)
- else
- db_configs
+ current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s
+
+ unless db_configs.find(&:for_current_env?)
+ db_configs << environment_url_config(current_env, "primary", {})
end
+
+ merge_db_environment_variables(current_env, db_configs.compact)
end
def walk_configs(env_name, config)
@@ -156,22 +158,22 @@ module ActiveRecord
end
end
- def merge_url_with_configs(url, configs)
- env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s
+ def merge_db_environment_variables(current_env, configs)
+ configs.map do |config|
+ next config if config.url_config? || config.env_name != current_env
- if configs.find(&:for_current_env?)
- configs.map do |config|
- if config.url_config?
- config
- else
- ActiveRecord::DatabaseConfigurations::UrlConfig.new(config.env_name, config.spec_name, url, config.config)
- end
- end
- else
- configs + [ActiveRecord::DatabaseConfigurations::UrlConfig.new(env, "primary", url)]
+ url_config = environment_url_config(current_env, config.spec_name, config.config)
+ url_config || config
end
end
+ def environment_url_config(env, spec_name, config)
+ url = ENV["DATABASE_URL"]
+ return unless url
+
+ ActiveRecord::DatabaseConfigurations::UrlConfig.new(env, spec_name, url, config)
+ end
+
def method_missing(method, *args, &blk)
case method
when :each, :first