Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Remove the reorder_hierarchy_job and inline it in the migration

Oliver Güntherf17ad241b12e
diff --git a/app/workers/projects/reorder_hierarchy_job.rb b/app/workers/projects/reorder_hierarchy_job.rb
deleted file mode 100644
index eafa3ed0efd..00000000000
--- a/app/workers/projects/reorder_hierarchy_job.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2024 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module Projects
- class ReorderHierarchyJob < ApplicationJob
- def perform
- Rails.logger.info { "Resorting siblings by name in the project's nested set." }
- Project.transaction { reorder! }
- end
-
- private
-
- def reorder!
- # Reorder the project roots
- reorder_siblings Project.roots
-
- # Reorder every project hierarchy
- Project
- .where(id: unique_parent_ids)
- .find_each { |project| reorder_siblings(project.children) }
- end
-
- def unique_parent_ids
- Project
- .where.not(parent_id: nil)
- .select(:parent_id)
- .distinct
- end
-
- def reorder_siblings(siblings)
- return unless siblings.many?
-
- # Resort children manually
- sorted = siblings.sort_by { |project| project.name.downcase }
-
- # Get the current first child
- first = siblings.first
-
- sorted.each_with_index do |child, i|
- if i == 0
- child.move_to_left_of(first) unless child == first
- else
- child.move_to_right_of(sorted[i - 1])
- end
- end
- end
- end
-end
diff --git a/db/migrate/20220202140507_reorder_project_children.rb b/db/migrate/20220202140507_reorder_project_children.rb
index f865f4054de..9970c0da3c9 100644
--- a/db/migrate/20220202140507_reorder_project_children.rb
+++ b/db/migrate/20220202140507_reorder_project_children.rb
@@ -27,7 +27,54 @@
#++
class ReorderProjectChildren < ActiveRecord::Migration[6.1]
+ class ProjectMigration < ApplicationRecord
+ include ::Projects::Hierarchy
+ self.table_name = 'projects'
+ end
+
def up
- ::Projects::ReorderHierarchyJob.new.perform
+ Rails.logger.info { "Resorting siblings by name in the project's nested set." }
+ ProjectMigration.transaction { reorder! }
+ end
+
+ def down
+ # Nothing to do
+ end
+
+ private
+
+ def reorder!
+ # Reorder the project roots
+ reorder_siblings ProjectMigration.roots
+
+ # Reorder every project hierarchy
+ ProjectMigration
+ .where(id: unique_parent_ids)
+ .find_each { |project| reorder_siblings(project.children) }
+ end
+
+ def unique_parent_ids
+ ProjectMigration
+ .where.not(parent_id: nil)
+ .select(:parent_id)
+ .distinct
+ end
+
+ def reorder_siblings(siblings)
+ return unless siblings.many?
+
+ # Resort children manually
+ sorted = siblings.sort_by { |project| project.name.downcase }
+
+ # Get the current first child
+ first = siblings.first
+
+ sorted.each_with_index do |child, i|
+ if i == 0
+ child.move_to_left_of(first) unless child == first
+ else
+ child.move_to_right_of(sorted[i - 1])
+ end
+ end
end
end
diff --git a/spec/workers/projects/reorder_children_job_integration_spec.rb b/spec/migrations/reorder_project_children_spec.rb
similarity index 89%
rename from spec/workers/projects/reorder_children_job_integration_spec.rb
rename to spec/migrations/reorder_project_children_spec.rb
index 7f63c6258e9..ce2098186bc 100644
--- a/spec/workers/projects/reorder_children_job_integration_spec.rb
+++ b/spec/migrations/reorder_project_children_spec.rb
@@ -27,9 +27,11 @@
#++
require 'spec_helper'
+require Rails.root.join("db/migrate/20220202140507_reorder_project_children.rb")
-RSpec.describe Projects::ReorderHierarchyJob, type: :model do
- subject(:job) { described_class.perform_now }
+RSpec.describe ReorderProjectChildren, type: :model do
+ # Silencing migration logs, since we are not interested in that during testing
+ subject(:run_migration) { ActiveRecord::Migration.suppress_messages { described_class.new.up } }
shared_let(:parent_project_a) { create(:project, name: 'ParentA') }
shared_let(:parent_project_b) { create(:project, name: 'ParentB') }