# Help desk ticketing system database schema

Source: https://personalsoftwarefactory.com/schemas/help-desk
Updated: 2026-12-04

> Nine tables. tickets carries the design: one assignee, six statuses, a stored first-response due time and a version number for stale-reply checks, with table CHECKs that keep a new ticket unassigned and a solved ticket owned. messages holds the thread and the email Message-IDs that threading and duplicate detection rely on, and notifications is the outbox every email passes through.

- 9 tables, 8 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 19 seed rows from the blueprint
- The blueprint's test build passed 24 of 24 acceptance checks

Files: [help-desk.sql](https://personalsoftwarefactory.com/schemas/help-desk.sql) · [help-desk-seed.sql](https://personalsoftwarefactory.com/schemas/help-desk-seed.sql) · [the Help Desk blueprint](https://personalsoftwarefactory.com/blueprints/help-desk)

## Relationships

- tickets.requester_id references requesters.id: each tickets row points to exactly one requesters row, and one requesters row can have many tickets rows pointing to it. On delete: cascade.
- tickets.assignee_id references agents.id: each tickets row points to at most one agents row, and one agents row can have many tickets rows pointing to it.
- tickets.follow_up_of references tickets.id: each tickets row points to at most one another tickets row, and one tickets row can have many tickets rows pointing to it.
- messages.ticket_id references tickets.id: each messages row points to exactly one tickets row, and one tickets row can have many messages rows pointing to it. On delete: cascade.
- messages.author_agent_id references agents.id: each messages row points to at most one agents row, and one agents row can have many messages rows pointing to it.
- canned_replies.created_by references agents.id: each canned_replies row points to exactly one agents row, and one agents row can have many canned_replies rows pointing to it.
- events.ticket_id references tickets.id: each events row points to at most one tickets row, and one tickets row can have many events rows pointing to it. On delete: cascade.
- events.actor_agent_id references agents.id: each events row points to at most one agents row, and one agents row can have many events rows pointing to it.
- notifications.ticket_id references tickets.id: each notifications row points to at most one tickets row, and one tickets row can have many notifications rows pointing to it. On delete: cascade.

## Tables

Also described as: support ticket system database design, ticketing system tables, customer support database schema.

### agents

A support team member who answers tickets; deactivated, never deleted. Implements: Agent.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique | stored lowercase Check: `email = LOWER(email)` |
| `is_admin` | BOOLEAN | no | `FALSE` |  |  |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- App rule (R6): deactivating an agent who still has tickets that are not solved or closed is refused.

### settings

The single row of company settings, first-response targets, auto-close days and the ticket number counter. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no |  | Primary key | one row only Check: `id = 1` |
| `company_name` | TEXT | no |  |  |  |
| `support_email` | TEXT | no |  |  | the address customers write to and replies come from |
| `time_zone` | TEXT | no | `'America/New_York'` |  | IANA name; used for report weeks |
| `target_urgent_minutes` | INTEGER | no | `60` |  | Check: `target_urgent_minutes BETWEEN 15 AND 10080` |
| `target_high_minutes` | INTEGER | no | `240` |  | Check: `target_high_minutes BETWEEN 15 AND 10080` |
| `target_normal_minutes` | INTEGER | no | `480` |  | Check: `target_normal_minutes BETWEEN 15 AND 10080` |
| `target_low_minutes` | INTEGER | no | `1440` |  | Check: `target_low_minutes BETWEEN 15 AND 10080` |
| `auto_close_days` | INTEGER | no | `4` |  | days after solved_at Check: `auto_close_days BETWEEN 1 AND 28` |
| `acknowledgement_template` | TEXT | yes |  |  | placeholders: {requester_first_name}, {ticket_number}, {subject} |
| `last_ticket_number` | INTEGER | no | `1000` |  | counter for ticket numbers; deleting tickets never lowers it |

### requesters

A customer who wrote in by email or the web form. Implements: Requester.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `email` | TEXT | no |  | Unique | stored lowercase Check: `email = LOWER(email)` |
| `name` | TEXT | yes |  |  | optional Check: `LENGTH(name) <= 100` |
| `created_at` | TIMESTAMP | no |  |  |  |

### tickets

One support request with its status, priority, assignee and first-response times. Implements: Ticket.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | taken from settings.last_ticket_number + 1, never reused Check: `number >= 1001` |
| `subject` | TEXT | no |  |  | Check: `LENGTH(subject) BETWEEN 1 AND 150` |
| `requester_id` | TEXT | no |  | References requesters.id, on delete cascade | R14 removes a requester's tickets |
| `channel` | TEXT | no |  |  | Check: `channel IN ('email', 'web', 'agent')` |
| `status` | TEXT | no |  |  | Check: `status IN ('new', 'open', 'pending', 'on_hold', 'solved', 'closed')` |
| `priority` | TEXT | no | `'normal'` |  | Check: `priority IN ('low', 'normal', 'high', 'urgent')` |
| `assignee_id` | TEXT | yes |  | References agents.id | at most one assignee |
| `follow_up_of` | TEXT | yes |  | References tickets.id | the closed ticket this one follows up (R3) |
| `created_at` | TIMESTAMP | no |  |  |  |
| `first_response_at` | TIMESTAMP | yes |  |  | first public reply by an agent |
| `first_response_due_at` | TIMESTAMP | no |  |  |  |
| `solved_at` | TIMESTAMP | yes |  |  | set when solved, cleared when reopened, kept when closed |
| `closed_at` | TIMESTAMP | yes |  |  |  |
| `updated_at` | TIMESTAMP | no |  |  |  |
| `version` | INTEGER | no | `1` |  | Check: `version >= 1` |

- Table check: `status <> 'new' OR (assignee_id IS NULL AND first_response_at IS NULL)`
- Table check: `status NOT IN ('solved', 'closed') OR assignee_id IS NOT NULL`
- Table check: `(status IN ('solved', 'closed') AND solved_at IS NOT NULL) OR (status NOT IN ('solved', 'closed') AND solved_at IS NULL)`
- Table check: `(status = 'closed' AND closed_at IS NOT NULL) OR (status <> 'closed' AND closed_at IS NULL)`
- Index `tickets_status_idx` on (status)
- Index `tickets_assignee_idx` on (assignee_id)
- Index `tickets_requester_idx` on (requester_id)
- App rule (R6): assignee_id must point to an active agent.
- App rule (R1, R2): a ticket never returns to new; agents can never set new or closed; only the job closes tickets.
- App rule (R5): first_response_due_at = created_at + the target for the priority; recalculated on a priority change only before the first response.
- App rule (R13): version goes up by 1 on every change.

### messages

One entry in a ticket's thread: a requester email, a public reply or an internal note. Implements: Message.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `ticket_id` | TEXT | no |  | References tickets.id, on delete cascade |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('requester', 'public_reply', 'internal_note')` |
| `author_agent_id` | TEXT | yes |  | References agents.id | set for public replies and notes, empty for requester messages |
| `body` | TEXT | no |  |  | plain text, shown as text, never as HTML Check: `LENGTH(body) BETWEEN 1 AND 20000` |
| `email_message_id` | TEXT | yes |  | Unique | Message-ID header of the email received or sent; used for threading (R11) and duplicates (R12) |
| `created_at` | TIMESTAMP | no |  |  |  |

- Table check: `(kind = 'requester' AND author_agent_id IS NULL) OR (kind <> 'requester' AND author_agent_id IS NOT NULL)`
- Table check: `kind <> 'internal_note' OR email_message_id IS NULL`
- Index `messages_ticket_idx` on (ticket_id, created_at)
- App rule (R7): internal notes are never emailed or included in anything the requester receives.

### canned_replies

A saved reply an agent can insert and edit before sending. Implements: CannedReply.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `title` | TEXT | no |  |  | Check: `LENGTH(title) BETWEEN 1 AND 80` |
| `body` | TEXT | no |  |  | placeholders: {requester_first_name}, {ticket_number}, {agent_first_name} Check: `LENGTH(body) BETWEEN 1 AND 5000` |
| `created_by` | TEXT | no |  | References agents.id |  |
| `updated_at` | TIMESTAMP | no |  |  |  |

- Unique index `canned_replies_title_uniq` on (LOWER(title))

### events

One append-only history entry for a ticket action or a requester deletion. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `ticket_id` | TEXT | yes |  | References tickets.id, on delete cascade | empty for requester_deleted |
| `actor_agent_id` | TEXT | yes |  | References agents.id | empty for the requester and the system |
| `type` | TEXT | no |  |  | Check: `type IN ('created', 'requester_replied', 'replied', 'note_added', 'assigned', 'status_changed', 'priority_changed', 'reopened', 'follow_up_created', 'closed', 'requester_deleted')` |
| `data` | TEXT | yes |  |  | JSON text with from/to values; requester_deleted stores only the ticket count |
| `created_at` | TIMESTAMP | no |  |  |  |

- Index `events_ticket_idx` on (ticket_id, created_at)
- App rule (R15): each action writes the events listed in section 6.

### notifications

One outgoing email in the outbox, with its send status and attempts. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('acknowledgement', 'public_reply', 'assignment')` |
| `to_email` | TEXT | no |  |  |  |
| `ticket_id` | TEXT | yes |  | References tickets.id, on delete cascade | R14 removes the outbox rows of deleted tickets |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `message_id_header` | TEXT | no |  |  | a new Message-ID for every email |
| `in_reply_to_header` | TEXT | yes |  |  | newest Message-ID among the requester's emails, the acknowledgement and earlier replies (R8) |
| `status` | TEXT | no | `'pending'` |  | logged = no email provider configured Check: `status IN ('pending', 'sent', 'logged', 'failed')` |
| `error` | TEXT | yes |  |  |  |
| `attempts` | INTEGER | no | `0` |  | 6 attempts in all (section 10) Check: `attempts BETWEEN 0 AND 6` |
| `created_at` | TIMESTAMP | no |  |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

- Index `notifications_status_idx` on (status)

### form_submissions

One public support form submission, kept to rate-limit by IP hash. Implements: FormSubmission.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `ip_hash` | TEXT | no |  |  | SHA-256 of the IP address with a server secret; the IP itself is never stored |
| `created_at` | TIMESTAMP | no |  |  |  |

- Index `form_submissions_ip_idx` on (ip_hash, created_at)

## The SQL

```sql
-- Help Desk database schema
-- Source: https://personalsoftwarefactory.com/blueprints/help-desk (data model, section 5 of the blueprint)
-- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC.
-- License: MIT. Free to copy and adapt.

-- A support team member who answers tickets. Agents are deactivated, never deleted.
-- App rule (R6): deactivating an agent who still has tickets that are not solved or closed is refused.
CREATE TABLE agents (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  active BOOLEAN NOT NULL DEFAULT TRUE
);

-- The single settings row for the one company and support address.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only
  company_name TEXT NOT NULL,
  support_email TEXT NOT NULL, -- the address customers write to and replies come from
  time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; used for report weeks
  target_urgent_minutes INTEGER NOT NULL DEFAULT 60 CHECK (target_urgent_minutes BETWEEN 15 AND 10080),
  target_high_minutes INTEGER NOT NULL DEFAULT 240 CHECK (target_high_minutes BETWEEN 15 AND 10080),
  target_normal_minutes INTEGER NOT NULL DEFAULT 480 CHECK (target_normal_minutes BETWEEN 15 AND 10080),
  target_low_minutes INTEGER NOT NULL DEFAULT 1440 CHECK (target_low_minutes BETWEEN 15 AND 10080),
  auto_close_days INTEGER NOT NULL DEFAULT 4 CHECK (auto_close_days BETWEEN 1 AND 28), -- days after solved_at
  acknowledgement_template TEXT, -- placeholders: {requester_first_name}, {ticket_number}, {subject}
  last_ticket_number INTEGER NOT NULL DEFAULT 1000 -- counter for ticket numbers; deleting tickets never lowers it
);

-- A customer who wrote in by email or the web form. Requesters never sign in.
CREATE TABLE requesters (
  id TEXT PRIMARY KEY,
  email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase
  name TEXT CHECK (LENGTH(name) <= 100), -- optional
  created_at TIMESTAMP NOT NULL
);

-- One support request with one assignee and a status.
-- App rule (R6): assignee_id must point to an active agent.
-- App rule (R1, R2): a ticket never returns to new; agents can never set new or closed; only the job closes tickets.
-- App rule (R5): first_response_due_at = created_at + the target for the priority; recalculated on a priority change only before the first response.
-- App rule (R13): version goes up by 1 on every change.
CREATE TABLE tickets (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE CHECK (number >= 1001), -- taken from settings.last_ticket_number + 1, never reused
  subject TEXT NOT NULL CHECK (LENGTH(subject) BETWEEN 1 AND 150),
  requester_id TEXT NOT NULL REFERENCES requesters(id) ON DELETE CASCADE, -- R14 removes a requester's tickets
  channel TEXT NOT NULL CHECK (channel IN ('email', 'web', 'agent')),
  status TEXT NOT NULL CHECK (status IN ('new', 'open', 'pending', 'on_hold', 'solved', 'closed')),
  priority TEXT NOT NULL DEFAULT 'normal' CHECK (priority IN ('low', 'normal', 'high', 'urgent')),
  assignee_id TEXT REFERENCES agents(id), -- at most one assignee
  follow_up_of TEXT REFERENCES tickets(id), -- the closed ticket this one follows up (R3)
  created_at TIMESTAMP NOT NULL,
  first_response_at TIMESTAMP, -- first public reply by an agent
  first_response_due_at TIMESTAMP NOT NULL,
  solved_at TIMESTAMP, -- set when solved, cleared when reopened, kept when closed
  closed_at TIMESTAMP,
  updated_at TIMESTAMP NOT NULL,
  version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1),
  CHECK (status <> 'new' OR (assignee_id IS NULL AND first_response_at IS NULL)), -- R1: assignment or a first reply makes it open
  CHECK (status NOT IN ('solved', 'closed') OR assignee_id IS NOT NULL), -- R4: solving needs an owner
  CHECK ((status IN ('solved', 'closed') AND solved_at IS NOT NULL) OR (status NOT IN ('solved', 'closed') AND solved_at IS NULL)),
  CHECK ((status = 'closed' AND closed_at IS NOT NULL) OR (status <> 'closed' AND closed_at IS NULL))
);
CREATE INDEX tickets_status_idx ON tickets (status);
CREATE INDEX tickets_assignee_idx ON tickets (assignee_id);
CREATE INDEX tickets_requester_idx ON tickets (requester_id);

-- One entry in a ticket's thread: a requester email, a public reply or an internal note.
-- App rule (R7): internal notes are never emailed or included in anything the requester receives.
CREATE TABLE messages (
  id TEXT PRIMARY KEY,
  ticket_id TEXT NOT NULL REFERENCES tickets(id) ON DELETE CASCADE,
  kind TEXT NOT NULL CHECK (kind IN ('requester', 'public_reply', 'internal_note')),
  author_agent_id TEXT REFERENCES agents(id), -- set for public replies and notes, empty for requester messages
  body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 20000), -- plain text, shown as text, never as HTML
  email_message_id TEXT UNIQUE, -- Message-ID header of the email received or sent; used for threading (R11) and duplicates (R12)
  created_at TIMESTAMP NOT NULL,
  CHECK ((kind = 'requester' AND author_agent_id IS NULL) OR (kind <> 'requester' AND author_agent_id IS NOT NULL)),
  CHECK (kind <> 'internal_note' OR email_message_id IS NULL) -- internal notes are not emails
);
CREATE INDEX messages_ticket_idx ON messages (ticket_id, created_at);

-- A saved reply an agent can insert into the reply box. It never sends on its own (R9).
CREATE TABLE canned_replies (
  id TEXT PRIMARY KEY,
  title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 80),
  body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 5000), -- placeholders: {requester_first_name}, {ticket_number}, {agent_first_name}
  created_by TEXT NOT NULL REFERENCES agents(id),
  updated_at TIMESTAMP NOT NULL
);
CREATE UNIQUE INDEX canned_replies_title_uniq ON canned_replies (LOWER(title)); -- unique ignoring case

-- One append-only history entry. Never edited or deleted, except by R14.
-- App rule (R15): each action writes the events listed in section 6.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  ticket_id TEXT REFERENCES tickets(id) ON DELETE CASCADE, -- empty for requester_deleted
  actor_agent_id TEXT REFERENCES agents(id), -- empty for the requester and the system
  type TEXT NOT NULL CHECK (type IN ('created', 'requester_replied', 'replied', 'note_added', 'assigned', 'status_changed', 'priority_changed', 'reopened', 'follow_up_created', 'closed', 'requester_deleted')),
  data TEXT, -- JSON text with from/to values; requester_deleted stores only the ticket count
  created_at TIMESTAMP NOT NULL
);
CREATE INDEX events_ticket_idx ON events (ticket_id, created_at);

-- One outgoing email in the outbox. Written first, then sent.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL CHECK (kind IN ('acknowledgement', 'public_reply', 'assignment')),
  to_email TEXT NOT NULL,
  ticket_id TEXT REFERENCES tickets(id) ON DELETE CASCADE, -- R14 removes the outbox rows of deleted tickets
  subject TEXT NOT NULL,
  body TEXT NOT NULL,
  message_id_header TEXT NOT NULL, -- a new Message-ID for every email
  in_reply_to_header TEXT, -- newest Message-ID among the requester's emails, the acknowledgement and earlier replies (R8)
  status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), -- logged = no email provider configured
  error TEXT,
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 6), -- 6 attempts in all (section 10)
  created_at TIMESTAMP NOT NULL,
  sent_at TIMESTAMP
);
CREATE INDEX notifications_status_idx ON notifications (status);

-- One public support form submission, kept for the rate limit (5 per IP hash per hour).
CREATE TABLE form_submissions (
  id TEXT PRIMARY KEY,
  ip_hash TEXT NOT NULL, -- SHA-256 of the IP address with a server secret; the IP itself is never stored
  created_at TIMESTAMP NOT NULL
);
CREATE INDEX form_submissions_ip_idx ON form_submissions (ip_hash, created_at);
```

## Seed data

Loads the settings row, the 3 agents, the 7 requesters and the 8 tickets from section 14, with T fixed at 2026-12-04 12:00:00 UTC. Left out: the messages, because the spec leaves their text to the builder and gives no time for Ben's internal note; the 2 canned replies, because the spec gives no author or time and only sketches the second one's text; and events, which the spec does not seed. Tickets load at version 1, and updated_at is the latest time the spec gives for each ticket.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/help-desk.sql https://personalsoftwarefactory.com/schemas/help-desk-seed.sql | sqlite3 help_desk.db
# PostgreSQL
createdb help_desk && curl -s https://personalsoftwarefactory.com/schemas/help-desk.sql https://personalsoftwarefactory.com/schemas/help-desk-seed.sql | psql -v ON_ERROR_STOP=1 help_desk
```

## Design decisions

### Ticket numbers come from a stored counter

Section 5 says numbers run from 1001 and are taken from a stored counter, last_ticket_number, that deleting tickets never lowers. It lives on the one settings row, and tickets.number is UNIQUE with a floor of 1001. Taking MAX(number) + 1 instead would reuse a number after R14 deletes the newest tickets, and an old reply with that number in its subject could thread into a stranger's ticket. The build test found section 5 had nowhere to store this counter; it was added. (This point comes from a gap the blueprint's build test found.)

### The first-response due time is stored, not computed

R5 sets first_response_due_at at creation from the priority's target and recalculates it only while there is no first response. Storing it lets the queue sort waiting tickets by due time with a plain ORDER BY, and keeps the deadline fixed once an agent has replied, even if the priority or the settings change later.

### Status rules that fit one row are CHECK constraints

R1 means a new ticket has no assignee and no first response; R4 means a solved or closed ticket has an assignee; section 6 means solved_at is set exactly when the ticket is solved or closed and closed_at exactly when it is closed. The transitions themselves (never back to new, only the job closes) stay in the app, because they compare a row with its previous state.

### Message-IDs are unique and live on messages

R11 threads an inbound email by its In-Reply-To or References headers, and R12 ignores an email whose Message-ID was already received; a UNIQUE email_message_id makes the duplicate check a constraint, not a race. The build test found that "the last email in the thread" could pick an internal note for In-Reply-To; R8 now names only emails, and a CHECK here keeps a note's Message-ID empty. (This point comes from a gap the blueprint's build test found.)

### Deleting a requester cascades

R14 removes a requester's tickets, messages and outbox rows and keeps one requester_deleted event with no ticket and no personal data. ON DELETE CASCADE from requesters to tickets, and from tickets to messages, events and notifications, does that in one DELETE. The kept event has an empty ticket_id, so the cascade cannot reach it.

### A version column for stale replies

R13 stops a reply when the ticket changed since the agent opened it. tickets.version starts at 1 and goes up on every change; the app sends the version it showed and refuses the write when it no longer matches. Without it, two agents answer the same customer, which is the problem section 1 starts from.

### Rate limiting and retries have their own columns

The build test found section 5 had nowhere to store the support form's rate limit, the ticket counter or email retry attempts. form_submissions keeps only a salted IP hash and a time, and notifications.attempts is capped at 6 by a CHECK, the total section 10 allows. (This point comes from a gap the blueprint's build test found.)

## Example queries

### Unassigned queue, waiting tickets first

```sql
SELECT t.number, t.subject, r.email AS requester, t.priority, CASE WHEN t.first_response_at IS NOT NULL THEN 'responded' WHEN t.first_response_due_at < '2026-12-04 12:00:00' THEN 'overdue' ELSE 'due' END AS first_response_state, t.first_response_due_at FROM tickets t JOIN requesters r ON r.id = t.requester_id WHERE t.status IN ('new', 'open') AND t.assignee_id IS NULL ORDER BY CASE WHEN t.first_response_at IS NULL THEN 0 ELSE 1 END, CASE WHEN t.first_response_at IS NULL THEN t.first_response_due_at END, t.updated_at;
```

On the seed data at T: 2 rows, #1002 (overdue) above #1001 (due at 13:00).

### Queue counts for the sidebar

```sql
SELECT SUM(CASE WHEN status IN ('new', 'open') AND assignee_id IS NULL THEN 1 ELSE 0 END) AS unassigned, SUM(CASE WHEN assignee_id = 'agent_jo' AND status NOT IN ('solved', 'closed') THEN 1 ELSE 0 END) AS mine_jo, SUM(CASE WHEN status IN ('new', 'open', 'on_hold') THEN 1 ELSE 0 END) AS all_open, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending, SUM(CASE WHEN status IN ('solved', 'closed') AND solved_at >= '2026-11-04 12:00:00' THEN 1 ELSE 0 END) AS solved_30_days FROM tickets;
```

On the seed data: 1 row, Unassigned 2, Mine for Jo Park 2, All open 4, Pending 1, Solved 3.

### A requester's ticket history

```sql
SELECT t.number, t.subject, t.status, t.created_at, f.number AS follow_up_of FROM tickets t JOIN requesters r ON r.id = t.requester_id LEFT JOIN tickets f ON f.id = t.follow_up_of WHERE r.email = 'max@bluebird.example' ORDER BY t.created_at DESC;
```

On the seed data: 2 rows for Max Ford, #1001 (new) and #1008 (closed).

### Tickets the scheduled job closes

```sql
SELECT t.number, t.subject, t.solved_at FROM tickets t WHERE t.status = 'solved' AND t.solved_at <= '2026-11-30 12:00:00' ORDER BY t.solved_at;
```

On the seed data, with the cutoff at T minus auto_close_days (4): 1 row, #1007 solved 2026-11-29; #1006 stays solved.

### One week of the report

```sql
SELECT COUNT(*) AS created, SUM(CASE WHEN first_response_at IS NOT NULL THEN 1 ELSE 0 END) AS with_first_response, SUM(CASE WHEN first_response_at IS NOT NULL AND first_response_at <= first_response_due_at THEN 1 ELSE 0 END) AS within_target, (SELECT COUNT(*) FROM tickets s WHERE s.solved_at >= '2026-11-30 05:00:00' AND s.solved_at < '2026-12-07 05:00:00') AS solved FROM tickets WHERE created_at >= '2026-11-30 05:00:00' AND created_at < '2026-12-07 05:00:00';
```

On the seed data, for the week of Monday 2026-11-30 in America/New_York: 1 row, 6 created, 4 with a first response, 4 within target, 1 solved.

## What was not tested

- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.

## The blueprint

This schema is section 5 of the [Help Desk blueprint](https://personalsoftwarefactory.com/blueprints/help-desk), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/help-desk/blueprint.md
