Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| QuotationsCleanupCommercialCorruption | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\TblQuotations; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Support\Facades\DB; |
| 8 | |
| 9 | /** |
| 10 | * FIRE-1743: one-off cleanup of tbl_quotations.commercial rows corrupted |
| 11 | * by an unidentified callsite that stored the same name repeated N times |
| 12 | * separated by "\n" (e.g. "Solanas - Gallex\nSolanas - Gallex\nSolanas - |
| 13 | * Gallex\nSolanas - Gallex"). The corrupt values never match a real |
| 14 | * tbl_users.name, which is what produced the "blank name" and "missing |
| 15 | * commercial" symptoms on /organization/business-goals and |
| 16 | * /commercial-productivity. |
| 17 | * |
| 18 | * Strategy: normalise line endings, take the first non-empty line, trim. |
| 19 | * Matches the TblQuotations::setCommercialAttribute mutator that stops |
| 20 | * new corruption at write time. |
| 21 | * |
| 22 | * Idempotent: only touches rows whose value contains \r or \n. Safe to |
| 23 | * re-run. |
| 24 | */ |
| 25 | class QuotationsCleanupCommercialCorruption extends Command |
| 26 | { |
| 27 | protected $signature = 'quotations:cleanup-commercial-corruption |
| 28 | {--dry-run : Report what would change without writing} |
| 29 | {--chunk=500 : How many candidate rows to inspect per batch}'; |
| 30 | |
| 31 | protected $description = 'FIRE-1743: strip newline-repeated corruption from tbl_quotations.commercial.'; |
| 32 | |
| 33 | public function handle(): int |
| 34 | { |
| 35 | $dryRun = (bool) $this->option('dry-run'); |
| 36 | $chunkSize = max(1, (int) $this->option('chunk')); |
| 37 | |
| 38 | $candidates = TblQuotations::query() |
| 39 | ->whereNotNull('commercial') |
| 40 | ->where(function ($q) { |
| 41 | $q->where('commercial', 'LIKE', "%\n%") |
| 42 | ->orWhere('commercial', 'LIKE', "%\r%"); |
| 43 | }); |
| 44 | |
| 45 | $total = (clone $candidates)->count(); |
| 46 | |
| 47 | if ($total === 0) { |
| 48 | $this->info('No quotations with corrupted `commercial` values. Nothing to clean.'); |
| 49 | |
| 50 | return self::SUCCESS; |
| 51 | } |
| 52 | |
| 53 | $this->info(($dryRun ? '[DRY RUN] ' : '')."Scanning {$total} candidate row(s)…"); |
| 54 | |
| 55 | $scanned = 0; |
| 56 | $changed = 0; |
| 57 | $cleared = 0; |
| 58 | |
| 59 | $candidates->orderBy('id')->chunkById($chunkSize, function ($rows) use ( |
| 60 | $dryRun, |
| 61 | &$scanned, |
| 62 | &$changed, |
| 63 | &$cleared, |
| 64 | ) { |
| 65 | foreach ($rows as $row) { |
| 66 | $scanned++; |
| 67 | |
| 68 | $original = (string) $row->commercial; |
| 69 | $normalised = str_replace(["\r\n", "\r"], "\n", $original); |
| 70 | $firstLine = strtok($normalised, "\n"); |
| 71 | $trimmed = $firstLine === false ? '' : trim($firstLine); |
| 72 | $cleaned = $trimmed === '' ? null : $trimmed; |
| 73 | |
| 74 | if ($cleaned === $original) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | $changed++; |
| 79 | if ($cleaned === null) { |
| 80 | $cleared++; |
| 81 | } |
| 82 | |
| 83 | if (! $dryRun) { |
| 84 | DB::table('tbl_quotations') |
| 85 | ->where('id', $row->id) |
| 86 | ->update(['commercial' => $cleaned, 'updated_at' => date('Y-m-d H:i:s')]); |
| 87 | } |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | $this->line(''); |
| 92 | $this->line(' Scanned ............. '.number_format($scanned)); |
| 93 | $this->line(' Would change ........ '.number_format($changed)); |
| 94 | $this->line(' Cleared (→ NULL) .... '.number_format($cleared)); |
| 95 | |
| 96 | if ($dryRun) { |
| 97 | $this->warn('Dry run — no rows were written. Re-run without --dry-run to apply.'); |
| 98 | } else { |
| 99 | $this->info("Done. Updated {$changed} row(s)."); |
| 100 | } |
| 101 | |
| 102 | return self::SUCCESS; |
| 103 | } |
| 104 | } |