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

Merge pull request #41760 from Shopify/register-task-precedence

Give precedence to the DatabaseTasks registered last
This commit is contained in:
Rafael França 2021-03-25 17:28:46 -04:00 committed by GitHub
commit 30ef29289b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View file

@ -537,7 +537,7 @@ module ActiveRecord
end
def class_for_adapter(adapter)
_key, task = @tasks.each_pair.detect { |pattern, _task| adapter[pattern] }
_key, task = @tasks.reverse_each.detect { |pattern, _task| adapter[pattern] }
unless task
raise DatabaseNotSupported, "Rake tasks not supported by '#{adapter}' adapter"
end

View file

@ -185,6 +185,21 @@ module ActiveRecord
end
end
def test_register_task_precedence
klazz = Class.new do
def initialize(*arguments); end
def structure_dump(filename); end
end
instance = klazz.new
klazz.stub(:new, instance) do
assert_called_with(instance, :structure_dump, ["awesome-file.sql", nil]) do
ActiveRecord::Tasks::DatabaseTasks.register_task(/custom_mysql/, klazz)
ActiveRecord::Tasks::DatabaseTasks.structure_dump({ "adapter" => :custom_mysql }, "awesome-file.sql")
end
end
end
def test_unregistered_task
assert_raise(ActiveRecord::Tasks::DatabaseNotSupported) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump({ "adapter" => :bar }, "awesome-file.sql")