View | Details | Raw Unified | Return to bug 24152
Collapse All | Expand All

(-)a/Koha/Objects.pm (-4 / +32 lines)
Lines 23-28 use Carp; Link Here
23
use List::MoreUtils qw( none );
23
use List::MoreUtils qw( none );
24
24
25
use Koha::Database;
25
use Koha::Database;
26
use Koha::DateUtils qw( dt_from_string );
26
27
27
=head1 NAME
28
=head1 NAME
28
29
Lines 171-185 sub search_related { Link Here
171
    }
172
    }
172
}
173
}
173
174
175
=head3 filter_by_last_update
176
177
my $filtered_objects = $objects->filter_by_last_update
178
179
days exclusive
180
from inclusive
181
to   inclusive
182
183
=cut
184
174
sub filter_by_last_update {
185
sub filter_by_last_update {
175
    my ( $self, $params ) = @_;
186
    my ( $self, $params ) = @_;
176
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
187
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
188
    my $conditions;
189
    Koha::Exceptions::MissingParameter->throw(
190
        "Missing mandatory parameter: days or from or to")
191
      unless exists $params->{days}
192
          or exists $params->{from}
193
          or exists $params->{to};
194
195
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
196
    if ( exists $params->{days} ) {
197
        $conditions->{'<'} = $dtf->format_date( dt_from_string->subtract( days => $params->{days} ) );
198
    }
199
    if ( exists $params->{from} ) {
200
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
201
        $conditions->{'>='} = $dtf->format_date( $from );
202
    }
203
    if ( exists $params->{to} ) {
204
        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
205
        $conditions->{'<='} = $dtf->format_date( $to );
206
    }
207
177
    return $self->_resultset->search(
208
    return $self->_resultset->search(
178
        {
209
        {
179
            $timestamp_column_name => {
210
            $timestamp_column_name => $conditions
180
                '<' =>
181
                  [ \'DATE_SUB(CURDATE(), INTERVAL ? DAY)', $params->{days} ]
182
            }
183
        }
211
        }
184
    );
212
    );
185
}
213
}
(-)a/misc/cronjobs/cleanup_database.pl (-2 / +23 lines)
Lines 49-55 use Koha::Old::Checkouts; Link Here
49
use Koha::Old::Holds;
49
use Koha::Old::Holds;
50
use Koha::Old::Patrons;
50
use Koha::Old::Patrons;
51
use Koha::Item::Transfers;
51
use Koha::Item::Transfers;
52
52
use Koha::PseudonymizedTransactions;
53
53
54
sub usage {
54
sub usage {
55
    print STDERR <<USAGE;
55
    print STDERR <<USAGE;
Lines 99-104 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu Link Here
99
   --old-issues DAYS       Purge checkouts (old_issues) returned more than DAYS days ago.
99
   --old-issues DAYS       Purge checkouts (old_issues) returned more than DAYS days ago.
100
   --old-reserves DAYS     Purge reserves (old_reserves) more than DAYS old.
100
   --old-reserves DAYS     Purge reserves (old_reserves) more than DAYS old.
101
   --transfers DAYS        Purge transfers completed more than DAYS day ago.
101
   --transfers DAYS        Purge transfers completed more than DAYS day ago.
102
   --pseudo-transactions DAYS   Purge the pseudonymized transactions that have been originally created more than DAYS days ago
103
                                DAYS is optional and can be replaced by:
104
                                    --pseudo-transactions-from YYYY-MM-DD and/or --pseudo-transactions-to YYYY-MM-DD
102
USAGE
105
USAGE
103
    exit $_[0];
106
    exit $_[0];
104
}
107
}
Lines 131-136 my $pDeletedPatrons; Link Here
131
my $pOldIssues;
134
my $pOldIssues;
132
my $pOldReserves;
135
my $pOldReserves;
133
my $pTransfers;
136
my $pTransfers;
137
my ( $pPseudoTransactions, $pPseudoTransactionsFrom, $pPseudoTransactionsTo );
134
138
135
GetOptions(
139
GetOptions(
136
    'h|help'            => \$help,
140
    'h|help'            => \$help,
Lines 160-166 GetOptions( Link Here
160
    'deleted-patrons:i' => \$pDeletedPatrons,
164
    'deleted-patrons:i' => \$pDeletedPatrons,
161
    'old-issues:i'      => \$pOldIssues,
165
    'old-issues:i'      => \$pOldIssues,
162
    'old-reserves:i'    => \$pOldReserves,
166
    'old-reserves:i'    => \$pOldReserves,
163
    'transfers:i'    => \$pTransfers,
167
    'transfers:i'       => \$pTransfers,
168
    'pseudo-transactions:i'      => \$pPseudoTransactions,
169
    'pseudo-transactions-from:s' => \$pPseudoTransactionsFrom,
170
    'pseudo-transactions-to:s'   => \$pPseudoTransactionsTo,
164
) || usage(1);
171
) || usage(1);
165
172
166
# Use default values
173
# Use default values
Lines 201-206 unless ( $sessions Link Here
201
    || $pOldIssues
208
    || $pOldIssues
202
    || $pOldReserves
209
    || $pOldReserves
203
    || $pTransfers
210
    || $pTransfers
211
    || defined $pPseudoTransactions
204
) {
212
) {
205
    print "You did not specify any cleanup work for the script to do.\n\n";
213
    print "You did not specify any cleanup work for the script to do.\n\n";
206
    usage(1);
214
    usage(1);
Lines 448-453 if ($pTransfers) { Link Here
448
    print "Done with purging transfers.\n" if $verbose;
456
    print "Done with purging transfers.\n" if $verbose;
449
}
457
}
450
458
459
if (defined $pPseudoTransactions) {
460
    print "Purging pseudonymized transactions older than $pPseudoTransactions days.\n" if $verbose;
461
    Koha::PseudonymizedTransactions->filter_by_last_update(
462
        {
463
            timestamp_column_name => 'datetime',
464
            ( $pPseudoTransactions     ? ( days => $pPseudoTransactions     ) : () ),
465
            ( $pPseudoTransactionsFrom ? ( from => $pPseudoTransactionsFrom ) : () ),
466
            ( $pPseudoTransactionsTo   ? ( to   => $pPseudoTransactionsTo   ) : () ),
467
        }
468
    )->delete;
469
    print "Done with purging pseudonymized transactions.\n" if $verbose;
470
}
471
451
exit(0);
472
exit(0);
452
473
453
sub RemoveOldSessions {
474
sub RemoveOldSessions {
(-)a/t/db_dependent/Koha/Objects.t (-3 / +94 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Copyright 2015 Koha Development team
3
# Copyright 2019 Koha Development team
4
#
4
#
5
# This file is part of Koha
5
# This file is part of Koha
6
#
6
#
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 21;
22
use Test::More tests => 22;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 30-37 use Koha::Patron::Category; Link Here
30
use Koha::Patron::Categories;
30
use Koha::Patron::Categories;
31
use Koha::Patrons;
31
use Koha::Patrons;
32
use Koha::Database;
32
use Koha::Database;
33
use Koha::DateUtils qw( dt_from_string );
33
34
34
use t::lib::TestBuilder;
35
use t::lib::TestBuilder;
36
use t::lib::Mocks;
35
37
36
use Try::Tiny;
38
use Try::Tiny;
37
39
Lines 759-764 subtest "attributes_from_api() tests" => sub { Link Here
759
        $city->attributes_from_api($api_attributes)
761
        $city->attributes_from_api($api_attributes)
760
    );
762
    );
761
763
764
    $schema->storage->txn_rollback;
765
766
};
767
768
subtest "filter_by_last_update" => sub {
769
770
    $schema->storage->txn_begin;
771
772
    my $now = dt_from_string->truncate( to => 'day' );
773
    my @borrowernumbers;
774
    # Building 6 patrons that have been created today, yesterday, ... 1 per day
775
    for my $i ( 0 .. 5 ) {
776
        push @borrowernumbers,
777
          $builder->build_object(
778
            {
779
                class => 'Koha::Patrons',
780
                value => { updated_on => $now->clone->subtract( days => $i ) }
781
            }
782
          )->borrowernumber;
783
    }
784
785
    my $patrons = Koha::Patrons->search(
786
        { borrowernumber => { -in => \@borrowernumbers } } );
787
788
    try {
789
        $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
790
          ->count;
791
    }
792
    catch {
793
        ok(
794
            $_->isa('Koha::Exceptions::MissingParameter'),
795
            'Should raise an exception if no parameter given'
796
        );
797
    };
798
799
    my $count = $patrons->filter_by_last_update(
800
        { timestamp_column_name => 'updated_on', days => 2 } )->count;
801
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
802
803
    $count = $patrons->filter_by_last_update(
804
        { timestamp_column_name => 'updated_on', days => 1 } )->count;
805
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
806
807
    $count = $patrons->filter_by_last_update(
808
        { timestamp_column_name => 'updated_on', days => 0 } )->count;
809
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
810
811
    $count = $patrons->filter_by_last_update(
812
        { timestamp_column_name => 'updated_on', from => $now } )->count;
813
    is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
814
815
    $count = $patrons->filter_by_last_update(
816
        { timestamp_column_name => 'updated_on', to => $now } )->count;
817
    is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
818
819
    $count = $patrons->filter_by_last_update(
820
        {
821
            timestamp_column_name => 'updated_on',
822
            from                  => $now->clone->subtract( days => 4 ),
823
            to                    => $now->clone->subtract( days => 2 )
824
        }
825
    )->count;
826
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
827
828
    t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
829
    try {
830
        $count = $patrons->filter_by_last_update(
831
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
832
          ->count;
833
    }
834
    catch {
835
        ok(
836
            $_->isa(
837
                'No exception raised, from and to parameters can take an iso formatted date'
838
            )
839
        );
840
    };
841
    try {
842
        $count = $patrons->filter_by_last_update(
843
            { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
844
          ->count;
845
    }
846
    catch {
847
        ok(
848
            $_->isa(
849
                'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
850
            )
851
        );
852
    };
853
762
    $schema->storage->txn_rollback;
854
    $schema->storage->txn_rollback;
763
};
855
};
764
856
765
- 

Return to bug 24152