Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1926081 - Add modification_time datetime column to profiles table denoting when the profile was last updated #2345

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Bugzilla/DB/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
],
},

Expand Down
38 changes: 37 additions & 1 deletion Bugzilla/Install/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -838,6 +838,9 @@ sub update_table_definitions {
# Bug 1803658 - [email protected]
$dbh->bz_alter_column('ts_error', 'message', {TYPE => 'TEXT', NOTNULL => 1});

# Bug 1926081 - [email protected]
_migrate_profiles_modification_ts();

################################################################
# New --TABLE-- changes should go *** A B O V E *** this point #
################################################################
Expand Down Expand Up @@ -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__
Expand Down
15 changes: 14 additions & 1 deletion Bugzilla/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ sub DB_COLUMNS {
'profiles.mfa',
'profiles.mfa_required_date',
'profiles.nickname',
'profiles.bounce_count'
'profiles.bounce_count',
$dbh->sql_date_format('modification_ts', '%Y-%m-%d %H:%i:%s') . ' AS modification_ts',
),
;
}
Expand All @@ -107,6 +108,7 @@ use constant VALIDATORS => {
password_change_reason => \&_check_password_change_reason,
mfa => \&_check_mfa,
bounce_count => \&_check_numeric,
modification_ts => \&_check_timestamp,
};

sub UPDATE_COLUMNS {
Expand All @@ -124,6 +126,7 @@ sub UPDATE_COLUMNS {
mfa_required_date
nickname
bounce_count
modification_ts
);
push(@cols, 'cryptpassword') if exists $self->{cryptpassword};
return @cols;
Expand Down Expand Up @@ -337,6 +340,11 @@ sub update {
|| exists $changes->{cryptpassword})
);

# Update modification_ts if any changes were made
if (keys %{$changes}) {
$dbh->do('UPDATE profiles set modification_ts = NOW() WHERE userid = ?', undef, $self->id)
}

# XXX Can update profiles_activity here as soon as it understands
# field names like login_name.

Expand Down Expand Up @@ -448,6 +456,10 @@ sub _check_numeric {
return $value;
}

sub _check_timestamp {
return Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
dklawren marked this conversation as resolved.
Show resolved Hide resolved
}

################################################################################
# Mutators
################################################################################
Expand Down Expand Up @@ -669,6 +681,7 @@ sub showmybugslink { $_[0]->{showmybugslink}; }
sub email_disabled { $_[0]->{disable_mail} || !$_[0]->{is_enabled}; }
sub email_enabled { !$_[0]->email_disabled; }
sub last_seen_date { $_[0]->{last_seen_date}; }
sub modification_ts { $_[0]->{modification_ts}; }
sub password_change_required { $_[0]->{password_change_required}; }
sub password_change_reason { $_[0]->{password_change_reason}; }

Expand Down
7 changes: 6 additions & 1 deletion template/en/default/admin/users/edit.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ $(function() {
</td>
</tr>
[% END %]

<tr>
<th>Last Modified:</th>
<td>
[% otheruser.modification_ts FILTER html %]
</td>
</tr>
<tr>
<th>Last Login:</th>
<td>
Expand Down
Loading