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

Prioritize db_config for current env when resolving config.

This commit is contained in:
Guo Xiang Tan 2020-07-06 17:24:05 +08:00
parent 19df65dc77
commit 04443b2408
No known key found for this signature in database
GPG key ID: FBD110179AAC1F20
2 changed files with 24 additions and 4 deletions

View file

@ -79,10 +79,12 @@ module ActiveRecord
# If the application has multiple databases +find_db_config+ will return
# the first DatabaseConfig for the environment.
def find_db_config(env)
configurations.find do |db_config|
db_config.env_name == env.to_s ||
(db_config.for_current_env? && db_config.name == env.to_s)
end
configurations
.sort_by { |db_config| db_config.for_current_env? ? 0 : 1 }
.find do |db_config|
db_config.env_name == env.to_s ||
(db_config.for_current_env? && db_config.name == env.to_s)
end
end
# Returns the DatabaseConfigurations object as a Hash.

View file

@ -61,6 +61,24 @@ class DatabaseConfigurationsTest < ActiveRecord::TestCase
assert_equal "primary", config.name
end
def test_find_db_config_prioritize_db_config_object_for_the_current_env
config = ActiveRecord::DatabaseConfigurations.new({
"primary" => {
"adapter" => "randomadapter"
},
ActiveRecord::ConnectionHandling::DEFAULT_ENV.call => {
"primary" => {
"adapter" => "sqlite3",
"database" => ":memory:"
}
}
}).find_db_config("primary")
assert_equal "primary", config.name
assert_equal ActiveRecord::ConnectionHandling::DEFAULT_ENV.call, config.env_name
assert_equal ":memory:", config.database
end
def test_to_h_turns_db_config_object_back_into_a_hash_and_is_deprecated
configs = ActiveRecord::Base.configurations
assert_equal "ActiveRecord::DatabaseConfigurations", configs.class.name