diff --git a/examples/crash_reporter/backend/public/api/tickets.php b/examples/crash_reporter/backend/public/api/tickets.php index 508ab64..48912d0 100644 --- a/examples/crash_reporter/backend/public/api/tickets.php +++ b/examples/crash_reporter/backend/public/api/tickets.php @@ -21,11 +21,24 @@ try { json_out(['ok' => true] + $data); } catch (RuntimeException $e) { error_log('tickets api: ' . $e->getMessage()); - json_out([ + $out = [ 'ok' => false, 'error' => 'tickets_table_missing', 'hint' => $e->getMessage(), - ], 503); + ]; + if (cfg('debug')) { + try { + $pdo = Database::pdo(); + $out['db_driver'] = Database::driver(); + $out['tickets_table'] = Database::ticketsTableExists($pdo); + if (Database::isMysql()) { + $out['mysql_database'] = $pdo->query('SELECT DATABASE()')->fetchColumn(); + } + } catch (Throwable $diag) { + $out['diag_error'] = $diag->getMessage(); + } + } + json_out($out, 503); } catch (Throwable $e) { error_log('tickets api: ' . $e->getMessage()); $out = ['ok' => false, 'error' => 'list failed']; diff --git a/examples/crash_reporter/backend/src/Database.php b/examples/crash_reporter/backend/src/Database.php index aea8e3b..5990b98 100644 --- a/examples/crash_reporter/backend/src/Database.php +++ b/examples/crash_reporter/backend/src/Database.php @@ -159,15 +159,20 @@ final class Database { self::ensureTicketsTable($pdo); } + /** Active PDO; opens the pool when callers have not touched the DB yet (e.g. cached Auth session). */ + private static function connection(?PDO $pdo = null): PDO { + return $pdo ?? self::$pdo ?? self::pdo(); + } + public static function ticketsTableExists(?PDO $pdo = null): bool { - $pdo ??= self::$pdo; - return $pdo !== null && self::tableExists($pdo, 'tickets'); + return self::tableExists(self::connection($pdo), 'tickets'); } /** @throws RuntimeException when `tickets` is missing (MariaDB needs root migration). */ public static function requireTicketsTable(?PDO $pdo = null): void { + $pdo = self::connection($pdo); self::ensureTicketsTable($pdo); - if (!self::ticketsTableExists($pdo)) { + if (!self::tableExists($pdo, 'tickets')) { throw new RuntimeException(self::ticketsMigrationHint()); } } @@ -179,8 +184,8 @@ final class Database { } public static function ensureTicketsTable(?PDO $pdo = null): void { - $pdo ??= self::$pdo; - if ($pdo === null || self::tableExists($pdo, 'tickets')) { + $pdo = self::connection($pdo); + if (self::tableExists($pdo, 'tickets')) { return; } $ok = false;