2024-02-23 13:39:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2024-02-23 20:05:48 +00:00
|
|
|
class CleanupDatabase extends Command
|
2024-02-23 13:39:52 +00:00
|
|
|
{
|
|
|
|
protected $signature = 'cleanup:database {--yes}';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-23 13:39:52 +00:00
|
|
|
protected $description = 'Cleanup database';
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-03-12 09:57:07 +00:00
|
|
|
if ($this->option('yes')) {
|
|
|
|
echo "Running database cleanup...\n";
|
|
|
|
} else {
|
|
|
|
echo "Running database cleanup in dry-run mode...\n";
|
|
|
|
}
|
2024-02-23 13:39:52 +00:00
|
|
|
$keep_days = 60;
|
2024-03-12 09:57:07 +00:00
|
|
|
echo "Keep days: $keep_days\n";
|
2024-02-23 13:39:52 +00:00
|
|
|
// Cleanup failed jobs table
|
2024-03-26 10:17:48 +00:00
|
|
|
$failed_jobs = DB::table('failed_jobs')->where('failed_at', '<', now()->subDays(1));
|
2024-02-23 13:39:52 +00:00
|
|
|
$count = $failed_jobs->count();
|
|
|
|
echo "Delete $count entries from failed_jobs.\n";
|
|
|
|
if ($this->option('yes')) {
|
|
|
|
$failed_jobs->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup sessions table
|
|
|
|
$sessions = DB::table('sessions')->where('last_activity', '<', now()->subDays($keep_days)->timestamp);
|
|
|
|
$count = $sessions->count();
|
|
|
|
echo "Delete $count entries from sessions.\n";
|
|
|
|
if ($this->option('yes')) {
|
|
|
|
$sessions->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup activity_log table
|
2024-03-12 09:58:28 +00:00
|
|
|
$activity_log = DB::table('activity_log')->where('created_at', '<', now()->subDays($keep_days))->orderBy('created_at', 'desc')->skip(10);
|
2024-02-23 13:39:52 +00:00
|
|
|
$count = $activity_log->count();
|
|
|
|
echo "Delete $count entries from activity_log.\n";
|
|
|
|
if ($this->option('yes')) {
|
|
|
|
$activity_log->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup application_deployment_queues table
|
2024-03-12 09:58:28 +00:00
|
|
|
$application_deployment_queues = DB::table('application_deployment_queues')->where('created_at', '<', now()->subDays($keep_days))->orderBy('created_at', 'desc')->skip(10);
|
2024-02-23 13:39:52 +00:00
|
|
|
$count = $application_deployment_queues->count();
|
|
|
|
echo "Delete $count entries from application_deployment_queues.\n";
|
|
|
|
if ($this->option('yes')) {
|
|
|
|
$application_deployment_queues->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup webhooks table
|
2024-03-12 09:58:31 +00:00
|
|
|
$webhooks = DB::table('webhooks')->where('created_at', '<', now()->subDays($keep_days));
|
2024-02-23 13:39:52 +00:00
|
|
|
$count = $webhooks->count();
|
|
|
|
echo "Delete $count entries from webhooks.\n";
|
|
|
|
if ($this->option('yes')) {
|
|
|
|
$webhooks->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|