From f35ca0faf0bcc4ba6f09dbc11e90ceae02405ca8 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 25 Mar 2021 13:08:37 +0100 Subject: [PATCH] Give precedence to the DatabaseTasks registered last --- .../lib/active_record/tasks/database_tasks.rb | 2 +- .../test/cases/tasks/database_tasks_test.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 91bf707273..4a6e9acadc 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -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 diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index a61a2496cf..6f0cd7f944 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -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")