From f35f9f922ee75fb4140a1548d47d5a6bf97ec108 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 14 Sep 2023 15:32:17 -0500 Subject: [PATCH] safety & catch & log --- ...and_add_column_for_explicit_date_to_assets.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php b/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php index 60bca98b73..a29e343086 100644 --- a/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php +++ b/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php @@ -5,6 +5,7 @@ use Carbon\CarbonImmutable; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Log; class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration { @@ -24,7 +25,11 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration Asset::whereNotNull('asset_eol_date')->chunk(500, function ($assetsWithEolDates) { foreach ($assetsWithEolDates as $asset) { if ($asset->asset_eol_date && $asset->purchase_date) { - $months = CarbonImmutable::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); + try { + $months = CarbonImmutable::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); + } catch (\Exception $e) { + Log::info('asset_eol_date invalid for asset '.$asset->id); + } if ($asset->model->eol) { if ($months != $asset->model->eol) { $asset->update(['eol_explicit' => true]); @@ -40,8 +45,12 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration Asset::whereNull('asset_eol_date')->chunk(500, function ($assets) { foreach ($assets as $asset) { $model = Asset::find($asset->id)->model; - if (!empty($model->eol)) { - $asset_eol_date = CarbonImmutable::parse($asset->purchase_date)->addMonths($model->eol)->format('Y-m-d'); + if (!empty($model->eol) && !empty($asset->purchase_date)) { + try { + $asset_eol_date = CarbonImmutable::parse($asset->purchase_date)->addMonths($model->eol)->format('Y-m-d'); + } catch (\Exception $e) { + Log::info('purchase date invalid for asset '.$asset->id); + } $asset->update(['asset_eol_date' => $asset_eol_date]); } }