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