@@ -183,6 +183,16 module Redmine | |||||
183 | end |
|
183 | end | |
184 | end |
|
184 | end | |
185 |
|
185 | |||
|
186 | def rebuild_single_tree!(root_id) | |||
|
187 | root = Issue.where(:parent_id => nil).find(root_id) | |||
|
188 | transaction do | |||
|
189 | where(root_id: root_id).reorder(:id).lock.ids | |||
|
190 | where(root_id: root_id).update_all(:lft => nil, :rgt => nil) | |||
|
191 | where(root_id: root_id, parent_id: nil).update_all(["lft = ?, rgt = ?", 1, 2]) | |||
|
192 | rebuild_nodes(root_id) | |||
|
193 | end | |||
|
194 | end | |||
|
195 | ||||
186 | private |
|
196 | private | |
187 |
|
197 | |||
188 | def rebuild_nodes(parent_id = nil) |
|
198 | def rebuild_nodes(parent_id = nil) |
@@ -306,4 +306,23 class IssueNestedSetTest < ActiveSupport::TestCase | |||||
306 | assert_equal ic2, ic4.parent |
|
306 | assert_equal ic2, ic4.parent | |
307 | assert ic5.root? |
|
307 | assert ic5.root? | |
308 | end |
|
308 | end | |
|
309 | ||||
|
310 | def test_rebuild_single_tree | |||
|
311 | i1 = Issue.generate! | |||
|
312 | i2 = i1.generate_child! | |||
|
313 | i3 = i1.generate_child! | |||
|
314 | Issue.update_all(:lft => 7, :rgt => 7) | |||
|
315 | ||||
|
316 | Issue.rebuild_single_tree!(i1.id) | |||
|
317 | ||||
|
318 | i1.reload | |||
|
319 | assert_equal [1, 6], [i1.lft, i1.rgt] | |||
|
320 | i2.reload | |||
|
321 | assert_equal [2, 3], [i2.lft, i2.rgt] | |||
|
322 | i3.reload | |||
|
323 | assert_equal [4, 5], [i3.lft, i3.rgt] | |||
|
324 | ||||
|
325 | other = Issue.find(1) | |||
|
326 | assert_equal [7, 7], [other.lft, other.rgt] | |||
|
327 | end | |||
309 | end |
|
328 | end |
General Comments 0
You need to be logged in to leave comments.
Login now