Bug 32745: DBRev 22.12.00.025
[koha.git] / installer / data / mysql / db_revs / 220600020.pl
1 use Modern::Perl;
2
3 return {
4     bug_number => "31086",
5     description => "Do not allow NULL values in branchcodes for reserves",
6     up => sub {
7         my ($args) = @_;
8         my ($dbh, $out) = @$args{qw(dbh out)};
9
10         my $sth = $dbh->prepare(q{
11             SELECT borrowernumber, biblionumber
12             FROM reserves
13                 WHERE branchcode IS NULL;
14         });
15         $sth->execute;
16         my $holds_no_branch = $sth->fetchall_arrayref( {} );
17
18         if ( scalar @{$holds_no_branch} > 0 ) {
19             say $out "Holds with no branchcode were found and will be updated to the first branch in the system";
20             foreach my $hnb ( @{$holds_no_branch} ) {
21                 say $out "Please review hold for borrowernumber "
22                   . $hnb->{borrowernumber}
23                   . " on biblionumber "
24                   . $hnb->{biblionumber}
25                   . " to correct pickup branch if necessary";
26             }
27         }
28
29         # Ensure we have no NULL's in the branchcode field
30         $dbh->do(q{
31             UPDATE reserves SET branchcode = ( SELECT branchcode FROM branches LIMIT 1) WHERE branchcode IS NULL;
32         });
33
34         # Remove FOREIGN KEY CONSTRAINT
35         if ( foreign_key_exists( 'reserves', 'reserves_ibfk_4' ) ) {
36             $dbh->do(q{
37                 ALTER TABLE reserves DROP FOREIGN KEY reserves_ibfk_4;
38             });
39         }
40
41         # Set the NOT NULL configuration
42         $dbh->do(q{
43             ALTER TABLE reserves
44             MODIFY COLUMN `branchcode` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'foreign key from the branches table defining which branch the patron wishes to pick this hold up at'
45         });
46         # Replace the constraint
47         $dbh->do(q{
48             ALTER TABLE reserves ADD CONSTRAINT reserves_ibfk_4 FOREIGN KEY (branchcode) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE;
49         });
50
51         # Print useful stuff here
52         say $out "Removed NULL option from branchcode for reserves";
53     },
54 };