# Client document collection system database schema

Source: https://personalsoftwarefactory.com/schemas/document-requests
Updated: 2026-12-05

> Thirteen tables. requests and request_items carry the design: each request copies its template's items when it is created, so editing a template never changes a request already sent, and each item holds the client's answer and the staff review state. Files are rows under an item with a size limit in a CHECK, and retention marks them deleted instead of removing the row, so an old request keeps its progress. Client access is a token on the request plus single-use access codes and browser sessions stored only as hashes.

- 13 tables, 4 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-05
- 60 seed rows from the blueprint
- The blueprint's test build passed 14 of 14 acceptance checks

Files: [document-requests.sql](https://personalsoftwarefactory.com/schemas/document-requests.sql) · [document-requests-seed.sql](https://personalsoftwarefactory.com/schemas/document-requests-seed.sql) · [the Document Requests blueprint](https://personalsoftwarefactory.com/blueprints/document-requests)

## Relationships

- template_items.template_id references templates.id: each template_items row points to exactly one templates row, and one templates row can have many template_items rows pointing to it. On delete: cascade.
- requests.client_id references clients.id: each requests row points to exactly one clients row, and one clients row can have many requests rows pointing to it.
- requests.owner_id references staff.id: each requests row points to exactly one staff row, and one staff row can have many requests rows pointing to it.
- request_watchers.request_id references requests.id: each request_watchers row points to exactly one requests row, and one requests row can have many request_watchers rows pointing to it. On delete: cascade.
- request_watchers.staff_id references staff.id: each request_watchers row points to exactly one staff row, and one staff row can have many request_watchers rows pointing to it.
- request_items.request_id references requests.id: each request_items row points to exactly one requests row, and one requests row can have many request_items rows pointing to it. On delete: cascade.
- files.item_id references request_items.id: each files row points to exactly one request_items row, and one request_items row can have many files rows pointing to it. On delete: cascade.
- access_codes.request_id references requests.id: each access_codes row points to exactly one requests row, and one requests row can have many access_codes rows pointing to it. On delete: cascade.
- client_sessions.request_id references requests.id: each client_sessions row points to exactly one requests row, and one requests row can have many client_sessions rows pointing to it. On delete: cascade.

## Tables

Also described as: document request portal database design, client document checklist database tables, accounting client portal data model.

### settings

The one row of firm settings: name, sender address, retention and reminder timing. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no |  | Primary key | Check: `id = 1` |
| `firm_name` | TEXT | no |  |  |  |
| `from_email` | TEXT | no |  |  |  |
| `retention_days` | INTEGER | no | `90` |  | files of completed or cancelled requests are deleted this many days later Check: `retention_days >= 1` |
| `reminder_first_after_days` | INTEGER | no | `3` |  | first reminder, in days after sending Check: `reminder_first_after_days >= 1` |
| `reminder_every_days` | INTEGER | no | `4` |  | later reminders, counted from the first Check: `reminder_every_days >= 1` |
| `reminder_before_due_days` | INTEGER | no | `2` |  | one extra reminder this many days before the due date Check: `reminder_before_due_days >= 0` |
| `max_reminders` | INTEGER | no | `6` |  | never more reminders than this per request Check: `max_reminders >= 0` |

### staff

An admin or staff member at the firm who signs in. Implements: Staff.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique | sign-in identity |
| `role` | TEXT | no |  |  | Check: `role IN ('admin', 'staff')` |
| `active` | BOOLEAN | no | `TRUE` |  |  |

### templates

A reusable list of things to ask a client for. Implements: Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |

- App rule (R9): editing a template never changes requests already created from it.

### template_items

One item of a template, in order, with its kind, options and required flag. Implements: child table of Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `template_id` | TEXT | no |  | Primary key (part); References templates.id, on delete cascade |  |
| `position` | INTEGER | no |  | Primary key (part) | Check: `position >= 1` |
| `label` | TEXT | no |  |  |  |
| `help_text` | TEXT | yes |  |  |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('files', 'text', 'long_text', 'choice', 'date')` |
| `options` | TEXT | yes |  |  | choice items only: the choices, one per line |
| `required` | BOOLEAN | no |  |  |  |

- Table check: `(kind = 'choice') = (options IS NOT NULL)`

### clients

A client business and the contact who receives links and codes. Implements: Client.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `contact_name` | TEXT | no |  |  |  |
| `contact_email` | TEXT | no |  |  |  |

### requests

One request sent to one client, with its status, dates, link token and reminder count. Implements: Request.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | TEXT | no |  | Unique | e.g. R-0012 Check: `substr(number, 1, 2) = 'R-'` |
| `client_id` | TEXT | no |  | References clients.id |  |
| `title` | TEXT | no |  |  |  |
| `owner_id` | TEXT | no |  | References staff.id |  |
| `due_on` | DATE | no |  |  | a UTC day |
| `status` | TEXT | no | `'draft'` |  | Check: `status IN ('draft', 'sent', 'in_progress', 'submitted', 'completed', 'cancelled')` |
| `sent_at` | TIMESTAMP | yes |  |  |  |
| `submitted_at` | TIMESTAMP | yes |  |  | the last time the client pressed Submit |
| `completed_at` | TIMESTAMP | yes |  |  |  |
| `cancelled_at` | TIMESTAMP | yes |  |  | retention counts from here for cancelled requests |
| `token` | TEXT | no |  | Unique | random and long; the client link is /r/<token>. Resend link replaces it. |
| `token_revoked` | BOOLEAN | no | `FALSE` |  | TRUE when this token no longer opens the request |
| `reminders_sent` | INTEGER | no | `0` |  | Check: `reminders_sent >= 0` |
| `last_reminded_on` | DATE | yes |  |  | the UTC day of the last reminder, so no day gets two |

- Table check: `status <> 'draft' OR sent_at IS NULL`
- Table check: `status NOT IN ('sent', 'in_progress', 'submitted', 'completed') OR sent_at IS NOT NULL`
- Table check: `status <> 'submitted' OR submitted_at IS NOT NULL`
- Table check: `status <> 'completed' OR completed_at IS NOT NULL`
- Table check: `status <> 'cancelled' OR cancelled_at IS NOT NULL`
- Index `requests_owner` on (owner_id, status)
- Index `requests_client` on (client_id)
- App rule (section 7): status moves only by Send, the first client answer, Submit, accepting every required item, Request changes and Cancel.
- App rule (section 6): Submit needs every required item done; the request is completed when every required item is accepted.
- App rule (R4): reminders go only while sent or in progress, at most one a day and never more than max_reminders.
- App rule: a new request takes the highest existing number plus one.

### request_watchers

A staff member watching a request; stores the request's watcher list. Implements: join table.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `request_id` | TEXT | no |  | Primary key (part); References requests.id, on delete cascade |  |
| `staff_id` | TEXT | no |  | Primary key (part); References staff.id |  |

- App rule (section 4): only the owner or an admin adds watchers.

### request_items

One item on one request, copied from the template, with the answer and the review state. Implements: Item.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `request_id` | TEXT | no |  | References requests.id, on delete cascade |  |
| `position` | INTEGER | no |  |  | Check: `position >= 1` |
| `label` | TEXT | no |  |  |  |
| `help_text` | TEXT | yes |  |  |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('files', 'text', 'long_text', 'choice', 'date')` |
| `options` | TEXT | yes |  |  | choice items only: the choices, one per line |
| `required` | BOOLEAN | no |  |  |  |
| `answer_text` | TEXT | yes |  |  | the answer for text, long_text, choice (one of the options) and date (YYYY-MM-DD) items |
| `review` | TEXT | no | `'empty'` |  | Check: `review IN ('empty', 'answered', 'accepted', 'changes_requested')` |
| `review_comment` | TEXT | yes |  |  | required when changes are requested |
| `updated_at` | TIMESTAMP | yes |  |  |  |

- Unique together: `request_id`, `position`
- Table check: `(kind = 'choice') = (options IS NOT NULL)`
- Table check: `kind <> 'files' OR answer_text IS NULL`
- Table check: `review <> 'changes_requested' OR review_comment IS NOT NULL`
- App rule (section 6): an item is done for the client when it has an answer or at least one file, and not done again after changes are requested until the client answers or uploads.
- App rule (R5): only the owner, watchers and admins review.

### files

One uploaded file on a files item, kept as a row after retention deletes the stored bytes. Implements: File.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `item_id` | TEXT | no |  | References request_items.id, on delete cascade |  |
| `name` | TEXT | no |  |  | the name the client uploaded |
| `size` | INTEGER | no |  |  | bytes; at most 25 MB (25 x 1024 x 1024) Check: `size >= 0 AND size <= 26214400` |
| `type` | TEXT | no |  |  | MIME type, e.g. application/pdf |
| `storage_key` | TEXT | no |  | Unique | where the bytes live in private storage |
| `uploaded_at` | TIMESTAMP | no |  |  |  |
| `deleted_at` | TIMESTAMP | yes |  |  | set when retention deletes the stored file |

- Index `files_item` on (item_id)
- App rule (section 3): PDF, JPG, PNG, HEIC, CSV, XLSX or DOCX only.
- App rule (R7): retention removes the stored file and sets deleted_at; the row stays, so the item still counts as provided.

### access_codes

A hashed 6-digit code that lets a new browser open a request link. Implements: AccessCode.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key | added: the spec lists no id, and two codes can share a hash |
| `request_id` | TEXT | no |  | References requests.id, on delete cascade |  |
| `code_hash` | TEXT | no |  |  |  |
| `expires_at` | TIMESTAMP | no |  |  |  |
| `used_at` | TIMESTAMP | yes |  |  |  |
| `attempts` | INTEGER | no | `0` |  | wrong tries so far Check: `attempts BETWEEN 0 AND 5` |

- Index `access_codes_request` on (request_id)
- App rule (R1): valid 10 minutes and once; after 5 wrong tries a new code is needed.

### client_sessions

A client browser that entered a valid code, used to label client changes. Implements: ClientSession.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `browser_token_hash` | TEXT | no |  | Primary key | hash of the random token kept in that browser |
| `request_id` | TEXT | no |  | References requests.id, on delete cascade |  |
| `created_at` | TIMESTAMP | no |  |  |  |

### events

An append-only record of one change, including every review. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `at` | TIMESTAMP | no |  |  |  |
| `actor` | TEXT | no |  |  | a staff id, a client browser session, or the daily job |
| `type` | TEXT | no |  |  | e.g. item_accepted, reminder_sent, retention_warning |
| `data` | TEXT | yes |  |  | details of the change as JSON text |

- App rule (R10): events are never edited or deleted; every review is kept here.

### notifications

One email in the outbox, with its delivery status. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | which trigger in section 10, e.g. sent, access_code, reminder |
| `to_email` | TEXT | no |  |  |  |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `status` | TEXT | no | `'queued'` |  | logged when no email provider is configured Check: `status IN ('queued', 'sent', 'failed', 'logged')` |
| `error` | TEXT | yes |  |  |  |

- App rule (section 10): a failed email never blocks the action that caused it.

## The SQL

```sql
-- Document Requests database schema
-- Source: https://personalsoftwarefactory.com/blueprints/document-requests (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.

-- The single settings row. id is fixed at 1 so the table can never hold a second row.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY CHECK (id = 1),
  firm_name TEXT NOT NULL,
  from_email TEXT NOT NULL,
  retention_days INTEGER NOT NULL DEFAULT 90 CHECK (retention_days >= 1), -- files of completed or cancelled requests are deleted this many days later
  reminder_first_after_days INTEGER NOT NULL DEFAULT 3 CHECK (reminder_first_after_days >= 1), -- first reminder, in days after sending
  reminder_every_days INTEGER NOT NULL DEFAULT 4 CHECK (reminder_every_days >= 1), -- later reminders, counted from the first
  reminder_before_due_days INTEGER NOT NULL DEFAULT 2 CHECK (reminder_before_due_days >= 0), -- one extra reminder this many days before the due date
  max_reminders INTEGER NOT NULL DEFAULT 6 CHECK (max_reminders >= 0) -- never more reminders than this per request
);

-- A person at the firm who signs in: an admin or a staff member.
CREATE TABLE staff (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE, -- sign-in identity
  role TEXT NOT NULL CHECK (role IN ('admin', 'staff')),
  active BOOLEAN NOT NULL DEFAULT TRUE
);

-- A reusable list of things to ask a client for. Only admins create and edit templates.
-- App rule (R9): editing a template never changes requests already created from it.
CREATE TABLE templates (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL
);

-- One item of a template, in order. Stores the template items list.
CREATE TABLE template_items (
  template_id TEXT NOT NULL REFERENCES templates(id) ON DELETE CASCADE,
  position INTEGER NOT NULL CHECK (position >= 1),
  label TEXT NOT NULL,
  help_text TEXT,
  kind TEXT NOT NULL CHECK (kind IN ('files', 'text', 'long_text', 'choice', 'date')),
  options TEXT, -- choice items only: the choices, one per line
  required BOOLEAN NOT NULL,
  PRIMARY KEY (template_id, position),
  CHECK ((kind = 'choice') = (options IS NOT NULL))
);

-- A client business and the one contact who receives the link and the access codes.
CREATE TABLE clients (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  contact_name TEXT NOT NULL,
  contact_email TEXT NOT NULL
);

-- One request for documents and answers sent to one client.
-- App rule (section 7): status moves only by Send, the first client answer, Submit, accepting every required item, Request changes and Cancel.
-- App rule (section 6): Submit needs every required item done; the request is completed when every required item is accepted.
-- App rule (R4): reminders go only while sent or in progress, at most one a day and never more than max_reminders.
-- App rule: a new request takes the highest existing number plus one.
CREATE TABLE requests (
  id TEXT PRIMARY KEY,
  number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 2) = 'R-'), -- e.g. R-0012
  client_id TEXT NOT NULL REFERENCES clients(id),
  title TEXT NOT NULL,
  owner_id TEXT NOT NULL REFERENCES staff(id),
  due_on DATE NOT NULL, -- a UTC day
  status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'sent', 'in_progress', 'submitted', 'completed', 'cancelled')),
  sent_at TIMESTAMP,
  submitted_at TIMESTAMP, -- the last time the client pressed Submit
  completed_at TIMESTAMP,
  cancelled_at TIMESTAMP, -- retention counts from here for cancelled requests
  token TEXT NOT NULL UNIQUE, -- random and long; the client link is /r/<token>. Resend link replaces it.
  token_revoked BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE when this token no longer opens the request
  reminders_sent INTEGER NOT NULL DEFAULT 0 CHECK (reminders_sent >= 0),
  last_reminded_on DATE, -- the UTC day of the last reminder, so no day gets two
  CHECK (status <> 'draft' OR sent_at IS NULL),
  CHECK (status NOT IN ('sent', 'in_progress', 'submitted', 'completed') OR sent_at IS NOT NULL),
  CHECK (status <> 'submitted' OR submitted_at IS NOT NULL),
  CHECK (status <> 'completed' OR completed_at IS NOT NULL),
  CHECK (status <> 'cancelled' OR cancelled_at IS NOT NULL)
);
CREATE INDEX requests_owner ON requests (owner_id, status);
CREATE INDEX requests_client ON requests (client_id);

-- A staff member watching a request, who can then see and review it. Stores the request watcher_ids list.
-- App rule (section 4): only the owner or an admin adds watchers.
CREATE TABLE request_watchers (
  request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE,
  staff_id TEXT NOT NULL REFERENCES staff(id),
  PRIMARY KEY (request_id, staff_id)
);

-- One item on one request, copied from the template when the request is created.
-- App rule (section 6): an item is done for the client when it has an answer or at least one file, and not done again after changes are requested until the client answers or uploads.
-- App rule (R5): only the owner, watchers and admins review.
CREATE TABLE request_items (
  id TEXT PRIMARY KEY,
  request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE,
  position INTEGER NOT NULL CHECK (position >= 1),
  label TEXT NOT NULL,
  help_text TEXT,
  kind TEXT NOT NULL CHECK (kind IN ('files', 'text', 'long_text', 'choice', 'date')),
  options TEXT, -- choice items only: the choices, one per line
  required BOOLEAN NOT NULL,
  answer_text TEXT, -- the answer for text, long_text, choice (one of the options) and date (YYYY-MM-DD) items
  review TEXT NOT NULL DEFAULT 'empty' CHECK (review IN ('empty', 'answered', 'accepted', 'changes_requested')),
  review_comment TEXT, -- required when changes are requested
  updated_at TIMESTAMP,
  UNIQUE (request_id, position),
  CHECK ((kind = 'choice') = (options IS NOT NULL)),
  CHECK (kind <> 'files' OR answer_text IS NULL),
  CHECK (review <> 'changes_requested' OR review_comment IS NOT NULL)
);

-- One uploaded file on a files item.
-- App rule (section 3): PDF, JPG, PNG, HEIC, CSV, XLSX or DOCX only.
-- App rule (R7): retention removes the stored file and sets deleted_at; the row stays, so the item still counts as provided.
CREATE TABLE files (
  id TEXT PRIMARY KEY,
  item_id TEXT NOT NULL REFERENCES request_items(id) ON DELETE CASCADE,
  name TEXT NOT NULL, -- the name the client uploaded
  size INTEGER NOT NULL CHECK (size >= 0 AND size <= 26214400), -- bytes; at most 25 MB (25 x 1024 x 1024)
  type TEXT NOT NULL, -- MIME type, e.g. application/pdf
  storage_key TEXT NOT NULL UNIQUE, -- where the bytes live in private storage
  uploaded_at TIMESTAMP NOT NULL,
  deleted_at TIMESTAMP -- set when retention deletes the stored file
);
CREATE INDEX files_item ON files (item_id);

-- A 6-digit code emailed to the client contact to open the link in a new browser. Stored only as a hash.
-- App rule (R1): valid 10 minutes and once; after 5 wrong tries a new code is needed.
CREATE TABLE access_codes (
  id TEXT PRIMARY KEY, -- added: the spec lists no id, and two codes can share a hash
  request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE,
  code_hash TEXT NOT NULL,
  expires_at TIMESTAMP NOT NULL,
  used_at TIMESTAMP,
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 5) -- wrong tries so far
);
CREATE INDEX access_codes_request ON access_codes (request_id);

-- A client browser that entered a valid code. The timeline labels client changes by session.
CREATE TABLE client_sessions (
  browser_token_hash TEXT PRIMARY KEY, -- hash of the random token kept in that browser
  request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE,
  created_at TIMESTAMP NOT NULL
);

-- One change, written by every action. Append-only.
-- App rule (R10): events are never edited or deleted; every review is kept here.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  at TIMESTAMP NOT NULL,
  actor TEXT NOT NULL, -- a staff id, a client browser session, or the daily job
  type TEXT NOT NULL, -- e.g. item_accepted, reminder_sent, retention_warning
  data TEXT -- details of the change as JSON text
);

-- One email in the outbox. Every email is written here first, then sent.
-- App rule (section 10): a failed email never blocks the action that caused it.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL, -- which trigger in section 10, e.g. sent, access_code, reminder
  to_email TEXT NOT NULL,
  subject TEXT NOT NULL,
  body TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no email provider is configured
  error TEXT
);
```

## Seed data

Loads section 14 as of Thursday 2026-10-15: settings, 3 staff, the Year-end 2026 and Q2 receipts templates with 10 items, 3 clients, requests R-0009, R-0011, R-0012 and R-0013 with their 26 copied items, answers and review states, and 11 files (one per done file item, four on R-0009). Times are 12:00 UTC because the spec gives dates only. Stand-ins, because the spec gives none: request tokens and storage keys are readable placeholders, each file is 1,024 bytes of application/pdf, files are dated to the day of submission (R-0011, R-0009) or Oct 14 (R-0012), file names follow the spec's pattern (bluebird-credit-card-2026.pdf), and R-0009's reminder count is 0. R-0009's free-text answer is loaded as answered, since the spec says its files were accepted but not the answer. Help text and updated_at are NULL. No watchers, access codes, sessions, events or emails are seeded.

## Load it

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

## Design decisions

### Items are copied from the template into each request

Section 5 says an Item is copied from the template when the request is created, and R9 says editing a template never changes existing requests. So template_items and request_items are separate tables with the same label, help text, kind, options and required columns; there is no foreign key from an item back to the template. Without the copy, fixing a typo in a template would silently change what a client was already asked.

### Done is computed from answers and files, never stored

Section 6 counts an item as done when it has an answer or at least one file, and not done after changes are requested until the client answers or uploads again; client and staff progress count required items only. The build test found nothing said whether an item with changes requested still counts as done; it does not, so the progress query excludes review = changes_requested rather than trusting a stored flag. (This point comes from a gap the blueprint's build test found.)

### Retention marks files deleted and keeps the row

R7 deletes the files of completed or cancelled requests after retention_days, but section 6 says files removed under retention still count as provided. files.deleted_at records the removal while the row stays, so the item is still done. The build test found Request had no cancelled_at for R7 to count from; requests.cancelled_at now exists and a CHECK requires it on cancelled requests. (This point comes from a gap the blueprint's build test found.)

### Status and its timestamps are checked together

CHECKs require sent_at on every sent, in progress, submitted or completed request, no sent_at on a draft, submitted_at on a submitted request, completed_at on a completed one and cancelled_at on a cancelled one (section 7). A completed request is not required to have submitted_at, because the build test found completion did not say whether the client must submit again after a change request; accepting every required item completes it either way. (This point comes from a gap the blueprint's build test found.)

### Access codes count tries and get their own id

R1 allows at most 5 tries per code. The build test found AccessCode had nowhere to count them; attempts is now a column, CHECKed between 0 and 5. The spec gives AccessCode no id, but two requests can be sent the same 6 digits, so code_hash cannot be the key and an id column is added. ClientSession is keyed by its browser_token_hash, the hash of a random token. (This point comes from a gap the blueprint's build test found.)

### Lists become a join table and a text column

watcher_ids becomes the request_watchers join table, so a watcher is a real reference to staff and R6 visibility is a join. A choice item's options become a TEXT column with one option per line, because nothing in the spec queries options individually; a CHECK requires options on choice items and forbids them on every other kind.

### File size is capped at 25 MB in bytes

Section 3 allows files up to 25 MB, so files.size is CHECKed at no more than 26,214,400 bytes (25 x 1024 x 1024), the reading the test build used. The allowed types and the refusal message stay app rules: the type column records the MIME type of the upload, and the seven allowed types are checked when the file arrives, before any row is written.

## Example queries

### Requests list with client and staff progress

```sql
SELECT q.number, c.name AS client, q.title, s.name AS owner, q.status, q.due_on,
  SUM(CASE WHEN r.required = TRUE THEN 1 ELSE 0 END) AS required_items,
  SUM(CASE WHEN r.required = TRUE AND (r.answer_text IS NOT NULL OR EXISTS (SELECT 1 FROM files f WHERE f.item_id = r.id)) AND r.review <> 'changes_requested' THEN 1 ELSE 0 END) AS done,
  SUM(CASE WHEN r.required = TRUE AND r.review = 'accepted' THEN 1 ELSE 0 END) AS accepted,
  CASE WHEN q.status = 'sent' AND SUM(CASE WHEN r.required = TRUE AND (r.answer_text IS NOT NULL OR EXISTS (SELECT 1 FROM files f WHERE f.item_id = r.id)) AND r.review <> 'changes_requested' THEN 1 ELSE 0 END) = 0 THEN 'Not started' ELSE '' END AS note
FROM requests q
JOIN clients c ON c.id = q.client_id
JOIN staff s ON s.id = q.owner_id
JOIN request_items r ON r.request_id = q.id
WHERE q.status NOT IN ('completed', 'cancelled')
GROUP BY q.id, q.number, c.name, q.title, s.name, q.status, q.due_on
ORDER BY CASE WHEN q.due_on < '2026-10-15' THEN 0 ELSE 1 END, q.due_on, q.number;
```

On the seed data: 3 rows; R-0012 Bluebird Bakery has 5 of 7 required items done and 3 accepted, and R-0013 Pine Dental shows Not started.

### Client view: items on one request

```sql
SELECT r.position, r.label, r.kind,
  CASE WHEN r.required = TRUE THEN 'required' ELSE 'optional' END AS needed,
  COALESCE(r.answer_text, '') AS answer,
  (SELECT COUNT(*) FROM files f WHERE f.item_id = r.id) AS files,
  r.review
FROM request_items r
JOIN requests q ON q.id = r.request_id
WHERE q.token = 'token-placeholder-r0012' AND q.token_revoked = FALSE
ORDER BY r.position;
```

On the seed data, for R-0012: 8 rows; Vehicle mileage answered 12,400 business miles and accepted, Point-of-sale sales report empty with 0 files.

### Review queue: answered items waiting for staff

```sql
SELECT q.number, c.name AS client, s.name AS owner, COUNT(*) AS waiting
FROM request_items r
JOIN requests q ON q.id = r.request_id
JOIN clients c ON c.id = q.client_id
JOIN staff s ON s.id = q.owner_id
WHERE r.review = 'answered' AND q.status IN ('in_progress', 'submitted')
GROUP BY q.number, c.name, s.name
ORDER BY waiting DESC, q.number;
```

On the seed data: 2 rows; R-0011 Harbor Studio has 7 items waiting for Sam Lee and R-0012 has 2.

### Zip download: accepted files by folder

```sql
SELECT CASE WHEN r.position < 10 THEN '0' ELSE '' END || r.position || ' ' || r.label AS folder,
  f.name, f.size
FROM files f
JOIN request_items r ON r.id = f.item_id
JOIN requests q ON q.id = r.request_id
WHERE q.number = 'R-0012' AND r.review = 'accepted' AND r.kind = 'files' AND f.deleted_at IS NULL
ORDER BY r.position, f.name;
```

On the seed data, for R-0012: 2 rows; 02 Credit card statements holds bluebird-credit-card-2026.pdf.

### Reminders sent and outstanding required items

```sql
SELECT q.number, c.contact_email, q.reminders_sent, q.last_reminded_on,
  SUM(CASE WHEN r.required = TRUE AND NOT ((r.answer_text IS NOT NULL OR EXISTS (SELECT 1 FROM files f WHERE f.item_id = r.id)) AND r.review <> 'changes_requested') THEN 1 ELSE 0 END) AS still_needed
FROM requests q
JOIN clients c ON c.id = q.client_id
JOIN request_items r ON r.request_id = q.id
WHERE q.status IN ('sent', 'in_progress')
GROUP BY q.number, c.contact_email, q.reminders_sent, q.last_reminded_on
ORDER BY q.number;
```

On the seed data: 2 rows; R-0012 has had 2 reminders, the last on 2026-10-12, with 2 required items still needed; R-0013 has had none with 7 still needed.

## 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 [Document Requests blueprint](https://personalsoftwarefactory.com/blueprints/document-requests), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/document-requests/blueprint.md
