safety & catch & log

This commit is contained in:
spencerrlongg 2023-09-14 15:32:17 -05:00
parent 44441ec703
commit f35f9f922e

View file

@ -5,6 +5,7 @@ use Carbon\CarbonImmutable;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Log;
class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
{ {
@ -24,7 +25,11 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
Asset::whereNotNull('asset_eol_date')->chunk(500, function ($assetsWithEolDates) { Asset::whereNotNull('asset_eol_date')->chunk(500, function ($assetsWithEolDates) {
foreach ($assetsWithEolDates as $asset) { foreach ($assetsWithEolDates as $asset) {
if ($asset->asset_eol_date && $asset->purchase_date) { if ($asset->asset_eol_date && $asset->purchase_date) {
try {
$months = CarbonImmutable::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); $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 ($asset->model->eol) {
if ($months != $asset->model->eol) { if ($months != $asset->model->eol) {
$asset->update(['eol_explicit' => true]); $asset->update(['eol_explicit' => true]);
@ -40,8 +45,12 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
Asset::whereNull('asset_eol_date')->chunk(500, function ($assets) { Asset::whereNull('asset_eol_date')->chunk(500, function ($assets) {
foreach ($assets as $asset) { foreach ($assets as $asset) {
$model = Asset::find($asset->id)->model; $model = Asset::find($asset->id)->model;
if (!empty($model->eol)) { if (!empty($model->eol) && !empty($asset->purchase_date)) {
try {
$asset_eol_date = CarbonImmutable::parse($asset->purchase_date)->addMonths($model->eol)->format('Y-m-d'); $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]); $asset->update(['asset_eol_date' => $asset_eol_date]);
} }
} }