From 03f7f3306267629848e64bad84eda4d30f7589d7 Mon Sep 17 00:00:00 2001
From: Charles Farmer <charles.farmer@inLibro.com>
Date: Mon, 13 Aug 2018 10:54:09 -0400
Subject: [PATCH] Bug 21214: cleanup_database.pl --keep-lc to dodge deletion in
 message_queue based on letter_code

Librarians sometime worry if a certain email was sent or not to a patron (PREDUES, ODUES, etc.), and iirc, there are no way to have that information once the cleanup_database.pl has run, unless we dig back an archive of their database.
This patch would let an admin choose which letters to keep in the message_queue, based on its letter_code, by passing a comma-separated list to the script.

TEST PLAN:
1) Either you have already have data, or you'll want to add some new test lines in your message_queue table
    . INSERT INTO message_queue(borrowernumber,letter_code,message_transport_type,status,time_queued)
      VALUES(1,<YOUR CODE OF CHOICE>,'email','sent','2018-01-01 00:00:42');
2) Make a backup of this table
3) Run the script patchless with the --mail 1 option
    3.1) Messages older than 1 day should have been deleted
4) Put the data back in
5) Apply patch
6) Choose a letter_code you'd like to keep, and run the script with --mail 1 --keep-lc <CODE1>[,<CODE2>]
7) Messages older than 1 day that don't have the code <CODE1> [or <CODE2>] should've been deleted
---
 misc/cronjobs/cleanup_database.pl | 33 +++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl
index c2d8e9832c..8e29581f3e 100755
--- a/misc/cronjobs/cleanup_database.pl
+++ b/misc/cronjobs/cleanup_database.pl
@@ -67,6 +67,9 @@ Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose]
                       Defaults to 30 days if no days specified.
    -m --mail DAYS     purge items from the mail queue that are older than DAYS days.
                       Defaults to 30 days if no days specified.
+   --keep-lc LC[,LC]* only used in conjunction with -m/--mail.
+                      Keeps certain emails in the message_queue based on a
+                      comma-separated list of LETTER_CODES
    --merged           purged completed entries from need_merge_authorities.
    --import DAYS      purge records from import tables older than DAYS days.
                       Defaults to 60 days if no days specified.
@@ -114,6 +117,7 @@ my $sess_days;
 my $verbose;
 my $zebraqueue_days;
 my $mail;
+my $letter_codes;
 my $purge_merged;
 my $pImport;
 my $pLogs;
@@ -145,6 +149,7 @@ GetOptions(
     'sessdays:i'        => \$sess_days,
     'v|verbose'         => \$verbose,
     'm|mail:i'          => \$mail,
+    'keep-lc:s'         => \$letter_codes,
     'zebraqueue:i'      => \$zebraqueue_days,
     'merged'            => \$purge_merged,
     'import:i'          => \$pImport,
@@ -224,9 +229,18 @@ if ($pDebarments && $allDebarments) {
     usage(1);
 }
 
+<<<<<<< HEAD
 say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
 
 cronlogaction() unless $confirm;
+=======
+if (!$mail && $letter_codes) {
+    print "You should not specify --keep-lc if you're not cleaning emails.\n\n";
+    usage(1);
+}
+
+cronlogaction();
+>>>>>>> Bug 21214: cleanup_database.pl --keep-lc to dodge deletion in message_queue based on letter_code
 
 my $dbh = C4::Context->dbh();
 my $sth;
@@ -283,6 +297,7 @@ if ($zebraqueue_days) {
 if ($mail) {
     my $count = 0;
     print "Mail queue purge triggered for $mail days.\n" if $verbose;
+<<<<<<< HEAD
     $sth = $dbh->prepare(
         q{
             DELETE FROM message_queue
@@ -297,6 +312,24 @@ if ($mail) {
         say $confirm ? "$count messages were deleted from the mail queue." : "Message from message_queue would have been deleted";
         say "Done with message_queue purge.";
     }
+=======
+    print "Not deleting emails of type $letter_codes.\n" if ($verbose && $letter_codes);
+    my $query = q{
+        DELETE FROM message_queue
+        WHERE time_queued < date_sub(curdate(), INTERVAL ? DAY)
+          AND letter_code NOT IN ( ? )
+    };
+    my $quoted_codes = '';
+    if ($letter_codes) {
+        my @codes = map { "'$_'" } split(',', $letter_codes);
+        my $quoted_codes = join(',', @codes);
+    }
+    $sth = $dbh->prepare($query);
+    $sth->execute($mail, $quoted_codes) or die $dbh->errstr;
+    $count = $sth->rows;
+    $sth->finish;
+    print "$count messages were deleted from the mail queue.\nDone with message_queue purge.\n" if $verbose;
+>>>>>>> Bug 21214: cleanup_database.pl --keep-lc to dodge deletion in message_queue based on letter_code
 }
 
 if ($purge_merged) {
-- 
2.17.1