fixed register ۲
This commit is contained in:
+54
@@ -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 ستون را حذف نمیکند
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
-- fix_missing_total_referrals_column.sql
|
||||||
|
--
|
||||||
|
-- مشکل: php artisan migrate میگوید Nothing to migrate
|
||||||
|
-- ولی register خطای Unknown column 'total_referrals' میدهد.
|
||||||
|
--
|
||||||
|
-- علت: baseline_migrations.sql این migration را در جدول migrations ثبت کرده
|
||||||
|
-- بدون اینکه ALTER TABLE واقعاً اجرا شود.
|
||||||
|
--
|
||||||
|
-- راهحل: این فایل را مستقیم روی MySQL اجرا کنید (migrate کافی نیست).
|
||||||
|
--
|
||||||
|
-- mysql -u USER -p DATABASE_NAME < database/sql/fix_missing_total_referrals_column.sql
|
||||||
|
--
|
||||||
|
-- یا از phpMyAdmin / Adminer همین کوئریها را بزنید.
|
||||||
|
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
|
SET @db = DATABASE();
|
||||||
|
|
||||||
|
-- 1) ستون را اضافه کن (اگر نبود)
|
||||||
|
SET @sql = IF(
|
||||||
|
(SELECT COUNT(*) FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'customers' AND COLUMN_NAME = 'total_referrals') = 0,
|
||||||
|
IF(
|
||||||
|
(SELECT COUNT(*) FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'customers' AND COLUMN_NAME = 'referral_to_id') > 0,
|
||||||
|
'ALTER TABLE `customers` ADD COLUMN `total_referrals` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `referral_to_id`',
|
||||||
|
'ALTER TABLE `customers` ADD COLUMN `total_referrals` INT UNSIGNED NOT NULL DEFAULT 0'
|
||||||
|
),
|
||||||
|
'SELECT ''total_referrals already exists'' AS info'
|
||||||
|
);
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
|
-- 2) شمارنده معرفها را با داده واقعی همتراز کن
|
||||||
|
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`) <> '';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
-- 3) تأیید
|
||||||
|
SELECT
|
||||||
|
CASE
|
||||||
|
WHEN COUNT(*) > 0 THEN 'OK: total_referrals exists'
|
||||||
|
ELSE 'ERROR: column still missing'
|
||||||
|
END AS status
|
||||||
|
FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'customers'
|
||||||
|
AND COLUMN_NAME = 'total_referrals';
|
||||||
Reference in New Issue
Block a user