fix: 마이그레이션 실행 순서 및 멱등성 수정
- 작업지시 관련 마이그레이션 파일명 날짜 수정 (의존성 순서 해결) - 2025_01_08 → 2025_12_26_100400 (tenant_id 추가) - 2025_01_09 → 2025_12_26_100600 (assignees 테이블) - 2025_01_09 → 2025_12_26_183200 (process_type → process_id) - process_type → process_id 마이그레이션에 멱등성 체크 추가 - 컬럼 존재 여부 확인 (Schema::hasColumn) - FK 존재 여부 확인 (information_schema 쿼리)
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user