diff options
author | John Crepezzi <seejohnrun@github.com> | 2019-07-26 00:49:38 -0400 |
---|---|---|
committer | John Crepezzi <seejohnrun@github.com> | 2019-07-26 17:32:14 -0400 |
commit | 7df0eefacf1b6cbfdad8775ac86c6917f16bf859 (patch) | |
tree | 7e91b859acdb7230e5f82cfdb8310c953591e09b /activerecord/test/cases/connection_adapters | |
parent | 41bc4c6207147a5eeafa323d763e66c06e61cacf (diff) | |
download | rails-7df0eefacf1b6cbfdad8775ac86c6917f16bf859.tar.gz rails-7df0eefacf1b6cbfdad8775ac86c6917f16bf859.tar.bz2 rails-7df0eefacf1b6cbfdad8775ac86c6917f16bf859.zip |
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.
Diffstat (limited to 'activerecord/test/cases/connection_adapters')
-rw-r--r-- | activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb index 95e57f42e3..ee2972101f 100644 --- a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +++ b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb @@ -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 |