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