From 2e35954c9c37ecb300103107f0a7132f90400ae7 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 Signed-off-by: Signed-off-by: Sonia Bouis Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- Koha/Objects.pm | 13 +++++ misc/cronjobs/cleanup_database.pl | 79 +++++++++---------------------- 2 files changed, 36 insertions(+), 56 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 7824d83d27..b1877884a1 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -252,6 +252,19 @@ sub update { return $self->_resultset->update($fields); } +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 a1a9cae442..3b899e9150 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -42,6 +42,14 @@ 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::Old::Checkouts; +use Koha::Old::Holds; +use Koha::Old::Patrons; +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 checkouts 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.39.5