Skip to content

Commit

Permalink
feat(migration): make migration fit-for-purpose as core feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Aug 28, 2024
1 parent d6bdcf1 commit 8c6f83b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
21 changes: 5 additions & 16 deletions app/Console/Commands/ConvertCharacterSubtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

Expand All @@ -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);
});
}
}
};

0 comments on commit 8c6f83b

Please sign in to comment.