Skip to content

Commit

Permalink
Merge pull request rails#37223 from seejohnrun/fix-current-config-bug
Browse files Browse the repository at this point in the history
Fix a typo in DatabaseTasks#current_config
  • Loading branch information
eileencodes committed Sep 17, 2019
2 parents 9680984 + 9dfdc75 commit 84496b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions activerecord/test/cases/tasks/database_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 84496b8

Please sign in to comment.