diff --git a/database/migrations/2026_09_06_110218_fix_work_orders_company_id.php b/database/migrations/2026_09_06_110218_fix_work_orders_company_id.php new file mode 100644 index 0000000..1f81c45 --- /dev/null +++ b/database/migrations/2026_09_06_110218_fix_work_orders_company_id.php @@ -0,0 +1,52 @@ +where('slug', 'psauto')->first(); + if (! $psauto) { + return; // psauto doesn't exist on this environment — skip + } + + // Find companies that have WOs but are NOT psauto + $otherCompanyIds = DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', '!=', $psauto->id) + ->pluck('company_id') + ->unique(); + + foreach ($otherCompanyIds as $companyId) { + // Only move if that company has no users (ghost/test tenant) + $userCount = DB::table('users')->where('company_id', $companyId)->count(); + if ($userCount > 0) { + continue; // Real company with users — leave its WOs alone + } + + $moved = DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', $companyId) + ->update(['company_id' => $psauto->id]); + + \Illuminate\Support\Facades\Log::info( + "Migration fix_work_orders_company_id: moved {$moved} WOs from company_id={$companyId} to psauto company_id={$psauto->id}" + ); + } + } + + public function down(): void + { + // Not reversible — data migration only + } +};