diff --git a/database/migrations/2025_01_08_100000_add_tenant_id_to_work_order_sub_tables.php b/database/migrations/2025_12_26_100400_add_tenant_id_to_work_order_sub_tables.php similarity index 100% rename from database/migrations/2025_01_08_100000_add_tenant_id_to_work_order_sub_tables.php rename to database/migrations/2025_12_26_100400_add_tenant_id_to_work_order_sub_tables.php diff --git a/database/migrations/2025_01_09_100000_create_work_order_assignees_table.php b/database/migrations/2025_12_26_100600_create_work_order_assignees_table.php similarity index 100% rename from database/migrations/2025_01_09_100000_create_work_order_assignees_table.php rename to database/migrations/2025_12_26_100600_create_work_order_assignees_table.php diff --git a/database/migrations/2025_01_09_100000_change_work_orders_process_type_to_process_id.php b/database/migrations/2025_12_26_183200_change_work_orders_process_type_to_process_id.php similarity index 81% rename from database/migrations/2025_01_09_100000_change_work_orders_process_type_to_process_id.php rename to database/migrations/2025_12_26_183200_change_work_orders_process_type_to_process_id.php index 14ede0c..fbf1593 100644 --- a/database/migrations/2025_01_09_100000_change_work_orders_process_type_to_process_id.php +++ b/database/migrations/2025_12_26_183200_change_work_orders_process_type_to_process_id.php @@ -24,29 +24,43 @@ public function up(): void // 1. 절곡 공정이 없으면 각 테넌트별로 생성 $this->ensureBendingProcessExists(); - // 2. process_id 컬럼 추가 (nullable로 먼저 생성) - Schema::table('work_orders', function (Blueprint $table) { - $table->unsignedBigInteger('process_id') - ->nullable() - ->after('sales_order_id') - ->comment('공정 ID (FK → processes.id)'); - }); + // 2. process_id 컬럼 추가 (이미 존재하면 스킵) + if (!Schema::hasColumn('work_orders', 'process_id')) { + Schema::table('work_orders', function (Blueprint $table) { + $table->unsignedBigInteger('process_id') + ->nullable() + ->after('sales_order_id') + ->comment('공정 ID (FK → processes.id)'); + }); + } // 3. 기존 process_type 데이터를 process_id로 마이그레이션 $this->migrateProcessTypeToProcessId(); - // 4. process_id에 FK 제약 추가 및 NOT NULL로 변경 - Schema::table('work_orders', function (Blueprint $table) { - $table->foreign('process_id') - ->references('id') - ->on('processes') - ->onDelete('restrict'); - }); + // 4. process_id에 FK 제약 추가 (이미 존재하면 스킵) + $hasFk = DB::select(" + SELECT COUNT(*) as cnt FROM information_schema.KEY_COLUMN_USAGE + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'work_orders' + AND COLUMN_NAME = 'process_id' + AND REFERENCED_TABLE_NAME IS NOT NULL + ")[0]->cnt > 0; - // 5. 기존 process_type 컬럼 제거 - Schema::table('work_orders', function (Blueprint $table) { - $table->dropColumn('process_type'); - }); + if (!$hasFk) { + Schema::table('work_orders', function (Blueprint $table) { + $table->foreign('process_id') + ->references('id') + ->on('processes') + ->onDelete('restrict'); + }); + } + + // 5. 기존 process_type 컬럼 제거 (존재하면 제거) + if (Schema::hasColumn('work_orders', 'process_type')) { + Schema::table('work_orders', function (Blueprint $table) { + $table->dropColumn('process_type'); + }); + } } public function down(): void