33 lines
937 B
PHP
33 lines
937 B
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
use Config\Audit;
|
|
|
|
class PurgeOldAuditLogs extends BaseCommand
|
|
{
|
|
protected $group = 'ChartBoard';
|
|
protected $name = 'chartboard:audit:purge';
|
|
protected $description = 'Delete audit log rows older than the configured retention period.';
|
|
|
|
public function run(array $params)
|
|
{
|
|
$days = (new Audit())->retentionDays;
|
|
if ($days < 1) {
|
|
CLI::write('Retention days must be at least 1.', 'yellow');
|
|
|
|
return;
|
|
}
|
|
|
|
$cutoff = date('Y-m-d H:i:s', strtotime('-' . $days . ' days'));
|
|
$builder = db_connect()->table('audit_logs');
|
|
$builder->where('created_at <', $cutoff);
|
|
$builder->delete();
|
|
$count = db_connect()->affectedRows();
|
|
|
|
CLI::write("Deleted audit logs older than {$days} days (before {$cutoff}): {$count}", 'green');
|
|
}
|
|
}
|