fix: 데이터 이관 마이그레이션 null 코드 처리 추가

- materials.material_code가 null인 경우 자동 코드 생성 (SM-000001 형식)
- item_id_mappings 테이블 중복 생성 방지 로직 추가

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-13 15:44:59 +09:00
parent 80281e65b7
commit bc77d72ba6

View File

@@ -22,17 +22,21 @@
*/ */
public function up(): void public function up(): void
{ {
// ID 매핑 테이블 생성 // ID 매핑 테이블 생성 (없으면 생성, 있으면 초기화)
Schema::create('item_id_mappings', function (Blueprint $table) { if (! Schema::hasTable('item_id_mappings')) {
$table->id(); Schema::create('item_id_mappings', function (Blueprint $table) {
$table->string('source_table', 20)->comment('products or materials'); $table->id();
$table->unsignedBigInteger('source_id')->comment('원본 테이블 ID'); $table->string('source_table', 20)->comment('products or materials');
$table->unsignedBigInteger('item_id')->comment('items 테이블 ID'); $table->unsignedBigInteger('source_id')->comment('원본 테이블 ID');
$table->timestamps(); $table->unsignedBigInteger('item_id')->comment('items 테이블 ID');
$table->timestamps();
$table->unique(['source_table', 'source_id']); $table->unique(['source_table', 'source_id']);
$table->index('item_id'); $table->index('item_id');
}); });
} else {
DB::table('item_id_mappings')->truncate();
}
$productCount = 0; $productCount = 0;
$materialCount = 0; $materialCount = 0;
@@ -107,11 +111,17 @@ public function up(): void
->get(); ->get();
foreach ($materials as $material) { foreach ($materials as $material) {
// NULL 코드 처리: 자동 생성
$code = $material->material_code;
if (empty($code)) {
$code = sprintf('%s-%06d', $material->material_type, $material->id);
}
// items 테이블에 삽입 // items 테이블에 삽입
$itemId = DB::table('items')->insertGetId([ $itemId = DB::table('items')->insertGetId([
'tenant_id' => $material->tenant_id, 'tenant_id' => $material->tenant_id,
'item_type' => $material->material_type, 'item_type' => $material->material_type,
'code' => $material->material_code, 'code' => $code,
'name' => $material->name, 'name' => $material->name,
'unit' => $material->unit, 'unit' => $material->unit,
'category_id' => $material->category_id, 'category_id' => $material->category_id,