Skip to content

A free open source IT asset/license management system

transaction

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