Skip to content

Commit 273e25f

Browse files
committed
Updated migration code and fixup script added
1 parent 1aa7bda commit 273e25f

File tree

2 files changed

+117
-5
lines changed

2 files changed

+117
-5
lines changed

Bugzilla/Install/DB.pm

+39-5
Original file line numberDiff line numberDiff line change
@@ -4447,25 +4447,59 @@ sub _migrate_profiles_modification_ts {
44474447

44484448
$dbh->bz_add_column('profiles', 'modification_ts', {TYPE => 'DATETIME'});
44494449

4450+
# A fresh DB will not have this column yet as it is added later by an extension
4451+
# so we will need to ignore it later in this case.
4452+
my $has_creation_ts = $dbh->bz_column_info('profiles', 'creation_ts');
4453+
44504454
my $sth = $dbh->prepare(
44514455
'UPDATE profiles SET modification_ts = FROM_UNIXTIME(?) WHERE userid = ?');
44524456

4457+
# Todays timestamp
4458+
my $now_when
4459+
= $dbh->selectrow_array('SELECT UNIX_TIMESTAMP(LOCALTIMESTAMP(0))');
4460+
44534461
my $user_ids
4454-
= $dbh->selectall_arrayref('SELECT userid FROM profiles ORDER BY userid');
4462+
= $dbh->selectcol_arrayref('SELECT userid FROM profiles ORDER BY userid');
4463+
4464+
my $count = 1;
4465+
my $total = scalar @{$user_ids};
4466+
44554467
foreach my $user_id (@{$user_ids}) {
4456-
my ($audit_log_when) = $dbh->selectrow_array(
4468+
indicate_progress({total => $total, current => $count++, every => 25});
4469+
4470+
my $audit_log_when = $dbh->selectrow_array(
44574471
'SELECT UNIX_TIMESTAMP(at_time) FROM audit_log
44584472
WHERE class = \'Bugzilla::User\' AND object_id = ? ORDER BY at_time DESC '
44594473
. $dbh->sql_limit(1), undef, $user_id
44604474
);
4461-
my ($profiles_act_when) = $dbh->selectrow_array(
4475+
my $profiles_act_when = $dbh->selectrow_array(
44624476
'SELECT UNIX_TIMESTAMP(profiles_when) FROM profiles_activity
44634477
WHERE userid = ? ORDER BY profiles_when DESC '
44644478
. $dbh->sql_limit(1), undef, $user_id
44654479
);
44664480

4467-
# We use unix timestamps to make value comparison easier
4468-
my $modification_ts = max($audit_log_when, $profiles_act_when);
4481+
$audit_log_when ||= 0;
4482+
$profiles_act_when ||= 0;
4483+
4484+
my $creation_when = 0;
4485+
if ($has_creation_ts) {
4486+
$creation_when
4487+
= $dbh->selectrow_array(
4488+
'SELECT UNIX_TIMESTAMP(creation_ts) FROM profiles WHERE userid = ?',
4489+
undef, $user_id);
4490+
$creation_when ||= 0;
4491+
}
4492+
4493+
my $modification_ts = 0;
4494+
# IF we could not find anything then use todays date
4495+
if (!$audit_log_when && !$profiles_act_when && !$creation_when) {
4496+
$modification_ts = $now_when;
4497+
}
4498+
# We used unix timestamps to make value comparison easier without using DateTime instance of each.
4499+
else {
4500+
$modification_ts = max($audit_log_when, $profiles_act_when, $creation_when);
4501+
}
4502+
44694503
$sth->execute($modification_ts, $user_id);
44704504
}
44714505

scripts/bug1926081.pl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)