Bug 40057: Fix db revision

Test plan (ktd):
1) reset_all
2) Run first part of the 241200017.pl atomicupdate manually
	koha-mysql kohadev
	ALTER TABLE illrequests DROP FOREIGN KEY illrequests_bnfk;
3) Create an ILL request linked to a borrower that doesn't exist (simulate old data):
	INSERT INTO illrequests (`backend`, `borrowernumber`,`branchcode` ) VALUES ('Standard', '99999', 'CPL');
4) Run the 2nd part of the atomicupdate manually:
   ALTER TABLE illrequests ADD CONSTRAINT illrequests_bnfk FOREIGN KEY(`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE;
5) Notice you get the error:
	ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (., CONSTRAINT  FOREIGN KEY () REFERENCES  () ON DELETE SET NULL ON UPDATE CASCADE)
6) Run the added query in the patch:
	UPDATE illrequests SET borrowernumber = NULL WHERE borrowernumber NOT IN (SELECT borrowernumber FROM borrowers);
7) Repeat step 4)
8) Notice there's no errors

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 94d5c37db8)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Pedro Amorim 2025-06-03 11:17:15 +00:00 committed by Paul Derscheid
parent 893bd2af71
commit 5eb5d33f2f
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
1 changed files with 16 additions and 0 deletions

View File

@ -8,6 +8,22 @@ return {
my ($args) = @_;
my ( $dbh, $out ) = @$args{qw(dbh out)};
my $sth = $dbh->prepare(
q{
SELECT COUNT(*) FROM illrequests WHERE borrowernumber NOT IN (SELECT borrowernumber FROM borrowers)
}
);
$sth->execute;
my $count = $sth->fetchrow_array;
if ($count) {
say_warning( $out, "Found $count illrequests with a borrowernumber that doesn't exist in borrowers" );
$dbh->do(
q{ UPDATE illrequests SET borrowernumber = NULL WHERE borrowernumber NOT IN (SELECT borrowernumber FROM borrowers) }
);
say_info( $out, "Set those borrowernumbers to NULL" );
}
$dbh->do(q{ ALTER TABLE illrequests DROP FOREIGN KEY illrequests_bnfk });
$dbh->do(
q{