##// END OF EJS Templates
Use r13844 workaround for SQLServer only....
Jean-Philippe Lang -
r13463:dcda9977e376
parent child
Show More
@@ -1,195 +1,200
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module NestedSet
20 20 module IssueNestedSet
21 21 def self.included(base)
22 22 base.class_eval do
23 23 belongs_to :parent, :class_name => self.name
24 24
25 25 before_create :add_to_nested_set, :if => lambda {|issue| issue.parent.present?}
26 26 after_create :add_as_root, :if => lambda {|issue| issue.parent.blank?}
27 27 before_update :handle_parent_change, :if => lambda {|issue| issue.parent_id_changed?}
28 28 before_destroy :destroy_children
29 29 end
30 30 base.extend ClassMethods
31 31 base.send :include, Redmine::NestedSet::Traversing
32 32 end
33 33
34 34 private
35 35
36 36 def target_lft
37 37 scope_for_max_rgt = self.class.where(:root_id => root_id).where(:parent_id => parent_id)
38 38 if id
39 39 scope_for_max_rgt = scope_for_max_rgt.where("id < ?", id)
40 40 end
41 41 max_rgt = scope_for_max_rgt.maximum(:rgt)
42 42 if max_rgt
43 43 max_rgt + 1
44 44 elsif parent
45 45 parent.lft + 1
46 46 else
47 47 1
48 48 end
49 49 end
50 50
51 51 def add_to_nested_set(lock=true)
52 52 lock_nested_set if lock
53 53 parent.send :reload_nested_set_values
54 54 self.root_id = parent.root_id
55 55 self.lft = target_lft
56 56 self.rgt = lft + 1
57 57 self.class.where(:root_id => root_id).where("lft >= ? OR rgt >= ?", lft, lft).update_all([
58 58 "lft = CASE WHEN lft >= :lft THEN lft + 2 ELSE lft END, " +
59 59 "rgt = CASE WHEN rgt >= :lft THEN rgt + 2 ELSE rgt END",
60 60 {:lft => lft}
61 61 ])
62 62 end
63 63
64 64 def add_as_root
65 65 self.root_id = id
66 66 self.lft = 1
67 67 self.rgt = 2
68 68 self.class.where(:id => id).update_all(:root_id => root_id, :lft => lft, :rgt => rgt)
69 69 end
70 70
71 71 def handle_parent_change
72 72 lock_nested_set
73 73 reload_nested_set_values
74 74 if parent_id_was
75 75 remove_from_nested_set
76 76 end
77 77 if parent
78 78 move_to_nested_set
79 79 end
80 80 reload_nested_set_values
81 81 end
82 82
83 83 def move_to_nested_set
84 84 if parent
85 85 previous_root_id = root_id
86 86 self.root_id = parent.root_id
87 87
88 88 lft_after_move = target_lft
89 89 self.class.where(:root_id => parent.root_id).update_all([
90 90 "lft = CASE WHEN lft >= :lft THEN lft + :shift ELSE lft END, " +
91 91 "rgt = CASE WHEN rgt >= :lft THEN rgt + :shift ELSE rgt END",
92 92 {:lft => lft_after_move, :shift => (rgt - lft + 1)}
93 93 ])
94 94
95 95 self.class.where(:root_id => previous_root_id).update_all([
96 96 "root_id = :root_id, lft = lft + :shift, rgt = rgt + :shift",
97 97 {:root_id => parent.root_id, :shift => lft_after_move - lft}
98 98 ])
99 99
100 100 self.lft, self.rgt = lft_after_move, (rgt - lft + lft_after_move)
101 101 parent.send :reload_nested_set_values
102 102 end
103 103 end
104 104
105 105 def remove_from_nested_set
106 106 self.class.where(:root_id => root_id).where("lft >= ? AND rgt <= ?", lft, rgt).
107 107 update_all(["root_id = :id, lft = lft - :shift, rgt = rgt - :shift", {:id => id, :shift => lft - 1}])
108 108
109 109 self.class.where(:root_id => root_id).update_all([
110 110 "lft = CASE WHEN lft >= :lft THEN lft - :shift ELSE lft END, " +
111 111 "rgt = CASE WHEN rgt >= :lft THEN rgt - :shift ELSE rgt END",
112 112 {:lft => lft, :shift => rgt - lft + 1}
113 113 ])
114 114 self.root_id = id
115 115 self.lft, self.rgt = 1, (rgt - lft + 1)
116 116 end
117 117
118 118 def destroy_children
119 119 unless @without_nested_set_update
120 120 lock_nested_set
121 121 reload_nested_set_values
122 122 end
123 123 children.each {|c| c.send :destroy_without_nested_set_update}
124 124 reload
125 125 unless @without_nested_set_update
126 126 self.class.where(:root_id => root_id).where("lft > ? OR rgt > ?", lft, lft).update_all([
127 127 "lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
128 128 "rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
129 129 {:lft => lft, :shift => rgt - lft + 1}
130 130 ])
131 131 end
132 132 end
133 133
134 134 def destroy_without_nested_set_update
135 135 @without_nested_set_update = true
136 136 destroy
137 137 end
138 138
139 139 def reload_nested_set_values
140 140 self.root_id, self.lft, self.rgt = self.class.where(:id => id).pluck(:root_id, :lft, :rgt).first
141 141 end
142 142
143 143 def save_nested_set_values
144 144 self.class.where(:id => id).update_all(:root_id => root_id, :lft => lft, :rgt => rgt)
145 145 end
146 146
147 147 def move_possible?(issue)
148 148 !is_or_is_ancestor_of?(issue)
149 149 end
150 150
151 151 def lock_nested_set
152 lock = true
153 152 if self.class.connection.adapter_name =~ /sqlserver/i
154 153 lock = "WITH (ROWLOCK HOLDLOCK UPDLOCK)"
154 # Custom lock for SQLServer
155 # This can be problematic if root_id or parent root_id changes
156 # before locking
157 sets_to_lock = [root_id, parent.try(:root_id)].compact.uniq
158 self.class.reorder(:id).where(:root_id => sets_to_lock).lock(lock).ids
159 else
160 sets_to_lock = [id, parent_id].compact
161 self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock.ids
155 162 end
156 sets_to_lock = [root_id, parent.try(:root_id)].compact.uniq
157 self.class.reorder(:id).where(:root_id => sets_to_lock).lock(lock).ids
158 163 end
159 164
160 165 def nested_set_scope
161 166 self.class.order(:lft).where(:root_id => root_id)
162 167 end
163 168
164 169 def same_nested_set_scope?(issue)
165 170 root_id == issue.root_id
166 171 end
167 172
168 173 module ClassMethods
169 174 def rebuild_tree!
170 175 transaction do
171 176 reorder(:id).lock.ids
172 177 update_all(:root_id => nil, :lft => nil, :rgt => nil)
173 178 where(:parent_id => nil).update_all(["root_id = id, lft = ?, rgt = ?", 1, 2])
174 179 roots_with_children = joins("JOIN #{table_name} parent ON parent.id = #{table_name}.parent_id AND parent.id = parent.root_id").uniq.pluck("parent.id")
175 180 roots_with_children.each do |root_id|
176 181 rebuild_nodes(root_id)
177 182 end
178 183 end
179 184 end
180 185
181 186 private
182 187
183 188 def rebuild_nodes(parent_id = nil)
184 189 nodes = where(:parent_id => parent_id, :rgt => nil, :lft => nil).order(:id).to_a
185 190
186 191 nodes.each do |node|
187 192 node.send :add_to_nested_set, false
188 193 node.send :save_nested_set_values
189 194 rebuild_nodes node.id
190 195 end
191 196 end
192 197 end
193 198 end
194 199 end
195 200 end
General Comments 0
You need to be logged in to leave comments. Login now