diff --git a/database/migrations/2026_03_17_111249_replace_tenant1_account_codes_with_kis_standard.php b/database/migrations/2026_03_17_111249_replace_tenant1_account_codes_with_kis_standard.php new file mode 100644 index 00000000..43bd5d63 --- /dev/null +++ b/database/migrations/2026_03_17_111249_replace_tenant1_account_codes_with_kis_standard.php @@ -0,0 +1,68 @@ +where('tenant_id', '!=', 1) + ->whereRaw('LENGTH(code) = 5') + ->value('tenant_id'); + + if (! $sourceTenantId) { + // 소스 테넌트가 없으면 스킵 + return; + } + + // 1. tenant_id=1 기존 데이터 전체 삭제 + DB::table('account_codes')->where('tenant_id', 1)->delete(); + + // 2. 소스 테넌트의 5자리 코드를 tenant_id=1로 복사 (활성 상태) + $sourceCodes = DB::table('account_codes') + ->where('tenant_id', $sourceTenantId) + ->whereRaw('LENGTH(code) = 5') + ->get(); + + $now = now(); + $inserts = $sourceCodes->map(fn ($row) => [ + 'tenant_id' => 1, + 'code' => $row->code, + 'name' => $row->name, + 'category' => $row->category, + 'sub_category' => $row->sub_category, + 'parent_code' => $row->parent_code, + 'depth' => $row->depth, + 'department_type' => $row->department_type, + 'description' => $row->description, + 'sort_order' => $row->sort_order, + 'is_active' => true, + 'created_at' => $now, + 'updated_at' => $now, + ])->toArray(); + + // chunk insert (한 번에 100건씩) + foreach (array_chunk($inserts, 100) as $chunk) { + DB::table('account_codes')->insert($chunk); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // 롤백 불가 (기존 3자리 데이터 복원 불가능) + // 필요 시 백업에서 복원 + } +};