fallback handled
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('products')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('products', 'trade_by_quantity')) {
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('products', 'show_in_assets')) {
|
||||
$table->boolean('trade_by_quantity')->default(false)->after('show_in_assets');
|
||||
} else {
|
||||
$table->boolean('trade_by_quantity')->default(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$this->backfillQuantityTradeForLegacyProducts();
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('products') || ! Schema::hasColumn('products', 'trade_by_quantity')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
$table->dropColumn('trade_by_quantity');
|
||||
});
|
||||
}
|
||||
|
||||
private function backfillQuantityTradeForLegacyProducts(): void
|
||||
{
|
||||
$mainProductIds = $this->resolveMainProductIds();
|
||||
|
||||
DB::table('products')
|
||||
->where(function ($query) use ($mainProductIds) {
|
||||
$query->whereIn('group_type', ['آبشده', 'کاتد مس']);
|
||||
|
||||
if ($mainProductIds !== []) {
|
||||
$query->orWhereIn('id', $mainProductIds);
|
||||
}
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where('trade_by_quantity', false)
|
||||
->orWhereNull('trade_by_quantity');
|
||||
})
|
||||
->update(['trade_by_quantity' => true]);
|
||||
}
|
||||
|
||||
/** @return array<int, int> */
|
||||
private function resolveMainProductIds(): array
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
if (Schema::hasTable('settings')) {
|
||||
$value = DB::table('settings')->where('key', 'main_product')->value('value');
|
||||
$id = $this->parsePositiveInt($value);
|
||||
if ($id !== null) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
if (Schema::hasTable('const')) {
|
||||
$value = DB::table('const')->where('key', 'main_product')->value('value');
|
||||
$id = $this->parsePositiveInt($value);
|
||||
if ($id !== null) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
private function parsePositiveInt(mixed $value): ?int
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = trim((string) $value);
|
||||
if ($value === '' || ! ctype_digit($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = (int) $value;
|
||||
|
||||
return $id > 0 ? $id : null;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user