From e18258255dc8b6598693353124308d1ba5a4b21d Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Mon, 19 Oct 2015 14:42:15 +0300 Subject: [PATCH] Bug 13409 - Add ability to purge old special holidays using cleanup_database.pl v2 We are having a massive amount of special holidays in the calendar and those are cluttering the calendar views. A simple solution is to remove stale special holidays from the koha.special_holidays-table. Test Plan: 1) Create some special holidays in the past 2) Use the new --calendar DAYS option to test deleting special holidays 3) Verify only special holidays past the given amount of days were deleted P.S. No test included! Do not push without proper integration tests, preferably with Cucumber + PageObject and some kind of a cronjob-runner. --- Koha/Calendar.pm | 34 ++++++++++++++++++++++++++++++++++ misc/cronjobs/cleanup_database.pl | 17 ++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index a4eb3d1..25c83c5 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -88,6 +88,40 @@ sub exception_holidays { return $exception_holidays; } +=head cleanupCalendar +@STATIC + + Koha::Calendar::cleanupCalendar( {daysOld => 362} ); + +Removes all the special holidays which are more than . +@PARAM1 HASHRef, +Defaults: + daysOld => 362, #How many days of special holidays we preserve to the past? +@RETURNS 1 if operation succeeded +@THROWS die-signal if there was an error with the DB-query. + +Pending tests, shame on me! +=cut + +sub cleanupCalendar { + my ($params) = @_; + + my $dbh = C4::Context->dbh; #I know I shouldn't use this, but the Koha::Calendar-module doesn't use DBIx either. + + my $daysOld = $params->{daysOld} || 362; + + my $cleanup_sth = $dbh->prepare( + "DELETE FROM special_holidays WHERE TO_DAYS(DATE(CONCAT(year,'-',month,'-',day))) < (TO_DAYS(NOW()) - ?);" + ); + $cleanup_sth->execute( $daysOld ); + $exception_holidays = undef; #invalidate cache + + if ($cleanup_sth->errstr) { + die $cleanup_sth->errstr; + } + return 1; +} + sub single_holidays { my ( $self ) = @_; my $dbh = C4::Context->dbh; diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index fa27e9b..ec0aa71 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -59,6 +59,8 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu --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. + --calendar DAYS purge rows from koha.special_holidays-table older than DAYS days. + Defaults to 362 days if no days specified. --z3950 purge records from import tables that are the result of Z39.50 searches --logs DAYS purge entries from action_logs older than DAYS days. @@ -74,7 +76,7 @@ USAGE my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged, $pImport, - $pLogs, $pSearchhistory, $pZ3950, + $pLogs, $pSearchhistory, $pZ3950, $calendar, $pListShareInvites, ); @@ -87,6 +89,7 @@ GetOptions( 'zebraqueue:i' => \$zebraqueue_days, 'merged' => \$purge_merged, 'import:i' => \$pImport, + 'calendar:i' => \$calendar, 'z3950' => \$pZ3950, 'logs:i' => \$pLogs, 'searchhistory:i' => \$pSearchhistory, @@ -112,6 +115,7 @@ unless ( $sessions || $mail || $purge_merged || $pImport + || $calendar || $pLogs || $pSearchhistory || $pZ3950 @@ -188,6 +192,17 @@ if($purge_merged) { print "Done with purging need_merge_authorities.\n" if $verbose; } +if ($calendar) { + my $daysOld = $calendar || 362; + print "Purging koha.special_holidays from '$calendar' days ago.\n" if $verbose; + eval { + Koha::Calendar::cleanupCalendar(); + }; + if ($@) { + warn DateTime->now()->iso8601()." - Exception while purging the Calendar:\n".$@; + } +} + if($pImport) { print "Purging records from import tables.\n" if $verbose; PurgeImportTables(); -- 1.7.9.5