From b39e292e2048c596c78abe51f2e155b952edadb5 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Thu, 24 Oct 2024 18:16:03 -0400 Subject: [PATCH] Bug 1926081 - Add modification_time datetime column to profiles table denoting when the profile was last updated (schema only) --- Bugzilla/DB/Schema.pm | 10 ++++++---- Bugzilla/Install/DB.pm | 38 +++++++++++++++++++++++++++++++++++++- Bugzilla/User.pm | 7 +++++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 31715f07df..47119cf315 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -1002,12 +1002,14 @@ use constant ABSTRACT_SCHEMA => { mfa_required_date => {TYPE => 'DATETIME'}, forget_after_date => {TYPE => 'DATETIME'}, bounce_count => {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}, + modification_ts => {TYPE => 'DATETIME', NOTNULL => 1}, ], INDEXES => [ - profiles_login_name_idx => {FIELDS => ['login_name'], TYPE => 'UNIQUE'}, - profiles_extern_id_idx => {FIELDS => ['extern_id'], TYPE => 'UNIQUE'}, - profiles_nickname_idx => ['nickname'], - profiles_realname_ft_idx => {FIELDS => ['realname'], TYPE => 'FULLTEXT'}, + profiles_login_name_idx => {FIELDS => ['login_name'], TYPE => 'UNIQUE'}, + profiles_extern_id_idx => {FIELDS => ['extern_id'], TYPE => 'UNIQUE'}, + profiles_modification_ts_idx => ['modification_ts'], + profiles_nickname_idx => ['nickname'], + profiles_realname_ft_idx => {FIELDS => ['realname'], TYPE => 'FULLTEXT'}, ], }, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 3d804d0eb3..b2250d411f 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -26,7 +26,7 @@ use Bugzilla::Field; use Date::Parse; use Date::Format; use IO::File; -use List::MoreUtils qw(uniq); +use List::Util qw(max uniq); use URI; use URI::QueryParam; @@ -838,6 +838,9 @@ sub update_table_definitions { # Bug 1803658 - dkl@mozilla.com $dbh->bz_alter_column('ts_error', 'message', {TYPE => 'TEXT', NOTNULL => 1}); + # Bug 1926081 - dkl@mozilla.com + _migrate_profiles_modification_ts(); + ################################################################ # New --TABLE-- changes should go *** A B O V E *** this point # ################################################################ @@ -4437,6 +4440,39 @@ sub _update_see_also_any_url { } } +sub _migrate_profiles_modification_ts { + my $dbh = Bugzilla->dbh; + + return if $dbh->bz_column_info('profiles', 'modification_ts'); + + $dbh->bz_add_column('profiles', 'modification_ts', {TYPE => 'DATETIME'}); + + my $sth = $dbh->prepare( + 'UPDATE profiles SET modification_ts = FROM_UNIXTIME(?) WHERE userid = ?'); + + my $user_ids + = $dbh->selectall_arrayref('SELECT userid FROM profiles ORDER BY userid'); + foreach my $user_id (@{$user_ids}) { + my ($audit_log_when) = $dbh->selectrow_array( + 'SELECT UNIX_TIMESTAMP(at_time) FROM audit_log + WHERE class = \'Bugzilla::User\' AND object_id = ? ORDER BY at_time DESC ' + . $dbh->sql_limit(1), undef, $user_id + ); + my ($profiles_act_when) = $dbh->selectrow_array( + 'SELECT UNIX_TIMESTAMP(profiles_when) FROM profiles_activity + WHERE userid = ? ORDER BY profiles_when DESC ' + . $dbh->sql_limit(1), undef, $user_id + ); + + # We use unix timestamps to make value comparison easier + my $modification_ts = max($audit_log_when, $profiles_act_when); + $sth->execute($modification_ts, $user_id); + } + + $dbh->bz_alter_column('profiles', 'modification_ts', + {TYPE => 'DATETIME', NOTNULL => 1}); +} + 1; __END__ diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index 1e1cc804b4..347f4c1567 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -2658,6 +2658,9 @@ sub create { $params->{nickname} = _generate_nickname($params->{realname}, $params->{login_name}, 0); + my $modification_ts = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)'); + $params->{modification_ts} = $modification_ts; + my $user = $class->SUPER::create($params); # Turn on all email for the new user @@ -2698,8 +2701,8 @@ sub create { $dbh->do( 'INSERT INTO profiles_activity (userid, who, profiles_when, fieldid, newvalue) - VALUES (?, ?, NOW(), ?, NOW())', undef, - ($user->id, $who, $creation_date_fieldid) + VALUES (?, ?, ?, ?, ?)', undef, + ($user->id, $who, $modification_ts, $creation_date_fieldid, $modification_ts) ); $dbh->bz_commit_transaction();