Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions Bugzilla/DB/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,17 @@ use constant ABSTRACT_SCHEMA => {
REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'},
},
# The user who made the change. Not always the setter: redirecting a
# request keeps the original setter. Nullable until all pods write it.
who => {
# request keeps the original setter.
who => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'},
},

# Set to NULL when the requestee's account is deleted.
requestee_id => {
TYPE => 'INT3',
REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'},
REFERENCES => {TABLE => 'profiles', COLUMN => 'userid', DELETE => 'SET NULL'},
},
bug_id => {
TYPE => 'INT3',
Expand All @@ -777,6 +780,12 @@ use constant ABSTRACT_SCHEMA => {
],
},

# One row, the time checksetup added the flag_activity start rows: flag
# changes up to then are to be read from bugs_activity, later ones from
# flag_activity.
flag_activity_migration =>
{FIELDS => [cutoff => {TYPE => 'DATETIME', NOTNULL => 1},],},

# "flagtypes" defines the types of flags that can be set.
flagtypes => {
FIELDS => [
Expand Down
109 changes: 107 additions & 2 deletions Bugzilla/Install/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,21 @@ sub update_table_definitions {
# Before _migrate_flag_state_activity, which copies into this column.
_add_flag_activity_who();

# Bug 2085398 - xavier.lhour@gmail.com
# After _add_flag_activity_who, which fills the rows without it.
_set_flag_activity_who_not_null();

# Bug 2085398 - xavier.lhour@gmail.com
_set_flag_activity_requestee_fk_set_null();

# Bug 1806896 - xavier.lhour@gmail.com
_migrate_flag_state_activity();

# Bug 2085398 - xavier.lhour@gmail.com
# After _migrate_flag_state_activity, so that flags with copied rows are
# skipped.
_add_flag_activity_start_rows();

# Bug 2060356 - dkl@mozilla.com
_remove_duo_recovery_codes();

Expand Down Expand Up @@ -4579,8 +4591,47 @@ sub _migrate_flag_state_activity {
$dbh->bz_drop_table('flag_state_activity');
}

# Nullable for now: during the rolling deploy, pods still on the previous code
# insert rows without it. Bug 2085398 makes it NOT NULL once all pods write it.
# Until 2026-07-29, flag_activity only logged review, feedback and needinfo
# flags. Other flags last changed before then have no row there, so their next
# change would show no previous value in history and bugmail. Give each of them
# a row with its current state, dated at its last change: history and bugmail
# don't show it, as it is older than the cutoff.
sub _add_flag_activity_start_rows {
my $dbh = Bugzilla->dbh;

# The cutoff is set at the end, so this runs once, or again if it died.
return if $dbh->selectrow_array('SELECT 1 FROM flag_activity_migration');

my ($max_id) = $dbh->selectrow_array('SELECT MAX(id) FROM flags');
$max_id //= 0;

my $batch_size = 10_000;
my $added = 0;
for (my $from = 0; $from < $max_id; $from += $batch_size) {
$added += $dbh->do(
'INSERT INTO flag_activity
(flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id,
Comment thread
dklawren marked this conversation as resolved.
Resolved
attachment_id, status)
SELECT COALESCE(modification_date, creation_date), type_id, id,
setter_id, setter_id, requestee_id, bug_id, attach_id, status
FROM flags
WHERE id > ? AND id <= ?
AND NOT EXISTS (SELECT 1 FROM flag_activity
WHERE flag_activity.flag_id = flags.id)', undef,
$from, $from + $batch_size
);
}
print "Added a flag_activity row for $added flags.\n" if $added > 0;

# Flag changes up to this time are to be read from bugs_activity, later ones
# from flag_activity.
$dbh->do(
'INSERT INTO flag_activity_migration (cutoff) VALUES (LOCALTIMESTAMP(0))');
Comment thread
dklawren marked this conversation as resolved.
Resolved
}

# Added nullable: during the rolling deploy, pods still on the previous code
# inserted rows without it. _set_flag_activity_who_not_null makes it NOT NULL
# once all pods write it.
sub _add_flag_activity_who {
my $dbh = Bugzilla->dbh;

Expand All @@ -4597,6 +4648,60 @@ sub _add_flag_activity_who {
$dbh->do('UPDATE flag_activity SET who = setter_id WHERE who IS NULL');
}

# Every pod writes who since bug 2088705 was deployed.
sub _set_flag_activity_who_not_null {
my $dbh = Bugzilla->dbh;
return if $dbh->bz_column_info('flag_activity', 'who')->{NOTNULL};

my $mysql = $dbh->isa('Bugzilla::DB::Mysql');

# MySQL can't make a column of a foreign key NOT NULL. Drop the key and add
# it back right after.
$dbh->bz_drop_fk('flag_activity', 'who');
Comment thread
dklawren marked this conversation as resolved.
Resolved

# Without strict mode, which Bugzilla::DB::Mysql turns off, MySQL copies the
# whole table to make the column NOT NULL, blocking flag changes on live pods
# meanwhile. _add_flag_activity_who just filled every who, so strict mode has
# nothing to refuse.
my $sql_mode = $mysql && $dbh->selectrow_array('SELECT @@SESSION.sql_mode');
$dbh->do('SET SESSION sql_mode = ?',
undef, join(',', grep {length} $sql_mode, 'STRICT_TRANS_TABLES'))
if $mysql;
$dbh->bz_alter_column(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making who NOT NULL here will likely run as ALGORITHM=COPY on prod. innodb only does NULL -> NOT NULL in place when strict sql mode is on, and Bugzilla/DB/Mysql.pm strips STRICT_TRANS_TABLES/STRICT_ALL_TABLES from every session. that copies the whole flag_activity table and blocks flag changes on live pods while it runs, the same thing the foreign_key_checks = 0 step below tries to avoid

maybe turn strict mode on for this one alter (SET SESSION sql_mode = CONCAT(@@sql_mode, ',STRICT_TRANS_TABLES')) and restore it after, or run the ddl directly with ALGORITHM=INPLACE, LOCK=NONE so it fails instead of silently copying. the backfill already fills every who, so strict mode won't trip on nulls

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! :)

Strict mode (STRICT_TRANS_TABLES) is now added to the session sql_mode only around this alter, and the original mode is restored right after (like the foreign_key_checks step).

Checked on MySQL 8.0.42 by forcing ALGORITHM=INPLACE, LOCK=NONE onto the statement: it's refused without strict mode ("cannot silently convert NULL values [...] Try ALGORITHM=COPY") and goes through with it.

Fixed in b0f4c33

'flag_activity',
'who',
{
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}
}
);
$dbh->do('SET SESSION sql_mode = ?', undef, $sql_mode) if $mysql;

# Likewise with foreign_key_checks on to add the key. The values don't need
# the check: they were written under this key or copied from setter_id, which
# has its own.
$dbh->do('SET foreign_key_checks = 0') if $mysql;
$dbh->bz_add_fk('flag_activity', 'who',
$dbh->bz_fk_info('flag_activity', 'who'));
$dbh->do('SET foreign_key_checks = 1') if $mysql;
}

# The start rows name the requestees of older pending requests. Without this,
# their accounts couldn't be deleted any more.
sub _set_flag_activity_requestee_fk_set_null {
my $dbh = Bugzilla->dbh;
my $fk = $dbh->bz_fk_info('flag_activity', 'requestee_id');
return if !$fk || defined $fk->{DELETE};
$fk->{DELETE} = 'SET NULL';

# As for who above: the values were written under this key already.
my $mysql = $dbh->isa('Bugzilla::DB::Mysql');
$dbh->do('SET foreign_key_checks = 0') if $mysql;
$dbh->bz_alter_fk('flag_activity', 'requestee_id', $fk);
$dbh->do('SET foreign_key_checks = 1') if $mysql;
}

sub _remove_duo_recovery_codes {
my $dbh = Bugzilla->dbh;

Expand Down
15 changes: 13 additions & 2 deletions Bugzilla/Test/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use strict;
use warnings;

use base qw(Exporter);
our @EXPORT
= qw(create_user create_bug create_group create_oauth_client issue_api_key mock_useragent_tx);
our @EXPORT = qw(create_user create_bug create_group create_oauth_client
issue_api_key mock_useragent_tx set_flag_activity_cutoff);

use Bugzilla::Group;
use Bugzilla::User;
Expand Down Expand Up @@ -107,6 +107,17 @@ sub issue_api_key {
}
}

# Flag changes up to $when are to be read from bugs_activity, later ones from
# flag_activity. checksetup sets it to the time it ran, after the fixed dates of
# most tests.
sub set_flag_activity_cutoff {
my ($when) = @_;
my $dbh = Bugzilla->dbh;
$dbh->do('DELETE FROM flag_activity_migration');
$dbh->do('INSERT INTO flag_activity_migration (cutoff) VALUES (?)',
undef, $when);
}

sub create_bug {
state $check = compile_named(
short_desc => Str,
Expand Down
30 changes: 26 additions & 4 deletions editusers.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use Bugzilla::User;
use Bugzilla::Bug;
use Bugzilla::BugMail;
use Bugzilla::Flag;
use Bugzilla::FlagActivity;
use Bugzilla::Field;
use Bugzilla::Group;
use Bugzilla::Token;
Expand Down Expand Up @@ -390,9 +391,14 @@ elsif ($action eq 'del') {
undef, $otherUserID);
$vars->{'cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM cc WHERE who = ?',
undef, $otherUserID);
$vars->{'bugs_activity'}
= $dbh->selectrow_array('SELECT COUNT(*) FROM bugs_activity WHERE who = ?',
undef, $otherUserID);

# Flag changes are in flag_activity, not bugs_activity.
$vars->{'bugs_activity'} = $dbh->selectrow_array(
'SELECT (SELECT COUNT(*) FROM bugs_activity WHERE who = ?)
+ (SELECT COUNT(*) FROM flag_activity WHERE who = ? OR setter_id = ?)',
undef, ($otherUserID) x 3
);

$vars->{'component_cc'}
= $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?',
undef, $otherUserID);
Expand Down Expand Up @@ -527,7 +533,23 @@ elsif ($action eq 'delete') {
my ($removed, $added)
= Bugzilla::Flag->update_activity(\@old_summaries, \@new_summaries);
LogActivityEntry($bug_id, 'flagtypes.name', $removed, $added, $userid,
$timestamp, undef, $attach_id);
$timestamp, undef, $attach_id || undef);

# Logged in both tables, like every other flag change, until history
# and bugmail read flag changes after the cutoff from flag_activity.
foreach my $flag (@{$bugs{$bug_id}->{$attach_id}}) {
Bugzilla::FlagActivity->create({
Comment thread
dklawren marked this conversation as resolved.
Resolved
flag_when => $timestamp,
setter_id => $flag->setter_id,
who => $userid,
status => $flag->status,
type_id => $flag->type_id,
flag_id => $flag->id,
requestee_id => $flag->requestee_id,
bug_id => $bug_id,
attachment_id => $attach_id || undef,
});
}
}
$sth_set_bug_timestamp->execute($timestamp, $bug_id);
$updatedbugs{$bug_id} = 1;
Expand Down
94 changes: 94 additions & 0 deletions qa/t/4_test_user_delete_requestee.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use warnings;
use lib qw(lib ../../lib ../../local/lib/perl5);

use Bugzilla;

use Test::More "no_plan";

use QA::Util;

my ($sel, $config) = get_selenium();
my $dbh = Bugzilla->dbh;
my ($user_id, $flag_id);

# Runs even if a check below dies, so that a rerun finds a clean database.
END {
if ($flag_id) {
$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $flag_id);
$dbh->do('DELETE FROM flags WHERE id = ?', undef, $flag_id);
}
$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id) if $user_id;
eval {
set_parameters($sel,
{"Administrative Policies" => {"allowuserdeletion-off" => undef}});
logout($sel);
};
}

log_in($sel, $config, 'admin');
set_parameters($sel,
{"Administrative Policies" => {"allowuserdeletion-on" => undef}});

# A user asked to set a flag, and named as requestee in the flag history, can
# be deleted: the request is cleared and the history keeps the row without
# the requestee.
my $login = 'requestee@selenium.bugzilla.org';
$dbh->do(
"INSERT INTO profiles (login_name, realname, cryptpassword)
VALUES (?, 'Requestee', '*')", undef, $login
);
$user_id = $dbh->bz_last_key('profiles', 'userid');
my ($admin_id)
= $dbh->selectrow_array('SELECT userid FROM profiles WHERE login_name = ?',
undef, $config->{admin_user_login});
my ($bug_id) = $dbh->selectrow_array('SELECT MIN(bug_id) FROM bugs');
my ($type_id)
= $dbh->selectrow_array(
"SELECT MIN(id) FROM flagtypes WHERE target_type = 'b'");
$dbh->do(
"INSERT INTO flags (type_id, status, bug_id, creation_date,
modification_date, setter_id, requestee_id)
VALUES (?, '?', ?, NOW(), NOW(), ?, ?)", undef, $type_id, $bug_id, $admin_id,
$user_id
);
$flag_id = $dbh->bz_last_key('flags', 'id');
$dbh->do(
"INSERT INTO flag_activity
(flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status)
VALUES (NOW(), ?, ?, ?, ?, ?, ?, '?')", undef, $type_id, $flag_id, $admin_id,
$admin_id, $user_id, $bug_id
);
my $history_id = $dbh->bz_last_key('flag_activity', 'id');

$sel->open_ok("/editusers.cgi?action=del&userid=$user_id");
$sel->title_is("Confirm deletion of user $login");
ok(!$sel->is_text_present('You cannot delete this user account'),
'The requestee can be deleted');
$sel->click_ok("delete");
$sel->wait_for_page_to_load_ok(WAIT_TIME);
$sel->title_is("User $login deleted");

is(
scalar $dbh->selectrow_array(
'SELECT COUNT(*) FROM flag_activity WHERE id = ? AND requestee_id IS NULL',
undef, $history_id
),
1,
'The flag history keeps the row without the requestee'
);
is(
scalar $dbh->selectrow_array(
'SELECT COUNT(*) FROM flags WHERE id = ? AND requestee_id IS NULL',
undef, $flag_id
),
1,
'The request is cleared'
);
6 changes: 3 additions & 3 deletions t/bmo/bugmail-flag-events.t
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ sub new_bug {
sub add_activity {
my ($bug, $flag_id, $when, $status, $setter, $requestee, %extra) = @_;
$dbh->do(
'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id,
'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who,
requestee_id, bug_id, attachment_id, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)', undef, $when,
$extra{type_id} // $type_id, $flag_id, $setter->id,
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', undef, $when,
$extra{type_id} // $type_id, $flag_id, $setter->id, $setter->id,
$requestee ? $requestee->id : undef, $bug->id, $extra{attachment_id}, $status
);
}
Expand Down
Loading