Fix createTicket duplicate-key retry handler for PHP 8.2

PHP 8.2 strict mysqli mode throws mysqli_sql_exception on duplicate key
rather than returning false from execute(). Replace the old if/else errno
check with try/catch on mysqli_sql_exception, re-throw non-1062 errors,
and use random_int range 100000000-999999999 (no leading zeros) for the
retry ID.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-11 14:17:34 -04:00
parent f7321863e6
commit 9a8940b9d0
+14 -9
View File
@@ -440,20 +440,25 @@ class TicketModel {
$visibilityGroups $visibilityGroups
); );
try {
if ($stmt->execute()) { if ($stmt->execute()) {
return [ return [
'success' => true, 'success' => true,
'ticket_id' => $ticket_id 'ticket_id' => $ticket_id
]; ];
} else { }
return ['success' => false, 'error' => $this->conn->error];
} catch (mysqli_sql_exception $e) {
// Handle duplicate key (errno 1062) caused by race condition between // Handle duplicate key (errno 1062) caused by race condition between
// the uniqueness SELECT above and this INSERT — regenerate and retry once // the uniqueness SELECT above and this INSERT — regenerate and retry once
if ($this->conn->errno === 1062) { if ($e->getCode() !== 1062) {
throw $e;
}
$stmt->close(); $stmt->close();
try { try {
$ticket_id = sprintf('%09d', random_int(100000000, 999999999)); $ticket_id = (string)random_int(100000000, 999999999);
} catch (Exception $e) { } catch (Exception $ex) {
$ticket_id = sprintf('%09d', mt_rand(100000000, 999999999)); $ticket_id = (string)mt_rand(100000000, 999999999);
} }
$stmt = $this->conn->prepare($sql); $stmt = $this->conn->prepare($sql);
$stmt->bind_param( $stmt->bind_param(
@@ -470,14 +475,14 @@ class TicketModel {
$visibility, $visibility,
$visibilityGroups $visibilityGroups
); );
try {
if ($stmt->execute()) { if ($stmt->execute()) {
return ['success' => true, 'ticket_id' => $ticket_id]; return ['success' => true, 'ticket_id' => $ticket_id];
} }
} catch (mysqli_sql_exception $e2) {
// Second attempt also hit duplicate — extremely rare
} }
return [ return ['success' => false, 'error' => 'Failed to create ticket due to ID collision'];
'success' => false,
'error' => $this->conn->error
];
} }
} }