diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2019-07-25 20:30:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-25 20:30:48 -0400 |
commit | 0989956007b3b13de2e0b0496ab472d0b43516fb (patch) | |
tree | 449ba5e0e7bf4644cf91bc1b19289cccf742f7cf /activerecord/lib | |
parent | d45507b931d055c016ccf80185469730f034860b (diff) | |
parent | 5e260574a43d5c2fef8b170138baa9f7e10bfb24 (diff) | |
download | rails-0989956007b3b13de2e0b0496ab472d0b43516fb.tar.gz rails-0989956007b3b13de2e0b0496ab472d0b43516fb.tar.bz2 rails-0989956007b3b13de2e0b0496ab472d0b43516fb.zip |
Merge pull request #36770 from seejohnrun/database-env-current-env-only
Only merge DATABASE_URL settings into the current env
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/database_configurations.rb | 38 |
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 |