From 5505c5b50d8fca6fd0d17821ab8edbad0c371f08 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Tue, 11 Aug 2026 13:29:28 +0100 Subject: [PATCH] Action_logs cleanup script This should remove action_logs records without a valid item or target --- app/Console/Commands/CleanUpActionLogs.php | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 app/Console/Commands/CleanUpActionLogs.php diff --git a/app/Console/Commands/CleanUpActionLogs.php b/app/Console/Commands/CleanUpActionLogs.php new file mode 100644 index 0000000000..ce762c0368 --- /dev/null +++ b/app/Console/Commands/CleanUpActionLogs.php @@ -0,0 +1,80 @@ +option('delete') && $this->option('to-sql')) { + $this->fail("Can't use both --delete and --to-sql at the same time"); + } + if ($this->option('delete')) { + if (!$this->option('force')) { + if (!$this->confirm('Do you really want to delete all action_logs with nonexistent items or targets?')) { + $this->fail("Command canceled"); + } + } + } + $map = [ + Accessory::class => 'accessories', + Asset::class => 'assets', + AssetModel::class => 'models', + Component::class => 'components', + Consumable::class => 'consumables', + License::class => 'licenses', + Location::class => 'locations', + User::class => 'users', + ]; + + $rows = []; + foreach (['item', 'target'] as $prefix) { + foreach ($map as $class => $table) { + $query = Actionlog::withTrashed()->where($prefix . '_type', $class)->whereNotIn($prefix . '_id', $class::select('id')->withTrashed()); + if ($this->option('delete')) { + $count = $query->delete(); + } elseif ($this->option('to-sql')) { + $count = $query->toRawSql(); + } else { + $count = $query->count(); + } + $rows[] = [$prefix, $class, $table, $count]; + // DELETE FROM action_logs where {$prefix}_type='$class' /* make sure about backslashes! */ AND {$prefix}_id NOT IN (SELECT id FROM $table) + } + } + if (!$this->option('delete')) { + $this->line("Hint: use --delete to delete these entries instead of counting them"); + $this->newLine(); + } + $this->table(['Attribute', 'Class', 'Table', 'Count'], $rows); + } +} \ No newline at end of file