From 5d3295607020313b844288e22efaa2da17f4ebcc Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 27 Nov 2023 12:35:13 -0800 Subject: [PATCH 1/5] Allow for migrating data when using sqlite --- ...d_add_column_for_explicit_date_to_assets.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 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 982bd8ac0d..e3fe4fb230 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; @@ -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(), ]); } @@ -66,4 +67,18 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration $table->dropColumn('eol_explicit'); }); } + + /** + * This method returns the correct database express for either + * mysql 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 models WHERE models.id = assets.model_id) || ' months')"); + } + + // Default to MySQL's method + return DB::raw('DATE_ADD(purchase_date, INTERVAL ' . DB::getTablePrefix() . 'models.eol MONTH)'); + } } From 37bd2970947a087837293e74ed1bc77fde39a64a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 27 Nov 2023 14:05:38 -0800 Subject: [PATCH 2/5] Fix typo --- ...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 e3fe4fb230..f47df93917 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 @@ -69,7 +69,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration } /** - * This method returns the correct database express for either + * This method returns the correct database expression for either * mysql or sqlite depending on the driver being used. */ private function eolUpdateExpression(): Expression From 72dbe951688b772b65d1a19f9fc15737428993ae Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 27 Nov 2023 15:55:33 -0800 Subject: [PATCH 3/5] Add table prefix --- ...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 f47df93917..aadceebc88 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 @@ -75,7 +75,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration private function eolUpdateExpression(): Expression { if (DB::getDriverName() === 'sqlite') { - return DB::raw("DATE(purchase_date, '+' || (SELECT eol FROM models WHERE models.id = assets.model_id) || ' months')"); + return DB::raw("DATE(purchase_date, '+' || (SELECT eol FROM " . DB::getTablePrefix() ."models WHERE models.id = assets.model_id) || ' months')"); } // Default to MySQL's method From 990358750b70e0dcfe5dd06dd3bf9a029d9ac414 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 27 Nov 2023 16:33:57 -0800 Subject: [PATCH 4/5] Add migration for postgres --- ...lized_eol_and_add_column_for_explicit_date_to_assets.php | 6 +++++- 1 file changed, 5 insertions(+), 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 aadceebc88..7be7e155d8 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 @@ -75,7 +75,11 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration 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')"); + 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 From 77ceac57475a9094c56792cb4c55fb954f0b1923 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 27 Nov 2023 16:36:51 -0800 Subject: [PATCH 5/5] Update docblock --- ...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 7be7e155d8..4781af7f0b 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 @@ -70,7 +70,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration /** * This method returns the correct database expression for either - * mysql or sqlite depending on the driver being used. + * mysql, postgres, or sqlite depending on the driver being used. */ private function eolUpdateExpression(): Expression {