提交 5e260574 编写于 作者: J John Crepezzi

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).
上级 ec7aa03c
......@@ -105,18 +105,20 @@ def build_configs(configs)
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 @@ def build_db_config_from_hash(env_name, spec_name, config)
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
......
......@@ -347,6 +347,22 @@ def test_tiered_configs_with_database_url
assert_equal expected, actual
end
end
def test_does_not_change_other_environments
ENV["DATABASE_URL"] = "postgres://localhost/foo"
config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" }, "default_env" => {} }
actual = resolve_spec(:production, config)
assert_equal config["production"].merge("name" => "production"), actual
actual = resolve_spec(:default_env, config)
assert_equal({
"host" => "localhost",
"database" => "foo",
"adapter" => "postgresql",
"name" => "default_env"
}, actual)
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册