From 37fc0decca4ae62612d7361e477d6bc7de7208f2 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 21 Jul 2023 14:32:11 +0200 Subject: [PATCH] Bug 33837: Remove datetime parameter Content-Type: text/plain; charset=utf-8 Signed-off-by: Marcel de Rooy [EDIT] Perltidied one line in cleanup_database.pl. --- Koha/Objects.pm | 10 +++---- misc/cronjobs/cleanup_database.pl | 6 +++++ t/db_dependent/Koha/Objects.t | 43 ++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 70f5be29b3..dc3d044bd3 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -235,7 +235,6 @@ sub update { my $filtered_objects = $objects->filter_by_last_update({ from => $date1, to => $date2, days|older_than => $days, min_days => $days, younger_than => $days, - datetime => 1, }); You should pass at least one of the parameters: from, to, days|older_than, @@ -243,7 +242,6 @@ min_days or younger_than. Make sure that they do not conflict with each other to get meaningful results. Note: from, to and min_days are inclusive! And by nature days|older_than and younger_than are exclusive. -The optional parameter datetime allows datetime comparison. The from and to parameters can be DateTime objects or date strings. @@ -257,20 +255,20 @@ sub filter_by_last_update { unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/; my $dtf = Koha::Database->new->schema->storage->datetime_parser; - my $method = $params->{datetime} ? 'format_datetime' : 'format_date'; foreach my $p ( qw/days older_than younger_than min_days/ ) { next if !exists $params->{$p}; my $dt = Koha::DateUtils::dt_from_string(); my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>'; - $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) ); + $dt->subtract( days => $params->{$p} )->truncate( to => 'day' ); + $conditions->{$operator} = $dtf->format_datetime( $dt ); } if ( exists $params->{from} ) { my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from}); - $conditions->{'>='} = $dtf->$method( $from ); + $conditions->{'>='} = $dtf->format_datetime( $from ); } if ( exists $params->{to} ) { my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to}); - $conditions->{'<='} = $dtf->$method( $to ); + $conditions->{'<='} = $dtf->format_datetime( $to ); } return $self->search( diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 60b3652012..6681a0b3bc 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -673,6 +673,12 @@ if ($pTransfers) { if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransactionsTo ) { print "Purging pseudonymized transactions\n" if $verbose; + if ($pPseudoTransactionsTo) { + $pPseudoTransactionsTo = dt_from_string($pPseudoTransactionsTo); + if ( $pPseudoTransactionsTo->hour == 0 && $pPseudoTransactionsTo->minute == 0 ) { + $pPseudoTransactionsTo->set( hour => 23, minute => 59, second => 59 ); + } + } my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update( { timestamp_column_name => 'datetime', diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index 09082df574..b7cda24f4e 100755 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -1188,22 +1188,35 @@ subtest "filter_by_last_update" => sub { ); }; - subtest 'Parameters datetime, older_than, younger_than' => sub { + subtest 'Parameters older_than, younger_than' => sub { my $now = dt_from_string(); - my $rs = Koha::Patrons->search({ borrowernumber => { -in => \@borrowernumbers } } ); - $rs->update({ updated_on => $now->clone->subtract( hours => 25 ) }); - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now })->count, 0, 'All updated yesterday' ); - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ) })->count, - 6, 'Yesterday, truncated from is inclusive' ); - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ), - datetime => 1 })->count, 0, 'Yesterday, not truncated, one hour too late' ); - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ), - datetime => 1 })->count, 6, 'Yesterday - 1h, not truncated, within time frame' ); - - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1 })->count, - 0, 'when using dates, we will find nothing' ); - is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1, datetime => 1 })->count, - 6, 'when using datetime, we will find them all' ); + my $rs = Koha::Patrons->search( { borrowernumber => { -in => \@borrowernumbers } } ); + $rs->update( { updated_on => $now->clone->subtract( hours => 25 ) } ); + is( + $rs->filter_by_last_update( { timestamp_column_name => 'updated_on', from => $now } )->count, 0, + 'All updated yesterday' + ); + is( + $rs->filter_by_last_update( + { timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 )->truncate( to => 'day' ) } + )->count, + 6, + 'Yesterday, truncated from is inclusive' + ); + is( + $rs->filter_by_last_update( + { timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ), } + )->count, + 0, + 'Yesterday, not truncated, one hour too late' + ); + is( + $rs->filter_by_last_update( + { timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ), } + )->count, + 6, + 'Yesterday - 1h, not truncated, within time frame' + ); }; $schema->storage->txn_rollback; -- 2.30.2