TT#64406 fix changing epoch/date column mode for 'cleanup'

Change-Id: Ie38c70d765efaa2e24b4394b7b1d4ef13c6a02d6
changes/28/32228/5
Rene Krenn 7 years ago
parent fb9442c93e
commit c0aef1c756

@ -160,7 +160,13 @@ sub init_cmds {
my ($self, $table) = @_;
$table or die "No table name provided in the 'cleanup' command";
$self->env('dbh') or die "Not connected to a DB in the 'cleanup' command";
foreach my $v (qw(time-column cleanup-days)) {
if ($self->env('timestamp-column')) {
$self->env('time-column-mode' => 'timestamp');
$self->env('time-column' => $self->env('timestamp-column'));
} elsif ($self->env('time-column')) {
$self->env('time-column-mode' => 'time');
}
foreach my $v (qw(time-column time-column-mode cleanup-days)) {
$self->env($v)
or die "Variable '$v' is not set in the 'cleanup' command";
}
@ -197,6 +203,11 @@ sub init_config {
last;
}
$env{$1} = $2;
if ($1 eq "time-column") {
$env{"timestamp-column"} = undef;
} elsif ($1 eq "timestamp-column") {
$env{"time-column"} = undef;
}
next;
}
@ -623,14 +634,22 @@ sub cleanup_table {
my $batch = $self->env('batch') // 0;
my $limit = $batch ? "limit $batch" : '';
my $col = $self->env('time-column');
my $col_mode = $self->env('time-column-mode');
my $dbh = $self->env('dbh');
my $cleanup_days = $self->env('cleanup-days');
my $deleted_rows = 0;
while (1) {
my $aff = $dbh->do(<<SQL, undef, $cleanup_days);
my $aff;
if ($col_mode eq "time") {
$aff = $dbh->do(<<SQL, undef, $cleanup_days);
DELETE FROM $table WHERE $col < date(date_sub(now(), interval ? day)) $limit
SQL
} else {
$aff = $dbh->do(<<SQL, undef, $cleanup_days);
DELETE FROM $table WHERE $col < unix_timestamp(date(date_sub(now(), interval ? day))) $limit
SQL
}
$deleted_rows += $aff;
last unless $aff > 0;
}

Loading…
Cancel
Save