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 (-1 / +44 lines)
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-patrons DAYS        Purge the pseudonymized patrons that have been originally enrolled more than DAYS days ago
100
                                DAYS is optional and can be replaced by
101
                                    --pseudo-patrons-from YYYY-MM-DD (iso formatted date, or as the 'dateformat' system preference is set
102
                                    and/or --pseudo-patrons-to YYYY-MM-DD
103
   --pseudo-transactions DAYS   Purge the pseudonymized transactions that have been originally created more than DAYS days ago
104
                                DAYS is optional and can be replaced by:
105
                                    --pseudo-transactions-from YYYY-MM-DD and/or --pseudo-patrons-to YYYY-MM-DD
99
USAGE
106
USAGE
100
    exit $_[0];
107
    exit $_[0];
101
}
108
}
Lines 128-133 my $pDeletedPatrons; Link Here
128
my $pOldIssues;
135
my $pOldIssues;
129
my $pOldReserves;
136
my $pOldReserves;
130
my $pTransfers;
137
my $pTransfers;
138
my ( $pPseudoPatrons, $pPseudoPatronsFrom, $pPseudoPatronsTo );
139
my ( $pPseudoTransactions, $pPseudoTransactionsFrom, $pPseudoTransactionsTo );
131
140
132
GetOptions(
141
GetOptions(
133
    'h|help'            => \$help,
142
    'h|help'            => \$help,
Lines 157-163 GetOptions( Link Here
157
    'deleted-patrons:i' => \$pDeletedPatrons,
166
    'deleted-patrons:i' => \$pDeletedPatrons,
158
    'old-issues:i'      => \$pOldIssues,
167
    'old-issues:i'      => \$pOldIssues,
159
    'old-reserves:i'    => \$pOldReserves,
168
    'old-reserves:i'    => \$pOldReserves,
160
    'transfers:i'    => \$pTransfers,
169
    'transfers:i'       => \$pTransfers,
170
    'pseudo-patrons:i'           => \$pPseudoPatrons,
171
    'pseudo-patrons-from:s'      => \$pPseudoPatronsFrom,
172
    'pseudo-patrons-to:s'        => \$pPseudoPatronsTo,
173
    'pseudo-transactions:i'      => \$pPseudoTransactions,
174
    'pseudo-transactions-from:s' => \$pPseudoTransactionsFrom,
175
    'pseudo-transactions-to:s'   => \$pPseudoTransactionsTo,
161
) || usage(1);
176
) || usage(1);
162
177
163
# Use default values
178
# Use default values
Lines 198-203 unless ( $sessions Link Here
198
    || $pOldIssues
213
    || $pOldIssues
199
    || $pOldReserves
214
    || $pOldReserves
200
    || $pTransfers
215
    || $pTransfers
216
    || defined $pPseudoPatrons
217
    || defined $pPseudoTransactions
201
) {
218
) {
202
    print "You did not specify any cleanup work for the script to do.\n\n";
219
    print "You did not specify any cleanup work for the script to do.\n\n";
203
    usage(1);
220
    usage(1);
Lines 430-435 if ($pTransfers) { Link Here
430
    print "Done with purging transfers.\n" if $verbose;
447
    print "Done with purging transfers.\n" if $verbose;
431
}
448
}
432
449
450
if (defined $pPseudoPatrons) {
451
    print "Purging pseudonymized patrons older than $pPseudoPatrons days.\n" if $verbose;
452
    my $anonymized_patrons = Koha::Anonymized::Patrons->filter_by_last_update(
453
        {
454
            timestamp_column_name => 'dateenrolled',
455
            ( $pPseudoPatrons     ? ( days => $pPseudoPatrons     ) : () ),
456
            ( $pPseudoPatronsFrom ? ( from => $pPseudoPatronsFrom ) : () ),
457
            ( $pPseudoPatronsTo   ? ( to   => $pPseudoPatronsTo   ) : () ),
458
        }
459
    )->delete;
460
    print "Done with purging pseudonymized patrons.\n" if $verbose;
461
}
462
463
if (defined $pPseudoTransactions) {
464
    print "Purging pseudonymized transactions older than $pPseudoTransactions days.\n" if $verbose;
465
    Koha::Anonymized::Transactions->filter_by_last_update(
466
        {
467
            timestamp_column_name => 'datetime',
468
            ( $pPseudoTransactions     ? ( days => $pPseudoTransactions     ) : () ),
469
            ( $pPseudoTransactionsFrom ? ( from => $pPseudoTransactionsFrom ) : () ),
470
            ( $pPseudoTransactionsTo   ? ( to   => $pPseudoTransactionsTo   ) : () ),
471
        }
472
    )->delete;
473
    print "Done with purging pseudonymized transactions.\n" if $verbose;
474
}
475
433
exit(0);
476
exit(0);
434
477
435
sub RemoveOldSessions {
478
sub RemoveOldSessions {
(-)a/t/db_dependent/Koha/Objects.t (-3 / +93 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 => 17;
22
use Test::More tests => 18;
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 330-332 subtest "TO_JSON() tests" => sub { Link Here
330
332
331
    $schema->storage->txn_rollback;
333
    $schema->storage->txn_rollback;
332
};
334
};
333
- 
335
336
subtest "filter_by_last_update" => sub {
337
338
    $schema->storage->txn_begin;
339
340
    my $now = dt_from_string->truncate( to => 'day' );
341
    my @borrowernumbers;
342
    # Building 6 patrons that have been created today, yesterday, ... 1 per day
343
    for my $i ( 0 .. 5 ) {
344
        push @borrowernumbers,
345
          $builder->build_object(
346
            {
347
                class => 'Koha::Patrons',
348
                value => { updated_on => $now->clone->subtract( days => $i ) }
349
            }
350
          )->borrowernumber;
351
    }
352
353
    my $patrons = Koha::Patrons->search(
354
        { borrowernumber => { -in => \@borrowernumbers } } );
355
356
    try {
357
        $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
358
          ->count;
359
    }
360
    catch {
361
        ok(
362
            $_->isa('Koha::Exceptions::MissingParameter'),
363
            'Should raise an exception if no parameter given'
364
        );
365
    };
366
367
    my $count = $patrons->filter_by_last_update(
368
        { timestamp_column_name => 'updated_on', days => 2 } )->count;
369
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
370
371
    $count = $patrons->filter_by_last_update(
372
        { timestamp_column_name => 'updated_on', days => 1 } )->count;
373
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
374
375
    $count = $patrons->filter_by_last_update(
376
        { timestamp_column_name => 'updated_on', days => 0 } )->count;
377
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
378
379
    $count = $patrons->filter_by_last_update(
380
        { timestamp_column_name => 'updated_on', from => $now } )->count;
381
    is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
382
383
    $count = $patrons->filter_by_last_update(
384
        { timestamp_column_name => 'updated_on', to => $now } )->count;
385
    is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
386
387
    $count = $patrons->filter_by_last_update(
388
        {
389
            timestamp_column_name => 'updated_on',
390
            from                  => $now->clone->subtract( days => 4 ),
391
            to                    => $now->clone->subtract( days => 2 )
392
        }
393
    )->count;
394
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
395
396
    t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
397
    try {
398
        $count = $patrons->filter_by_last_update(
399
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
400
          ->count;
401
    }
402
    catch {
403
        ok(
404
            $_->isa(
405
                'No exception raised, from and to parameters can take an iso formatted date'
406
            )
407
        );
408
    };
409
    try {
410
        $count = $patrons->filter_by_last_update(
411
            { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
412
          ->count;
413
    }
414
    catch {
415
        ok(
416
            $_->isa(
417
                'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
418
            )
419
        );
420
    };
421
422
    $schema->storage->txn_rollback;
423
};

Return to bug 24152