From 99d409c0a5a98c449f8b285ec2685ebdd9026a7c Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 14:32:28 -0500 Subject: [PATCH 1/9] transaction --- ...add_column_for_explicit_date_to_assets.php | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 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 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); + } } } - } + }); }); } From cb10c7af2798b82f8aec4b05ae42137785d89829 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 14:33:35 -0500 Subject: [PATCH 2/9] oops --- ...ormalized_eol_and_add_column_for_explicit_date_to_assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ce1528defb..c1eee5ea1b 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 @@ -36,7 +36,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration if ($asset->model->eol) { if ($months != $asset->model->eol) { $assetToUpdateEolExplicit = $asset->id; - //$asset->update(['eol_explicit' => true]); + $asset->update(['eol_explicit' => true]); } } else { $asset->update(['eol_explicit' => true]); From ad6b5020059dc77c520cab6be4b0c74e60f68398 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 14:37:18 -0500 Subject: [PATCH 3/9] comments --- ...malized_eol_and_add_column_for_explicit_date_to_assets.php | 4 ++-- 1 file changed, 2 insertions(+), 2 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 c1eee5ea1b..306b551f7b 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 @@ -23,7 +23,7 @@ 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 - $assetsToUpdateEolExplicit = []; + //$assetsToUpdateEolExplicit = []; DB::transaction(function() { Asset::whereNotNull('asset_eol_date')->with('model')->chunkById(500, function ($assetsWithEolDates) { foreach ($assetsWithEolDates as $asset) { @@ -35,7 +35,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } if ($asset->model->eol) { if ($months != $asset->model->eol) { - $assetToUpdateEolExplicit = $asset->id; + //$assetToUpdateEolExplicit = $asset->id; $asset->update(['eol_explicit' => true]); } } else { From 101bd9c4049fc6d2fe7ef583224c40449dc9a63d Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 14:47:14 -0500 Subject: [PATCH 4/9] kill transaction, try update array --- ...eol_and_add_column_for_explicit_date_to_assets.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 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 306b551f7b..7b477c9a3b 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 @@ -23,8 +23,7 @@ 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 - //$assetsToUpdateEolExplicit = []; - DB::transaction(function() { + $assetsToUpdateEolExplicit = []; Asset::whereNotNull('asset_eol_date')->with('model')->chunkById(500, function ($assetsWithEolDates) { foreach ($assetsWithEolDates as $asset) { if ($asset->asset_eol_date && $asset->purchase_date) { @@ -35,20 +34,19 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } if ($asset->model->eol) { if ($months != $asset->model->eol) { - //$assetToUpdateEolExplicit = $asset->id; + $assetsToUpdateEolExplicit = $asset->id; $asset->update(['eol_explicit' => true]); } } else { + $assetsToUpdateEolExplicit = $asset->id; $asset->update(['eol_explicit' => true]); } } } }); - }); - //Asset::whereIn('id', $assetToUpdateEolExplicit)->update(['eol_explicit' => true]); + Asset::whereIn('id', $assetsToUpdateEolExplicit)->update(['eol_explicit' => true]); // 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) { @@ -61,7 +59,6 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } } }); - }); } From 6cda9056b8854d7b877c6cb90c361018bc7d51bd Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 14:56:18 -0500 Subject: [PATCH 5/9] oops --- ...malized_eol_and_add_column_for_explicit_date_to_assets.php | 4 ++-- 1 file changed, 2 insertions(+), 2 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 7b477c9a3b..756d48f849 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 @@ -35,11 +35,11 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration if ($asset->model->eol) { if ($months != $asset->model->eol) { $assetsToUpdateEolExplicit = $asset->id; - $asset->update(['eol_explicit' => true]); + //$asset->update(['eol_explicit' => true]); } } else { $assetsToUpdateEolExplicit = $asset->id; - $asset->update(['eol_explicit' => true]); + //$asset->update(['eol_explicit' => true]); } } } From 46af40bd027168a1ab521e58c6cc49d433c28884 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 15:20:16 -0500 Subject: [PATCH 6/9] ????? --- ...add_column_for_explicit_date_to_assets.php | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 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 756d48f849..15f54f4e05 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 @@ -23,7 +23,6 @@ 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 - $assetsToUpdateEolExplicit = []; Asset::whereNotNull('asset_eol_date')->with('model')->chunkById(500, function ($assetsWithEolDates) { foreach ($assetsWithEolDates as $asset) { if ($asset->asset_eol_date && $asset->purchase_date) { @@ -34,31 +33,30 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } if ($asset->model->eol) { if ($months != $asset->model->eol) { - $assetsToUpdateEolExplicit = $asset->id; - //$asset->update(['eol_explicit' => true]); + $asset->update(['eol_explicit' => true]); } } else { - $assetsToUpdateEolExplicit = $asset->id; - //$asset->update(['eol_explicit' => true]); + $asset->update(['eol_explicit' => true]); } } } }); - Asset::whereIn('id', $assetsToUpdateEolExplicit)->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); - } - } - } - }); + // 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); + // } + // } + // } + // }); + Asset::whereNull('asset_eol_date')->whereNotNull('purchase_date')->has('model') + ->update(['asset_eol_date' => DB::raw('LEFT JOIN models ON assets.model_id = models.id) DATE_ADD(purchase_date, INTERVAL models.eol MONTH)')]); } From 8c9961aba8ab27c796b0391f5c4b8659082032f3 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 15:40:42 -0500 Subject: [PATCH 7/9] refactor --- ...eol_and_add_column_for_explicit_date_to_assets.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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 15f54f4e05..86f1844fce 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 @@ -55,8 +55,15 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration // } // } // }); - Asset::whereNull('asset_eol_date')->whereNotNull('purchase_date')->has('model') - ->update(['asset_eol_date' => DB::raw('LEFT JOIN models ON assets.model_id = models.id) DATE_ADD(purchase_date, INTERVAL models.eol MONTH)')]); + // Asset::whereNull('asset_eol_date')->whereNotNull('purchase_date')->has('model') + // ->update(['asset_eol_date' => DB::raw('LEFT JOIN models ON assets.model_id = models.id) DATE_ADD(purchase_date, INTERVAL models.eol MONTH)')]); + DB::table('assets') + ->whereNull('asset_eol_date') + ->whereNotNull('purchase_date') + ->join('models', 'assets.model_id', '=', 'models.id') + ->update([ + 'asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL models.eol MONTH)') + ]); } From cc82e86eeb761437ce9c6cff575274c0950ab4d3 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 16:54:56 -0500 Subject: [PATCH 8/9] one more check --- ...normalized_eol_and_add_column_for_explicit_date_to_assets.php | 1 + 1 file changed, 1 insertion(+) 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 86f1844fce..a84688f773 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 @@ -60,6 +60,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration DB::table('assets') ->whereNull('asset_eol_date') ->whereNotNull('purchase_date') + ->whereNotNull('model_id') ->join('models', 'assets.model_id', '=', 'models.id') ->update([ 'asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL models.eol MONTH)') From c07c0c0f74bbc33c391a2b014f635e3387c7a121 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 4 Oct 2023 16:55:35 -0500 Subject: [PATCH 9/9] cleanup --- ...and_add_column_for_explicit_date_to_assets.php | 15 --------------- 1 file changed, 15 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 a84688f773..8bceca535e 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 @@ -42,21 +42,6 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } }); - // 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); - // } - // } - // } - // }); - // Asset::whereNull('asset_eol_date')->whereNotNull('purchase_date')->has('model') - // ->update(['asset_eol_date' => DB::raw('LEFT JOIN models ON assets.model_id = models.id) DATE_ADD(purchase_date, INTERVAL models.eol MONTH)')]); DB::table('assets') ->whereNull('asset_eol_date') ->whereNotNull('purchase_date')