From 2323f61d4355685e4a601cb2a66226788403f12b Mon Sep 17 00:00:00 2001 From: Vasyka Date: Sun, 6 Sep 2026 11:02:39 +0000 Subject: [PATCH] fix(data): migrate WOs from ghost tenants into psauto company One-time data migration that moves work orders created with wrong company_id (Octane singleton race condition) back into psauto. Only moves WOs from companies that have zero users (ghost/test tenants), so legitimate separate companies are not affected. Co-Authored-By: Claude Sonnet 4.6 --- ...9_06_110218_fix_work_orders_company_id.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 database/migrations/2026_09_06_110218_fix_work_orders_company_id.php 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 + } +};