From c774e969d79eb172e6aeaeb3cddfc4855b660980 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 16:34:07 -0700 Subject: [PATCH 01/20] Scaffold command --- .../Commands/FixActionLogTimestamps.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 app/Console/Commands/FixActionLogTimestamps.php diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php new file mode 100644 index 0000000000..ccba045ea4 --- /dev/null +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -0,0 +1,54 @@ +option('dryrun')) { + $this->dryrun = true; + } + + if ($this->dryrun) { + $this->info('This is a DRY RUN - no changes will be saved.'); + } + + // Logs that were improperly timestamped should have created_at in the 1970s + $logs = Actionlog::whereYear('created_at', '1970')->get(); + + $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps.'); + + // @todo: write ids to console + + foreach ($logs as $log) { + if (!$this->dryrun){ + $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + } else { + $this->line(vsprintf('DRYRUN...Would update log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + } + } + } +} From a8cccffa1e0e46e56d48c86ba6204f9380e2673a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 16:41:22 -0700 Subject: [PATCH 02/20] Update output --- app/Console/Commands/FixActionLogTimestamps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index ccba045ea4..fd8c946066 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -47,7 +47,7 @@ class FixActionLogTimestamps extends Command if (!$this->dryrun){ $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); } else { - $this->line(vsprintf('DRYRUN...Would update log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + $this->line(vsprintf('DRYRUN: Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); } } } From c8177eb51eaef9ce578af40688a584f0baf7b89c Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 16:50:16 -0700 Subject: [PATCH 03/20] Update timestamps --- app/Console/Commands/FixActionLogTimestamps.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index fd8c946066..de4b00b05c 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Models\Actionlog; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\Model; class FixActionLogTimestamps extends Command { @@ -43,9 +44,16 @@ class FixActionLogTimestamps extends Command // @todo: write ids to console + // @todo: get confirmation? + foreach ($logs as $log) { if (!$this->dryrun){ $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + + Model::withoutTimestamps(function () use ($log) { + $log->created_at = $log->updated_at; + $log->saveQuietly(); + }); } else { $this->line(vsprintf('DRYRUN: Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); } From 4954d972bb9c57b7a49cc72f052fe3c9c773637b Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 16:52:20 -0700 Subject: [PATCH 04/20] Write table of ids --- app/Console/Commands/FixActionLogTimestamps.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index de4b00b05c..406d36ba28 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -40,9 +40,18 @@ class FixActionLogTimestamps extends Command // Logs that were improperly timestamped should have created_at in the 1970s $logs = Actionlog::whereYear('created_at', '1970')->get(); - $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps.'); + $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps:'); - // @todo: write ids to console + $this->table( + ['ID', 'Created At', 'Updated At'], + $logs->map(function ($log) { + return [ + $log->id, + $log->created_at, + $log->updated_at, + ]; + }) + ); // @todo: get confirmation? From dd14eac1ebdac6e638c0936d30e502b2844d2009 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 16:56:07 -0700 Subject: [PATCH 05/20] Prompt for confirmation --- app/Console/Commands/FixActionLogTimestamps.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 406d36ba28..26071369ae 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -53,7 +53,9 @@ class FixActionLogTimestamps extends Command }) ); - // @todo: get confirmation? + if (!$this->dryrun && !$this->confirm('Update these logs?')) { + return 0; + } foreach ($logs as $log) { if (!$this->dryrun){ @@ -67,5 +69,7 @@ class FixActionLogTimestamps extends Command $this->line(vsprintf('DRYRUN: Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); } } + + return 0; } } From 147e61006290e8521533ae02a12b53274185590f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 17:00:36 -0700 Subject: [PATCH 06/20] Add todos --- app/Console/Commands/FixActionLogTimestamps.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 26071369ae..54b4d28772 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -15,6 +15,7 @@ class FixActionLogTimestamps extends Command */ protected $signature = 'snipeit:fix-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; + // @todo: /** * The console command description. * @@ -40,6 +41,8 @@ class FixActionLogTimestamps extends Command // Logs that were improperly timestamped should have created_at in the 1970s $logs = Actionlog::whereYear('created_at', '1970')->get(); + // @todo: handle created_by being null... + $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps:'); $this->table( From c3a48182fd05962e4c7d56b7235b7ce9d1b89ca7 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 17:03:26 -0700 Subject: [PATCH 07/20] Display the created_by --- app/Console/Commands/FixActionLogTimestamps.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 54b4d28772..f13d12524e 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -15,7 +15,7 @@ class FixActionLogTimestamps extends Command */ protected $signature = 'snipeit:fix-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; - // @todo: + // @todo: /** * The console command description. * @@ -23,7 +23,7 @@ class FixActionLogTimestamps extends Command */ protected $description = 'Command description'; - private $dryrun = false; + private bool $dryrun = false; /** * Execute the console command. @@ -46,10 +46,11 @@ class FixActionLogTimestamps extends Command $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps:'); $this->table( - ['ID', 'Created At', 'Updated At'], + ['ID', 'Created By', 'Created At', 'Updated At'], $logs->map(function ($log) { return [ $log->id, + $log->created_by, $log->created_at, $log->updated_at, ]; From fffc606d9ab52fdcc4648d1a9ce84b78aae07557 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 17:06:02 -0700 Subject: [PATCH 08/20] Improve output --- app/Console/Commands/FixActionLogTimestamps.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index f13d12524e..7e2aa50e30 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -61,19 +61,25 @@ class FixActionLogTimestamps extends Command return 0; } - foreach ($logs as $log) { - if (!$this->dryrun){ - $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + if ($this->dryrun) { + $this->info('DRY RUN. NOT ACTUALLY UPDATING LOGS.'); + } + foreach ($logs as $log) { + $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + + if (!$this->dryrun) { Model::withoutTimestamps(function () use ($log) { $log->created_at = $log->updated_at; $log->saveQuietly(); }); - } else { - $this->line(vsprintf('DRYRUN: Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); } } + if ($this->dryrun) { + $this->info('DRY RUN. NO CHANGES WERE ACTUALLY MADE.'); + } + return 0; } } From 935d3eea9f00a81889da980b04b54cfd86c95f90 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 11 Mar 2025 17:17:39 -0700 Subject: [PATCH 09/20] Attempt to match and populate created_by --- .../Commands/FixActionLogTimestamps.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 7e2aa50e30..256b5f3d46 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -67,13 +67,24 @@ class FixActionLogTimestamps extends Command foreach ($logs as $log) { $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + $log->created_at = $log->updated_at; + + $createdBy = $this->getCreatedByAttributeFromSimilarLog($log); + + if ($createdBy) { + $this->line(vsprintf('Updating log id:%s created_by to %s', [$log->id, $createdBy])); + $log->created_by = $createdBy; + } else { + $this->line(vsprintf('No created_by found for log id:%s', [$log->id])); + } if (!$this->dryrun) { Model::withoutTimestamps(function () use ($log) { - $log->created_at = $log->updated_at; $log->saveQuietly(); }); } + + $this->newLine(); } if ($this->dryrun) { @@ -82,4 +93,26 @@ class FixActionLogTimestamps extends Command return 0; } + + private function getCreatedByAttributeFromSimilarLog(Actionlog $log): null|int + { + if (is_null($log->created_by)) { + $similarLog = Actionlog::query() + ->whereNotNull('created_by') + ->where([ + 'action_type' => 'checkin from', + 'note' => 'Bulk checkin items', + 'target_id' => $log->target_id, + 'target_type' => $log->target_type, + 'created_at' => $log->updated_at, + ]) + ->first(); + + if ($similarLog) { + return $similarLog->created_by; + } + + return null; + } + } } From 2091f66f5bb175d9b3d66fc4d5a0483feff5c5fb Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 16:30:36 -0700 Subject: [PATCH 10/20] Reorganize --- .../Commands/FixActionLogTimestamps.php | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 256b5f3d46..ad3da7e49c 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -69,13 +69,15 @@ class FixActionLogTimestamps extends Command $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); $log->created_at = $log->updated_at; - $createdBy = $this->getCreatedByAttributeFromSimilarLog($log); + if (is_null($log->created_by)) { + $createdBy = $this->getCreatedByAttributeFromSimilarLog($log); - if ($createdBy) { - $this->line(vsprintf('Updating log id:%s created_by to %s', [$log->id, $createdBy])); - $log->created_by = $createdBy; - } else { - $this->line(vsprintf('No created_by found for log id:%s', [$log->id])); + if ($createdBy) { + $this->line(vsprintf('Updating log id:%s created_by to %s', [$log->id, $createdBy])); + $log->created_by = $createdBy; + } else { + $this->warn(vsprintf('No created_by found for log id:%s', [$log->id])); + } } if (!$this->dryrun) { @@ -96,23 +98,21 @@ class FixActionLogTimestamps extends Command private function getCreatedByAttributeFromSimilarLog(Actionlog $log): null|int { - if (is_null($log->created_by)) { - $similarLog = Actionlog::query() - ->whereNotNull('created_by') - ->where([ - 'action_type' => 'checkin from', - 'note' => 'Bulk checkin items', - 'target_id' => $log->target_id, - 'target_type' => $log->target_type, - 'created_at' => $log->updated_at, - ]) - ->first(); + $similarLog = Actionlog::query() + ->whereNotNull('created_by') + ->where([ + 'action_type' => 'checkin from', + 'note' => 'Bulk checkin items', + 'target_id' => $log->target_id, + 'target_type' => $log->target_type, + 'created_at' => $log->updated_at, + ]) + ->first(); - if ($similarLog) { - return $similarLog->created_by; - } - - return null; + if ($similarLog) { + return $similarLog->created_by; } + + return null; } } From 73f64c53b1d074d354c60ed5d225493b35300052 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 16:42:39 -0700 Subject: [PATCH 11/20] Skip logs where created_by cannot be set --- .../Commands/FixActionLogTimestamps.php | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index ad3da7e49c..60ca9c00e5 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -36,6 +36,7 @@ class FixActionLogTimestamps extends Command if ($this->dryrun) { $this->info('This is a DRY RUN - no changes will be saved.'); + $this->newLine(); } // Logs that were improperly timestamped should have created_at in the 1970s @@ -62,33 +63,40 @@ class FixActionLogTimestamps extends Command } if ($this->dryrun) { + $this->newLine(); $this->info('DRY RUN. NOT ACTUALLY UPDATING LOGS.'); } foreach ($logs as $log) { - $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); - $log->created_at = $log->updated_at; + $this->newLine(); + $this->info('Processing log id:' . $log->id); if (is_null($log->created_by)) { - $createdBy = $this->getCreatedByAttributeFromSimilarLog($log); + $createdByFromSimilarLog = $this->getCreatedByAttributeFromSimilarLog($log); - if ($createdBy) { - $this->line(vsprintf('Updating log id:%s created_by to %s', [$log->id, $createdBy])); - $log->created_by = $createdBy; + if ($createdByFromSimilarLog) { + $this->line(vsprintf('Updating log id:%s created_by to %s', [$log->id, $createdByFromSimilarLog])); + $log->created_by = $createdByFromSimilarLog; } else { $this->warn(vsprintf('No created_by found for log id:%s', [$log->id])); + $this->warn('Skipping updating this log since no similar log was found to update created_by from.'); + + continue; } } + $this->line(vsprintf('Updating log id:%s from %s to %s', [$log->id, $log->created_at, $log->updated_at])); + $log->created_at = $log->updated_at; + if (!$this->dryrun) { Model::withoutTimestamps(function () use ($log) { $log->saveQuietly(); }); } - - $this->newLine(); } + $this->newLine(); + if ($this->dryrun) { $this->info('DRY RUN. NO CHANGES WERE ACTUALLY MADE.'); } From 9cea6cee265c6f0fde2c984587bc477bb43f3139 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 16:42:50 -0700 Subject: [PATCH 12/20] Docblock --- app/Console/Commands/FixActionLogTimestamps.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 60ca9c00e5..61d974cec0 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -104,6 +104,10 @@ class FixActionLogTimestamps extends Command return 0; } + /** + * This method attempts to find a bulk check in log that was + * created at the same time as the log passed in. + */ private function getCreatedByAttributeFromSimilarLog(Actionlog $log): null|int { $similarLog = Actionlog::query() From 32b194ddc76d32855819e6984832d2485a3e8b8f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 16:44:37 -0700 Subject: [PATCH 13/20] Update description --- app/Console/Commands/FixActionLogTimestamps.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixActionLogTimestamps.php index 61d974cec0..503758e7dc 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixActionLogTimestamps.php @@ -15,13 +15,12 @@ class FixActionLogTimestamps extends Command */ protected $signature = 'snipeit:fix-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; - // @todo: /** * The console command description. * * @var string */ - protected $description = 'Command description'; + protected $description = 'This script attempts to fix timestamps and missing created_by values for bulk checkin entries in the log table'; private bool $dryrun = false; @@ -42,8 +41,6 @@ class FixActionLogTimestamps extends Command // Logs that were improperly timestamped should have created_at in the 1970s $logs = Actionlog::whereYear('created_at', '1970')->get(); - // @todo: handle created_by being null... - $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps:'); $this->table( From 388dc232415715af109be68760123863043895e3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 17:02:05 -0700 Subject: [PATCH 14/20] Add comments --- ...php => FixBulkAccessoryCheckinActionLogTimestamps.php} | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) rename app/Console/Commands/{FixActionLogTimestamps.php => FixBulkAccessoryCheckinActionLogTimestamps.php} (87%) diff --git a/app/Console/Commands/FixActionLogTimestamps.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php similarity index 87% rename from app/Console/Commands/FixActionLogTimestamps.php rename to app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php index 503758e7dc..e586415029 100644 --- a/app/Console/Commands/FixActionLogTimestamps.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php @@ -6,14 +6,14 @@ use App\Models\Actionlog; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Model; -class FixActionLogTimestamps extends Command +class FixBulkAccessoryCheckinActionLogTimestamps extends Command { /** * The name and signature of the console command. * * @var string */ - protected $signature = 'snipeit:fix-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; + protected $signature = 'snipeit:fix-bulk-accessory-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; /** * The console command description. @@ -68,6 +68,9 @@ class FixActionLogTimestamps extends Command $this->newLine(); $this->info('Processing log id:' . $log->id); + // created_by was not being set for accessory bulk checkins + // so let's see if there was another bulk checkin log + // with the same timestamp and a created_by value we can use. if (is_null($log->created_by)) { $createdByFromSimilarLog = $this->getCreatedByAttributeFromSimilarLog($log); @@ -78,6 +81,7 @@ class FixActionLogTimestamps extends Command $this->warn(vsprintf('No created_by found for log id:%s', [$log->id])); $this->warn('Skipping updating this log since no similar log was found to update created_by from.'); + // If we can't find a similar log then let's skip updating it continue; } } From 650839b68ad8c080373c95b0a60f5aab26840fad Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 12 Mar 2025 17:05:16 -0700 Subject: [PATCH 15/20] Update command name --- ...stamps.php => FixBulkAccessoryCheckinActionLogEntries.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename app/Console/Commands/{FixBulkAccessoryCheckinActionLogTimestamps.php => FixBulkAccessoryCheckinActionLogEntries.php} (96%) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php similarity index 96% rename from app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php rename to app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index e586415029..610ce938a4 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogTimestamps.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -6,14 +6,14 @@ use App\Models\Actionlog; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Model; -class FixBulkAccessoryCheckinActionLogTimestamps extends Command +class FixBulkAccessoryCheckinActionLogEntries extends Command { /** * The name and signature of the console command. * * @var string */ - protected $signature = 'snipeit:fix-bulk-accessory-action-log-timestamps {--dryrun : Run the sync process but don\'t update the database}'; + protected $signature = 'snipeit:fix-bulk-accessory-action-log-entries {--dryrun : Run the sync process but don\'t update the database}'; /** * The console command description. From faee50c2226d2f07ecf9b9f4fcddebe9872ab451 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 3 Apr 2025 12:56:07 -0700 Subject: [PATCH 16/20] Use dry-run instead of dryrun --- .../Commands/FixBulkAccessoryCheckinActionLogEntries.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index 610ce938a4..70d0a4460b 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -13,7 +13,7 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command * * @var string */ - protected $signature = 'snipeit:fix-bulk-accessory-action-log-entries {--dryrun : Run the sync process but don\'t update the database}'; + protected $signature = 'snipeit:fix-bulk-accessory-action-log-entries {--dry-run : Run the sync process but don\'t update the database}'; /** * The console command description. @@ -29,7 +29,7 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command */ public function handle() { - if ($this->option('dryrun')) { + if ($this->option('dry-run')) { $this->dryrun = true; } From 6dd3ab2ec90fe4bca1368c8ae626c7af31a89130 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 3 Apr 2025 12:56:46 -0700 Subject: [PATCH 17/20] Exit early if no logs found --- .../Commands/FixBulkAccessoryCheckinActionLogEntries.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index 70d0a4460b..8879430c6b 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -41,6 +41,11 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command // Logs that were improperly timestamped should have created_at in the 1970s $logs = Actionlog::whereYear('created_at', '1970')->get(); + if ($logs->isEmpty()) { + $this->info('No logs found with incorrect timestamps.'); + return 0; + } + $this->info('Found ' . $logs->count() . ' logs with incorrect timestamps:'); $this->table( From 4ba58b25468575edbdf4cd378c50736487644046 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 3 Apr 2025 13:10:45 -0700 Subject: [PATCH 18/20] Constrain query --- .../FixBulkAccessoryCheckinActionLogEntries.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index 8879430c6b..47a2586080 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Models\Accessory; use App\Models\Actionlog; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Model; @@ -38,8 +39,14 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command $this->newLine(); } - // Logs that were improperly timestamped should have created_at in the 1970s - $logs = Actionlog::whereYear('created_at', '1970')->get(); + $logs = Actionlog::query() + // only look for accessory checkin logs + ->where('item_type', Accessory::class) + // that were part of a bulk checkin + ->where('note', 'Bulk checkin items') + // logs that were improperly timestamped should have created_at in the 1970s + ->whereYear('created_at', '1970') + ->get(); if ($logs->isEmpty()) { $this->info('No logs found with incorrect timestamps.'); From 18ef88bd6790b012ce8509915eafcffd2773dd77 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 3 Apr 2025 13:13:52 -0700 Subject: [PATCH 19/20] Improve comment --- .../Commands/FixBulkAccessoryCheckinActionLogEntries.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index 47a2586080..096f8f9e64 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -118,6 +118,9 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command } /** + * Hopefully the bulk checkin included other items like assets or licenses + * so we can use one of those logs to get the correct created_by value. + * * This method attempts to find a bulk check in log that was * created at the same time as the log passed in. */ From 9a8e5bf61e8fb8b899d36be0513d052e1cd7ee57 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 3 Apr 2025 14:39:40 -0700 Subject: [PATCH 20/20] Backup before running updates --- .../FixBulkAccessoryCheckinActionLogEntries.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php index 096f8f9e64..694d8d3b30 100644 --- a/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php +++ b/app/Console/Commands/FixBulkAccessoryCheckinActionLogEntries.php @@ -14,7 +14,7 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command * * @var string */ - protected $signature = 'snipeit:fix-bulk-accessory-action-log-entries {--dry-run : Run the sync process but don\'t update the database}'; + protected $signature = 'snipeit:fix-bulk-accessory-action-log-entries {--dry-run : Run the sync process but don\'t update the database} {--skip-backup : Skip pre-execution backup}'; /** * The console command description. @@ -24,15 +24,15 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command protected $description = 'This script attempts to fix timestamps and missing created_by values for bulk checkin entries in the log table'; private bool $dryrun = false; + private bool $skipBackup = false; /** * Execute the console command. */ public function handle() { - if ($this->option('dry-run')) { - $this->dryrun = true; - } + $this->skipBackup = $this->option('skip-backup'); + $this->dryrun = $this->option('dry-run'); if ($this->dryrun) { $this->info('This is a DRY RUN - no changes will be saved.'); @@ -71,6 +71,11 @@ class FixBulkAccessoryCheckinActionLogEntries extends Command return 0; } + if (!$this->dryrun && !$this->skipBackup) { + $this->info('Backing up the database before making changes...'); + $this->call('snipeit:backup'); + } + if ($this->dryrun) { $this->newLine(); $this->info('DRY RUN. NOT ACTUALLY UPDATING LOGS.');