prevent issue tree hierarchy is broken in race conditions (#6579)

awesome_nested_set 2.1.6 uses lock.
Issue model uses as same way.

git-svn-id: http://svn.redmine.org/redmine/branches/2.5-stable@12925 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2014-02-25 06:49:53 +00:00
parent 63212e5c16
commit 9abdbd423d
1 changed files with 13 additions and 7 deletions

View File

@ -1312,13 +1312,19 @@ class Issue < ActiveRecord::Base
move_to_right_of(root) move_to_right_of(root)
end end
old_root_id = root_id old_root_id = root_id
self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) in_tenacious_transaction do
@parent_issue.reload_nested_set if @parent_issue
self.reload_nested_set
self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
cond = ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]
self.class.base_class.select('id').lock(true).where(cond)
target_maxright = nested_set_scope.maximum(right_column_name) || 0 target_maxright = nested_set_scope.maximum(right_column_name) || 0
offset = target_maxright + 1 - lft offset = target_maxright + 1 - lft
Issue.where(["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]). Issue.where(cond).
update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset]) update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset])
self[left_column_name] = lft + offset self[left_column_name] = lft + offset
self[right_column_name] = rgt + offset self[right_column_name] = rgt + offset
end
if @parent_issue if @parent_issue
move_to_child_of(@parent_issue) move_to_child_of(@parent_issue)
end end