fix: [db] daily_work_logs sam→codebridge 데이터 이관 마이그레이션 추가
- 2026_03_19_200000에서 existingTables 누락으로 데이터 미이관된 문제 해결 - 부모→자식 순서로 복사, 자식→부모 순서로 삭제 (FK 안전) - 건수 검증 후 sam 테이블 삭제
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* daily_work_logs / daily_work_log_items 데이터를 sam → codebridge DB로 이관.
|
||||
*
|
||||
* 배경:
|
||||
* - 2026_03_19_200000 마이그레이션에서 codebridge에 빈 테이블은 생성했으나
|
||||
* existingTables에 daily_work_logs가 누락되어 데이터 이관이 빠짐.
|
||||
* - MNG 모델은 이미 $connection = 'codebridge'로 변경되어
|
||||
* 운영서버에서 데이터가 보이지 않는 상태.
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
private string $cb = 'codebridge';
|
||||
|
||||
// 복사 순서: 부모 → 자식 (FK 참조 순서)
|
||||
private array $copyOrder = [
|
||||
'daily_work_logs',
|
||||
'daily_work_log_items',
|
||||
];
|
||||
|
||||
// 삭제 순서: 자식 → 부모 (FK 역순)
|
||||
private array $dropOrder = [
|
||||
'daily_work_log_items',
|
||||
'daily_work_logs',
|
||||
];
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
$samDb = config('database.connections.mysql.database', 'sam');
|
||||
$cbDb = config('database.connections.codebridge.database', 'codebridge');
|
||||
|
||||
// ─── Phase 1: 데이터 복사 (부모 → 자식 순서) ───
|
||||
foreach ($this->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에 있으므로 자동 롤백 불가
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user