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 1eb8d0084a..d2a8bb97fb 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 @@ -18,34 +18,37 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration Schema::table('assets', function (Blueprint $table) { $table->boolean('eol_explicit')->default(false)->after('asset_eol_date'); }); - - + + // Update the eol_explicit column with the value from asset_eol_date if it exists and is different from the calculated value - $assetsWithEolDates = Asset::whereNotNull('asset_eol_date')->get(); - foreach($assetsWithEolDates as $asset) { - if($asset->asset_eol_date && $asset->purchase_date) { - $months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); - if($asset->model->eol) { - if($months != $asset->model->eol) { + $assetsWithEolDates = Asset::whereNotNull('asset_eol_date')->chunk(100, function ($assetsWithEolDates) { + foreach ($assetsWithEolDates as $asset) { + if ($asset->asset_eol_date && $asset->purchase_date) { + $months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); + if ($asset->model->eol) { + if ($months != $asset->model->eol) { + $asset->update(['eol_explicit' => true]); + } + } else { $asset->update(['eol_explicit' => true]); } - } else { - $asset->update(['eol_explicit' => true]); } } - } + }); unset($assetsWithEolDates); // Update the asset_eol_date column with the calculated value if it doesn't exist - $assets = Asset::whereNull('asset_eol_date')->get(); - foreach ($assets as $asset) { - $model = Asset::find($asset->id)->model; - if (!empty($model->eol)) { + $assets = Asset::whereNull('asset_eol_date')->chunk(100, function ($assets) { + foreach ($assets as $asset) { + $model = Asset::find($asset->id)->model; + if (!empty($model->eol)) { $asset_eol_date = Carbon::parse($asset->purchase_date)->addMonths($model->eol)->format('Y-m-d'); $asset->update(['asset_eol_date' => $asset_eol_date]); } } - } + }); + } + /** * Reverse the migrations.