From 241c2ec3c286e52b465cb4a63fbe659b7bca5558 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 13 Nov 2026 20:38:23 +0100 Subject: [PATCH 01/10] Bug 2085398 - Make flag_activity.who NOT NULL, add a starting flag_activity row for older flags and store the cutoff --- Bugzilla/DB/Schema.pm | 11 +++- Bugzilla/Install/DB.pm | 70 +++++++++++++++++++++- Bugzilla/Test/Util.pm | 16 ++++- t/bmo/bugmail-flag-events.t | 6 +- t/flag-activity-start-rows.t | 111 +++++++++++++++++++++++++++++++++++ t/flag-activity-who.t | 46 ++++++++------- 6 files changed, 231 insertions(+), 29 deletions(-) create mode 100644 t/flag-activity-start-rows.t diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 77cf05127a..d77d05b69c 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -748,9 +748,10 @@ 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'}, }, requestee_id => { @@ -777,6 +778,12 @@ use constant ABSTRACT_SCHEMA => { ], }, + # One row: flag changes up to this time are read from bugs_activity, later + # ones from flag_activity. + # See Bugzilla::FlagActivity->cutoff. + flag_activity_migration => + {FIELDS => [cutoff => {TYPE => 'DATETIME', NOTNULL => 1},],}, + # "flagtypes" defines the types of flags that can be set. flagtypes => { FIELDS => [ diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 71e5300dee..14bec60f7c 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -852,9 +852,18 @@ 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 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(); @@ -4579,8 +4588,46 @@ 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, + 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; + + # See Bugzilla::FlagActivity->cutoff. + $dbh->do( + 'INSERT INTO flag_activity_migration (cutoff) VALUES (LOCALTIMESTAMP(0))'); +} + +# 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; @@ -4597,6 +4644,25 @@ 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}; + + # MySQL can't make a column of a foreign key NOT NULL. Drop the key: + # bz_setup_foreign_keys adds it back at the end of checksetup. + $dbh->bz_drop_fk('flag_activity', 'who'); + $dbh->bz_alter_column( + 'flag_activity', + 'who', + { + TYPE => 'INT3', + NOTNULL => 1, + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} + } + ); +} + sub _remove_duo_recovery_codes { my $dbh = Bugzilla->dbh; diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index d5b6a938c4..9d8109967c 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -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; @@ -107,6 +107,18 @@ sub issue_api_key { } } +# Flag changes up to $when are read from bugs_activity, later ones from +# flag_activity: see Bugzilla::FlagActivity->cutoff. 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); + delete Bugzilla->request_cache->{flag_activity_cutoff}; +} + sub create_bug { state $check = compile_named( short_desc => Str, diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 4f13367206..abfd5907a1 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -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 ); } diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t new file mode 100644 index 0000000000..141161c72f --- /dev/null +++ b/t/flag-activity-start-rows.t @@ -0,0 +1,111 @@ +#!/usr/bin/env perl +# 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 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::Install::DB; +use Bugzilla::User; +use Test2::V0; + +# A flag last changed before flag_activity logged its type gets a starting +# row, so that its next change shows the previous value. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $setter = create_user('setter@bmo.test', '*'); +create_user('bob@bmo.test', '*'); +my ($needinfo_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'needinfo'"); + +my $bug_id = create_bug( + short_desc => 'Flag activity start rows test bug', + comment => 'Bug for the flag activity start rows tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; + +# Future dates, so that the changes come after the bug creation. +Bugzilla->set_user($setter); +my $bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2099-01-01 10:00:00'); +my ($flag) = @{Bugzilla::Bug->new($bug_id)->flags}; + +# As if it had been set before flag_activity logged it. +$dbh->do('DELETE FROM flag_activity WHERE bug_id = ?', undef, $bug_id); + +# checksetup already ran on this database: forget it, so that it walks the +# flags again. +sub add_start_rows { + $dbh->do('DELETE FROM flag_activity_migration'); + Bugzilla::Install::DB::_add_flag_activity_start_rows(); +} + +add_start_rows() for 1 .. 2; + +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', + undef, $flag->id + ), + 1, + 'one starting row, however many times checksetup runs' +); + +# flags.modification_date can be NULL, flag_activity.flag_when can't. +$bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2099-01-01 12:00:00'); +my ($undated) + = grep { $_->status eq '?' } @{Bugzilla::Bug->new($bug_id)->flags}; +$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); +$dbh->do('UPDATE flags SET modification_date = NULL WHERE id = ?', + undef, $undated->id); + +add_start_rows(); +is( + scalar $dbh->selectrow_array( + 'SELECT flag_when FROM flag_activity WHERE flag_id = ?', + undef, $undated->id + ), + scalar $dbh->selectrow_array( + 'SELECT creation_date FROM flags WHERE id = ?', + undef, $undated->id + ), + 'a flag without a modification date is dated at its creation' +); + +# Once the cutoff is set, checksetup doesn't walk the flags again. +$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); +Bugzilla::Install::DB::_add_flag_activity_start_rows(); +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', + undef, $undated->id + ), + 0, + 'the start rows are only added once' +); + +done_testing; diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index 2dc6109712..b58383729b 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -18,15 +18,14 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::Install::DB; use Bugzilla::User; use Test2::V0; -# Redirecting a request keeps the original setter, but flag_activity.who -# records the user who redirected it. The column stays nullable while pods on -# the previous code still insert rows without it. +# Redirecting a request keeps the original setter, but flag_activity.who and +# the bug history record the user who redirected it. create_user('automation@bmo.tld', '*'); Bugzilla->set_user(Bugzilla::User->super_user); @@ -35,6 +34,7 @@ my $dbh = Bugzilla->dbh; my $requester = create_user('requester@bmo.test', '*'); my $redirector = create_user('redirector@bmo.test', '*'); create_user($_, '*') for qw(bob@bmo.test carol@bmo.test); +set_flag_activity_cutoff('2025-01-01 00:00:00'); my $bug_id = create_bug( short_desc => 'Flag redirect test bug', @@ -75,22 +75,28 @@ is( 'who is the user who made each change' ); -# A pod on the previous code inserts a row without who: it's saved, and the -# next checksetup fills it with the setter. -$dbh->do( - 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, bug_id, - status) - VALUES (?, ?, ?, ?, ?, ?)', undef, '2026-01-01 12:00:00', $needinfo_id, - $flag->id, $requester->id, $bug_id, 'X' -); -Bugzilla::Install::DB::_add_flag_activity_who(); -my ($who) - = $dbh->selectrow_array( - "SELECT who FROM flag_activity WHERE flag_id = ? AND status = 'X'", +my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); +my ($redirect) = grep { + grep { $_->{fieldname} eq 'flagtypes.name' && $_->{added} =~ /carol/ } + @{$_->{changes}} +} @$operations; +is($redirect->{who}, 'redirector@bmo.test', 'history shows the redirector'); + +# A database left by bug 2088705: who nullable, with rows that pods on the +# older code inserted without it. The next checksetup fills them and makes the +# column NOT NULL. +$dbh->bz_alter_column('flag_activity', 'who', + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); +$dbh->do('UPDATE flag_activity SET who = NULL WHERE flag_id = ?', undef, $flag->id); -is($who, $requester->id, - 'a row inserted without who gets the setter on the next checksetup'); -ok(!$dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, - 'the column stays nullable'); +Bugzilla::Install::DB->update_table_definitions({}); +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE who IS NULL'), + 0, + 'the rows without who are filled' +); +ok($dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, + 'and the column is NOT NULL'); done_testing; From 5c5f944b04391dd4621a24502be02a4ede435a27 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 16:21:24 +0100 Subject: [PATCH 02/10] Bug 2085398 - Log flag_activity when a deleted user's requests are cleared, and refuse to delete a requestee kept in flag_activity --- editusers.cgi | 31 +++++++++- qa/t/4_test_user_delete_requestee.t | 56 +++++++++++++++++++ .../admin/users/confirm-delete.html.tmpl | 11 +++- 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 qa/t/4_test_user_delete_requestee.t diff --git a/editusers.cgi b/editusers.cgi index 9446ff2922..b0cbefd3a3 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -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; @@ -390,8 +391,18 @@ 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 = ?', + + # 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 + ); + + # The foreign key forbids deleting a requestee kept in the flag history. + $vars->{'flag_activity_requestee'} + = $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE requestee_id = ?', undef, $otherUserID); $vars->{'component_cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?', @@ -528,6 +539,22 @@ elsif ($action eq 'delete') { = Bugzilla::Flag->update_activity(\@old_summaries, \@new_summaries); LogActivityEntry($bug_id, 'flagtypes.name', $removed, $added, $userid, $timestamp, undef, $attach_id); + + # 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({ + 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; diff --git a/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t new file mode 100644 index 0000000000..82e961d46f --- /dev/null +++ b/qa/t/4_test_user_delete_requestee.t @@ -0,0 +1,56 @@ +# 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(); + +log_in($sel, $config, 'admin'); +set_parameters($sel, + {"Administrative Policies" => {"allowuserdeletion-on" => undef}}); + +# flag_activity.requestee_id keeps a user from being deleted, so the +# confirmation page must not offer it. +my $dbh = Bugzilla->dbh; +$dbh->do( + "INSERT INTO profiles (login_name, realname, cryptpassword) + VALUES ('requestee\@selenium.bugzilla.org', 'Requestee', '*')" +); +my $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'); +$dbh->do( + "INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) + VALUES (NOW(), ?, 0, ?, ?, ?, ?, 'X')", undef, $type_id, $admin_id, $admin_id, + $user_id, $bug_id +); + +# The page has its verdict only if there is also something safe to report. +$dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)', undef, $bug_id, + $user_id); + +$sel->open_ok("/editusers.cgi?action=del&userid=$user_id"); +$sel->is_text_present_ok('named in the flag history'); +$sel->is_text_present_ok('You cannot delete this user account'); + +$dbh->do('DELETE FROM cc WHERE who = ?', undef, $user_id); +$dbh->do('DELETE FROM flag_activity WHERE requestee_id = ?', undef, $user_id); +$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id); + +logout($sel); diff --git a/template/en/default/admin/users/confirm-delete.html.tmpl b/template/en/default/admin/users/confirm-delete.html.tmpl index 203edfc838..445671be1f 100644 --- a/template/en/default/admin/users/confirm-delete.html.tmpl +++ b/template/en/default/admin/users/confirm-delete.html.tmpl @@ -26,6 +26,8 @@ # entries on # cc number of bugs the viewed user is cc list member # of + # flag_activity_requestee: number of flag history rows naming the viewed user + # as requestee # flags.requestee: number of flags the viewed user is being asked for # flags.setter: number of flags the viewed user has set # longdescs: number of bug comments the viewed user has written @@ -102,7 +104,8 @@ [% ELSE %] [% accept_deletion = 1 %] - [% IF attachments || reporter || bugs_activity || flags.setter || longdescs || profiles_activity %] + [% IF attachments || reporter || bugs_activity || flag_activity_requestee || + flags.setter || longdescs || profiles_activity %]

The following deletions are unsafe and would generate referential integrity inconsistencies!

@@ -166,6 +169,12 @@ not showing up in [% terms.bug %] activity logs any more. [% END %] + [% IF flag_activity_requestee %] +
  • + [% otheruser.login FILTER html %] has been asked to set flags. + The database refuses to delete a user named in the flag history. +
  • + [% END %] [% IF flags.setter %]
  • [% otheruser.login FILTER html %] has From 693fdefcfada951af55ff19f8d8b91943cf2b155 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 16:26:10 +0100 Subject: [PATCH 03/10] Bug 2085398 - Add the flag_activity.who foreign key back with foreign_key_checks off --- Bugzilla/Install/DB.pm | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 14bec60f7c..d931a39094 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4649,8 +4649,8 @@ sub _set_flag_activity_who_not_null { my $dbh = Bugzilla->dbh; return if $dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}; - # MySQL can't make a column of a foreign key NOT NULL. Drop the key: - # bz_setup_foreign_keys adds it back at the end of checksetup. + # 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'); $dbh->bz_alter_column( 'flag_activity', @@ -4661,6 +4661,16 @@ sub _set_flag_activity_who_not_null { REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} } ); + + # With foreign_key_checks on, MySQL copies the whole table to add the key, + # blocking flag changes on live pods meanwhile. The values don't need the + # check: they were written under this key or copied from setter_id, which has + # its own. + my $mysql = $dbh->isa('Bugzilla::DB::Mysql'); + $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; } sub _remove_duo_recovery_codes { From f8e4e923f137ebc8ab072384117cd71b59e37d6a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 16:34:33 +0100 Subject: [PATCH 04/10] Bug 2085398 - Test that the flag_activity.who foreign key is added back --- t/flag-activity-who.t | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index b58383729b..09a21f5d1e 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -99,4 +99,18 @@ is( ok($dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, 'and the column is NOT NULL'); +# The foreign key dropped for the change is back at the end of checksetup. +ok($dbh->bz_fk_info('flag_activity', 'who')->{created}, + 'and the who foreign key is added back'); + +# And right after the change, not by bz_setup_foreign_keys: on MySQL it would +# copy the whole table to add it. +$dbh->bz_alter_column('flag_activity', 'who', + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); +Bugzilla::Install::DB::_set_flag_activity_who_not_null(); +ok( + $dbh->bz_fk_info('flag_activity', 'who')->{created}, + 'the who foreign key is added back with the NOT NULL change' +); + done_testing; From b27aea08d071fd28932323f8f7db45bac921bb96 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 16:40:50 +0100 Subject: [PATCH 05/10] Bug 2085398 - Test the starting flag_activity row of an attachment flag --- t/flag-activity-start-rows.t | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index 141161c72f..a00a9d27b7 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -19,6 +19,7 @@ use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Attachment; use Bugzilla::Bug; use Bugzilla::Install::DB; use Bugzilla::User; @@ -108,4 +109,44 @@ is( 'the start rows are only added once' ); +# An attachment flag: the starting row copies each column from the flag. +my $attach_id = Bugzilla::Attachment->create({ + bug => Bugzilla::Bug->new($bug_id), + creation_ts => '2099-01-01 13:00:00', + data => 'patch', + description => 'Attachment with a flag', + filename => 'patch.txt', + ispatch => 0, + isprivate => 0, + mimetype => 'text/plain', +})->id; +my $requestee = Bugzilla::User->new({name => 'bob@bmo.test'}); +$dbh->do( + "INSERT INTO flags (type_id, status, bug_id, attach_id, creation_date, + modification_date, setter_id, requestee_id) + VALUES (?, '?', ?, ?, '2099-01-01 13:00:00', '2099-01-01 14:00:00', ?, ?)", + undef, $needinfo_id, $bug_id, $attach_id, $setter->id, $requestee->id +); +my $attach_flag_id = $dbh->bz_last_key('flags', 'id'); + +add_start_rows(); +is( + $dbh->selectrow_hashref( + 'SELECT flag_when, type_id, setter_id, who, requestee_id, bug_id, + attachment_id, status + FROM flag_activity WHERE flag_id = ?', undef, $attach_flag_id + ), + { + flag_when => '2099-01-01 14:00:00', + type_id => $needinfo_id, + setter_id => $setter->id, + who => $setter->id, + requestee_id => $requestee->id, + bug_id => $bug_id, + attachment_id => $attach_id, + status => '?', + }, + 'the starting row of an attachment flag has its columns' +); + done_testing; From 29c7e1dd575bf67116a00351440d4a1f53abc79a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 16:43:57 +0100 Subject: [PATCH 06/10] Bug 2085398 - Don't refer to Bugzilla::FlagActivity->cutoff before #2796 --- Bugzilla/DB/Schema.pm | 6 +++--- Bugzilla/Install/DB.pm | 3 ++- Bugzilla/Test/Util.pm | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index d77d05b69c..ee4ca4d691 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -778,9 +778,9 @@ use constant ABSTRACT_SCHEMA => { ], }, - # One row: flag changes up to this time are read from bugs_activity, later - # ones from flag_activity. - # See Bugzilla::FlagActivity->cutoff. + # 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},],}, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index d931a39094..d92dd781e5 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4620,7 +4620,8 @@ sub _add_flag_activity_start_rows { } print "Added a flag_activity row for $added flags.\n" if $added > 0; - # See Bugzilla::FlagActivity->cutoff. + # 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))'); } diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index 9d8109967c..b88399879a 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -107,16 +107,15 @@ sub issue_api_key { } } -# Flag changes up to $when are read from bugs_activity, later ones from -# flag_activity: see Bugzilla::FlagActivity->cutoff. checksetup sets it to the -# time it ran, after the fixed dates of most tests. +# 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); - delete Bugzilla->request_cache->{flag_activity_cutoff}; } sub create_bug { From 1ea2e80c91f1d618be165e3d3dc75c8e1abde409 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 16:05:02 +0100 Subject: [PATCH 07/10] Bug 2085398 - Set flag_activity.requestee_id to NULL when the requestee's account is deleted --- Bugzilla/DB/Schema.pm | 4 +++- Bugzilla/Install/DB.pm | 18 ++++++++++++++++++ t/flag-activity-start-rows.t | 28 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index ee4ca4d691..863de24c15 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -754,9 +754,11 @@ use constant ABSTRACT_SCHEMA => { 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', diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index d92dd781e5..2bded965f7 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -856,6 +856,9 @@ sub update_table_definitions { # 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(); @@ -4674,6 +4677,21 @@ sub _set_flag_activity_who_not_null { $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; diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index a00a9d27b7..df5cc00be9 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -149,4 +149,32 @@ is( 'the starting row of an attachment flag has its columns' ); +# Requestees named in the flag history can still be deleted. On a database +# where the requestee foreign key has no ON DELETE rule, checksetup adds it. +$dbh->bz_alter_fk('flag_activity', 'requestee_id', + {TABLE => 'profiles', COLUMN => 'userid'}); +Bugzilla::Install::DB->update_table_definitions({}); +is($dbh->bz_fk_info('flag_activity', 'requestee_id')->{DELETE}, + 'SET NULL', 'the requestee foreign key sets NULL on delete'); + +my $gone = create_user('gone@bmo.test', '*'); +$dbh->do( + "INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) + VALUES ('2099-01-01 15:00:00', ?, ?, ?, ?, ?, ?, 'X')", undef, $needinfo_id, + $attach_flag_id, $setter->id, $setter->id, $gone->id, $bug_id +); +my $row_id = $dbh->bz_last_key('flag_activity', 'id'); +ok(eval { $dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $gone->id) }, + 'a requestee named in the flag history can be deleted') + or diag $@; +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE id = ? AND requestee_id IS NULL', + undef, $row_id + ), + 1, + 'and the history row is kept without the requestee' +); + done_testing; From a7f15172538d1a80ac02cb731225fa3d0abf69b9 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 16:31:44 +0100 Subject: [PATCH 08/10] Bug 2085398 - Let editusers delete a requestee kept in flag_activity --- editusers.cgi | 7 +-- qa/t/4_test_user_delete_requestee.t | 62 ++++++++++++++----- .../admin/users/confirm-delete.html.tmpl | 11 +--- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/editusers.cgi b/editusers.cgi index b0cbefd3a3..7a20b72878 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -399,11 +399,6 @@ elsif ($action eq 'del') { undef, ($otherUserID) x 3 ); - # The foreign key forbids deleting a requestee kept in the flag history. - $vars->{'flag_activity_requestee'} - = $dbh->selectrow_array( - 'SELECT COUNT(*) FROM flag_activity WHERE requestee_id = ?', - undef, $otherUserID); $vars->{'component_cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?', undef, $otherUserID); @@ -538,7 +533,7 @@ 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. diff --git a/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t index 82e961d46f..f1635cc1a8 100644 --- a/qa/t/4_test_user_delete_requestee.t +++ b/qa/t/4_test_user_delete_requestee.t @@ -21,36 +21,64 @@ log_in($sel, $config, 'admin'); set_parameters($sel, {"Administrative Policies" => {"allowuserdeletion-on" => undef}}); -# flag_activity.requestee_id keeps a user from being deleted, so the -# confirmation page must not offer it. -my $dbh = Bugzilla->dbh; +# 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 $dbh = Bugzilla->dbh; +my $login = 'requestee@selenium.bugzilla.org'; $dbh->do( "INSERT INTO profiles (login_name, realname, cryptpassword) - VALUES ('requestee\@selenium.bugzilla.org', 'Requestee', '*')" + VALUES (?, 'Requestee', '*')", undef, $login ); my $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'); +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 +); +my $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(), ?, 0, ?, ?, ?, ?, 'X')", undef, $type_id, $admin_id, $admin_id, - $user_id, $bug_id + VALUES (NOW(), ?, ?, ?, ?, ?, ?, '?')", undef, $type_id, $flag_id, $admin_id, + $admin_id, $user_id, $bug_id ); - -# The page has its verdict only if there is also something safe to report. -$dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)', undef, $bug_id, - $user_id); +my $history_id = $dbh->bz_last_key('flag_activity', 'id'); $sel->open_ok("/editusers.cgi?action=del&userid=$user_id"); -$sel->is_text_present_ok('named in the flag history'); -$sel->is_text_present_ok('You cannot delete this user account'); +$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' +); -$dbh->do('DELETE FROM cc WHERE who = ?', undef, $user_id); -$dbh->do('DELETE FROM flag_activity WHERE requestee_id = ?', undef, $user_id); -$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id); +$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $flag_id); +$dbh->do('DELETE FROM flags WHERE id = ?', undef, $flag_id); logout($sel); diff --git a/template/en/default/admin/users/confirm-delete.html.tmpl b/template/en/default/admin/users/confirm-delete.html.tmpl index 445671be1f..203edfc838 100644 --- a/template/en/default/admin/users/confirm-delete.html.tmpl +++ b/template/en/default/admin/users/confirm-delete.html.tmpl @@ -26,8 +26,6 @@ # entries on # cc number of bugs the viewed user is cc list member # of - # flag_activity_requestee: number of flag history rows naming the viewed user - # as requestee # flags.requestee: number of flags the viewed user is being asked for # flags.setter: number of flags the viewed user has set # longdescs: number of bug comments the viewed user has written @@ -104,8 +102,7 @@ [% ELSE %] [% accept_deletion = 1 %] - [% IF attachments || reporter || bugs_activity || flag_activity_requestee || - flags.setter || longdescs || profiles_activity %] + [% IF attachments || reporter || bugs_activity || flags.setter || longdescs || profiles_activity %]

    The following deletions are unsafe and would generate referential integrity inconsistencies!

    @@ -169,12 +166,6 @@ not showing up in [% terms.bug %] activity logs any more.
  • [% END %] - [% IF flag_activity_requestee %] -
  • - [% otheruser.login FILTER html %] has been asked to set flags. - The database refuses to delete a user named in the flag history. -
  • - [% END %] [% IF flags.setter %]
  • [% otheruser.login FILTER html %] has From 55a159ba8e9ba40f06b1124dd2316572aee7037a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 16:46:13 +0100 Subject: [PATCH 09/10] Bug 2085398 - Always clean up after the requestee deletion test and turn allowuserdeletion back off --- qa/t/4_test_user_delete_requestee.t | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t index f1635cc1a8..25b9f57b1f 100644 --- a/qa/t/4_test_user_delete_requestee.t +++ b/qa/t/4_test_user_delete_requestee.t @@ -16,6 +16,22 @@ 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, @@ -24,13 +40,12 @@ set_parameters($sel, # 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 $dbh = Bugzilla->dbh; my $login = 'requestee@selenium.bugzilla.org'; $dbh->do( "INSERT INTO profiles (login_name, realname, cryptpassword) VALUES (?, 'Requestee', '*')", undef, $login ); -my $user_id = $dbh->bz_last_key('profiles', 'userid'); +$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}); @@ -44,7 +59,7 @@ $dbh->do( VALUES (?, '?', ?, NOW(), NOW(), ?, ?)", undef, $type_id, $bug_id, $admin_id, $user_id ); -my $flag_id = $dbh->bz_last_key('flags', '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) @@ -77,8 +92,3 @@ is( 1, 'The request is cleared' ); - -$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $flag_id); -$dbh->do('DELETE FROM flags WHERE id = ?', undef, $flag_id); - -logout($sel); From b0f4c33b27e8e14a5f0a1b2e9c4886e049797e0f Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 17:13:21 +0100 Subject: [PATCH 10/10] Bug 2085398 - Make flag_activity.who NOT NULL in place on MySQL --- Bugzilla/Install/DB.pm | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 2bded965f7..f6fce34876 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4653,9 +4653,20 @@ 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'); + + # 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( 'flag_activity', 'who', @@ -4665,12 +4676,11 @@ sub _set_flag_activity_who_not_null { REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} } ); + $dbh->do('SET SESSION sql_mode = ?', undef, $sql_mode) if $mysql; - # With foreign_key_checks on, MySQL copies the whole table to add the key, - # blocking flag changes on live pods meanwhile. The values don't need the - # check: they were written under this key or copied from setter_id, which has - # its own. - my $mysql = $dbh->isa('Bugzilla::DB::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'));