Bug 34000: Add autoMemberNumValue system preference

Introduces a new system preference, autoMemberNumValue, which tracks
the highest auto-generated cardnumber ever issued. It is seeded from
the MAX cardnumber across both borrowers and deletedborrowers so
pre-upgrade deletions do not free numbers for re-issuance.

This preference is managed internally by Koha::Patron::fixup_cardnumber
and has no UI (no YAML entry).

Test plan:
1. Apply the atomicupdate:
   perl installer/data/mysql/updatedatabase.pl
2. Verify the preference exists:
   SELECT * FROM systempreferences WHERE variable = 'autoMemberNumValue';
3. Confirm the seed value matches:
   SELECT 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)
   );
4. Re-run the atomicupdate and confirm the already-exists warning.
5. Drop the syspref, re-apply the atomicupdate, confirm it seeds
   correctly on fresh installs via mandatory/sysprefs.sql.

Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Jacob O'Mara <Jacob.omara@openfifth.co.uk>
This commit is contained in:
Martin Renvoize 2026-04-21 10:56:58 +01:00 committed by Wainui Witika-Park
parent 7c33726cd3
commit 78e7d2aa8b
No known key found for this signature in database
GPG Key ID: 2D0A6AB5DAF1B78D
2 changed files with 49 additions and 1 deletions

View File

@ -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";
},
};

View File

@ -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'),