From d3d3d8b7b557e343a06fa25b512f2bfee6c7f8e0 Mon Sep 17 00:00:00 2001 From: mohammad sedighi Date: Fri, 26 Jun 2026 22:41:37 +0330 Subject: [PATCH] fix migrate --- ...al_referrals_column_on_customers_table.php | 112 +++++++++--------- 1 file changed, 59 insertions(+), 53 deletions(-) diff --git a/database/migrations/2026_06_25_000001_ensure_total_referrals_column_on_customers_table.php b/database/migrations/2026_06_25_000001_ensure_total_referrals_column_on_customers_table.php index 8ae6bf1..6f6bf72 100644 --- a/database/migrations/2026_06_25_000001_ensure_total_referrals_column_on_customers_table.php +++ b/database/migrations/2026_06_25_000001_ensure_total_referrals_column_on_customers_table.php @@ -1,7 +1,6 @@ string('referral_id', 20)->nullable()->after('type'); - } else { - $table->string('referral_id', 20)->nullable(); - } - }); - } + $this->addCustomerColumnIfMissing('referral_id', 'VARCHAR(20) NULL', 'type'); + $this->addCustomerColumnIfMissing('referral_to_id', 'VARCHAR(20) NULL', 'referral_id'); + $this->addCustomerColumnIfMissing('total_referrals', 'INT UNSIGNED NOT NULL DEFAULT 0', 'referral_to_id'); - if (! Schema::hasColumn('customers', 'referral_to_id')) { - Schema::table('customers', function (Blueprint $table) { - if (Schema::hasColumn('customers', 'referral_id')) { - $table->string('referral_to_id', 20)->nullable()->after('referral_id'); - } else { - $table->string('referral_to_id', 20)->nullable(); - } - }); - } - - if (! Schema::hasColumn('customers', 'total_referrals')) { - Schema::table('customers', function (Blueprint $table) { - if (Schema::hasColumn('customers', 'referral_to_id')) { - $table->unsignedInteger('total_referrals')->default(0)->after('referral_to_id'); - } else { - $table->unsignedInteger('total_referrals')->default(0); - } - }); - } + DB::purge(); + DB::reconnect(); if ( - ! Schema::hasColumn('customers', 'referral_id') - || ! Schema::hasColumn('customers', 'referral_to_id') - || ! Schema::hasColumn('customers', 'total_referrals') + ! $this->customerColumnExists('referral_id') + || ! $this->customerColumnExists('referral_to_id') + || ! $this->customerColumnExists('total_referrals') ) { return; } @@ -64,6 +40,36 @@ return new class extends Migration // عمداً خالی — rollback ستون را حذف نمی‌کند } + 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}"); + } + private function backfillMissingReferralIds(): void { $query = DB::table('customers') @@ -71,13 +77,11 @@ return new class extends Migration $q->whereNull('referral_id')->orWhere('referral_id', ''); }); - if (Schema::hasColumn('customers', 'deleted_at')) { + if ($this->customerColumnExists('deleted_at')) { $query->whereNull('deleted_at'); } - $customerIds = $query->pluck('id'); - - foreach ($customerIds as $customerId) { + foreach ($query->pluck('id') as $customerId) { do { $uniqueId = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 4); } while (DB::table('customers')->where('referral_id', $uniqueId)->exists()); @@ -88,23 +92,25 @@ return new class extends Migration private function backfillTotalReferrals(): void { - $deletedAtClause = Schema::hasColumn('customers', 'deleted_at') - ? 'AND `deleted_at` IS NULL' - : ''; + $query = DB::table('customers') + ->whereNotNull('referral_id') + ->where('referral_id', '!=', ''); - DB::statement(" - UPDATE `customers` AS c - SET c.`total_referrals` = ( - SELECT COUNT(*) - FROM ( - SELECT `id` - FROM `customers` - WHERE `referral_to_id` = c.`referral_id` - {$deletedAtClause} - ) AS r - ) - WHERE c.`referral_id` IS NOT NULL - AND TRIM(c.`referral_id`) <> '' - "); + if ($this->customerColumnExists('deleted_at')) { + $query->whereNull('deleted_at'); + } + + foreach ($query->get(['id', 'referral_id']) as $referrer) { + $countQuery = DB::table('customers') + ->where('referral_to_id', $referrer->referral_id); + + if ($this->customerColumnExists('deleted_at')) { + $countQuery->whereNull('deleted_at'); + } + + DB::table('customers') + ->where('id', $referrer->id) + ->update(['total_referrals' => $countQuery->count()]); + } } };