From b7a199ffe742545234ca97d5c2a199eed0ef44a1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 23 Apr 2021 12:36:54 +0000 Subject: [PATCH] Bug 15563: (follow-up) Combine methods, add count, obey confirm This patch moves the SQL to a subroutine and uses the Label delete method to remove the batches We now return a count and do not delete if --confirm flag is not passed Repeate previous test plan, confirm works as expected JD amended patch: perltidy Signed-off-by: Jonathan Druart --- misc/cronjobs/cleanup_database.pl | 67 ++++++++++++++++++------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 85967e7807..1897111496 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -625,38 +625,22 @@ if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransact if ($labels) { print "Purging item label batches last added to more than $labels days ago.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE from creator_batches - WHERE batch_id in - (SELECT batch_id - FROM (SELECT batch_id - FROM creator_batches - WHERE creator='labels' - GROUP BY batch_id - HAVING max(timestamp) < date_sub(curdate(),interval ? day)) a) - } - ); - $sth->execute($labels) or die $dbh->errstr; - print "Done with purging item label batches last added to more than $labels days ago.\n" if $verbose; + my $count = PurgeCreatorBatches($labels, 'labels', $confirm); + if ($verbose) { + say $confirm + ? sprintf "Done with purging %d item label batches last added to more than %d days ago.\n", $count, $labels + : sprintf "%d item label batches would have been purged.", $count; + } } if ($cards) { print "Purging card creator batches last added to more than $cards days ago.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE from creator_batches - WHERE batch_id in - (SELECT batch_id - FROM (SELECT batch_id - FROM creator_batches - WHERE creator='patroncards' - GROUP BY batch_id - HAVING max(timestamp) < date_sub(curdate(),interval ? day)) a); - } - ); - $sth->execute($cards) or die $dbh->errstr; - print "Done with purging card creator batches last added to more than $cards days ago.\n" if $verbose; + my $count = PurgeCreatorBatches($labels, 'patroncards', $confirm); + if ($verbose) { + say $confirm + ? sprintf "Done with purging %d card creator batches last added to more than %d days ago.\n", $count, $labels + : sprintf "%d card creator batches would have been purged.", $count; + } } exit(0); @@ -747,6 +731,33 @@ sub PurgeDebarments { return $count; } +sub PurgeCreatorBatches { + require C4::Labels::Batch; + my ( $days, $creator, $doit ) = @_; + my $count = 0; + $sth = $dbh->prepare( + q{ + SELECT batch_id, branch_code FROM creator_batches + WHERE batch_id in + (SELECT batch_id + FROM (SELECT batch_id + FROM creator_batches + WHERE creator=? + GROUP BY batch_id + HAVING max(timestamp) <= date_sub(curdate(),interval ? day)) a) + } + ); + $sth->execute( $creator, $days ) or die $dbh->errstr; + while ( my ( $batch_id, $branch_code ) = $sth->fetchrow_array ) { + C4::Labels::Batch::delete( + batch_id => $batch_id, + branch_code => $branch_code + ) if $doit; + $count++; + } + return $count; +} + sub DeleteExpiredSelfRegs { my $cnt= C4::Members::DeleteExpiredOpacRegistrations(); print "Removed $cnt expired self-registered borrowers\n" if $verbose; -- 2.39.5