diff --git a/installer/data/mysql/atomicupdate/bug_34000.pl b/installer/data/mysql/atomicupdate/bug_34000.pl new file mode 100755 index 0000000000..74e45c52e4 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_34000.pl @@ -0,0 +1,47 @@ +use Modern::Perl; + +return { + bug_number => "34000", + description => "Prevent re-issuance of auto-generated cardnumbers", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + my $exists = $dbh->selectrow_array(q{SELECT 1 FROM systempreferences WHERE variable = 'autoMemberNumValue'}); + + if ($exists) { + say $out "System preference 'autoMemberNumValue' already exists, skipping"; + return; + } + + # Seed from the highest numeric cardnumber ever issued, considering both live + # and deleted patrons so pre-upgrade deletions don't free up their numbers. + my ($seed) = $dbh->selectrow_array( + q{ + SELECT COALESCE( + GREATEST( + COALESCE( + (SELECT MAX(CAST(cardnumber AS SIGNED)) FROM borrowers + WHERE cardnumber REGEXP '^-?[0-9]+$'), + 0 + ), + COALESCE( + (SELECT MAX(CAST(cardnumber AS SIGNED)) FROM deletedborrowers + WHERE cardnumber REGEXP '^-?[0-9]+$'), + 0 + ) + ), + 0 + ) + } + ); + $seed //= 0; + + $dbh->do( + q{INSERT IGNORE INTO systempreferences (variable, value) VALUES ('autoMemberNumValue', ?)}, + undef, $seed + ); + + say $out "Added new system preference 'autoMemberNumValue' seeded to $seed"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index c3fea41911..dbf1db276b 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -86,7 +86,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('AutomaticItemReturn','1',NULL,'If ON, Koha will automatically set up a transfer of this item to its homebranch','YesNo'), ('AutoLinkBiblios','0',NULL,'If enabled, link biblio to authorities on creation and edit','YesNo'), ('AutomaticConfirmTransfer','0',NULL,'Defines whether transfers should be automatically confirmed at checkin if modal dismissed','YesNo'), -('autoMemberNum','0','','If ON, patron number is auto-calculated','YesNo'), +('autoMemberNum','0',NULL,'If ON, patron number is auto-calculated','YesNo'), +('autoMemberNumValue','0',NULL,'Stores the last auto-generated cardnumber to prevent re-issuance','Integer'), ('AutoRemoveOverduesRestrictions','0',NULL,'Defines whether an OVERDUES debarment should be lifted automatically if all overdue items are returned by the patron.','YesNo'), ('AutoRenewalNotices','cron','cron|preferences|never','How should Koha determine whether to end autorenewal notices','Choice'), ('AutoResumeSuspendedHolds','1',NULL,'Allow suspended holds to be automatically resumed by a set date.','YesNo'),