fix(data): v2 WO company_id fix — also covers test companies with 1-2 users

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 <noreply@anthropic.com>
This commit is contained in:
2026-09-06 11:06:28 +00:00
parent 2323f61d43
commit 6e18da755d
@@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* v2 — previous migration skipped companies with ≥1 user.
* This one moves WOs from any company whose slug/name matches "test"
* patterns OR whose slug is not "psauto" and has ≤2 users (no real ops).
*/
return new class extends Migration
{
public function up(): void
{
$psauto = DB::table('companies')->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 {}
};