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

(-)a/misc/cronjobs/cleanup_database.pl (-10 / +53 lines)
Lines 20-27 Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
22
23
BEGIN {
23
use constant DEFAULT_ZEBRAQ_PURGEDAYS => 30;
24
use constant DEFAULT_IMPORT_PURGEDAYS => 60;
25
use constant DEFAULT_LOGS_PURGEDAYS => 180;
24
26
27
BEGIN {
25
    # find Koha's Perl modules
28
    # find Koha's Perl modules
26
    # test carefully before changing this
29
    # test carefully before changing this
27
    use FindBin;
30
    use FindBin;
Lines 31-44 BEGIN { Link Here
31
use C4::Context;
34
use C4::Context;
32
use C4::Dates;
35
use C4::Dates;
33
36
34
#use C4::Debug;
35
#use C4::Letters;
36
#use File::Spec;
37
use Getopt::Long;
37
use Getopt::Long;
38
38
39
sub usage {
39
sub usage {
40
    print STDERR <<USAGE;
40
    print STDERR <<USAGE;
41
Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged]
41
Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS]
42
42
43
   -h --help          prints this help message, and exits, ignoring all
43
   -h --help          prints this help message, and exits, ignoring all
44
                      other options
44
                      other options
Lines 47-61 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu Link Here
47
   --sessdays DAYS    purge only sessions older than DAYS days.
47
   --sessdays DAYS    purge only sessions older than DAYS days.
48
   -v --verbose       will cause the script to give you a bit more information
48
   -v --verbose       will cause the script to give you a bit more information
49
                      about the run.
49
                      about the run.
50
   --zebraqueue DAYS  purge completed entries from the zebraqueue from 
50
   --zebraqueue DAYS  purge completed zebraqueue entries older than DAYS days.
51
                      more than DAYS days ago.
51
                      Defaults to 30 days if no days specified.
52
   -m --mail          purge the mail queue. 
52
   -m --mail          purge the mail queue. 
53
   --merged           purged completed entries from need_merge_authorities.
53
   --merged           purged completed entries from need_merge_authorities.
54
   --import DAYS      purge records from import tables older than DAYS days.
55
                      Defaults to 60 days if no days specified.
56
   --logs DAYS        purge entries from action_logs older than DAYS days.
57
                      Defaults to 180 days if no days specified.
54
USAGE
58
USAGE
55
    exit $_[0];
59
    exit $_[0];
56
}
60
}
57
61
58
my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged);
62
my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged, $pImport, $pLogs);
59
63
60
GetOptions(
64
GetOptions(
61
    'h|help'       => \$help,
65
    'h|help'       => \$help,
Lines 65-78 GetOptions( Link Here
65
    'm|mail'       => \$mail,
69
    'm|mail'       => \$mail,
66
    'zebraqueue:i' => \$zebraqueue_days,
70
    'zebraqueue:i' => \$zebraqueue_days,
67
    'merged'       => \$purge_merged,
71
    'merged'       => \$purge_merged,
72
    'import:i'     => \$pImport,
73
    'logs:i'       => \$pLogs,
68
) || usage(1);
74
) || usage(1);
75
69
$sessions=1 if $sess_days && $sess_days>0;
76
$sessions=1 if $sess_days && $sess_days>0;
77
# if --import, --logs or --zebraqueue were passed without number of days,
78
# use defaults
79
$pImport= DEFAULT_IMPORT_PURGEDAYS if defined($pImport) && $pImport==0;
80
$pLogs= DEFAULT_LOGS_PURGEDAYS if defined($pLogs) && $pLogs==0;
81
$zebraqueue_days= DEFAULT_ZEBRAQ_PURGEDAYS if defined($zebraqueue_days) && $zebraqueue_days==0;
70
82
71
if ($help) {
83
if ($help) {
72
    usage(0);
84
    usage(0);
73
}
85
}
74
86
75
if ( !( $sessions || $zebraqueue_days || $mail || $purge_merged) ) {
87
if ( !( $sessions || $zebraqueue_days || $mail || $purge_merged || $pImport || $pLogs) ) {
76
    print "You did not specify any cleanup work for the script to do.\n\n";
88
    print "You did not specify any cleanup work for the script to do.\n\n";
77
    usage(1);
89
    usage(1);
78
}
90
}
Lines 145-150 if($purge_merged) { Link Here
145
    print "Done with purging need_merge_authorities.\n" if $verbose;
157
    print "Done with purging need_merge_authorities.\n" if $verbose;
146
}
158
}
147
159
160
if($pImport) {
161
    print "Purging records from import tables.\n" if $verbose;
162
    PurgeImportTables();
163
    print "Done with purging import tables.\n" if $verbose;
164
}
165
166
if($pLogs) {
167
    print "Purging records from action_logs.\n" if $verbose;
168
    $sth = $dbh->prepare("DELETE FROM action_logs WHERE timestamp < date_sub(curdate(), interval ? DAY)");
169
    $sth->execute($pLogs) or die $dbh->errstr;
170
    print "Done with purging action_logs.\n" if $verbose;
171
}
172
148
exit(0);
173
exit(0);
149
174
150
sub RemoveOldSessions {
175
sub RemoveOldSessions {
Lines 173-175 sub RemoveOldSessions { Link Here
173
        print "$count sessions were deleted.\n";
198
        print "$count sessions were deleted.\n";
174
    }
199
    }
175
}
200
}
176
- 
201
202
sub PurgeImportTables {
203
    #First purge import_records
204
    #Delete cascades to import_biblios, import_items and import_record_matches
205
    $sth = $dbh->prepare("DELETE FROM import_records WHERE upload_timestamp < date_sub(curdate(), interval ? DAY)");
206
    $sth->execute($pImport) or die $dbh->errstr;
207
208
    # Now purge import_batches
209
    # Timestamp cannot be used here without care, because records are added
210
    # continuously to batches without updating timestamp (z3950 search).
211
    # So we only delete older empty batches.
212
    # This delete will therefore not have a cascading effect.
213
    $sth = $dbh->prepare("DELETE ba
214
 FROM import_batches ba
215
 LEFT JOIN import_records re ON re.import_batch_id=ba.import_batch_id
216
 WHERE re.import_record_id IS NULL AND
217
 ba.upload_timestamp < date_sub(curdate(), interval ? DAY)");
218
    $sth->execute($pImport) or die $dbh->errstr;
219
}

Return to bug 7240