Skip to content

A free open source IT asset/license management system

add marcus' migration changes to branch

spencerrlongg7c30261ec889
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 982bd8ac0d..e0c2c229bb 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
@@ -3,6 +3,7 @@
use App\Models\Asset;
use Carbon\CarbonImmutable;
use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
@@ -25,24 +26,24 @@ 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) {
- $asset->update(['eol_explicit' => true]);
- }
- } else {
+ 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) {
$asset->update(['eol_explicit' => true]);
}
+ } else {
+ $asset->update(['eol_explicit' => true]);
}
}
- });
+ }
+ });
DB::table('assets')
->whereNull('asset_eol_date')
@@ -50,7 +51,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
->whereNotNull('model_id')
->join('models', 'assets.model_id', '=', 'models.id')
->update([
- 'asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL ' . DB::getTablePrefix() . 'models.eol MONTH)')
+ 'asset_eol_date' => $this->eolUpdateExpression(),
]);
}
@@ -63,7 +64,25 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
public function down()
{
Schema::table('assets', function (Blueprint $table) {
- $table->dropColumn('eol_explicit');
+ $table->dropColumn('eol_explicit');
});
}
-}
+
+ /**
+ * This method returns the correct database expression for either
+ * mysql, postgres, or sqlite depending on the driver being used.
+ */
+ private function eolUpdateExpression(): Expression
+ {
+ if (DB::getDriverName() === 'sqlite') {
+ return DB::raw("DATE(purchase_date, '+' || (SELECT eol FROM " . DB::getTablePrefix() . "models WHERE models.id = assets.model_id) || ' months')");
+ }
+
+ if (DB::getDriverName() === 'pgsql') {
+ return DB::raw("date(purchase_date + interval '1 month' * (SELECT eol FROM " . DB::getTablePrefix() . "models WHERE models.id = assets.model_id))");
+ }
+
+ // Default to MySQL's method
+ return DB::raw('DATE_ADD(purchase_date, INTERVAL ' . DB::getTablePrefix() . 'models.eol MONTH)');
+ }
+}
\ No newline at end of file