From fd546c908bb350c59a85ea6b774e8705a730d071 Mon Sep 17 00:00:00 2001 From: Didier Gautheron Date: Thu, 30 Jan 2020 10:35:31 +0100 Subject: [PATCH] Bug 24541: Purge old messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit misc/cronjobs/cleanup_database.pl provides some database cleanup. This patch adds cleanup for messages table. Test plan : - Count messages : select count(*),date(message_date) from messages group by date(message_date); - Run cleanup : misc/cronjobs/cleanup_database.pl -v --confirm --messages 365 - Recount messages Signed-off-by: Séverine QUEUNE --- misc/cronjobs/cleanup_database.pl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index c2d8e98..062efe6 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -23,6 +23,7 @@ use constant DEFAULT_ZEBRAQ_PURGEDAYS => 30; use constant DEFAULT_MAIL_PURGEDAYS => 30; use constant DEFAULT_IMPORT_PURGEDAYS => 60; use constant DEFAULT_LOGS_PURGEDAYS => 180; +use constant DEFAULT_MESSAGES_PURGEDAYS => 365; use constant DEFAULT_SEARCHHISTORY_PURGEDAYS => 30; use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS => 14; use constant DEFAULT_DEBARMENTS_PURGEDAYS => 30; @@ -50,6 +51,7 @@ use Koha::Old::Holds; use Koha::Old::Patrons; use Koha::Item::Transfers; use Koha::PseudonymizedTransactions; +use Koha::Patron::Messages; sub usage { print STDERR < \$help, @@ -150,6 +155,7 @@ GetOptions( 'import:i' => \$pImport, 'z3950' => \$pZ3950, 'logs:i' => \$pLogs, + 'messages:i' => \$pMessages, 'fees:i' => \$fees_days, 'searchhistory:i' => \$pSearchhistory, 'list-invites:i' => \$pListShareInvites, @@ -182,6 +188,7 @@ $mail = DEFAULT_MAIL_PURGEDAYS if defined($mail) $pSearchhistory = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhistory) && $pSearchhistory == 0; $pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0; $pDebarments = DEFAULT_DEBARMENTS_PURGEDAYS if defined($pDebarments) && $pDebarments == 0; +$pMessages = DEFAULT_MESSAGES_PURGEDAYS if defined($pMessages) && $pMessages == 0; if ($help) { usage(0); @@ -214,6 +221,7 @@ unless ( $sessions || defined $pPseudoTransactions || $pPseudoTransactionsFrom || $pPseudoTransactionsTo + || $pMessages ) { print "You did not specify any cleanup work for the script to do.\n\n"; usage(1); @@ -334,6 +342,19 @@ if ($pLogs) { print "Done with purging action_logs.\n" if $verbose; } +if ($pMessages) { + print "Purging messages older than $pMessages days.\n" if $verbose; + my $messages = Koha::Patron::Messages->filter_by_last_update( + { timestamp_column_name => 'message_date', days => $pMessages } ); + my $count = $messages->count; + $messages->delete if $confirm; + if ( $verbose ) { + say $confirm + ? sprintf( "Done with purging %d messages", $count ) + : sprintf( "%d messages would have been removed", $count ); + } +} + if ($fees_days) { print "Purging records from accountlines.\n" if $verbose; purge_zero_balance_fees( $fees_days ) if $confirm; -- 2.1.4