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