From 3c9ecda66a6d14b389463c7d293e14e1a21935a3 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 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 --- 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 c2d8e9832c..0ae8b06127 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; @@ -68,6 +69,8 @@ Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] -m --mail DAYS purge items from the mail queue that are older than DAYS days. Defaults to 30 days if no days specified. --merged purged completed entries from need_merge_authorities. + --messages DAYS purge entries from messages table older than DAYS days. + Defaults to 365 days if no days specified. --import DAYS purge records from import tables older than DAYS days. Defaults to 60 days if no days specified. --z3950 purge records from import tables that are the result @@ -137,6 +140,7 @@ my $pOldIssues; my $pOldReserves; my $pTransfers; my ( $pPseudoTransactions, $pPseudoTransactionsFrom, $pPseudoTransactionsTo ); +my $pMessages; GetOptions( 'h|help' => \$help, @@ -150,6 +154,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 +187,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 +220,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 +341,20 @@ if ($pLogs) { print "Done with purging action_logs.\n" if $verbose; } +if ($pMessages) { + print "Purging records from messages.\n" if $verbose; + $sth = $dbh->prepare( + q{ + DELETE FROM messages + WHERE message_date < date_sub(curdate(), INTERVAL ? DAY) + } + ); + if ( $confirm ) { + $sth->execute($pMessages) or die $dbh->errstr; + } + print "Done with purging messages.\n" if $verbose; +} + if ($fees_days) { print "Purging records from accountlines.\n" if $verbose; purge_zero_balance_fees( $fees_days ) if $confirm; -- 2.11.0