# Leave management system database schema

Source: https://personalsoftwarefactory.com/schemas/pto-tracker
Updated: 2026-12-04

> Twelve tables. The design rests on requests, leave_days and allowances: every booking is a request, every half-day it takes is a leave_days row whose unique key makes double booking impossible, and each person has one allowance row per year. Day amounts are stored as integer half-days, and balances are computed from the requests every time instead of being kept as a running total.

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

Files: [pto-tracker.sql](https://personalsoftwarefactory.com/schemas/pto-tracker.sql) · [pto-tracker-seed.sql](https://personalsoftwarefactory.com/schemas/pto-tracker-seed.sql) · [the Time-Off Tracker blueprint](https://personalsoftwarefactory.com/blueprints/pto-tracker)

## Relationships

- people.team_id references teams.id: each people row points to exactly one teams row, and one teams row can have many people rows pointing to it.
- people.approver_id references people.id: each people row points to at most one another people row, and one people row can have many people rows pointing to it.
- allowances.person_id references people.id: each allowances row points to exactly one people row, and one people row can have many allowances rows pointing to it.
- year_closes.closed_by references people.id: each year_closes row points to exactly one people row, and one people row can have many year_closes rows pointing to it.
- requests.person_id references people.id: each requests row points to exactly one people row, and one people row can have many requests rows pointing to it.
- requests.leave_type_id references leave_types.id: each requests row points to exactly one leave_types row, and one leave_types row can have many requests rows pointing to it.
- requests.created_by references people.id: each requests row points to exactly one people row, and one people row can have many requests rows pointing to it.
- requests.decided_by references people.id: each requests row points to at most one people row, and one people row can have many requests rows pointing to it.
- requests.cancelled_by references people.id: each requests row points to at most one people row, and one people row can have many requests rows pointing to it.
- leave_days.request_id references requests.id: each leave_days row points to exactly one requests row, and one requests row can have many leave_days rows pointing to it.
- leave_days.person_id references people.id: each leave_days row points to exactly one people row, and one people row can have many leave_days rows pointing to it.
- events.request_id references requests.id: each events row points to at most one requests row, and one requests row can have many events rows pointing to it.
- events.person_id references people.id: each events row points to exactly one people row, and one people row can have many events rows pointing to it.
- events.actor_id references people.id: each events row points to at most one people row, and one people row can have many events rows pointing to it.
- notifications.person_id references people.id: each notifications row points to exactly one people row, and one people row can have many notifications rows pointing to it.
- notifications.request_id references requests.id: each notifications row points to at most one requests row, and one requests row can have many notifications rows pointing to it.

## Tables

Also described as: PTO tracker database design, employee leave management database tables, vacation tracker SQL schema.

### settings

The single company row: name and the carry-over cap in half-days. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no | `1` | Primary key | always 1, so there can only be one row Check: `id = 1` |
| `company_name` | TEXT | no |  |  |  |
| `carry_over_cap` | INTEGER | no | `10` |  | half-days; 10 = 5 days; 0 = nothing carries over Check: `carry_over_cap >= 0` |

### teams

A team or department people belong to. Implements: Team.

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

### people

Someone who can sign in, with their team, approver, working week, yearly allowance, borrow limit and calendar feed token. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique | stored lower-case Check: `email = LOWER(email)` |
| `team_id` | TEXT | no |  | References teams.id |  |
| `approver_id` | TEXT | yes |  | References people.id | null: admins decide (R9) |
| `is_admin` | BOOLEAN | no | `FALSE` |  |  |
| `working_days` | TEXT | no | `'1,2,3,4,5'` |  | comma-separated ISO weekdays, 1 = Monday to 7 = Sunday, at least one Check: `working_days <> ''` |
| `annual_allowance` | INTEGER | no | `40` |  | half-days; copied to entitlement when a year's allowance row is created Check: `annual_allowance >= 0` |
| `borrow_limit` | INTEGER | no | `0` |  | half-days available may go below zero Check: `borrow_limit >= 0` |
| `calendar_token` | TEXT | no |  | Unique | random secret in the calendar feed URL (section 13) Check: `LENGTH(calendar_token) >= 32` |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Table check: `approver_id IS NULL OR approver_id <> id`
- App rule (R9, R15): only active people can be picked as approvers.
- App rule (R15): there must always be at least one active admin.

### leave_types

A kind of leave, with whether it counts against the allowance and whether it needs approval. Implements: LeaveType.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  | Unique |  |
| `code` | TEXT | no |  | Unique | shown on the calendar, e.g. AL Check: `LENGTH(code) = 2` |
| `color` | TEXT | no |  |  | hex, e.g. #2f7d6d Check: `color LIKE '#%'` |
| `counts_against_allowance` | BOOLEAN | no |  |  |  |
| `needs_approval` | BOOLEAN | no |  |  | false: requests are approved straight away |
| `position` | INTEGER | no |  |  | display order |
| `active` | BOOLEAN | no |  |  | inactive types can't be picked for new requests |

### public_holidays

A company public holiday, which is never charged. Implements: PublicHoliday.

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

### allowances

One person's entitlement, carry-over and adjustment for one leave year. Implements: Allowance.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | no |  | References people.id |  |
| `year` | INTEGER | no |  |  |  |
| `entitlement` | INTEGER | no |  |  | half-days Check: `entitlement >= 0` |
| `carried_over` | INTEGER | no | `0` |  | half-days, may be negative; written only when the previous year is closed |
| `adjustment` | INTEGER | no | `0` |  | half-days, may be negative |
| `adjustment_note` | TEXT | yes |  |  | required when adjustment is not 0 |

- Unique together: `person_id`, `year`
- Table check: `adjustment = 0 OR (adjustment_note IS NOT NULL AND adjustment_note <> '')`
- App rule (section 5): write a row only when something is saved for that year; viewing never creates one.
- App rule (R7): balances are computed from requests every time; no running total is stored.

### year_closes

A closed leave year and the carry-over cap that was used to close it. Implements: YearClose.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `year` | INTEGER | no |  | Primary key |  |
| `carry_over_cap` | INTEGER | no |  |  | half-days; the cap used for this close Check: `carry_over_cap >= 0` |
| `closed_by` | TEXT | no |  | References people.id |  |
| `closed_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

### requests

One booking of leave, with its half-day amount, status and decision or cancellation details. Implements: Request.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | no |  | References people.id | whose leave it is |
| `leave_type_id` | TEXT | no |  | References leave_types.id |  |
| `year` | INTEGER | no |  |  | the year of start_date |
| `start_date` | DATE | no |  |  |  |
| `start_half` | TEXT | no |  |  | pm: first day is afternoon only Check: `start_half IN ('am', 'pm')` |
| `end_date` | DATE | no |  |  |  |
| `end_half` | TEXT | no |  |  | am: last day is morning only Check: `end_half IN ('am', 'pm')` |
| `days` | INTEGER | no |  |  | half-days taken (section 6) Check: `days > 0` |
| `counts_against_allowance` | BOOLEAN | no |  |  | copied from the leave type at creation |
| `status` | TEXT | no |  |  | Check: `status IN ('pending', 'approved', 'declined', 'cancelled')` |
| `note` | TEXT | yes |  |  | trimmed Check: `note IS NULL OR LENGTH(note) <= 500` |
| `created_by` | TEXT | no |  | References people.id | the person, or the admin who recorded it |
| `decided_by` | TEXT | yes |  | References people.id | set by approve and decline |
| `decided_at` | TIMESTAMP | yes |  |  |  |
| `decision_note` | TEXT | yes |  |  | optional when approved; the reason when declined |
| `cancelled_by` | TEXT | yes |  | References people.id |  |
| `cancelled_at` | TIMESTAMP | yes |  |  |  |
| `cancel_reason` | TEXT | yes |  |  | required when an admin cancels someone else's leave |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `updated_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Table check: `end_date >= start_date`
- Table check: `NOT (start_date = end_date AND start_half = 'pm' AND end_half = 'am')`
- Table check: `decided_by IS NULL OR decided_by <> person_id`
- Table check: `status <> 'declined' OR (decided_by IS NOT NULL AND decided_at IS NOT NULL AND COALESCE(LENGTH(decision_note), 0) >= 3)`
- Table check: `status <> 'cancelled' OR (cancelled_by IS NOT NULL AND cancelled_at IS NOT NULL)`
- Table check: `status <> 'cancelled' OR cancelled_by = person_id OR COALESCE(LENGTH(cancel_reason), 0) >= 3`
- Index `requests_person_year` on (person_id, year)
- Index `requests_status` on (status)
- App rule (R2): end_date is in the same year as start_date, and year is that year.
- App rule (section 8): status moves pending -> approved | declined | cancelled, approved -> cancelled; declined and cancelled are final.
- App rule (R5, R8): save the request, its leave_days and its event in one transaction that locks the person's allowance row.
- App rule (R12): days, the leave_days rows and counts_against_allowance are set once at creation.

### leave_days

One morning or afternoon session taken by a pending or approved request. Implements: LeaveDay.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `request_id` | TEXT | no |  | References requests.id |  |
| `person_id` | TEXT | no |  | References people.id | same as the request's person |
| `date` | DATE | no |  |  |  |
| `half` | TEXT | no |  |  | Check: `half IN ('am', 'pm')` |

- Unique together: `person_id`, `date`, `half`
- Index `leave_days_date` on (date)
- Index `leave_days_request` on (request_id)
- App rule (section 5): rows are written with the request and deleted when it is declined or cancelled.

### events

One append-only history entry about a request or an allowance. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `request_id` | TEXT | yes |  | References requests.id | null for allowance changes and carry-over |
| `person_id` | TEXT | no |  | References people.id | whose leave or allowance it is about |
| `actor_id` | TEXT | yes |  | References people.id | null for system actions |
| `type` | TEXT | no |  |  | Check: `type IN ('requested', 'recorded', 'approved', 'declined', 'cancelled', 'allowance_changed', 'carried_over')` |
| `note` | TEXT | yes |  |  |  |
| `data` | TEXT | yes |  |  | JSON text, e.g. old and new values for an allowance change |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Index `events_request` on (request_id)
- Index `events_person` on (person_id)
- App rule (R13): every request action and allowance change writes exactly one event; never updated or deleted.

### notifications

One email in the outbox, with its send status and attempt count. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | no |  | References people.id | recipient |
| `request_id` | TEXT | yes |  | References requests.id |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('needs_approval', 'booked', 'recorded', 'approved', 'declined', 'cancelled', 'weekly_digest', 'year_closed')` |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `status` | TEXT | no | `'queued'` |  | logged: no email provider configured Check: `status IN ('queued', 'sent', 'logged', 'failed')` |
| `attempts` | INTEGER | no | `0` |  | Check: `attempts >= 0` |
| `error` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

- Index `notifications_status` on (status)
- App rule (section 11): queued and failed notifications are retried until they have had 5 attempts.

### digest_runs

One weekly digest run, keyed by the Monday it covers so it runs only once. Implements: DigestRun.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `week_start` | DATE | no |  | Primary key | the Monday of the week the digest covers |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

## The SQL

```sql
-- Time-Off Tracker database schema
-- Source: https://personalsoftwarefactory.com/blueprints/pto-tracker (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.
--
-- Every day amount is an integer number of half-days: 1 day = 2, half a day = 1.
-- Leave dates are calendar dates (YYYY-MM-DD) in the team time zone, with no time.

-- The single settings row for the company.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row
  company_name TEXT NOT NULL,
  carry_over_cap INTEGER NOT NULL DEFAULT 10 CHECK (carry_over_cap >= 0) -- half-days; 10 = 5 days; 0 = nothing carries over
);

-- A team (department) people belong to.
CREATE TABLE teams (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);

-- A person who can sign in and book leave.
-- App rule (R9, R15): only active people can be picked as approvers.
-- App rule (R15): there must always be at least one active admin.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lower-case
  team_id TEXT NOT NULL REFERENCES teams(id),
  approver_id TEXT REFERENCES people(id), -- null: admins decide (R9)
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  working_days TEXT NOT NULL DEFAULT '1,2,3,4,5' CHECK (working_days <> ''), -- comma-separated ISO weekdays, 1 = Monday to 7 = Sunday, at least one
  annual_allowance INTEGER NOT NULL DEFAULT 40 CHECK (annual_allowance >= 0), -- half-days; copied to entitlement when a year's allowance row is created
  borrow_limit INTEGER NOT NULL DEFAULT 0 CHECK (borrow_limit >= 0), -- half-days available may go below zero
  calendar_token TEXT NOT NULL UNIQUE CHECK (LENGTH(calendar_token) >= 32), -- random secret in the calendar feed URL (section 13)
  active BOOLEAN NOT NULL DEFAULT TRUE,
  CHECK (approver_id IS NULL OR approver_id <> id) -- never their own approver (R15)
);

-- A kind of leave, such as Annual leave or Sick leave.
CREATE TABLE leave_types (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  code TEXT NOT NULL UNIQUE CHECK (LENGTH(code) = 2), -- shown on the calendar, e.g. AL
  color TEXT NOT NULL CHECK (color LIKE '#%'), -- hex, e.g. #2f7d6d
  counts_against_allowance BOOLEAN NOT NULL,
  needs_approval BOOLEAN NOT NULL, -- false: requests are approved straight away
  position INTEGER NOT NULL, -- display order
  active BOOLEAN NOT NULL -- inactive types can't be picked for new requests
);

-- A company public holiday. Never charged against the allowance.
CREATE TABLE public_holidays (
  id TEXT PRIMARY KEY,
  date DATE NOT NULL UNIQUE,
  name TEXT NOT NULL
);

-- One person's allowance for one leave year (the calendar year).
-- A missing row means entitlement = annual_allowance, carried_over 0, adjustment 0.
-- App rule (section 5): write a row only when something is saved for that year; viewing never creates one.
-- App rule (R7): balances are computed from requests every time; no running total is stored.
CREATE TABLE allowances (
  id TEXT PRIMARY KEY,
  person_id TEXT NOT NULL REFERENCES people(id),
  year INTEGER NOT NULL,
  entitlement INTEGER NOT NULL CHECK (entitlement >= 0), -- half-days
  carried_over INTEGER NOT NULL DEFAULT 0, -- half-days, may be negative; written only when the previous year is closed
  adjustment INTEGER NOT NULL DEFAULT 0, -- half-days, may be negative
  adjustment_note TEXT, -- required when adjustment is not 0
  UNIQUE (person_id, year),
  CHECK (adjustment = 0 OR (adjustment_note IS NOT NULL AND adjustment_note <> ''))
);

-- A closed leave year. A row means the year is closed and nothing in it may change (R14).
CREATE TABLE year_closes (
  year INTEGER PRIMARY KEY,
  carry_over_cap INTEGER NOT NULL CHECK (carry_over_cap >= 0), -- half-days; the cap used for this close
  closed_by TEXT NOT NULL REFERENCES people(id),
  closed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- One booking of leave. Never edited: to change dates, cancel and request again.
-- App rule (R2): end_date is in the same year as start_date, and year is that year.
-- App rule (section 8): status moves pending -> approved | declined | cancelled, approved -> cancelled; declined and cancelled are final.
-- App rule (R5, R8): save the request, its leave_days and its event in one transaction that locks the person's allowance row.
-- App rule (R12): days, the leave_days rows and counts_against_allowance are set once at creation.
CREATE TABLE requests (
  id TEXT PRIMARY KEY,
  person_id TEXT NOT NULL REFERENCES people(id), -- whose leave it is
  leave_type_id TEXT NOT NULL REFERENCES leave_types(id),
  year INTEGER NOT NULL, -- the year of start_date
  start_date DATE NOT NULL,
  start_half TEXT NOT NULL CHECK (start_half IN ('am', 'pm')), -- pm: first day is afternoon only
  end_date DATE NOT NULL,
  end_half TEXT NOT NULL CHECK (end_half IN ('am', 'pm')), -- am: last day is morning only
  days INTEGER NOT NULL CHECK (days > 0), -- half-days taken (section 6)
  counts_against_allowance BOOLEAN NOT NULL, -- copied from the leave type at creation
  status TEXT NOT NULL CHECK (status IN ('pending', 'approved', 'declined', 'cancelled')),
  note TEXT CHECK (note IS NULL OR LENGTH(note) <= 500), -- trimmed
  created_by TEXT NOT NULL REFERENCES people(id), -- the person, or the admin who recorded it
  decided_by TEXT REFERENCES people(id), -- set by approve and decline
  decided_at TIMESTAMP,
  decision_note TEXT, -- optional when approved; the reason when declined
  cancelled_by TEXT REFERENCES people(id),
  cancelled_at TIMESTAMP,
  cancel_reason TEXT, -- required when an admin cancels someone else's leave
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CHECK (end_date >= start_date), -- R2
  CHECK (NOT (start_date = end_date AND start_half = 'pm' AND end_half = 'am')), -- R3: one day can't run afternoon to morning
  CHECK (decided_by IS NULL OR decided_by <> person_id), -- R9: nobody decides their own request
  CHECK (status <> 'declined' OR (decided_by IS NOT NULL AND decided_at IS NOT NULL AND COALESCE(LENGTH(decision_note), 0) >= 3)), -- R10
  CHECK (status <> 'cancelled' OR (cancelled_by IS NOT NULL AND cancelled_at IS NOT NULL)),
  CHECK (status <> 'cancelled' OR cancelled_by = person_id OR COALESCE(LENGTH(cancel_reason), 0) >= 3) -- R11
);

CREATE INDEX requests_person_year ON requests (person_id, year);
CREATE INDEX requests_status ON requests (status);

-- One half-day session (am or pm) taken by a pending or approved request.
-- App rule (section 5): rows are written with the request and deleted when it is declined or cancelled.
CREATE TABLE leave_days (
  id TEXT PRIMARY KEY,
  request_id TEXT NOT NULL REFERENCES requests(id),
  person_id TEXT NOT NULL REFERENCES people(id), -- same as the request's person
  date DATE NOT NULL,
  half TEXT NOT NULL CHECK (half IN ('am', 'pm')),
  UNIQUE (person_id, date, half) -- makes double booking impossible (R5)
);

CREATE INDEX leave_days_date ON leave_days (date);
CREATE INDEX leave_days_request ON leave_days (request_id);

-- One entry in the history of a request or an allowance. Append-only.
-- App rule (R13): every request action and allowance change writes exactly one event; never updated or deleted.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  request_id TEXT REFERENCES requests(id), -- null for allowance changes and carry-over
  person_id TEXT NOT NULL REFERENCES people(id), -- whose leave or allowance it is about
  actor_id TEXT REFERENCES people(id), -- null for system actions
  type TEXT NOT NULL CHECK (type IN ('requested', 'recorded', 'approved', 'declined', 'cancelled', 'allowance_changed', 'carried_over')),
  note TEXT,
  data TEXT, -- JSON text, e.g. old and new values for an allowance change
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX events_request ON events (request_id);
CREATE INDEX events_person ON events (person_id);

-- One email in the outbox, queued in the same transaction as the action that caused it.
-- App rule (section 11): queued and failed notifications are retried until they have had 5 attempts.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  person_id TEXT NOT NULL REFERENCES people(id), -- recipient
  request_id TEXT REFERENCES requests(id),
  kind TEXT NOT NULL CHECK (kind IN ('needs_approval', 'booked', 'recorded', 'approved', 'declined', 'cancelled', 'weekly_digest', 'year_closed')),
  subject TEXT NOT NULL,
  body TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), -- logged: no email provider configured
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
  error TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  sent_at TIMESTAMP
);

CREATE INDEX notifications_status ON notifications (status);

-- One weekly digest run. The unique week_start stops a second run for the same week.
CREATE TABLE digest_runs (
  week_start DATE PRIMARY KEY, -- the Monday of the week the digest covers
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
```

## Seed data

Loads section 15 in full: the Fernhill Studio settings row (cap 10 half-days), 3 teams, 7 people, 3 leave types, 8 public holidays, a 2026 and a 2027 allowance row for each person, the 18 requests, the 187 half-day leave_days rows that the pending and approved requests take, and the 34 history events their statuses imply. Times are 09:00 for requests and 10:00 for decisions in America/New_York, stored in UTC; the spec gives no time for the two cancellations, so they use 10:00 too. The spec gives no IDs or calendar tokens, so the seed uses readable placeholder IDs and placeholder tokens; replace the tokens before real use. The outbox, closed years and digest runs are empty because the spec seeds none.

## Load it

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

## Design decisions

### Day amounts are integer half-days

Section 5 stores every day amount as an integer number of half-days: 1 day is 2, half a day is 1. Alex's Mon 21 to Thu 24 Dec request with a morning-only last day is stored as 7, not 3.5. Decimal days invite rounding errors in balance sums, and integers make the borrow limit and carry-over cap comparisons exact.

### One row per half-day session blocks double booking

leave_days has a unique key on (person_id, date, half), as section 5 and R5 require. Two overlapping requests for the same person cannot both be saved, even if they arrive at the same moment, because the second insert fails. Rows exist only for pending and approved requests and are deleted on decline or cancel, so those never block a new booking.

### Balances are computed, never stored

R7 and section 7 say balances are computed from the rows every time. allowances holds only entitlement, carried_over and adjustment; approved, pending and available are sums over requests. A running total that actions add to and subtract from drifts as soon as one action fails halfway, and then the person and the system disagree.

### Allowance rows are written on save, never on view

When a year has no allowances row, the app uses the person's annual_allowance with zero carry-over and adjustment, and writes the row only when something is saved for that year (section 5). Our build test found that creating the row whenever a balance was needed meant just viewing last year created rows, which then blocked closing the current year; the spec now says viewing never writes. (This point comes from a gap the blueprint's build test found.)

### Day counts are frozen on the request

requests stores days and counts_against_allowance at creation, copied from the day count and the leave type (R12). Adding a public holiday or changing someone's working days later does not change existing bookings. Without the copy, editing a holiday would silently change past balances.

### Single-row decision rules are CHECK constraints

Rules that fit in one row are enforced by the database: nobody decides their own request (R9), a decline needs a reason of at least 3 characters (R10), an admin cancelling someone else's leave needs a reason of at least 3 characters (R11), and a one-day request cannot run from afternoon to morning (R3). The status transitions, the allowance limit (R8) and closed years (R14) span several rows and stay in the app, named in comments.

### Working days are a short text list

Section 5 describes working_days as a list of ISO weekday numbers with a default of Monday to Friday and at least one day. A TEXT column such as '1,2,3,4' keeps that default and the at-least-one rule in the table, and the day count in section 6 reads the whole list at once. A child table would lose both rules to app code.

## Example queries

### Balances for a year, in half-days

```sql
SELECT
  p.name,
  a.entitlement + a.carried_over + a.adjustment AS total,
  COALESCE(SUM(CASE WHEN r.status = 'approved' THEN r.days END), 0) AS approved,
  COALESCE(SUM(CASE WHEN r.status = 'pending' THEN r.days END), 0) AS pending,
  a.entitlement + a.carried_over + a.adjustment - COALESCE(SUM(r.days), 0) AS available
FROM allowances a
JOIN people p ON p.id = a.person_id
LEFT JOIN requests r ON r.person_id = a.person_id AND r.year = a.year AND r.counts_against_allowance = TRUE AND r.status IN ('approved', 'pending')
WHERE a.year = 2026
GROUP BY p.name, a.entitlement, a.carried_over, a.adjustment
ORDER BY p.name;
```

On the seed data: 7 rows. Maya Rivera has total 40, approved 10, pending 10 and available 20 half-days (20, 5, 5 and 10 days); Priya Nair has available -2.

### Who is out on a given day

```sql
SELECT DISTINCT p.name, t.name AS team, lt.name AS leave_type, r.start_date, r.end_date
FROM leave_days d
JOIN requests r ON r.id = d.request_id
JOIN people p ON p.id = d.person_id
JOIN teams t ON t.id = p.team_id
JOIN leave_types lt ON lt.id = r.leave_type_id
WHERE d.date = '2026-10-13' AND r.status = 'approved' AND p.active = TRUE
ORDER BY p.name;
```

On the seed data: 1 row, Tom Bennett, Design, Annual leave, 2026-10-13 to 2026-10-16.

### Out next week, approved and pending listed apart

```sql
SELECT p.name, r.status, MIN(d.date) AS first_day, MAX(d.date) AS last_day
FROM leave_days d
JOIN requests r ON r.id = d.request_id
JOIN people p ON p.id = d.person_id
WHERE d.date >= '2026-10-19' AND d.date <= '2026-10-25' AND p.active = TRUE
GROUP BY p.name, r.id, r.status
ORDER BY r.status, p.name;
```

On the seed data: 2 rows. Jo Park approved for 2026-10-22 to 2026-10-23, and Maya Rivera pending for 2026-10-19 to 2026-10-23.

### Requests waiting for one approver, oldest first

```sql
SELECT p.name, lt.name AS leave_type, r.start_date, r.end_date, r.days
FROM requests r
JOIN people p ON p.id = r.person_id
JOIN people ap ON ap.id = p.approver_id
JOIN leave_types lt ON lt.id = r.leave_type_id
WHERE r.status = 'pending' AND ap.id = 'person-alex' AND ap.active = TRUE AND r.person_id <> ap.id
ORDER BY r.created_at;
```

On the seed data: 2 rows for Alex Morgan, Rosa Quinn's 2027-01-04 to 2027-01-08 request first, then Maya Rivera's 2026-10-19 to 2026-10-23.

### Year-end carry-over preview

```sql
SELECT
  b.name,
  b.remaining,
  CASE WHEN b.remaining <= 0 THEN b.remaining WHEN b.remaining < s.carry_over_cap THEN b.remaining ELSE s.carry_over_cap END AS carried,
  b.remaining - CASE WHEN b.remaining <= 0 THEN b.remaining WHEN b.remaining < s.carry_over_cap THEN b.remaining ELSE s.carry_over_cap END AS forfeited
FROM (SELECT p.name, a.entitlement + a.carried_over + a.adjustment - COALESCE(SUM(r.days), 0) AS remaining FROM allowances a JOIN people p ON p.id = a.person_id LEFT JOIN requests r ON r.person_id = a.person_id AND r.year = a.year AND r.counts_against_allowance = TRUE AND r.status IN ('approved', 'pending') WHERE a.year = 2026 AND p.active = TRUE GROUP BY p.name, a.entitlement, a.carried_over, a.adjustment) b
CROSS JOIN settings s
ORDER BY b.name;
```

On the seed data: 7 rows, matching the section 9 table in half-days. Rosa Quinn has 30 remaining, 10 carried and 20 forfeited; Priya Nair carries -2 (her borrowed day is paid back). The app refuses the actual close while any 2026 request is pending.

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