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 170-184 sub search_related { Link Here
170
    }
171
    }
171
}
172
}
172
173
174
=head3 filter_by_last_update
175
176
my $filtered_objects = $objects->filter_by_last_update
177
178
days exclusive
179
from inclusive
180
to   inclusive
181
182
=cut
183
173
sub filter_by_last_update {
184
sub filter_by_last_update {
174
    my ( $self, $params ) = @_;
185
    my ( $self, $params ) = @_;
175
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
186
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
187
    my $conditions;
188
    Koha::Exceptions::MissingParameter->throw(
189
        "Missing mandatory parameter: days or from or to")
190
      unless exists $params->{days}
191
          or exists $params->{from}
192
          or exists $params->{to};
193
194
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
195
    if ( exists $params->{days} ) {
196
        $conditions->{'<'} = $dtf->format_date( dt_from_string->subtract( days => $params->{days} ) );
197
    }
198
    if ( exists $params->{from} ) {
199
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
200
        $conditions->{'>='} = $dtf->format_date( $from );
201
    }
202
    if ( exists $params->{to} ) {
203
        my $to = ref($params->{to}) ? $params->{to} : dt_to_string($params->{to});
204
        $conditions->{'<='} = $dtf->format_date( $to );
205
    }
206
176
    return $self->_resultset->search(
207
    return $self->_resultset->search(
177
        {
208
        {
178
            $timestamp_column_name => {
209
            $timestamp_column_name => $conditions
179
                '<' =>
180
                  [ \'DATE_SUB(CURDATE(), INTERVAL ? DAY)', $params->{days} ]
181
            }
182
        }
210
        }
183
    );
211
    );
184
}
212
}
(-)a/misc/cronjobs/cleanup_database.pl (-2 / +23 lines)
Lines 46-52 use Koha::Old::Biblios; Link Here
46
use Koha::Old::Items;
46
use Koha::Old::Items;
47
use Koha::Old::Biblioitems;
47
use Koha::Old::Biblioitems;
48
use Koha::Item::Transfers;
48
use Koha::Item::Transfers;
49
49
use Koha::PseudonymizedTransactions;
50
50
51
sub usage {
51
sub usage {
52
    print STDERR <<USAGE;
52
    print STDERR <<USAGE;
Lines 96-101 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu Link Here
96
   --old-issues DAYS       Purge checkouts (old_issues) returned more than DAYS days ago.
96
   --old-issues DAYS       Purge checkouts (old_issues) returned more than DAYS days ago.
97
   --old-reserves DAYS     Purge reserves (old_reserves) more than DAYS old.
97
   --old-reserves DAYS     Purge reserves (old_reserves) more than DAYS old.
98
   --transfers DAYS        Purge transfers completed more than DAYS day ago.
98
   --transfers DAYS        Purge transfers completed more than DAYS day ago.
99
   --pseudo-transactions DAYS   Purge the pseudonymized transactions that have been originally created more than DAYS days ago
100
                                DAYS is optional and can be replaced by:
101
                                    --pseudo-transactions-from YYYY-MM-DD and/or --pseudo-transactions-to YYYY-MM-DD
99
USAGE
102
USAGE
100
    exit $_[0];
103
    exit $_[0];
101
}
104
}
Lines 128-133 my $pDeletedPatrons; Link Here
128
my $pOldIssues;
131
my $pOldIssues;
129
my $pOldReserves;
132
my $pOldReserves;
130
my $pTransfers;
133
my $pTransfers;
134
my ( $pPseudoTransactions, $pPseudoTransactionsFrom, $pPseudoTransactionsTo );
131
135
132
GetOptions(
136
GetOptions(
133
    'h|help'            => \$help,
137
    'h|help'            => \$help,
Lines 157-163 GetOptions( Link Here
157
    'deleted-patrons:i' => \$pDeletedPatrons,
161
    'deleted-patrons:i' => \$pDeletedPatrons,
158
    'old-issues:i'      => \$pOldIssues,
162
    'old-issues:i'      => \$pOldIssues,
159
    'old-reserves:i'    => \$pOldReserves,
163
    'old-reserves:i'    => \$pOldReserves,
160
    'transfers:i'    => \$pTransfers,
164
    'transfers:i'       => \$pTransfers,
165
    'pseudo-transactions:i'      => \$pPseudoTransactions,
166
    'pseudo-transactions-from:s' => \$pPseudoTransactionsFrom,
167
    'pseudo-transactions-to:s'   => \$pPseudoTransactionsTo,
161
) || usage(1);
168
) || usage(1);
162
169
163
# Use default values
170
# Use default values
Lines 198-203 unless ( $sessions Link Here
198
    || $pOldIssues
205
    || $pOldIssues
199
    || $pOldReserves
206
    || $pOldReserves
200
    || $pTransfers
207
    || $pTransfers
208
    || defined $pPseudoTransactions
201
) {
209
) {
202
    print "You did not specify any cleanup work for the script to do.\n\n";
210
    print "You did not specify any cleanup work for the script to do.\n\n";
203
    usage(1);
211
    usage(1);
Lines 430-435 if ($pTransfers) { Link Here
430
    print "Done with purging transfers.\n" if $verbose;
438
    print "Done with purging transfers.\n" if $verbose;
431
}
439
}
432
440
441
if (defined $pPseudoTransactions) {
442
    print "Purging pseudonymized transactions older than $pPseudoTransactions days.\n" if $verbose;
443
    Koha::Anonymized::Transactions->filter_by_last_update(
444
        {
445
            timestamp_column_name => 'datetime',
446
            ( $pPseudoTransactions     ? ( days => $pPseudoTransactions     ) : () ),
447
            ( $pPseudoTransactionsFrom ? ( from => $pPseudoTransactionsFrom ) : () ),
448
            ( $pPseudoTransactionsTo   ? ( to   => $pPseudoTransactionsTo   ) : () ),
449
        }
450
    )->delete;
451
    print "Done with purging pseudonymized transactions.\n" if $verbose;
452
}
453
433
exit(0);
454
exit(0);
434
455
435
sub RemoveOldSessions {
456
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 => 19;
22
use Test::More tests => 20;
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-762 subtest "attributes_from_api() tests" => sub { Link Here
759
    );
761
    );
760
762
761
    $schema->storage->txn_rollback;
763
    $schema->storage->txn_rollback;
764
765
};
766
767
subtest "filter_by_last_update" => sub {
768
769
    $schema->storage->txn_begin;
770
771
    my $now = dt_from_string->truncate( to => 'day' );
772
    my @borrowernumbers;
773
    # Building 6 patrons that have been created today, yesterday, ... 1 per day
774
    for my $i ( 0 .. 5 ) {
775
        push @borrowernumbers,
776
          $builder->build_object(
777
            {
778
                class => 'Koha::Patrons',
779
                value => { updated_on => $now->clone->subtract( days => $i ) }
780
            }
781
          )->borrowernumber;
782
    }
783
784
    my $patrons = Koha::Patrons->search(
785
        { borrowernumber => { -in => \@borrowernumbers } } );
786
787
    try {
788
        $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
789
          ->count;
790
    }
791
    catch {
792
        ok(
793
            $_->isa('Koha::Exceptions::MissingParameter'),
794
            'Should raise an exception if no parameter given'
795
        );
796
    };
797
798
    my $count = $patrons->filter_by_last_update(
799
        { timestamp_column_name => 'updated_on', days => 2 } )->count;
800
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
801
802
    $count = $patrons->filter_by_last_update(
803
        { timestamp_column_name => 'updated_on', days => 1 } )->count;
804
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
805
806
    $count = $patrons->filter_by_last_update(
807
        { timestamp_column_name => 'updated_on', days => 0 } )->count;
808
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
809
810
    $count = $patrons->filter_by_last_update(
811
        { timestamp_column_name => 'updated_on', from => $now } )->count;
812
    is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
813
814
    $count = $patrons->filter_by_last_update(
815
        { timestamp_column_name => 'updated_on', to => $now } )->count;
816
    is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
817
818
    $count = $patrons->filter_by_last_update(
819
        {
820
            timestamp_column_name => 'updated_on',
821
            from                  => $now->clone->subtract( days => 4 ),
822
            to                    => $now->clone->subtract( days => 2 )
823
        }
824
    )->count;
825
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
826
827
    t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
828
    try {
829
        $count = $patrons->filter_by_last_update(
830
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
831
          ->count;
832
    }
833
    catch {
834
        ok(
835
            $_->isa(
836
                'No exception raised, from and to parameters can take an iso formatted date'
837
            )
838
        );
839
    };
840
    try {
841
        $count = $patrons->filter_by_last_update(
842
            { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
843
          ->count;
844
    }
845
    catch {
846
        ok(
847
            $_->isa(
848
                'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
849
            )
850
        );
851
    };
852
853
    $schema->storage->txn_rollback;
762
};
854
};
763
- 

Return to bug 24152