diff options
| author | grothedev <grothedev@gmail.com> | 2026-03-06 11:24:29 -0600 |
|---|---|---|
| committer | grothedev <grothedev@gmail.com> | 2026-03-06 11:24:29 -0600 |
| commit | d0304c13cbd5671c8020a142ec8c504d8a29b63c (patch) | |
| tree | 64671d71b4164a29b36beeac1942895d9968e36f /database | |
| parent | 11e296dc247d2f7d15fe677c0b1b3be659cc135d (diff) | |
improvements
Diffstat (limited to 'database')
| -rw-r--r-- | database/migrations/2026_03_06_171913_fix_pivot_table_fk_types.php | 98 | ||||
| -rw-r--r-- | database/migrations/99999_pivots.php | 31 |
2 files changed, 110 insertions, 19 deletions
diff --git a/database/migrations/2026_03_06_171913_fix_pivot_table_fk_types.php b/database/migrations/2026_03_06_171913_fix_pivot_table_fk_types.php new file mode 100644 index 0000000..14acf86 --- /dev/null +++ b/database/migrations/2026_03_06_171913_fix_pivot_table_fk_types.php @@ -0,0 +1,98 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + // Drop the broken quest_tag table (quests table has no real columns) + Schema::dropIfExists('quest_tag'); + + // Fix FK column types: integer unsigned -> unsignedBigInteger + // to match the bigIncrements id() columns on parent tables. + // For each pivot table: drop FKs, alter column types, re-add FKs. + + $pivots = [ + 'link_tag' => [ + ['col' => 'link_id', 'ref_table' => 'links'], + ['col' => 'tag_id', 'ref_table' => 'tags'], + ], + 'file_tag' => [ + ['col' => 'file_id', 'ref_table' => 'files'], + ['col' => 'tag_id', 'ref_table' => 'tags'], + ], + 'tag_writing' => [ + ['col' => 'writing_id', 'ref_table' => 'writings'], + ['col' => 'tag_id', 'ref_table' => 'tags'], + ], + 'role_user' => [ + ['col' => 'user_id', 'ref_table' => 'users'], + ['col' => 'role_id', 'ref_table' => 'roles'], + ], + ]; + + foreach ($pivots as $table => $columns) { + if (!Schema::hasTable($table)) { + continue; + } + + // Drop existing foreign keys + Schema::table($table, function (Blueprint $t) use ($columns) { + foreach ($columns as $fk) { + try { + $t->dropForeign([$fk['col']]); + } catch (\Exception $e) { + // FK may not exist + } + } + }); + + // Change column types + Schema::table($table, function (Blueprint $t) use ($columns) { + foreach ($columns as $fk) { + $t->unsignedBigInteger($fk['col'])->change(); + } + }); + + // Re-add foreign keys + Schema::table($table, function (Blueprint $t) use ($columns) { + foreach ($columns as $fk) { + $t->foreign($fk['col'])->references('id')->on($fk['ref_table'])->onDelete('cascade'); + } + }); + } + + // Create the missing tag_quote pivot table + if (!Schema::hasTable('tag_quote')) { + Schema::create('tag_quote', function (Blueprint $table) { + $table->unsignedBigInteger('quote_id')->index(); + $table->foreign('quote_id')->references('id')->on('quotes')->onDelete('cascade'); + $table->unsignedBigInteger('tag_id')->index(); + $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); + }); + } + } + + public function down(): void + { + // Drop tag_quote (we created it) + Schema::dropIfExists('tag_quote'); + + // Recreate quest_tag + if (!Schema::hasTable('quest_tag')) { + Schema::create('quest_tag', function (Blueprint $table) { + $table->unsignedBigInteger('quest_id')->index(); + $table->unsignedBigInteger('tag_id')->index(); + }); + } + + // Reverting column types back to integer unsigned is risky + // if IDs exceed integer range, so we leave them as unsignedBigInteger. + } +}; diff --git a/database/migrations/99999_pivots.php b/database/migrations/99999_pivots.php index edd497f..64cdbd5 100644 --- a/database/migrations/99999_pivots.php +++ b/database/migrations/99999_pivots.php @@ -13,39 +13,33 @@ return new class extends Migration public function up(): void { Schema::create('link_tag', function (Blueprint $table) { - $table->integer('link_id')->unsigned()->index(); + $table->unsignedBigInteger('link_id')->index(); $table->foreign('link_id')->references('id')->on('links')->onDelete('cascade'); - $table->integer('tag_id')->unsigned()->index(); + $table->unsignedBigInteger('tag_id')->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); Schema::create('file_tag', function (Blueprint $table) { - $table->integer('file_id')->unsigned()->index(); + $table->unsignedBigInteger('file_id')->index(); $table->foreign('file_id')->references('id')->on('files')->onDelete('cascade'); - $table->integer('tag_id')->unsigned()->index(); + $table->unsignedBigInteger('tag_id')->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); - Schema::create('quest_tag', function (Blueprint $table) { - $table->integer('quest_id')->unsigned()->index(); - $table->foreign('quest_id')->references('id')->on('quests')->onDelete('cascade'); - $table->integer('tag_id')->unsigned()->index(); - $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); - }); - Schema::create('tag_writing', function (Blueprint $table){ - $table->integer('writing_id')->unsigned()->index(); + Schema::create('tag_writing', function (Blueprint $table) { + $table->unsignedBigInteger('writing_id')->index(); $table->foreign('writing_id')->references('id')->on('writings')->onDelete('cascade'); - $table->integer('tag_id')->unsigned()->index(); + $table->unsignedBigInteger('tag_id')->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); Schema::create('role_user', function (Blueprint $table) { - $table->integer('user_id')->unsigned()->index(); + $table->unsignedBigInteger('user_id')->index(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); - $table->integer('role_id')->unsigned()->index(); + $table->unsignedBigInteger('role_id')->index(); $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); }); Schema::create('tag_quote', function (Blueprint $table) { - $table->integer('quote_id')->unsigned()->index(); + $table->unsignedBigInteger('quote_id')->index(); $table->foreign('quote_id')->references('id')->on('quotes')->onDelete('cascade'); - $table->integer('tag_id')->unsigned()->index(); + $table->unsignedBigInteger('tag_id')->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); } @@ -57,9 +51,8 @@ return new class extends Migration { Schema::dropIfExists('link_tag'); Schema::dropIfExists('file_tag'); - Schema::dropIfExists('quest_tag'); Schema::dropIfExists('tag_writing'); + Schema::dropIfExists('role_user'); Schema::dropIfExists('tag_quote'); - //Schema::dropIfExists('role_user'); } }; |
