blob: 0cc84a8b8ff9ca02c0b2723fdd65fc8f1fd883fc [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001-- Copyright 2016 The Chromium Authors. All Rights Reserved.
2--
3-- Use of this source code is governed by a BSD-style
4-- license that can be found in the LICENSE file or at
5-- https://developers.google.com/open-source/licenses/bsd
6
7
8DROP PROCEDURE IF EXISTS BackfillIssueTimestampsChunk;
9DROP PROCEDURE IF EXISTS BackfillIssueTimestampsManyChunks;
10
11delimiter //
12
13CREATE PROCEDURE BackfillIssueTimestampsChunk(
14 IN in_pid SMALLINT UNSIGNED, IN in_chunk_size SMALLINT UNSIGNED)
15BEGIN
16
17 DECLARE done INT DEFAULT FALSE;
18
19 DECLARE c_issue_id INT;
20
21 DECLARE curs CURSOR FOR
22 SELECT id FROM Issue
23 WHERE project_id=in_pid
24 AND (owner_modified = 0 OR owner_modified IS NULL)
25 AND (status_modified = 0 OR status_modified IS NULL)
26 AND (component_modified = 0 OR component_modified IS NULL)
27 LIMIT in_chunk_size;
28
29 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
30 OPEN curs;
31
32 issue_loop: LOOP
33 FETCH curs INTO c_issue_id;
34 IF done THEN
35 LEAVE issue_loop;
36 END IF;
37
38 -- Indicate progress.
39 SELECT c_issue_id AS 'Processing:';
40
41 -- Set the fields to the largest timestamp of any relevant update.
42 UPDATE Issue
43 SET
44 owner_modified = (SELECT MAX(created)
45 FROM IssueUpdate
46 JOIN Comment ON IssueUpdate.comment_id = Comment.id
47 WHERE field = 'owner'
48 AND IssueUpdate.issue_id = c_issue_id),
49 status_modified = (SELECT MAX(created)
50 FROM IssueUpdate
51 JOIN Comment ON IssueUpdate.comment_id = Comment.id
52 WHERE field = 'status'
53 AND IssueUpdate.issue_id = c_issue_id),
54 component_modified = (SELECT MAX(created)
55 FROM IssueUpdate
56 JOIN Comment ON IssueUpdate.comment_id = Comment.id
57 WHERE field = 'component'
58 AND IssueUpdate.issue_id = c_issue_id)
59 WHERE id = c_issue_id;
60
61 -- If no update was found, use the issue opened timestamp.
62 UPDATE Issue SET owner_modified = opened
63 WHERE id = c_issue_id AND owner_modified IS NULL;
64
65 UPDATE Issue SET status_modified = opened
66 WHERE id = c_issue_id AND status_modified IS NULL;
67
68 UPDATE Issue SET component_modified = opened
69 WHERE id = c_issue_id AND component_modified IS NULL;
70
71 END LOOP;
72
73 CLOSE curs;
74END;
75
76CREATE PROCEDURE BackfillIssueTimestampsManyChunks(
77 IN in_pid SMALLINT UNSIGNED, IN in_num_chunks SMALLINT UNSIGNED)
78BEGIN
79 WHILE in_num_chunks > 0 DO
80 CALL BackfillIssueTimestampsChunk(in_pid, 1000);
81 SET in_num_chunks = in_num_chunks - 1;
82 END WHILE;
83END;
84
85
86//
87
88
89delimiter ;
90