diff --git a/database/migrations/2026_03_20_120000_migrate_daily_work_logs_to_codebridge.php b/database/migrations/2026_03_20_120000_migrate_daily_work_logs_to_codebridge.php new file mode 100644 index 00000000..067972ad --- /dev/null +++ b/database/migrations/2026_03_20_120000_migrate_daily_work_logs_to_codebridge.php @@ -0,0 +1,84 @@ +copyOrder as $table) { + if (! Schema::hasTable($table)) { + continue; + } + + $samCount = DB::table($table)->count(); + if ($samCount === 0) { + continue; + } + + if (! Schema::connection($this->cb)->hasTable($table)) { + throw new \RuntimeException( + "[MIGRATION ABORT] codebridge.{$table} 테이블이 없습니다. " + .'2026_03_19_200000 마이그레이션을 먼저 실행하세요.' + ); + } + + $cbExisting = DB::connection($this->cb)->table($table)->count(); + if ($cbExisting > 0) { + continue; + } + + DB::connection($this->cb)->statement( + "INSERT INTO `{$cbDb}`.`{$table}` SELECT * FROM `{$samDb}`.`{$table}`" + ); + + $cbCount = DB::connection($this->cb)->table($table)->count(); + if ($samCount !== $cbCount) { + throw new \RuntimeException( + "[MIGRATION ABORT] 데이터 불일치! {$table}: sam={$samCount}, codebridge={$cbCount}" + ); + } + } + + // ─── Phase 2: sam 테이블 삭제 (자식 → 부모 순서) ─── + DB::statement('SET FOREIGN_KEY_CHECKS=0'); + foreach ($this->dropOrder as $table) { + Schema::dropIfExists($table); + } + DB::statement('SET FOREIGN_KEY_CHECKS=1'); + } + + public function down(): void + { + // 롤백은 수동 처리 — 데이터가 이미 codebridge에 있으므로 자동 롤백 불가 + } +};