From cba3c5051952767c3573f84a53fa98d224dff81b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 24 Sep 2019 18:05:38 -0300 Subject: [PATCH] Bug 24152: Add method Koha::Objects->filter_by_last_update --- Koha/Objects.pm | 13 +++++++ misc/cronjobs/cleanup_database.pl | 76 +++++++++++---------------------------- 2 files changed, 33 insertions(+), 56 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 31387e1442..c72f87df09 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -170,6 +170,19 @@ sub search_related { } } +sub filter_by_last_update { + my ( $self, $params ) = @_; + my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp'; + return $self->_resultset->search( + { + $timestamp_column_name => { + '<' => + [ \'DATE_SUB(CURDATE(), INTERVAL ? DAY)', $params->{days} ] + } + } + ); +} + =head3 single my $object = Koha::Objects->search({}, { rows => 1 })->single diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 73621ce260..ef49ee4cda 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -42,6 +42,11 @@ use Getopt::Long; use C4::Log; use C4::Accounts; use Koha::UploadedFiles; +use Koha::Old::Biblios; +use Koha::Old::Items; +use Koha::Old::Biblioitems; +use Koha::Item::Transfers; + sub usage { print STDERR <prepare( - q{ - DELETE FROM statistics - WHERE datetime < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - $sth->execute($pStatistics); + Koha::Statistics->filter_by_last_update( + { timestamp_column_name => 'datetime', days => $pStatistics } )->delete; print "Done with purging statistics.\n" if $verbose; } if ($pDeletedCatalog) { print "Purging deleted catalog older than $pDeletedCatalog days.\n" if $verbose; - my $sth1 = $dbh->prepare( - q{ - DELETE FROM deleteditems - WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - my $sth2 = $dbh->prepare( - q{ - DELETE FROM deletedbiblioitems - WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - my $sth3 = $dbh->prepare( - q{ - DELETE FROM deletedbiblio - WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - # deletedbiblio_metadata is managed by FK with deletedbiblio - $sth1->execute($pDeletedCatalog); - $sth2->execute($pDeletedCatalog); - $sth3->execute($pDeletedCatalog); + Koha::Old::Items ->filter_by_last_update( { days => $pDeletedCatalog } )->delete; + Koha::Old::Biblioitems->filter_by_last_update( { days => $pDeletedCatalog } )->delete; + Koha::Old::Biblios ->filter_by_last_update( { days => $pDeletedCatalog } )->delete; print "Done with purging deleted catalog.\n" if $verbose; } if ($pDeletedPatrons) { print "Purging deleted patrons older than $pDeletedPatrons days.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE FROM deletedborrowers - WHERE updated_on < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - $sth->execute($pDeletedPatrons); + Koha::Old::Patrons->filter_by_last_update( + { timestamp_column_name => 'updated_on', days => $pDeletedPatrons } ) + ->delete; print "Done with purging deleted patrons.\n" if $verbose; } if ($pOldIssues) { print "Purging old issues older than $pOldIssues days.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE FROM old_issues - WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - $sth->execute($pOldIssues); + Koha::Old::Checkouts->filter_by_last_update( { days => $pOldIssues } )->delete; print "Done with purging old issues.\n" if $verbose; } if ($pOldReserves) { print "Purging old reserves older than $pOldReserves days.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE FROM old_reserves - WHERE timestamp < DATE_SUB(CURDATE(), INTERVAL ? DAY) - } - ); - $sth->execute($pOldReserves); + Koha::Old::Holds->filter_by_last_update( { days => $pOldReserves } )->delete; print "Done with purging old reserves.\n" if $verbose; } if ($pTransfers) { print "Purging arrived item transfers older than $pTransfers days.\n" if $verbose; - $sth = $dbh->prepare( - q{ - DELETE FROM branchtransfers - WHERE datearrived < DATE_SUB(CURDATE(), INTERVAL ? DAY) + Koha::Item::Transfers->filter_by_last_update( + { + timestamp_column_name => 'datearrived', + days => $pTransfers, } - ); - $sth->execute($pTransfers); + )->delete; print "Done with purging transfers.\n" if $verbose; } -- 2.11.0