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: eileencodes <eileencodes@gmail.com>
This commit is contained in:
John Crepezzi 2019-09-17 10:19:09 -04:00
parent a1b6c1669f
commit 9dfdc752eb
2 changed files with 55 additions and 3 deletions

@ -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