fix:마이그레이션 인덱스 존재 여부 체크 추가
- sales_scenario_unique 인덱스 삭제 전 존재 여부 확인 - sales_scenario_checkpoint_unique 생성 전 존재 여부 확인 - 서버 환경에서 이미 수동으로 인덱스가 변경된 경우 대응 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration
|
||||||
{
|
{
|
||||||
@@ -11,19 +12,29 @@
|
|||||||
*/
|
*/
|
||||||
public function up(): void
|
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) {
|
Schema::table('sales_scenario_checklists', function (Blueprint $table) {
|
||||||
// 기존 유니크 키 삭제
|
|
||||||
$table->dropUnique('sales_scenario_unique');
|
|
||||||
|
|
||||||
// checkpoint_index를 nullable로 변경
|
|
||||||
$table->unsignedTinyInteger('checkpoint_index')->nullable()->change();
|
$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
|
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) {
|
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->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'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user