fixed register ۲

This commit is contained in:
2026-06-25 21:20:18 +03:30
parent 05dc2fecae
commit 936cc88e53
2 changed files with 114 additions and 0 deletions
@@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* اگر migration قدیمی در جدول migrations ثبت شده ولی ستون total_referrals ساخته نشده
* (Nothing to migrate + Unknown column total_referrals) — این فایل idempotent ستون را درست می‌کند.
*/
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('customers')) {
return;
}
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 (! Schema::hasColumn('customers', 'total_referrals')) {
return;
}
DB::statement("
UPDATE `customers` AS c
SET c.`total_referrals` = (
SELECT COUNT(*)
FROM (
SELECT `id`
FROM `customers`
WHERE `referral_to_id` = c.`referral_id`
AND `deleted_at` IS NULL
) AS r
)
WHERE c.`referral_id` IS NOT NULL
AND TRIM(c.`referral_id`) <> ''
");
}
public function down(): void
{
// عمداً خالی — rollback ستون را حذف نمی‌کند
}
};