|
| 1 | +#!/usr/bin/env perl |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +# |
| 6 | +# This Source Code Form is "Incompatible With Secondary Licenses", as |
| 7 | +# defined by the Mozilla Public License, v. 2.0. |
| 8 | + |
| 9 | +use 5.10.1; |
| 10 | +use strict; |
| 11 | +use warnings; |
| 12 | +use lib qw(. lib local/lib/perl5); |
| 13 | + |
| 14 | +use Bugzilla; |
| 15 | +use Bugzilla::Constants; |
| 16 | +use Bugzilla::Install::Util qw(indicate_progress); |
| 17 | + |
| 18 | +use List::Util qw(max); |
| 19 | + |
| 20 | +Bugzilla->usage_mode(USAGE_MODE_CMDLINE); |
| 21 | + |
| 22 | +my $dbh = Bugzilla->dbh; |
| 23 | + |
| 24 | +my $sth = $dbh->prepare( |
| 25 | + 'UPDATE profiles SET modification_ts = FROM_UNIXTIME(?) WHERE userid = ?'); |
| 26 | + |
| 27 | +# Todays timestamp |
| 28 | +my $now_when |
| 29 | + = $dbh->selectrow_array('SELECT UNIX_TIMESTAMP(LOCALTIMESTAMP(0))'); |
| 30 | + |
| 31 | +my $user_ids |
| 32 | + = $dbh->selectcol_arrayref('SELECT userid FROM profiles ORDER BY userid'); |
| 33 | + |
| 34 | +my $count = 1; |
| 35 | +my $total = scalar @{$user_ids}; |
| 36 | + |
| 37 | +foreach my $user_id (@{$user_ids}) { |
| 38 | + indicate_progress({total => $total, current => $count++, every => 25}); |
| 39 | + |
| 40 | + my $audit_log_when = $dbh->selectrow_array( |
| 41 | + 'SELECT UNIX_TIMESTAMP(at_time) FROM audit_log |
| 42 | + WHERE class = \'Bugzilla::User\' AND object_id = ? ORDER BY at_time DESC ' |
| 43 | + . $dbh->sql_limit(1), undef, $user_id |
| 44 | + ); |
| 45 | + my $profiles_act_when = $dbh->selectrow_array( |
| 46 | + 'SELECT UNIX_TIMESTAMP(profiles_when) FROM profiles_activity |
| 47 | + WHERE userid = ? ORDER BY profiles_when DESC ' |
| 48 | + . $dbh->sql_limit(1), undef, $user_id |
| 49 | + ); |
| 50 | + |
| 51 | + my $creation_when |
| 52 | + = $dbh->selectrow_array( |
| 53 | + 'SELECT UNIX_TIMESTAMP(creation_ts) FROM profiles WHERE userid = ?', |
| 54 | + undef, $user_id); |
| 55 | + |
| 56 | + $creation_when ||= 0; |
| 57 | + $audit_log_when ||= 0; |
| 58 | + $profiles_act_when ||= 0; |
| 59 | + |
| 60 | + my $modification_ts = 0; |
| 61 | + |
| 62 | + # IF we could not find anything then use todays date |
| 63 | + if (!$audit_log_when && !$profiles_act_when && !$creation_when) { |
| 64 | + $modification_ts = $now_when; |
| 65 | + } |
| 66 | + |
| 67 | +# We used unix timestamps to make value comparison easier without using DateTime instance of each. |
| 68 | + else { |
| 69 | + $modification_ts = max($audit_log_when, $profiles_act_when, $creation_when); |
| 70 | + } |
| 71 | + |
| 72 | + $sth->execute($modification_ts, $user_id); |
| 73 | +} |
| 74 | + |
| 75 | +$dbh->bz_alter_column('profiles', 'modification_ts', |
| 76 | + {TYPE => 'DATETIME', NOTNULL => 1}); |
| 77 | + |
| 78 | +1; |
0 commit comments