Bug 31086: Notify of problematic holds during upgrade
[koha.git] / installer / data / mysql / atomicupdate / bug_31086_do_not_allow_null_branchcode_in_reserves.pl
1 use Modern::Perl;
2 use Koha::Holds;
3
4 return {
5     bug_number => "31086",
6     description => "Do not allow null values in branchcodes for reserves",
7     up => sub {
8         my ($args) = @_;
9         my ($dbh, $out) = @$args{qw(dbh out)};
10
11         my $holds_no_branch = Koha::Holds->search({ branchcode => undef });
12         if( $holds_no_branch->count > 0 ){
13             say $out "Holds with no branchcode were found and will be updated to the first branch in the system";
14             while ( my $hnb = $holds_no_branch->next ){
15                 say $out "Please review hold for borrowernumber " . $hnb->borrowernumber . " on biblionumber " . $hnb->biblionumber . " to correct pickup branch if necessary";
16             }
17         }
18
19         # Ensure we have no NULL's in the branchcode field
20         $dbh->do(q{
21             UPDATE reserves SET branchcode = ( SELECT branchcode FROM branches LIMIT 1) WHERE branchcode IS NULL;
22         });
23
24         # Set the NOT NULL configuration
25         $dbh->do(q{
26             ALTER TABLE reserves
27             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'
28         });
29
30         # Print useful stuff here
31         say $out "Removed NULL option from branchcode for reserves";
32     },
33 };