fix migrate
This commit is contained in:
+59
-53
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
@@ -17,40 +16,17 @@ return new class extends Migration
|
||||
return;
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('customers', 'referral_id')) {
|
||||
Schema::table('customers', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('customers', 'type')) {
|
||||
$table->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()]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user