1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #32271 from eileencodes/fix-three-tier-default-connection

Fix default connection handling with three-tier config
This commit is contained in:
Eileen M. Uchitelle 2018-03-16 15:18:26 -04:00 committed by GitHub
commit 9700dacffe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 2 deletions

View file

@ -156,7 +156,6 @@ module ActiveRecord
env_config = config[env] if config[env].is_a?(Hash) && !(config[env].key?("adapter") || config[env].key?("url"))
end
config.reject! { |k, v| v.is_a?(Hash) && !(v.key?("adapter") || v.key?("url")) }
config.merge! env_config if env_config
config.each do |key, value|

View file

@ -57,6 +57,10 @@ module ActiveRecord
spec = resolver.resolve(config).symbolize_keys
spec[:name] = spec_name
# use the primary config if a config is not passed in and
# it's a three tier config
spec = spec[spec_name.to_sym] if spec[spec_name.to_sym]
connection_handler.establish_connection(spec)
end

View file

@ -71,6 +71,54 @@ module ActiveRecord
ENV["RAILS_ENV"] = previous_env
end
unless in_memory_db?
def test_establish_connection_using_3_level_config_defaults_to_default_env_primary_db
previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"
config = {
"default_env" => {
"primary" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" },
"readonly" => { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
},
"another_env" => {
"primary" => { "adapter" => "sqlite3", "database" => "db/another-primary.sqlite3" },
"readonly" => { "adapter" => "sqlite3", "database" => "db/another-readonly.sqlite3" }
}
}
@prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config
ActiveRecord::Base.establish_connection
assert_equal "db/primary.sqlite3", ActiveRecord::Base.connection.pool.spec.config[:database]
ensure
ActiveRecord::Base.configurations = @prev_configs
ENV["RAILS_ENV"] = previous_env
ActiveRecord::Base.establish_connection(:arunit)
end
def test_establish_connection_using_2_level_config_defaults_to_default_env_primary_db
previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env"
config = {
"default_env" => {
"adapter" => "sqlite3", "database" => "db/primary.sqlite3"
},
"another_env" => {
"adapter" => "sqlite3", "database" => "db/bad-primary.sqlite3"
}
}
@prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config
ActiveRecord::Base.establish_connection
assert_equal "db/primary.sqlite3", ActiveRecord::Base.connection.pool.spec.config[:database]
ensure
ActiveRecord::Base.configurations = @prev_configs
ENV["RAILS_ENV"] = previous_env
ActiveRecord::Base.establish_connection(:arunit)
end
end
def test_establish_connection_using_two_level_configurations
config = { "development" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" } }
@prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config

View file

@ -97,7 +97,7 @@ module Rails
elsif configurations[environment].blank? && configurations[connection].blank?
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
else
configurations[environment].presence || configurations[connection]
configurations[connection] || configurations[environment].presence
end
end
end