From 951587562601171c2946b50d2d1cbee2915613a2 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 21 Jul 2021 19:15:27 +0000 Subject: [PATCH] Bug 18631: Add option to specify logs to preserve or delete in cleanup_database.pl This adds two new parameters: --log_module --preserve_log These can be repeated to include or exclude specific modules from the cleanup To test: 0 - Apply patch 1 - Enable cataloging log and borrowers log 2 - Make some changes to borrowers and records 3 - run cleanup databse with --logs 0 --preserve_log=MEMBERS --preserve_log=CATALOGUING 4 - nothing is removed 5 - run cleanup databse with --logs 0 --log_module=MEMBERS 6 - the borrower logs are removed, the record changes remain 7 - run cleanup database with --logs 0 8 - record changes are removed (all other logs too) Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Rebecca Coert Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- misc/cronjobs/cleanup_database.pl | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 2adccfda04..08ca37d3fa 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -80,6 +80,8 @@ Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] amountoutstanding is 0 or NULL. In the case of --fees, DAYS must be greater than or equal to 1. + --log_modules Specify which action log modules to trim. Repeatable. + --preserve_logs Specify which action logs to exclude. Repeatable. --logs DAYS purge entries from action_logs older than DAYS days. Defaults to 180 days if no days specified. --searchhistory DAYS purge entries from search_history older than DAYS days. @@ -150,6 +152,8 @@ my $pMessages; my $lock_days = C4::Context->preference('LockExpiredDelay'); my $labels; my $cards; +my @log_modules; +my @preserve_logs; GetOptions( 'h|help' => \$help, @@ -163,6 +167,8 @@ GetOptions( 'import:i' => \$pImport, 'z3950' => \$pZ3950, 'logs:i' => \$pLogs, + 'log_module:s' => \@log_modules, + 'preserve_log:s' => \@preserve_logs, 'messages:i' => \$pMessages, 'fees:i' => \$fees_days, 'searchhistory:i' => \$pSearchhistory, @@ -343,14 +349,22 @@ if ($pZ3950) { if ($pLogs) { print "Purging records from action_logs.\n" if $verbose; - $sth = $dbh->prepare( - q{ + my $log_query = q{ DELETE FROM action_logs WHERE timestamp < date_sub(curdate(), INTERVAL ? DAY) - } - ); + }; + my @query_params = (); + if( @preserve_logs ){ + $log_query .= " AND module NOT IN (" . join(',',('?') x @preserve_logs ) . ")"; + push @query_params, @preserve_logs; + } + if( @log_modules ){ + $log_query .= " AND module IN (" . join(',',('?') x @log_modules ) . ")"; + push @query_params, @log_modules; + } + $sth = $dbh->prepare( $log_query ); if ( $confirm ) { - $sth->execute($pLogs) or die $dbh->errstr; + $sth->execute($pLogs, @query_params) or die $dbh->errstr; } print "Done with purging action_logs.\n" if $verbose; } -- 2.39.5