mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow separate database env variables per-connection
This commit adds a feature which allows separate database ENV variables to be defined for each spec in a 3-tier config. The names for the environment variables will be `#{name.upcase}_DATABASE_URL` This commit also introduces a change in behavior around handling of `DATABASE_URL`. Instead of using `DATABASE_URL` to change _all_ specs in a multi-database configuration, it will now only affect the `primary` connection.
This commit is contained in:
parent
41bc4c6207
commit
7df0eefacf
3 changed files with 45 additions and 7 deletions
|
@ -1,3 +1,7 @@
|
|||
* Make the DATABASE_URL env variable only affect the primary connection. Add new env variables for multiple databases.
|
||||
|
||||
*John Crepezzi*, *Eileen Uchitelle*
|
||||
|
||||
* Add a warning for enum elements with 'not_' prefix.
|
||||
|
||||
class Foo
|
||||
|
@ -56,5 +60,4 @@
|
|||
|
||||
*Michael Duchemin*
|
||||
|
||||
|
||||
Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activerecord/CHANGELOG.md) for previous changes.
|
||||
|
|
|
@ -168,12 +168,19 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def environment_url_config(env, spec_name, config)
|
||||
url = ENV["DATABASE_URL"]
|
||||
url = environment_value_for(spec_name)
|
||||
return unless url
|
||||
|
||||
ActiveRecord::DatabaseConfigurations::UrlConfig.new(env, spec_name, url, config)
|
||||
end
|
||||
|
||||
def environment_value_for(spec_name)
|
||||
spec_env_key = "#{spec_name.upcase}_DATABASE_URL"
|
||||
url = ENV[spec_env_key]
|
||||
url ||= ENV["DATABASE_URL"] if spec_name == "primary"
|
||||
url
|
||||
end
|
||||
|
||||
def method_missing(method, *args, &blk)
|
||||
case method
|
||||
when :each, :first
|
||||
|
|
|
@ -334,6 +334,8 @@ module ActiveRecord
|
|||
}
|
||||
}
|
||||
|
||||
configs = ActiveRecord::DatabaseConfigurations.new(config)
|
||||
actual = configs.configs_for(env_name: "default_env", spec_name: "primary").config
|
||||
expected = {
|
||||
"adapter" => "postgresql",
|
||||
"database" => "foo",
|
||||
|
@ -341,11 +343,37 @@ module ActiveRecord
|
|||
"pool" => 5
|
||||
}
|
||||
|
||||
["primary", "animals"].each do |spec_name|
|
||||
configs = ActiveRecord::DatabaseConfigurations.new(config)
|
||||
actual = configs.configs_for(env_name: "default_env", spec_name: spec_name).config
|
||||
assert_equal expected, actual
|
||||
end
|
||||
assert_equal expected, actual
|
||||
|
||||
configs = ActiveRecord::DatabaseConfigurations.new(config)
|
||||
actual = configs.configs_for(env_name: "default_env", spec_name: "animals").config
|
||||
expected = { "pool" => 5 }
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
def test_separate_database_env_vars
|
||||
ENV["DATABASE_URL"] = "postgres://localhost/foo"
|
||||
ENV["PRIMARY_DATABASE_URL"] = "postgres://localhost/primary"
|
||||
ENV["ANIMALS_DATABASE_URL"] = "postgres://localhost/animals"
|
||||
|
||||
config = {
|
||||
"default_env" => {
|
||||
"primary" => { "pool" => 5 },
|
||||
"animals" => { "pool" => 5 }
|
||||
}
|
||||
}
|
||||
|
||||
configs = ActiveRecord::DatabaseConfigurations.new(config)
|
||||
actual = configs.configs_for(env_name: "default_env", spec_name: "primary").config
|
||||
assert_equal "primary", actual["database"]
|
||||
|
||||
configs = ActiveRecord::DatabaseConfigurations.new(config)
|
||||
actual = configs.configs_for(env_name: "default_env", spec_name: "animals").config
|
||||
assert_equal "animals", actual["database"]
|
||||
ensure
|
||||
ENV.delete("PRIMARY_DATABASE_URL")
|
||||
ENV.delete("ANIMALS_DATABASE_URL")
|
||||
end
|
||||
|
||||
def test_does_not_change_other_environments
|
||||
|
|
Loading…
Reference in a new issue