From ef32046e857cf26ae4b3d7b6b677a2b6a36c393e Mon Sep 17 00:00:00 2001 From: adonis Date: Sun, 2 Aug 2026 14:00:00 +0330 Subject: [PATCH] =?UTF-8?q?=D8=A8=D9=87=D8=B7=20=D8=B4=DB=8C=DB=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...nsure_moshtari_code_on_customers_table.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 database/migrations/2026_08_02_000001_ensure_moshtari_code_on_customers_table.php diff --git a/database/migrations/2026_08_02_000001_ensure_moshtari_code_on_customers_table.php b/database/migrations/2026_08_02_000001_ensure_moshtari_code_on_customers_table.php new file mode 100644 index 0000000..7dcb782 --- /dev/null +++ b/database/migrations/2026_08_02_000001_ensure_moshtari_code_on_customers_table.php @@ -0,0 +1,80 @@ +firstExistingColumn([ + 'tahesab_id', + 'finance_code', + 'pargar_id', + 'mobile', + ]); + + $this->addCustomerColumnIfMissing( + 'moshtari_code', + 'INT NULL DEFAULT NULL', + $after + ); + } + + public function down(): void + { + // عمداً خالی — rollback ستون حسابداری را حذف نمی‌کند + } + + /** @param list $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}"); + } +};