From b170755c3dbc2f125f94ec40baeeb5055fe2cf3b Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 6 Feb 2024 15:52:46 +0000 Subject: [PATCH 1/2] Switch to bulk updating to handle audit interval updates Signed-off-by: snipe --- app/Http/Controllers/SettingsController.php | 27 +++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 3ba114b43..86b2abf7c 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -28,6 +28,7 @@ use App\Http\Requests\SlackSettingsRequest; use Illuminate\Support\Str; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Validator; +use Carbon\Carbon; /** * This controller handles all actions related to Settings for @@ -636,21 +637,21 @@ class SettingsController extends Controller // Check if the audit interval has changed - if it has, we want to update ALL of the assets audit dates if ($request->input('audit_interval') != $setting->audit_interval) { - // Be careful - this could be a negative number + // This could be a negative number if the user is trying to set the audit interval to a lower number than it was before $audit_diff_months = ((int)$request->input('audit_interval') - (int)($setting->audit_interval)); - - // Grab all assets that have an existing next_audit_date, chunking to handle very large datasets - Asset::whereNotNull('next_audit_date')->chunk(200, function ($assets) use ($audit_diff_months) { - // Update assets' next_audit_date values - foreach ($assets as $asset) { - if ($asset->next_audit_date != '') { - $old_next_audit = new \DateTime($asset->next_audit_date); - $asset->next_audit_date = $old_next_audit->modify($audit_diff_months . ' month')->format('Y-m-d'); - $asset->forceSave(); - } - } - }); + // Batch update the dates. We have to use this method to avoid time limit exceeded errors on very large datasets, + // but it DOES mean this change doesn't get logged in the action logs, since it skips the observer. + // @see https://stackoverflow.com/questions/54879160/laravel-observer-not-working-on-bulk-insert + $affected = Asset::whereNotNull('next_audit_date') + ->whereNull('deleted_at') + ->update( + ['next_audit_date' => DB::raw('DATE_ADD(`next_audit_date`, INTERVAL '.$audit_diff_months.' MONTH)')] + ); + + \Log::debug($affected .' assets affected by audit interval update'); + + } $alert_email = rtrim($request->input('alert_email'), ','); From bf674a0f4d32f168bbc2001464a51b1ee04e056a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 6 Feb 2024 15:58:36 +0000 Subject: [PATCH 2/2] Removed backticks Signed-off-by: snipe --- app/Http/Controllers/SettingsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 86b2abf7c..31ea724c0 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -646,7 +646,7 @@ class SettingsController extends Controller $affected = Asset::whereNotNull('next_audit_date') ->whereNull('deleted_at') ->update( - ['next_audit_date' => DB::raw('DATE_ADD(`next_audit_date`, INTERVAL '.$audit_diff_months.' MONTH)')] + ['next_audit_date' => DB::raw('DATE_ADD(next_audit_date, INTERVAL '.$audit_diff_months.' MONTH)')] ); \Log::debug($affected .' assets affected by audit interval update');