chore: add tenant:diagnose artisan command for production DB debugging
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DiagnoseTenantCommand extends Command
|
||||
{
|
||||
protected $signature = 'tenant:diagnose {slug : Company slug e.g. psauto}';
|
||||
protected $description = 'Show users, work-order company_id distribution and visibility for a tenant';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$slug = $this->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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user