]> source.dussan.org Git - redmine.git/commitdiff
Adds .rebuild_single_tree! to rebuild a single tree (#24167).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 21 Dec 2016 10:35:36 +0000 (10:35 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 21 Dec 2016 10:35:36 +0000 (10:35 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@16111 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/nested_set/issue_nested_set.rb
test/unit/issue_nested_set_test.rb

index afbe6b99516865bd55ae9b8b36b7ca81b4011005..8753f25deba436ac26324497e2c7ceb5009b6a88 100644 (file)
@@ -183,6 +183,16 @@ module Redmine
           end
         end
 
+        def rebuild_single_tree!(root_id)
+          root = Issue.where(:parent_id => nil).find(root_id)
+          transaction do
+            where(root_id: root_id).reorder(:id).lock.ids
+            where(root_id: root_id).update_all(:lft => nil, :rgt => nil)
+            where(root_id: root_id, parent_id: nil).update_all(["lft = ?, rgt = ?", 1, 2])
+            rebuild_nodes(root_id)
+          end
+        end
+
         private
 
         def rebuild_nodes(parent_id = nil)
index 88f65d3f6442827ab8ab3502233aaacd1576c90f..4062839a3a1223fe59177aba9645a66a0fa5be2e 100644 (file)
@@ -306,4 +306,23 @@ class IssueNestedSetTest < ActiveSupport::TestCase
     assert_equal ic2, ic4.parent
     assert ic5.root?
   end
+
+  def test_rebuild_single_tree
+    i1 = Issue.generate!
+    i2 = i1.generate_child!
+    i3 = i1.generate_child!
+    Issue.update_all(:lft => 7, :rgt => 7)
+
+    Issue.rebuild_single_tree!(i1.id)
+
+    i1.reload
+    assert_equal [1, 6], [i1.lft, i1.rgt]
+    i2.reload
+    assert_equal [2, 3], [i2.lft, i2.rgt]
+    i3.reload
+    assert_equal [4, 5], [i3.lft, i3.rgt]
+
+    other = Issue.find(1)
+    assert_equal [7, 7], [other.lft, other.rgt]
+  end
 end