From 7b1d296bd4bcc302b311168728000258f6afdc7c Mon Sep 17 00:00:00 2001 From: Vasyka Date: Sun, 6 Sep 2026 10:33:38 +0000 Subject: [PATCH] chore: add tenant:diagnose artisan command for production DB debugging Co-Authored-By: Claude Sonnet 4.6 --- .../Commands/DiagnoseTenantCommand.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 app/Console/Commands/DiagnoseTenantCommand.php diff --git a/app/Console/Commands/DiagnoseTenantCommand.php b/app/Console/Commands/DiagnoseTenantCommand.php new file mode 100644 index 0000000..390aeda --- /dev/null +++ b/app/Console/Commands/DiagnoseTenantCommand.php @@ -0,0 +1,62 @@ +argument('slug'); + + $company = DB::table('companies')->where('slug', $slug)->first(); + if (! $company) { + $this->error("Company with slug '{$slug}' not found."); + $this->line('Available slugs:'); + DB::table('companies')->get(['id','slug','name','status'])->each( + fn ($c) => $this->line(" id={$c->id} slug={$c->slug} name={$c->name} status={$c->status}") + ); + return; + } + + $this->info("Company: [{$company->id}] {$company->name} (slug={$company->slug}, status={$company->status})"); + $this->newLine(); + + // Users in this company + $users = DB::table('users')->where('company_id', $company->id)->orderBy('id')->get(); + $this->info("=== Users in company (company_id={$company->id}) ==="); + foreach ($users as $u) { + $this->line(" id={$u->id} name={$u->name} role={$u->role} status={$u->status}"); + } + $this->newLine(); + + // Work orders: distribution by company_id + $this->info('=== Work orders: company_id distribution ==='); + $dist = DB::table('work_orders')->whereNull('deleted_at') + ->selectRaw('company_id, count(*) as cnt') + ->groupBy('company_id')->get(); + foreach ($dist as $d) { + $flag = $d->company_id === $company->id ? ' ← this tenant' : ' ← DIFFERENT TENANT!'; + $this->line(" company_id={$d->company_id} count={$d->cnt}{$flag}"); + } + $this->newLine(); + + // Work orders for THIS company + $wos = DB::table('work_orders') + ->where('company_id', $company->id) + ->whereNull('deleted_at') + ->orderBy('id') + ->get(['id','number','company_id','master_id','status','created_at']); + + $this->info("=== Work orders for company_id={$company->id} ({$wos->count()} total) ==="); + foreach ($wos as $w) { + $master = $w->master_id ? "master_id={$w->master_id}" : 'master_id=null'; + $this->line(" id={$w->id} #{$w->number} {$master} status={$w->status} created={$w->created_at}"); + } + } +}