diff --git a/database/migrations/2026_01_29_090000_fix_sales_scenario_checklists_unique_key.php b/database/migrations/2026_01_29_090000_fix_sales_scenario_checklists_unique_key.php index fee45fd..75fb113 100644 --- a/database/migrations/2026_01_29_090000_fix_sales_scenario_checklists_unique_key.php +++ b/database/migrations/2026_01_29_090000_fix_sales_scenario_checklists_unique_key.php @@ -3,6 +3,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\DB; return new class extends Migration { @@ -11,19 +12,29 @@ */ public function up(): void { + // 기존 유니크 키가 존재하면 삭제 + $indexes = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_unique'"); + if (count($indexes) > 0) { + Schema::table('sales_scenario_checklists', function (Blueprint $table) { + $table->dropUnique('sales_scenario_unique'); + }); + } + + // checkpoint_index를 nullable로 변경 Schema::table('sales_scenario_checklists', function (Blueprint $table) { - // 기존 유니크 키 삭제 - $table->dropUnique('sales_scenario_unique'); - - // checkpoint_index를 nullable로 변경 $table->unsignedTinyInteger('checkpoint_index')->nullable()->change(); - - // 새 유니크 키 생성 (checkpoint_id 기반) - $table->unique( - ['tenant_id', 'scenario_type', 'step_id', 'checkpoint_id'], - 'sales_scenario_checkpoint_unique' - ); }); + + // 새 유니크 키가 존재하지 않으면 생성 + $newIndexes = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_checkpoint_unique'"); + if (count($newIndexes) === 0) { + Schema::table('sales_scenario_checklists', function (Blueprint $table) { + $table->unique( + ['tenant_id', 'scenario_type', 'step_id', 'checkpoint_id'], + 'sales_scenario_checkpoint_unique' + ); + }); + } } /** @@ -31,18 +42,28 @@ public function up(): void */ public function down(): void { + // 새 유니크 키가 존재하면 삭제 + $indexes = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_checkpoint_unique'"); + if (count($indexes) > 0) { + Schema::table('sales_scenario_checklists', function (Blueprint $table) { + $table->dropUnique('sales_scenario_checkpoint_unique'); + }); + } + + // checkpoint_index를 NOT NULL로 복원 Schema::table('sales_scenario_checklists', function (Blueprint $table) { - // 새 유니크 키 삭제 - $table->dropUnique('sales_scenario_checkpoint_unique'); - - // checkpoint_index를 NOT NULL로 복원 $table->unsignedTinyInteger('checkpoint_index')->nullable(false)->change(); - - // 기존 유니크 키 복원 - $table->unique( - ['tenant_id', 'user_id', 'step_id', 'checkpoint_index'], - 'sales_scenario_unique' - ); }); + + // 기존 유니크 키가 존재하지 않으면 복원 + $oldIndexes = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_unique'"); + if (count($oldIndexes) === 0) { + Schema::table('sales_scenario_checklists', function (Blueprint $table) { + $table->unique( + ['tenant_id', 'user_id', 'step_id', 'checkpoint_index'], + 'sales_scenario_unique' + ); + }); + } } };