Skip to content

fix(tickets): allocate ticket IDs atomically - #631

Merged
Sayrix merged 1 commit into
mainfrom
fix/atomic-ticket-ids
Aug 9, 2026
Merged

fix(tickets): allocate ticket IDs atomically#631
Sayrix merged 1 commit into
mainfrom
fix/atomic-ticket-ids

Conversation

@Sayrix

@Sayrix Sayrix commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replace COUNT(tickets) + 1 with an atomic SQLite-backed ID reservation.
  • Use the reserved value for both the displayed ticket number and database primary key.
  • Initialize the sequence from the highest existing ticket ID without requiring a schema migration.
  • Prevent concurrent ticket creation and existing sequence gaps from producing duplicate numbers.

Implementation

The allocator stores its sequence in the existing app_meta table. A single upsert catches up with existing ticket records and serializes concurrent reservations before any Discord resources are created.

A failed Discord operation may consume an ID and leave a gap. Reserved IDs are intentionally never reused, preserving uniqueness.

Summary by CodeRabbit

  • New Features

    • Ticket numbers are now assigned through a reliable, sequential reservation process.
    • Newly created tickets retain their assigned number consistently across displayed information and stored records.
  • Bug Fixes

    • Prevented duplicate or conflicting ticket numbers during ticket creation, including when multiple tickets are created simultaneously.

@Sayrix

Sayrix commented Aug 9, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 66252079-178a-4c6e-a547-381a21460b6d

📥 Commits

Reviewing files that changed from the base of the PR and between 3f57ad0 and ffd9999.

📒 Files selected for processing (2)
  • src/features/tickets/id-allocation.ts
  • src/features/tickets/ticket-workflow.ts

📝 Walkthrough

Walkthrough

The change adds atomic ticket ID reservation through appMetaTable. Ticket creation reserves the ID before Discord operations, uses it for ticket numbering, and stores it as the ticket primary key. The count-based ticket-number helper is removed.

Changes

Ticket ID allocation

Layer / File(s) Summary
Atomic ticket ID reservation
src/features/tickets/id-allocation.ts
reserveTicketId upserts ticketIdSequence, synchronizes it with existing ticket IDs, and validates that the result is a positive safe integer.
Ticket creation integration
src/features/tickets/ticket-workflow.ts
Ticket creation reserves an ID before Discord operations, uses it for rendering, stores it as the ticket primary key, and removes count-based numbering.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ticketWorkflow
  participant reserveTicketId
  participant app.db
  participant Discord
  participant ticketsTable
  ticketWorkflow->>reserveTicketId: reserve ticket ID
  reserveTicketId->>app.db: atomically update ticketIdSequence
  app.db-->>reserveTicketId: return reserved ID
  reserveTicketId-->>ticketWorkflow: return numeric ticket ID
  ticketWorkflow->>Discord: create resources with rendered ticket number
  ticketWorkflow->>ticketsTable: persist reserved ID as primary key
Loading

Poem

A rabbit counts IDs in a burrow so neat,
One number reserved before Discord we meet.
The sequence moves forward, safe and bright,
The ticket keeps its key, exactly right.
Hop, hop—no counting seats tonight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: atomic allocation of ticket IDs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/atomic-ticket-ids

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sayrix
Sayrix merged commit 5cfed33 into main Aug 9, 2026
5 checks passed
@Sayrix
Sayrix deleted the fix/atomic-ticket-ids branch August 9, 2026 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant