You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_nested_set.rb 7.5KB

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