diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 77cf05127a..863de24c15 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -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', @@ -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 => [ diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 71e5300dee..f6fce34876 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -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(); @@ -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, + 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))'); +} + +# 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 +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'); + + # 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', + { + 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; diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index d5b6a938c4..b88399879a 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,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, diff --git a/editusers.cgi b/editusers.cgi index 9446ff2922..7a20b72878 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,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); @@ -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({ + 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..25b9f57b1f --- /dev/null +++ b/qa/t/4_test_user_delete_requestee.t @@ -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' +); 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..df5cc00be9 --- /dev/null +++ b/t/flag-activity-start-rows.t @@ -0,0 +1,180 @@ +#!/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::Attachment; +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' +); + +# 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' +); + +# 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; diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index 2dc6109712..09a21f5d1e 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,42 @@ 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'); + +# 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;