blob: ededfc74030f59630ca18aa88d69c336073a2271 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001-- 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.
Copybara854996b2021-09-07 19:36:02 +00004
5
6DROP PROCEDURE IF EXISTS BackfillLastVisitTimestamp;
7
8delimiter //
9
10CREATE PROCEDURE BackfillLastVisitTimestamp(
11 IN in_now_ts INT, IN in_days_ago INT, IN in_num_days INT)
12BEGIN
13
14 DECLARE done INT DEFAULT FALSE;
15
16 DECLARE c_user_id INT;
17 DECLARE c_comment_ts INT;
18
19 DECLARE curs CURSOR FOR
20 SELECT MAX(created), commenter_id FROM Comment
21 WHERE created >= in_now_ts - 60 * 60 * 24 * in_days_ago
22 AND created < in_now_ts - 60 * 60 * 24 * (in_days_ago - in_num_days)
23 GROUP BY commenter_id
24 ORDER BY MAX(created);
25
26 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
27 OPEN curs;
28
29 user_loop: LOOP
30 FETCH curs INTO c_comment_ts, c_user_id;
31 IF done THEN
32 LEAVE user_loop;
33 END IF;
34
35 -- Indicate progress.
36 SELECT c_comment_ts AS 'Processing:';
37
38 -- Set last_visit_timestamp for one user if not already set.
39 UPDATE User
40 SET last_visit_timestamp = IFNULL(last_visit_timestamp, c_comment_ts)
41 WHERE user_id = c_user_id;
42
43 END LOOP;
44
45END;
46
47
48//
49
50
51delimiter ;
52
53
54-- If run locally do all at once:
55-- CALL BackfillLastVisitTimestamp(1476915669, 180, 180);
56
57-- If run on staging or production, consider the last 180 days
58-- in chunks of 30 days at a time:
59-- CALL BackfillLastVisitTimestamp(1476915669, 30, 30);
60-- CALL BackfillLastVisitTimestamp(1476915669, 60, 30);
61-- CALL BackfillLastVisitTimestamp(1476915669, 90, 30);
62-- CALL BackfillLastVisitTimestamp(1476915669, 120, 30);
63-- CALL BackfillLastVisitTimestamp(1476915669, 150, 30);
64-- CALL BackfillLastVisitTimestamp(1476915669, 180, 30);