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

replace CT.rebuid! by CTE for speed

ulferts647b455572be
diff --git a/db/migrate/20220319211253_add_parent_id_to_wp.rb b/db/migrate/20220319211253_add_parent_id_to_wp.rb
index 38e2883d76a..fab053bead9 100644
--- a/db/migrate/20220319211253_add_parent_id_to_wp.rb
+++ b/db/migrate/20220319211253_add_parent_id_to_wp.rb
@@ -38,7 +38,7 @@ class AddParentIdToWp < ActiveRecord::Migration[6.1]
add_closure_tree_table
- ClosureTreeWorkPackage.rebuild!
+ build_closure_tree
cleanup_transitive_relations
@@ -128,6 +128,41 @@ class AddParentIdToWp < ActiveRecord::Migration[6.1]
# rubocop:enable Rails/CreateTableWithTimestamps
end
+ # Creates the actual closure tree data.
+ # This recursive query is used over ClosureTreeWorkPackage.rebuild! for speed
+ # but its result is equivalent.
+ def build_closure_tree
+ execute <<~SQL.squish
+ WITH RECURSIVE closure_tree(ancestor_id, descendant_id, generations) AS (
+ SELECT
+ id ancestor_id,
+ id descendant_id,
+ 0 generations
+ FROM work_packages
+ UNION
+ SELECT
+ closure_tree.ancestor_id,
+ work_packages.id descendant_id,
+ closure_tree.generations + 1
+ FROM closure_tree
+ JOIN work_packages ON work_packages.parent_id = closure_tree.descendant_id
+ )
+
+ INSERT INTO
+ work_package_hierarchies (
+ ancestor_id,
+ descendant_id,
+ generations
+ )
+ SELECT
+ ancestor_id,
+ descendant_id,
+ generations
+ FROM
+ closure_tree
+ SQL
+ end
+
def add_relation_index
add_index :relations, %i[from_id to_id relation_type],
unique: true