From 6e18da755dc8798447b405bc944332de0b62f89e Mon Sep 17 00:00:00 2001 From: Vasyka Date: Sun, 6 Sep 2026 11:06:28 +0000 Subject: [PATCH] =?UTF-8?q?fix(data):=20v2=20WO=20company=5Fid=20fix=20?= =?UTF-8?q?=E2=80=94=20also=20covers=20test=20companies=20with=201-2=20use?= =?UTF-8?q?rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previous migration skipped companies with ≥1 user. Now moves WOs from any company whose slug/name contains 'test' or has ≤2 users, which covers the single test account scenario. Co-Authored-By: Claude Sonnet 4.6 --- ...6_120000_fix_work_orders_company_id_v2.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 database/migrations/2026_09_06_120000_fix_work_orders_company_id_v2.php 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 {} +};