Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | -- Copyright 2019 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 | -- Update all IssueSnapshot rows that incorrectly have their period_end |
| 8 | -- set to the maximum value 4294967295. For all affected rows, this |
| 9 | -- script update them to the period_end time of the rows with same period_start time; |
| 10 | -- if such rows don't exist, update period_end to be the same as period_start. |
| 11 | -- Bug: crbug.com/monorail/6020 |
| 12 | |
| 13 | CREATE TABLE IssueSnapshotsToUpdate (id INT, issue_id INT, period_start INT UNSIGNED, update_time INT UNSIGNED); |
| 14 | |
| 15 | INSERT INTO IssueSnapshotsToUpdate (id, issue_id, period_start, update_time) |
| 16 | -- Get ids that needs update and append with correct period_end. |
| 17 | (SELECT ToUpdate.id, IssueSnapshot.issue_id, IssueSnapshot.period_start, IssueSnapshot.period_end |
| 18 | FROM IssueSnapshot INNER JOIN ( |
| 19 | -- Get correct period_end to update. |
| 20 | SELECT NeedsUpdate.id, IssueSnapshot.issue_id, IssueSnapshot.period_start, MIN(IssueSnapshot.period_end) |
| 21 | AS update_time FROM IssueSnapshot |
| 22 | INNER JOIN ( |
| 23 | -- Get duplicate rows by filtering out the correct rows. |
| 24 | SELECT id, issue_id, period_start, period_end FROM IssueSnapshot |
| 25 | WHERE period_end = 4294967295 |
| 26 | AND id NOT IN ( |
| 27 | -- Get ids of the correct rows. |
| 28 | SELECT id FROM IssueSnapshot |
| 29 | INNER JOIN ( |
| 30 | -- Get correct rows for each issue_id that should have max period_end. |
| 31 | SELECT issue_id, MAX(period_start) AS maxStart |
| 32 | FROM IssueSnapshot |
| 33 | WHERE period_end = 4294967295 |
| 34 | GROUP BY issue_id) AS MaxISTable |
| 35 | ON IssueSnapshot.issue_id = MaxISTable.issue_id |
| 36 | AND IssueSnapshot.period_start = MaxISTable.maxStart) |
| 37 | ) AS NeedsUpdate |
| 38 | ON NeedsUpdate.issue_id = IssueSnapshot.issue_id |
| 39 | AND NeedsUpdate.period_start = IssueSnapshot.period_start |
| 40 | GROUP BY NeedsUpdate.issue_id, IssueSnapshot.period_start |
| 41 | ) AS ToUpdate |
| 42 | ON IssueSnapshot.issue_id = ToUpdate.issue_id |
| 43 | AND IssueSnapshot.period_start = ToUpdate.period_start |
| 44 | AND IssueSnapshot.period_end = ToUpdate.update_time |
| 45 | ); |
| 46 | |
| 47 | UPDATE IssueSnapshot INNER JOIN IssueSnapshotsToUpdate |
| 48 | ON IssueSnapshot.id = IssueSnapshotsToUpdate.id |
| 49 | SET IssueSnapshot.period_end = CASE WHEN IssueSnapshotsToUpdate.update_time = 4294967295 |
| 50 | THEN IssueSnapshotsToUpdate.period_start ELSE IssueSnapshotsToUpdate.update_time END; |
| 51 | |
| 52 | DROP TABLE IssueSnapshotsToUpdate; |