بهط شیی

This commit is contained in:
2026-08-02 14:00:00 +03:30
parent eaff9dead2
commit ef32046e85
@@ -0,0 +1,80 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* ستون moshtari_code روی بعضی دپلوی‌ها نیست و register با
* findClaimableImportedByMobile → Unknown column 'moshtari_code' می‌خورد.
* این مایگریشن idempotent است و اگر ستون باشد هیچ کاری نمی‌کند.
*/
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('customers')) {
return;
}
$after = $this->firstExistingColumn([
'tahesab_id',
'finance_code',
'pargar_id',
'mobile',
]);
$this->addCustomerColumnIfMissing(
'moshtari_code',
'INT NULL DEFAULT NULL',
$after
);
}
public function down(): void
{
// عمداً خالی — rollback ستون حسابداری را حذف نمی‌کند
}
/** @param list<string> $candidates */
private function firstExistingColumn(array $candidates): ?string
{
foreach ($candidates as $column) {
if ($this->customerColumnExists($column)) {
return $column;
}
}
return null;
}
private function customersTable(): string
{
return DB::getTablePrefix().'customers';
}
private function customerColumnExists(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->customersTable(), $column]
);
return (int) $result->cnt > 0;
}
private function addCustomerColumnIfMissing(string $column, string $definition, ?string $after = null): void
{
if ($this->customerColumnExists($column)) {
return;
}
$table = $this->customersTable();
$afterClause = ($after !== null && $this->customerColumnExists($after))
? " AFTER `{$after}`"
: '';
DB::statement("ALTER TABLE `{$table}` ADD COLUMN `{$column}` {$definition}{$afterClause}");
}
};