From 82a5c7965bc72e00a1fdc2323819d98f9fcb8c32 Mon Sep 17 00:00:00 2001
From: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Date: Tue, 14 Apr 2020 15:30:17 +0000
Subject: [PATCH] Bug 15563: Add label 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

Signed-off-by: Kelly McElligott <kelly@bywatersolutions.com>
---
 misc/cronjobs/cleanup_database.pl | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl
index a1a9cae442..abafdad3b5 100755
--- a/misc/cronjobs/cleanup_database.pl
+++ b/misc/cronjobs/cleanup_database.pl
@@ -90,6 +90,8 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
    --old-issues DAYS       Purge checkouts (old_issues) returned more than DAYS days ago.
    --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.
+
 USAGE
     exit $_[0];
 }
@@ -122,6 +124,7 @@ my $pDeletedPatrons;
 my $pOldIssues;
 my $pOldReserves;
 my $pTransfers;
+my $labels;
 
 GetOptions(
     'h|help'            => \$help,
@@ -152,6 +155,7 @@ GetOptions(
     'old-issues:i'      => \$pOldIssues,
     'old-reserves:i'    => \$pOldReserves,
     'transfers:i'    => \$pTransfers,
+    'labels:i'            => \$labels,
 ) || usage(1);
 
 # Use default values
@@ -192,6 +196,7 @@ unless ( $sessions
     || $pOldIssues
     || $pOldReserves
     || $pTransfers
+    || $labels
 ) {
     print "You did not specify any cleanup work for the script to do.\n\n";
     usage(1);
@@ -480,6 +485,18 @@ if ($pTransfers) {
     print "Done with purging transfers.\n" if $verbose;
 }
 
+if ($labels) {
+    print "Purging label items older than $labels days from batches.\n" if $verbose;
+    $sth = $dbh->prepare(
+        q{
+            DELETE FROM creator_batches
+            WHERE timestamp < date_sub(curdate(), interval ? day) AND creator='Labels'
+        }
+    );
+    $sth->execute($labels) or die $dbh->errstr;
+    print "Done with purging label items older than $labels days from batches.\n" if $verbose;
+}
+
 exit(0);
 
 sub RemoveOldSessions {
-- 
2.11.0