1
0
mirror of git://f0xx.org/android_cast synced 2026-07-29 07:39:15 +03:00

Fix tickets API false missing-table when session skips DB.

Auth: :user() returns early when companies are cached, so requireTicketsTable() ran with no PDO and always failed. Always open the connection before checking tickets.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Anton Afanasyeu
2026-05-24 22:57:38 +02:00
parent 483f635843
commit 253f932434
2 changed files with 25 additions and 7 deletions

View File

@@ -21,11 +21,24 @@ try {
json_out(['ok' => true] + $data); json_out(['ok' => true] + $data);
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
error_log('tickets api: ' . $e->getMessage()); error_log('tickets api: ' . $e->getMessage());
json_out([ $out = [
'ok' => false, 'ok' => false,
'error' => 'tickets_table_missing', 'error' => 'tickets_table_missing',
'hint' => $e->getMessage(), '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) { } catch (Throwable $e) {
error_log('tickets api: ' . $e->getMessage()); error_log('tickets api: ' . $e->getMessage());
$out = ['ok' => false, 'error' => 'list failed']; $out = ['ok' => false, 'error' => 'list failed'];

View File

@@ -159,15 +159,20 @@ final class Database {
self::ensureTicketsTable($pdo); 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 { public static function ticketsTableExists(?PDO $pdo = null): bool {
$pdo ??= self::$pdo; return self::tableExists(self::connection($pdo), 'tickets');
return $pdo !== null && self::tableExists($pdo, 'tickets');
} }
/** @throws RuntimeException when `tickets` is missing (MariaDB needs root migration). */ /** @throws RuntimeException when `tickets` is missing (MariaDB needs root migration). */
public static function requireTicketsTable(?PDO $pdo = null): void { public static function requireTicketsTable(?PDO $pdo = null): void {
$pdo = self::connection($pdo);
self::ensureTicketsTable($pdo); self::ensureTicketsTable($pdo);
if (!self::ticketsTableExists($pdo)) { if (!self::tableExists($pdo, 'tickets')) {
throw new RuntimeException(self::ticketsMigrationHint()); throw new RuntimeException(self::ticketsMigrationHint());
} }
} }
@@ -179,8 +184,8 @@ final class Database {
} }
public static function ensureTicketsTable(?PDO $pdo = null): void { public static function ensureTicketsTable(?PDO $pdo = null): void {
$pdo ??= self::$pdo; $pdo = self::connection($pdo);
if ($pdo === null || self::tableExists($pdo, 'tickets')) { if (self::tableExists($pdo, 'tickets')) {
return; return;
} }
$ok = false; $ok = false;