fix migrate
This commit is contained in:
+59
-53
@@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
@@ -17,40 +16,17 @@ return new class extends Migration
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! Schema::hasColumn('customers', 'referral_id')) {
|
$this->addCustomerColumnIfMissing('referral_id', 'VARCHAR(20) NULL', 'type');
|
||||||
Schema::table('customers', function (Blueprint $table) {
|
$this->addCustomerColumnIfMissing('referral_to_id', 'VARCHAR(20) NULL', 'referral_id');
|
||||||
if (Schema::hasColumn('customers', 'type')) {
|
$this->addCustomerColumnIfMissing('total_referrals', 'INT UNSIGNED NOT NULL DEFAULT 0', 'referral_to_id');
|
||||||
$table->string('referral_id', 20)->nullable()->after('type');
|
|
||||||
} else {
|
|
||||||
$table->string('referral_id', 20)->nullable();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! Schema::hasColumn('customers', 'referral_to_id')) {
|
DB::purge();
|
||||||
Schema::table('customers', function (Blueprint $table) {
|
DB::reconnect();
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
! Schema::hasColumn('customers', 'referral_id')
|
! $this->customerColumnExists('referral_id')
|
||||||
|| ! Schema::hasColumn('customers', 'referral_to_id')
|
|| ! $this->customerColumnExists('referral_to_id')
|
||||||
|| ! Schema::hasColumn('customers', 'total_referrals')
|
|| ! $this->customerColumnExists('total_referrals')
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -64,6 +40,36 @@ return new class extends Migration
|
|||||||
// عمداً خالی — rollback ستون را حذف نمیکند
|
// عمداً خالی — 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
|
private function backfillMissingReferralIds(): void
|
||||||
{
|
{
|
||||||
$query = DB::table('customers')
|
$query = DB::table('customers')
|
||||||
@@ -71,13 +77,11 @@ return new class extends Migration
|
|||||||
$q->whereNull('referral_id')->orWhere('referral_id', '');
|
$q->whereNull('referral_id')->orWhere('referral_id', '');
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Schema::hasColumn('customers', 'deleted_at')) {
|
if ($this->customerColumnExists('deleted_at')) {
|
||||||
$query->whereNull('deleted_at');
|
$query->whereNull('deleted_at');
|
||||||
}
|
}
|
||||||
|
|
||||||
$customerIds = $query->pluck('id');
|
foreach ($query->pluck('id') as $customerId) {
|
||||||
|
|
||||||
foreach ($customerIds as $customerId) {
|
|
||||||
do {
|
do {
|
||||||
$uniqueId = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 4);
|
$uniqueId = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 4);
|
||||||
} while (DB::table('customers')->where('referral_id', $uniqueId)->exists());
|
} while (DB::table('customers')->where('referral_id', $uniqueId)->exists());
|
||||||
@@ -88,23 +92,25 @@ return new class extends Migration
|
|||||||
|
|
||||||
private function backfillTotalReferrals(): void
|
private function backfillTotalReferrals(): void
|
||||||
{
|
{
|
||||||
$deletedAtClause = Schema::hasColumn('customers', 'deleted_at')
|
$query = DB::table('customers')
|
||||||
? 'AND `deleted_at` IS NULL'
|
->whereNotNull('referral_id')
|
||||||
: '';
|
->where('referral_id', '!=', '');
|
||||||
|
|
||||||
DB::statement("
|
if ($this->customerColumnExists('deleted_at')) {
|
||||||
UPDATE `customers` AS c
|
$query->whereNull('deleted_at');
|
||||||
SET c.`total_referrals` = (
|
}
|
||||||
SELECT COUNT(*)
|
|
||||||
FROM (
|
foreach ($query->get(['id', 'referral_id']) as $referrer) {
|
||||||
SELECT `id`
|
$countQuery = DB::table('customers')
|
||||||
FROM `customers`
|
->where('referral_to_id', $referrer->referral_id);
|
||||||
WHERE `referral_to_id` = c.`referral_id`
|
|
||||||
{$deletedAtClause}
|
if ($this->customerColumnExists('deleted_at')) {
|
||||||
) AS r
|
$countQuery->whereNull('deleted_at');
|
||||||
)
|
}
|
||||||
WHERE c.`referral_id` IS NOT NULL
|
|
||||||
AND TRIM(c.`referral_id`) <> ''
|
DB::table('customers')
|
||||||
");
|
->where('id', $referrer->id)
|
||||||
|
->update(['total_referrals' => $countQuery->count()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user