|
@@ -0,0 +1,105 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+use Illuminate\Database\Migrations\Migration;
|
|
|
|
|
+use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
+use Illuminate\Support\Facades\Schema;
|
|
|
|
|
+
|
|
|
|
|
+return new class extends Migration
|
|
|
|
|
+{
|
|
|
|
|
+ public function up(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ // ── Step 1: release any FKs / indexes that may still exist ───────────
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $existing = $this->existingConstraints();
|
|
|
|
|
+
|
|
|
|
|
+ if (in_array('product_variant_images_product_image_id_foreign', $existing['fks'])) {
|
|
|
|
|
+ $table->dropForeign('product_variant_images_product_image_id_foreign');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (in_array('product_variant_images_product_variant_id_foreign', $existing['fks'])) {
|
|
|
|
|
+ $table->dropForeign('product_variant_images_product_variant_id_foreign');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (in_array('variant_image_unique', $existing['indexes'])) {
|
|
|
|
|
+ $table->dropUnique('variant_image_unique');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (in_array('idx_image_id', $existing['indexes'])) {
|
|
|
|
|
+ $table->dropIndex('idx_image_id');
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // ── Step 2: rename column if it hasn't been renamed yet ──────────────
|
|
|
|
|
+ if (Schema::hasColumn('product_variant_images', 'product_image_id')) {
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $table->renameColumn('product_image_id', 'media_id');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ── Step 3: ensure media_id is bigint unsigned to match media.id ─────
|
|
|
|
|
+ DB::statement('ALTER TABLE product_variant_images MODIFY COLUMN media_id BIGINT UNSIGNED NOT NULL');
|
|
|
|
|
+
|
|
|
|
|
+ // ── Step 4: restore constraints ──────────────────────────────────────
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $table->foreign('product_variant_id')
|
|
|
|
|
+ ->references('id')
|
|
|
|
|
+ ->on('product_variants')
|
|
|
|
|
+ ->onDelete('cascade');
|
|
|
|
|
+
|
|
|
|
|
+ $table->foreign('media_id')
|
|
|
|
|
+ ->references('id')
|
|
|
|
|
+ ->on('media')
|
|
|
|
|
+ ->onDelete('cascade');
|
|
|
|
|
+
|
|
|
|
|
+ $table->unique(['product_variant_id', 'media_id'], 'variant_media_unique');
|
|
|
|
|
+ $table->index('media_id', 'idx_media_id');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function down(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $table->dropForeign(['media_id']);
|
|
|
|
|
+ $table->dropForeign(['product_variant_id']);
|
|
|
|
|
+ $table->dropUnique('variant_media_unique');
|
|
|
|
|
+ $table->dropIndex('idx_media_id');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ DB::statement('ALTER TABLE product_variant_images MODIFY COLUMN media_id INT UNSIGNED NOT NULL');
|
|
|
|
|
+
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $table->renameColumn('media_id', 'product_image_id');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ Schema::table('product_variant_images', function (Blueprint $table) {
|
|
|
|
|
+ $table->foreign('product_variant_id')
|
|
|
|
|
+ ->references('id')
|
|
|
|
|
+ ->on('product_variants')
|
|
|
|
|
+ ->onDelete('cascade');
|
|
|
|
|
+
|
|
|
|
|
+ $table->unique(['product_variant_id', 'product_image_id'], 'variant_image_unique');
|
|
|
|
|
+ $table->index('product_image_id', 'idx_image_id');
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function existingConstraints(): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $fks = DB::select("
|
|
|
|
|
+ SELECT CONSTRAINT_NAME
|
|
|
|
|
+ FROM information_schema.TABLE_CONSTRAINTS
|
|
|
|
|
+ WHERE TABLE_NAME = 'product_variant_images'
|
|
|
|
|
+ AND CONSTRAINT_SCHEMA = DATABASE()
|
|
|
|
|
+ AND CONSTRAINT_TYPE = 'FOREIGN KEY'
|
|
|
|
|
+ ");
|
|
|
|
|
+
|
|
|
|
|
+ $indexes = DB::select("
|
|
|
|
|
+ SELECT DISTINCT INDEX_NAME
|
|
|
|
|
+ FROM information_schema.STATISTICS
|
|
|
|
|
+ WHERE TABLE_NAME = 'product_variant_images'
|
|
|
|
|
+ AND TABLE_SCHEMA = DATABASE()
|
|
|
|
|
+ ");
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'fks' => array_column($fks, 'CONSTRAINT_NAME'),
|
|
|
|
|
+ 'indexes' => array_column($indexes, 'INDEX_NAME'),
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+};
|