Bug 37000: (Bug 36120 follow-up) Improve reliability of database update

This patch adds a series of fallthroughs to ensure pickup_library_id is
always set prior to adding the NOT NULL constraint.

We initially only looked at items.homebranch but as that's a nullable
field itself, we now look at items.holdingbranch before finally
defaulting to the first available branch in the branches table in the
worst case.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Martin Renvoize 2024-06-04 11:58:54 +01:00
parent b9ca6b4a36
commit 8cedcfd5c8
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F

View file

@ -1,4 +1,5 @@
use Modern::Perl; use Modern::Perl;
use Koha::Installer::Output qw(say_warning say_failure say_success say_info);
return { return {
bug_number => "36120", bug_number => "36120",
@ -14,24 +15,49 @@ return {
ADD COLUMN `pickup_library_id` varchar(10) DEFAULT NULL COMMENT 'Identifier for booking pickup library' AFTER `item_id`, ADD COLUMN `pickup_library_id` varchar(10) DEFAULT NULL COMMENT 'Identifier for booking pickup library' AFTER `item_id`,
ADD CONSTRAINT `bookings_ibfk_4` FOREIGN KEY (`pickup_library_id`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE ADD CONSTRAINT `bookings_ibfk_4` FOREIGN KEY (`pickup_library_id`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
} }
); ) == 1
&& say_success( $out, "Added column 'bookings.pickup_library_id'" );
say $out "Added column 'bookings.pickup_library_id'"; my $updated = $dbh->do(
$dbh->do(
q{UPDATE bookings JOIN items ON bookings.item_id = items.itemnumber SET bookings.pickup_library_id = items.homebranch } q{UPDATE bookings JOIN items ON bookings.item_id = items.itemnumber SET bookings.pickup_library_id = items.homebranch }
); );
say $out "Set existing bookings pickup location to item homebranch"; if ( $updated != '0E0' ) {
say_success( $out, "Set $updated existing bookings pickup location to item homebranch" );
} else {
say_info( $out, "No bookings found that need updating to include a pickup library" );
}
$updated = $dbh->do(
q{UPDATE bookings JOIN items ON bookings.item_id = items.itemnumber SET pickup_library_id = items.holdingbranch WHERE pickup_library_id IS NULL}
);
if ( $updated != '0E0' ) {
say_success(
$out,
"Set $updated existing bookings pickup location to item holdingbranch where items.homebranch was null"
);
}
my ($firstBranch) = $dbh->selectrow_array(q{SELECT branchcode FROM branches LIMIT 1});
$updated = $dbh->do(
q{UPDATE bookings SET pickup_library_id = ? WHERE pickup_library_id IS NULL}, undef,
$firstBranch
);
if ( $updated != '0E0' ) {
say_warning(
$out,
"Some $updated bookings still had a null pickup location value so we have set them to $firstBranch"
);
}
$dbh->do( $dbh->do(
q{ q{
ALTER TABLE bookings ALTER TABLE bookings
MODIFY pickup_library_id varchar(10) NOT NULL COMMENT 'Identifier for booking pickup library' MODIFY pickup_library_id varchar(10) NOT NULL COMMENT 'Identifier for booking pickup library'
} }
); ) == 1 && say_success( $out, "Updated column 'bookings.pickup_library_id' to NOT NULL" );
say $out "Set pickup_library_id to NOT NULL";
} }
}, },
}; };