feat: update migration to modify enum columns and rename 'reject' to 'reject_selling' in product_prices and orders

This commit is contained in:
Yoga Pangestu 2026-08-16 21:14:35 +07:00
parent 99cd75120f
commit 099a342a20

View File

@ -7,12 +7,18 @@
{ {
public function up(): void public function up(): void
{ {
// 1. product_prices: rename 'reject' → 'reject_selling', then add 'reject_capital' rows // 1. Modify enum columns FIRST to include new values (MySQL only)
if (DB::getDriverName() === 'mysql') {
DB::statement("ALTER TABLE product_prices MODIFY COLUMN type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject_capital','reject_selling') NOT NULL");
DB::statement("ALTER TABLE orders MODIFY COLUMN price_type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject_capital','reject_selling') NOT NULL");
}
// 2. product_prices: rename 'reject' → 'reject_selling'
DB::table('product_prices') DB::table('product_prices')
->where('type', 'reject') ->where('type', 'reject')
->update(['type' => 'reject_selling']); ->update(['type' => 'reject_selling']);
// Insert reject_capital rows (price = 0) for each variant that now has reject_selling // 3. Insert reject_capital rows (price = 0) for each variant that now has reject_selling
$rejectSellingPrices = DB::table('product_prices') $rejectSellingPrices = DB::table('product_prices')
->where('type', 'reject_selling') ->where('type', 'reject_selling')
->get(); ->get();
@ -27,25 +33,15 @@ public function up(): void
]); ]);
} }
// 2. orders: rename 'reject' → 'reject_selling' // 4. orders: rename 'reject' → 'reject_selling'
DB::table('orders') DB::table('orders')
->where('price_type', 'reject') ->where('price_type', 'reject')
->update(['price_type' => 'reject_selling']); ->update(['price_type' => 'reject_selling']);
// 3. Modify enum columns to reflect new values (MySQL only)
if (DB::getDriverName() === 'mysql') {
DB::statement("ALTER TABLE product_prices MODIFY COLUMN type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject_capital','reject_selling') NOT NULL");
DB::statement("ALTER TABLE orders MODIFY COLUMN price_type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject_capital','reject_selling') NOT NULL");
}
} }
public function down(): void public function down(): void
{ {
// Reverse: rename back and clean up // 1. Rename data back FIRST while ENUM still allows new values
DB::table('product_prices')
->where('type', 'reject_capital')
->delete();
DB::table('product_prices') DB::table('product_prices')
->where('type', 'reject_selling') ->where('type', 'reject_selling')
->update(['type' => 'reject']); ->update(['type' => 'reject']);
@ -54,6 +50,12 @@ public function down(): void
->where('price_type', 'reject_selling') ->where('price_type', 'reject_selling')
->update(['price_type' => 'reject']); ->update(['price_type' => 'reject']);
// 2. Delete reject_capital rows
DB::table('product_prices')
->where('type', 'reject_capital')
->delete();
// 3. Modify enum columns to remove new values (MySQL only)
if (DB::getDriverName() === 'mysql') { if (DB::getDriverName() === 'mysql') {
DB::statement("ALTER TABLE product_prices MODIFY COLUMN type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject') NOT NULL"); DB::statement("ALTER TABLE product_prices MODIFY COLUMN type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject') NOT NULL");
DB::statement("ALTER TABLE orders MODIFY COLUMN price_type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject') NOT NULL"); DB::statement("ALTER TABLE orders MODIFY COLUMN price_type ENUM('distributor','agent','sub_agent','wholesale','retail','tiktok','shopee','capital','reject') NOT NULL");