From 8c6f83b776bd0e4b7249bf1ff80ae6f5abc21af1 Mon Sep 17 00:00:00 2001 From: ScuffedNewt Date: Wed, 28 Aug 2024 17:40:11 +0100 Subject: [PATCH] feat(migration): make migration fit-for-purpose as core feature --- .../Commands/ConvertCharacterSubtype.php | 21 ++++------------ ...8_28_103434_convert_character_subtypes.php | 24 ++++++++++++++----- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/app/Console/Commands/ConvertCharacterSubtype.php b/app/Console/Commands/ConvertCharacterSubtype.php index 0d8816ba46..c944bf16a8 100644 --- a/app/Console/Commands/ConvertCharacterSubtype.php +++ b/app/Console/Commands/ConvertCharacterSubtype.php @@ -45,28 +45,17 @@ public function handle() { return; } - Schema::create('character_image_subtypes', function (Blueprint $table) { - $table->integer('character_image_id')->unsigned(); - $table->integer('subtype_id')->unsigned(); - }); - - // for design update requests - // has to be two for the renameColumn to work - Schema::table('design_updates', function (Blueprint $table) { - // rename the column - $table->renameColumn('subtype_id', 'subtype_ids'); - }); - Schema::table('design_updates', function (Blueprint $table) { - // make it a string type instead of an integer - $table->string('subtype_ids')->nullable()->default(null)->change(); - }); - $updates = DB::table('design_updates')->where('subtype_ids', '!=', null)->get(); + // DESIGN UPDATES + $updates = DB::table('design_updates')->where('subtype_id', '!=', null)->get(); // make the string into an array foreach ($updates as $update) { $update->update([ 'subtype_ids' => json_encode([$update->subtype_ids]), ]); } + Schema::table('design_updates', function (Blueprint $table) { + $table->dropColumn('subtype_id'); + }); $characterImages = CharacterImage::whereNotNull('subtype_id')->get(); diff --git a/database/migrations/2024_08_28_103434_convert_character_subtypes.php b/database/migrations/2024_08_28_103434_convert_character_subtypes.php index 4adfcbb8f6..fca99bb1b7 100644 --- a/database/migrations/2024_08_28_103434_convert_character_subtypes.php +++ b/database/migrations/2024_08_28_103434_convert_character_subtypes.php @@ -13,12 +13,14 @@ public function up(): void { // if (!Schema::hasTable('character_image_subtypes')) { - $this->call('convert-character-subtype'); - } + Schema::create('character_image_subtypes', function (Blueprint $table) { + $table->integer('character_image_id')->unsigned(); + $table->integer('subtype_id')->unsigned(); + }); - // check call was successful - if (!Schema::hasTable('character_image_subtypes')) { - throw new \Exception('The character_image_subtypes table does not exist.'); + Schema::table('design_updates', function (Blueprint $table) { + $table->string('subtype_ids')->nullable()->default(null); + }); } } @@ -28,6 +30,16 @@ public function up(): void public function down(): void { // - throw new \Exception('This migration cannot be reversed.'); + Schema::dropIfExists('character_image_subtypes'); + + Schema::table('design_updates', function (Blueprint $table) { + $table->dropColumn('subtype_ids'); + }); + + if (!Schema::hasColumn('design_updates', 'subtype_id')) { + Schema::table('design_updates', function (Blueprint $table) { + $table->integer('subtype_id')->unsigned()->nullable()->default(null); + }); + } } };