From 85c36df2a3f5aca70b05d8fe16e637a9b5fbaa6d Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 12 Mar 2024 10:58:28 +0100 Subject: [PATCH] Refactor database cleanup queries to keep the last 10 entries --- app/Console/Commands/CleanupDatabase.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/CleanupDatabase.php b/app/Console/Commands/CleanupDatabase.php index 110e49c49..0c8f9b884 100644 --- a/app/Console/Commands/CleanupDatabase.php +++ b/app/Console/Commands/CleanupDatabase.php @@ -36,7 +36,8 @@ class CleanupDatabase extends Command } // Cleanup activity_log table - $activity_log = DB::table('activity_log')->where('created_at', '<', now()->subDays($keep_days)); + // but keep the last 10 + $activity_log = DB::table('activity_log')->where('created_at', '<', now()->subDays($keep_days))->orderBy('created_at', 'desc')->skip(10); $count = $activity_log->count(); echo "Delete $count entries from activity_log.\n"; if ($this->option('yes')) { @@ -44,7 +45,7 @@ class CleanupDatabase extends Command } // Cleanup application_deployment_queues table - $application_deployment_queues = DB::table('application_deployment_queues')->where('created_at', '<', now()->subDays($keep_days)); + $application_deployment_queues = DB::table('application_deployment_queues')->where('created_at', '<', now()->subDays($keep_days))->orderBy('created_at', 'desc')->skip(10); $count = $application_deployment_queues->count(); echo "Delete $count entries from application_deployment_queues.\n"; if ($this->option('yes')) { @@ -52,7 +53,7 @@ class CleanupDatabase extends Command } // Cleanup webhooks table - $webhooks = DB::table('webhooks')->where('created_at', '<', now()->subDays($keep_days)); + $webhooks = DB::table('webhooks')->where('created_at', '<', now()->subDays($keep_days))->orderBy('created_at', 'desc')->skip(10); $count = $webhooks->count(); echo "Delete $count entries from webhooks.\n"; if ($this->option('yes')) {