diff --git a/database/migrations/2026_09_06_120000_fix_work_orders_company_id_v2.php b/database/migrations/2026_09_06_120000_fix_work_orders_company_id_v2.php new file mode 100644 index 0000000..c225e86 --- /dev/null +++ b/database/migrations/2026_09_06_120000_fix_work_orders_company_id_v2.php @@ -0,0 +1,56 @@ +where('slug', 'psauto')->first(); + if (! $psauto) { + return; + } + + // Get all companies that have WOs but are not psauto + $otherIds = DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', '!=', $psauto->id) + ->pluck('company_id') + ->unique(); + + foreach ($otherIds as $companyId) { + $company = DB::table('companies')->where('id', $companyId)->first(); + if (! $company) { + continue; + } + + $userCount = DB::table('users')->where('company_id', $companyId)->count(); + + // Move if: test/demo slug/name, OR company has ≤2 users (clearly not a real operational tenant) + $isTestCompany = str_contains(strtolower($company->slug), 'test') + || str_contains(strtolower($company->name ?? ''), 'test') + || $userCount <= 2; + + if (! $isTestCompany) { + Log::info("fix_wo_company_v2: skipping company [{$companyId}] {$company->name} ({$userCount} users) — looks like a real tenant"); + continue; + } + + $moved = DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', $companyId) + ->update(['company_id' => $psauto->id]); + + Log::info("fix_wo_company_v2: moved {$moved} WOs from company [{$companyId}] {$company->name} → psauto [{$psauto->id}]"); + } + } + + public function down(): void {} +};