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

(-)a/Koha/Objects.pm (-4 / +32 lines)
Lines 25-30 use Class::Inspector; Link Here
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::Exceptions::Object;
27
use Koha::Exceptions::Object;
28
use Koha::DateUtils qw( dt_from_string );
28
29
29
=head1 NAME
30
=head1 NAME
30
31
Lines 252-266 sub update { Link Here
252
    return $self->_resultset->update($fields);
253
    return $self->_resultset->update($fields);
253
}
254
}
254
255
256
=head3 filter_by_last_update
257
258
my $filtered_objects = $objects->filter_by_last_update
259
260
days exclusive
261
from inclusive
262
to   inclusive
263
264
=cut
265
255
sub filter_by_last_update {
266
sub filter_by_last_update {
256
    my ( $self, $params ) = @_;
267
    my ( $self, $params ) = @_;
257
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
268
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
269
    my $conditions;
270
    Koha::Exceptions::MissingParameter->throw(
271
        "Missing mandatory parameter: days or from or to")
272
      unless exists $params->{days}
273
          or exists $params->{from}
274
          or exists $params->{to};
275
276
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
277
    if ( exists $params->{days} ) {
278
        $conditions->{'<'} = $dtf->format_date( dt_from_string->subtract( days => $params->{days} ) );
279
    }
280
    if ( exists $params->{from} ) {
281
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
282
        $conditions->{'>='} = $dtf->format_date( $from );
283
    }
284
    if ( exists $params->{to} ) {
285
        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
286
        $conditions->{'<='} = $dtf->format_date( $to );
287
    }
288
258
    return $self->_resultset->search(
289
    return $self->_resultset->search(
259
        {
290
        {
260
            $timestamp_column_name => {
291
            $timestamp_column_name => $conditions
261
                '<' =>
262
                  [ \'DATE_SUB(CURDATE(), INTERVAL ? DAY)', $params->{days} ]
263
            }
264
        }
292
        }
265
    );
293
    );
266
}
294
}
(-)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 / +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 => 23;
22
use Test::More tests => 24;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Warn;
25
use Test::Warn;
Lines 31-36 use Koha::Patron::Category; Link Here
31
use Koha::Patron::Categories;
31
use Koha::Patron::Categories;
32
use Koha::Patrons;
32
use Koha::Patrons;
33
use Koha::Database;
33
use Koha::Database;
34
use Koha::DateUtils qw( dt_from_string );
34
35
35
use t::lib::TestBuilder;
36
use t::lib::TestBuilder;
36
use t::lib::Mocks;
37
use t::lib::Mocks;
Lines 1036-1041 subtest "attributes_from_api() tests" => sub { Link Here
1036
    );
1037
    );
1037
1038
1038
    $schema->storage->txn_rollback;
1039
    $schema->storage->txn_rollback;
1040
1041
};
1042
1043
subtest "filter_by_last_update" => sub {
1044
1045
    $schema->storage->txn_begin;
1046
1047
    my $now = dt_from_string->truncate( to => 'day' );
1048
    my @borrowernumbers;
1049
    # Building 6 patrons that have been created today, yesterday, ... 1 per day
1050
    for my $i ( 0 .. 5 ) {
1051
        push @borrowernumbers,
1052
          $builder->build_object(
1053
            {
1054
                class => 'Koha::Patrons',
1055
                value => { updated_on => $now->clone->subtract( days => $i ) }
1056
            }
1057
          )->borrowernumber;
1058
    }
1059
1060
    my $patrons = Koha::Patrons->search(
1061
        { borrowernumber => { -in => \@borrowernumbers } } );
1062
1063
    try {
1064
        $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
1065
          ->count;
1066
    }
1067
    catch {
1068
        ok(
1069
            $_->isa('Koha::Exceptions::MissingParameter'),
1070
            'Should raise an exception if no parameter given'
1071
        );
1072
    };
1073
1074
    my $count = $patrons->filter_by_last_update(
1075
        { timestamp_column_name => 'updated_on', days => 2 } )->count;
1076
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1077
1078
    $count = $patrons->filter_by_last_update(
1079
        { timestamp_column_name => 'updated_on', days => 1 } )->count;
1080
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1081
1082
    $count = $patrons->filter_by_last_update(
1083
        { timestamp_column_name => 'updated_on', days => 0 } )->count;
1084
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1085
1086
    $count = $patrons->filter_by_last_update(
1087
        { timestamp_column_name => 'updated_on', from => $now } )->count;
1088
    is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
1089
1090
    $count = $patrons->filter_by_last_update(
1091
        { timestamp_column_name => 'updated_on', to => $now } )->count;
1092
    is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
1093
1094
    $count = $patrons->filter_by_last_update(
1095
        {
1096
            timestamp_column_name => 'updated_on',
1097
            from                  => $now->clone->subtract( days => 4 ),
1098
            to                    => $now->clone->subtract( days => 2 )
1099
        }
1100
    )->count;
1101
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1102
1103
    t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
1104
    try {
1105
        $count = $patrons->filter_by_last_update(
1106
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1107
          ->count;
1108
    }
1109
    catch {
1110
        ok(
1111
            $_->isa(
1112
                'No exception raised, from and to parameters can take an iso formatted date'
1113
            )
1114
        );
1115
    };
1116
    try {
1117
        $count = $patrons->filter_by_last_update(
1118
            { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
1119
          ->count;
1120
    }
1121
    catch {
1122
        ok(
1123
            $_->isa(
1124
                'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1125
            )
1126
        );
1127
    };
1128
1129
    $schema->storage->txn_rollback;
1039
};
1130
};
1040
1131
1041
subtest "from_api_mapping() tests" => sub {
1132
subtest "from_api_mapping() tests" => sub {
1042
- 

Return to bug 24152