environment(['local', 'testing'])) { $this->error('Refused: only allowed in local or testing environment.'); return self::FAILURE; } $from = (string) $this->option('from'); $to = (string) $this->option('to'); $dryRun = (bool) $this->option('dry-run'); $query = Draw::query()->whereBetween('business_date', [$from, $to]); $total = (int) $query->count(); if ($total === 0) { $this->info("No draws found between {$from} and {$to}."); return self::SUCCESS; } $byStatus = (clone $query) ->selectRaw('status, count(*) as c') ->groupBy('status') ->orderByDesc('c') ->pluck('c', 'status'); $drawIds = (clone $query)->pluck('id'); $ticketOrders = (int) DB::table('ticket_orders')->whereIn('draw_id', $drawIds)->count(); $this->table( ['Metric', 'Value'], [ ['Date range', "{$from} .. {$to}"], ['Draws to delete', (string) $total], ['Ticket orders (cascade)', (string) $ticketOrders], ], ); $this->line('By status:'); foreach ($byStatus as $status => $count) { $this->line(" {$status}: {$count}"); } if ($dryRun) { $this->info('Dry run — no rows deleted.'); return self::SUCCESS; } if (! $this->option('force') && ! $this->confirm("Delete {$total} draws and related rows?", false)) { $this->warn('Aborted.'); return self::SUCCESS; } $deleted = 0; (clone $query)->orderBy('id')->chunkById(200, function ($draws) use (&$deleted): void { foreach ($draws as $draw) { $draw->delete(); $deleted++; } $this->output->write('.'); }); $this->newLine(); $this->info("Deleted {$deleted} draws."); if ($this->option('tick')) { $report = $tickService->tick(); $this->info(sprintf( 'Post-tick: status_updates=%d rng=%d planned_created=%d', array_sum($report['status_updates'] ?? []), $report['rng_rung'], $report['planned']['created'] ?? 0, )); } else { $this->comment('Skipped tick (pass --tick to run draw-tick after prune).'); } $head = Draw::query() ->whereNotIn('status', ['settled', 'cancelled']) ->orderBy('draw_time') ->first(['draw_no', 'status', 'draw_time']); if ($head !== null) { $this->info("Hall pipeline head is now: {$head->draw_no} ({$head->status}) @ {$head->draw_time}"); } else { $this->info('Hall pipeline head: none (all settled/cancelled).'); } return self::SUCCESS; } }