fix hamkar table
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* اگر 2026_05_25_163201 در جدول migrations از طریق baseline علامت خورده
|
||||||
|
* ولی ستونهای provider روی orders ساخته نشده — idempotent پر میکند.
|
||||||
|
* بدون این، deploy:tenant → Nothing to migrate و «تراکنش همکار» خالی میماند.
|
||||||
|
*/
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
if (! Schema::hasTable('orders')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->addOrdersColumnIfMissing(
|
||||||
|
'provider_id',
|
||||||
|
'BIGINT UNSIGNED NULL',
|
||||||
|
'id',
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$afterTotalPrice = $this->ordersColumnExists('total_price') ? 'total_price' : null;
|
||||||
|
$this->addOrdersColumnIfMissing(
|
||||||
|
'total_price_montzar',
|
||||||
|
'DECIMAL(20,4) NULL DEFAULT NULL',
|
||||||
|
$afterTotalPrice,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// عمداً خالی — rollback ستون را حذف نمیکند
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ordersTable(): string
|
||||||
|
{
|
||||||
|
return DB::getTablePrefix().'orders';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ordersColumnExists(string $column): bool
|
||||||
|
{
|
||||||
|
$result = DB::selectOne(
|
||||||
|
'SELECT COUNT(*) AS cnt FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?',
|
||||||
|
[DB::connection()->getDatabaseName(), $this->ordersTable(), $column]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (int) $result->cnt > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ordersIndexExists(string $indexName): bool
|
||||||
|
{
|
||||||
|
$result = DB::selectOne(
|
||||||
|
'SELECT COUNT(*) AS cnt FROM information_schema.STATISTICS
|
||||||
|
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?',
|
||||||
|
[DB::connection()->getDatabaseName(), $this->ordersTable(), $indexName]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (int) $result->cnt > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addOrdersColumnIfMissing(
|
||||||
|
string $column,
|
||||||
|
string $definition,
|
||||||
|
?string $after,
|
||||||
|
bool $withIndex
|
||||||
|
): void {
|
||||||
|
if ($this->ordersColumnExists($column)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$table = $this->ordersTable();
|
||||||
|
$afterClause = ($after !== null && $this->ordersColumnExists($after))
|
||||||
|
? " AFTER `{$after}`"
|
||||||
|
: '';
|
||||||
|
|
||||||
|
DB::statement("ALTER TABLE `{$table}` ADD COLUMN `{$column}` {$definition}{$afterClause}");
|
||||||
|
|
||||||
|
if ($withIndex && $column === 'provider_id' && ! $this->ordersIndexExists('orders_provider_id_index')) {
|
||||||
|
DB::statement("ALTER TABLE `{$table}` ADD INDEX `orders_provider_id_index` (`provider_id`)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user