提交 9dfdc752 编写于 作者: J John Crepezzi

Fix a typo in DatabaseTasks#current_config

This commit fixes a method typo that was introduced in #37199, also also
adds tests for the `DatabaseTasks#current_config` method given it's
current implementation.

While I was in here, I also made it so that if `current_config` is
called with an `env` it won't try to look up the `Rails.env`
potentially resulting in an error for non-Rails application.

IMO this method should be deprecated & removed as it's no longer used in
Rails, is confusing (for example: `test_current_config_read_after_set`),
and is currently dependent on Rails if `env` isn't passed.
Co-authored-by: Neileencodes <eileencodes@gmail.com>
上级 a1b6c166
......@@ -111,12 +111,13 @@ def seed_loader
end
def current_config(options = {})
options.reverse_merge! env: env
options[:spec] ||= "primary"
if options.has_key?(:config)
@current_config = options[:config]
else
@current_config ||= ActiveRecord::Base.configurations.configs_for(env_name: options[:env], spec_name: options[:spec]).db_config.configuration_hash
env_name = options[:env] || env
spec_name = options[:spec] || "primary"
@current_config ||= ActiveRecord::Base.configurations.configs_for(env_name: env_name, spec_name: spec_name)&.configuration_hash
end
end
......
......@@ -112,6 +112,57 @@ def test_raises_an_error_if_no_migrations_have_been_made
end
end
class DatabaseTasksCurrentConfigTask < ActiveRecord::TestCase
def test_current_config_set
hash = {}
with_stubbed_configurations do
ActiveRecord::Tasks::DatabaseTasks.current_config(config: hash, env: "production")
assert_equal hash, ActiveRecord::Tasks::DatabaseTasks.current_config(env: "production")
end
end
def test_current_config_read_none_found
with_stubbed_configurations do
config = ActiveRecord::Tasks::DatabaseTasks.current_config(env: "production", spec: "empty")
assert_nil config
end
end
def test_current_config_read_found
with_stubbed_configurations do
config = ActiveRecord::Tasks::DatabaseTasks.current_config(env: "production", spec: "exists")
assert_equal({ database: "my-db" }, config)
end
end
def test_current_config_read_after_set
hash = {}
with_stubbed_configurations do
ActiveRecord::Tasks::DatabaseTasks.current_config(config: hash, env: "production")
config = ActiveRecord::Tasks::DatabaseTasks.current_config(env: "production", spec: "exists")
assert_equal hash, config
end
end
private
def with_stubbed_configurations
old_configurations = ActiveRecord::Base.configurations
ActiveRecord::Base.configurations = { "production" => { "exists" => { "database" => "my-db" } } }
yield
ensure
ActiveRecord::Base.configurations = old_configurations
ActiveRecord::Tasks::DatabaseTasks.current_config = nil
end
end
class DatabaseTasksRegisterTask < ActiveRecord::TestCase
def test_register_task
klazz = Class.new do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册