transaction

This commit is contained in:
spencerrlongg 2023-10-04 14:32:28 -05:00
parent a2773aa895
commit 99d409c0a5

View file

@ -4,6 +4,7 @@ use App\Models\Asset;
use Carbon\CarbonImmutable; 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\DB;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -22,37 +23,44 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
// Update the eol_explicit column with the value from asset_eol_date if it exists and is different from the calculated value // Update the eol_explicit column with the value from asset_eol_date if it exists and is different from the calculated value
Asset::whereNotNull('asset_eol_date')->with('model')->chunkById(500, function ($assetsWithEolDates) { $assetsToUpdateEolExplicit = [];
foreach ($assetsWithEolDates as $asset) { DB::transaction(function() {
if ($asset->asset_eol_date && $asset->purchase_date) { Asset::whereNotNull('asset_eol_date')->with('model')->chunkById(500, function ($assetsWithEolDates) {
try { foreach ($assetsWithEolDates as $asset) {
$months = CarbonImmutable::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); if ($asset->asset_eol_date && $asset->purchase_date) {
} catch (\Exception $e) { try {
Log::info('asset_eol_date invalid for asset '.$asset->id); $months = CarbonImmutable::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
} } catch (\Exception $e) {
if ($asset->model->eol) { Log::info('asset_eol_date invalid for asset ' . $asset->id);
if ($months != $asset->model->eol) { }
if ($asset->model->eol) {
if ($months != $asset->model->eol) {
$assetToUpdateEolExplicit = $asset->id;
//$asset->update(['eol_explicit' => true]);
}
} else {
$asset->update(['eol_explicit' => true]); $asset->update(['eol_explicit' => true]);
} }
} else {
$asset->update(['eol_explicit' => true]);
} }
} }
} });
}); });
//Asset::whereIn('id', $assetToUpdateEolExplicit)->update(['eol_explicit' => true]);
// Update the asset_eol_date column with the calculated value if it doesn't exist // Update the asset_eol_date column with the calculated value if it doesn't exist
Asset::whereNull('asset_eol_date')->with('model')->chunkById(500, function ($assets) { DB::transaction(function () {
foreach ($assets as $asset) { Asset::whereNull('asset_eol_date')->with('model')->chunkById(500, function ($assets) {
if ($asset->model->eol && $asset->purchase_date) { foreach ($assets as $asset) {
try { if ($asset->model->eol && $asset->purchase_date) {
$asset_eol_date = CarbonImmutable::parse($asset->purchase_date)->addMonths($asset->model->eol)->format('Y-m-d'); try {
$asset->update(['asset_eol_date' => $asset_eol_date]); $asset_eol_date = CarbonImmutable::parse($asset->purchase_date)->addMonths($asset->model->eol)->format('Y-m-d');
} catch (\Exception $e) { $asset->update(['asset_eol_date' => $asset_eol_date]);
Log::info('purchase date invalid for asset '.$asset->id); } catch (\Exception $e) {
Log::info('purchase date invalid for asset ' . $asset->id);
}
} }
} }
} });
}); });
} }