From 1d5833e84eabc23d7cd28232bf9a29f9dff0311e Mon Sep 17 00:00:00 2001
From: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Date: Tue, 14 Apr 2020 21:12:45 +0000
Subject: [PATCH] Bug 15563: Add label and card creator batch deletion to
 cleanup_database

To test:
Apply patch
1- create a label batch with multiple items
2- update creator_batches to change timestamp on some items in batch 1 to more than 2 days ago
3- create a second label batch with multiple items
4- update creator_batches to change timestamp on all items in batch 2 to more than 2 days ago
5- create a patron card batch with multiple patrons
6- update creator_batches to change timestamp on some items to more than 2 days ago
7- run cron with --labels 2
8- confirm all labels with timestamp more than 2 days ago were deleted
9- confirm newer labels were not deleted
10- confirm batch 2 no longer shows in label creator
11- confirm no patrons card batches were changed
12- run cron with --cards 2
13- confirm all cards batches were deleted

Signed-off-by: Kelly McElligott <kelly@bywatersolutions.com>

Signed-off-by: Deb Stephenson <dstephen@dubuque.lib.ia.us>
---
 misc/cronjobs/cleanup_database.pl | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl
index abafdad3b5..8a116ba9a4 100755
--- a/misc/cronjobs/cleanup_database.pl
+++ b/misc/cronjobs/cleanup_database.pl
@@ -91,6 +91,7 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
    --old-reserves DAYS     Purge reserves (old_reserves) more than DAYS old.
    --transfers DAYS        Purge transfers completed more than DAYS day ago.
    --labels DAYS           Purge label items added more than DAYS days ago.
+   --cards DAY             Purge card creator patrons added more than DAYS days ago.
 
 USAGE
     exit $_[0];
@@ -125,6 +126,7 @@ my $pOldIssues;
 my $pOldReserves;
 my $pTransfers;
 my $labels;
+my $cards;
 
 GetOptions(
     'h|help'            => \$help,
@@ -155,7 +157,8 @@ GetOptions(
     'old-issues:i'      => \$pOldIssues,
     'old-reserves:i'    => \$pOldReserves,
     'transfers:i'    => \$pTransfers,
-    'labels:i'            => \$labels,
+    'labels:i'          => \$labels,
+    'cards:i'           => \$cards,
 ) || usage(1);
 
 # Use default values
@@ -197,6 +200,7 @@ unless ( $sessions
     || $pOldReserves
     || $pTransfers
     || $labels
+    || $cards
 ) {
     print "You did not specify any cleanup work for the script to do.\n\n";
     usage(1);
@@ -497,6 +501,18 @@ if ($labels) {
     print "Done with purging label items older than $labels days from batches.\n" if $verbose;
 }
 
+if ($cards) {
+    print "Purging card creator patrons older than $cards days from batches.\n" if $verbose;
+    $sth = $dbh->prepare(
+        q{
+            DELETE FROM creator_batches
+            WHERE timestamp < date_sub(curdate(), interval ? day) AND creator='Patroncards'
+        }
+    );
+    $sth->execute($cards) or die $dbh->errstr;
+    print "Done with purging card creator patrons older than $cards days from batches.\n" if $verbose;
+}
+
 exit(0);
 
 sub RemoveOldSessions {
-- 
2.11.0