diff --git a/database/migrations/2025_12_14_012415_drop_legacy_product_material_id_columns.php b/database/migrations/2025_12_14_012415_drop_legacy_product_material_id_columns.php new file mode 100644 index 0000000..79a8ade --- /dev/null +++ b/database/migrations/2025_12_14_012415_drop_legacy_product_material_id_columns.php @@ -0,0 +1,111 @@ +dropColumn('product_id'); + }); + } + + // 2. order_items.product_id 삭제 + if (Schema::hasTable('order_items') && Schema::hasColumn('order_items', 'product_id')) { + Schema::table('order_items', function (Blueprint $table) { + $table->dropColumn('product_id'); + }); + } + + // 3. quotes.product_id 삭제 + if (Schema::hasTable('quotes') && Schema::hasColumn('quotes', 'product_id')) { + Schema::table('quotes', function (Blueprint $table) { + $table->dropColumn('product_id'); + }); + } + + // 4. material_receipts.material_id 삭제 + if (Schema::hasTable('material_receipts') && Schema::hasColumn('material_receipts', 'material_id')) { + Schema::table('material_receipts', function (Blueprint $table) { + $table->dropColumn('material_id'); + }); + } + + // 5. lots.material_id 삭제 + if (Schema::hasTable('lots') && Schema::hasColumn('lots', 'material_id')) { + Schema::table('lots', function (Blueprint $table) { + $table->dropColumn('material_id'); + }); + } + + DB::statement("SELECT 'Dropped legacy product_id/material_id columns' AS result"); + } + + /** + * Reverse the migrations. + * + * 주의: 컬럼 복원만 수행, 데이터는 item_id_mappings로 복원 필요 + */ + public function down(): void + { + // 1. orders.product_id 복원 + if (Schema::hasTable('orders') && ! Schema::hasColumn('orders', 'product_id')) { + Schema::table('orders', function (Blueprint $table) { + $table->unsignedBigInteger('product_id')->nullable()->after('category_code') + ->comment('(deprecated) 기존 products 참조'); + }); + } + + // 2. order_items.product_id 복원 + if (Schema::hasTable('order_items') && ! Schema::hasColumn('order_items', 'product_id')) { + Schema::table('order_items', function (Blueprint $table) { + $table->unsignedBigInteger('product_id')->nullable()->after('serial_no') + ->comment('(deprecated) 기존 products 참조'); + }); + } + + // 3. quotes.product_id 복원 + if (Schema::hasTable('quotes') && ! Schema::hasColumn('quotes', 'product_id')) { + Schema::table('quotes', function (Blueprint $table) { + $table->unsignedBigInteger('product_id')->nullable()->after('product_category') + ->comment('(deprecated) 기존 products 참조'); + }); + } + + // 4. material_receipts.material_id 복원 + if (Schema::hasTable('material_receipts') && ! Schema::hasColumn('material_receipts', 'material_id')) { + Schema::table('material_receipts', function (Blueprint $table) { + $table->unsignedBigInteger('material_id')->nullable()->first() + ->comment('(deprecated) 기존 materials 참조'); + }); + } + + // 5. lots.material_id 복원 + if (Schema::hasTable('lots') && ! Schema::hasColumn('lots', 'material_id')) { + Schema::table('lots', function (Blueprint $table) { + $table->unsignedBigInteger('material_id')->nullable()->after('id') + ->comment('(deprecated) 기존 materials 참조'); + }); + } + + // 데이터 복원 안내 + DB::statement("SELECT 'Columns restored. Use item_id_mappings to restore data if needed.' AS warning"); + } +};