-
Notifications
You must be signed in to change notification settings - Fork 2
/
redmine_issues_reset_nested_set.sql
executable file
·77 lines (62 loc) · 2.03 KB
/
redmine_issues_reset_nested_set.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- http://www.redmine.org/issues/3722#note-4
-- Version: 0.1.0
-- old names
DROP PROCEDURE IF EXISTS Reset_Nested_Set_Issues;
DROP PROCEDURE IF EXISTS Reset_Nested_Set_Issues_recurse;
-- current names
DROP PROCEDURE IF EXISTS redmine_issues_ResetNestedSet;
DROP PROCEDURE IF EXISTS redmine_issues_ResetNestedSet_recurse;
DELIMITER //
CREATE PROCEDURE redmine_issues_ResetNestedSet()
BEGIN
-- ensure root_id is correct for roots. Do it quickly here.
UPDATE issues SET root_id = id WHERE parent_id IS NULL;
-- MySQL didn't/doesn't allowed OUT or INOUT parameters
SET @left_value = 1;
-- now do recusion
CALL redmine_issues_ResetNestedSet_recurse(NULL, NULL);
END;
//
CREATE PROCEDURE redmine_issues_ResetNestedSet_recurse(root INTEGER, parent INTEGER)
BEGIN
DECLARE done INTEGER DEFAULT 0;
DECLARE node INTEGER;
DECLARE roots CURSOR FOR SELECT id FROM issues WHERE parent_id IS NULL ORDER BY id;
DECLARE children CURSOR FOR SELECT id FROM issues WHERE parent_id = parent ORDER BY id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-- MySQL setting - allow up to 10 stored procedure recursions. Default is 0.
SET max_sp_recursion_depth := 10;
-- this is bypassed on first run
IF parent IS NOT NULL THEN
UPDATE issues SET root_id = root, lft = @left_value WHERE id = parent;
SET @left_value := @left_value + 1;
END IF;
OPEN roots;
OPEN children;
-- for 1st run, and for root nodes
IF parent IS NULL THEN
FETCH roots INTO node;
REPEAT
IF node IS NOT NULL THEN
CALL redmine_issues_ResetNestedSet_recurse(node, node);
SET @left_value := @left_value + 1;
END IF;
FETCH roots INTO node;
UNTIL done END REPEAT;
ELSE
FETCH children INTO node;
REPEAT
IF node IS NOT NULL THEN
CALL redmine_issues_ResetNestedSet_recurse(root, node);
SET @left_value := @left_value + 1;
END IF;
FETCH children INTO node;
UNTIL done END REPEAT;
END IF;
UPDATE issues SET rgt = @left_value WHERE id = parent;
CLOSE roots;
CLOSE children;
END;
//
DELIMITER ;
-- CALL redmine_issues_ResetNestedSet;