# Employee shift scheduling system database schema

Source: https://personalsoftwarefactory.com/schemas/shift-scheduling
Updated: 2026-12-04

> Thirteen tables. Shifts carry the design: each is a pair of UTC instants in a week, with an empty person meaning an open shift, while templates and recurring unavailability stay as local "HH:MM" times. Claims, swaps and drops live in one shift_requests table whose CHECK constraints tie the target, the reason and the pending_target status to the request kind, and a partial unique index allows one pending swap or drop per shift.

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

Files: [shift-scheduling.sql](https://personalsoftwarefactory.com/schemas/shift-scheduling.sql) · [shift-scheduling-seed.sql](https://personalsoftwarefactory.com/schemas/shift-scheduling-seed.sql) · [the Shift Scheduling blueprint](https://personalsoftwarefactory.com/blueprints/shift-scheduling)

## Relationships

- person_positions.person_id references people.id: each person_positions row points to exactly one people row, and one people row can have many person_positions rows pointing to it.
- person_positions.position_id references positions.id: each person_positions row points to exactly one positions row, and one positions row can have many person_positions rows pointing to it.
- availability_entries.person_id references people.id: each availability_entries row points to exactly one people row, and one people row can have many availability_entries rows pointing to it.
- time_off_requests.person_id references people.id: each time_off_requests row points to exactly one people row, and one people row can have many time_off_requests rows pointing to it.
- time_off_requests.decided_by references people.id: each time_off_requests row points to at most one people row, and one people row can have many time_off_requests rows pointing to it.
- templates.position_id references positions.id: each templates row points to exactly one positions row, and one positions row can have many templates rows pointing to it.
- template_weekdays.template_id references templates.id: each template_weekdays row points to exactly one templates row, and one templates row can have many template_weekdays rows pointing to it. On delete: cascade.
- weeks.published_by references people.id: each weeks row points to at most one people row, and one people row can have many weeks rows pointing to it.
- shifts.week_id references weeks.id: each shifts row points to exactly one weeks row, and one weeks row can have many shifts rows pointing to it.
- shifts.position_id references positions.id: each shifts row points to exactly one positions row, and one positions row can have many shifts rows pointing to it.
- shifts.person_id references people.id: each shifts row points to at most one people row, and one people row can have many shifts rows pointing to it.
- shift_requests.shift_id references shifts.id: each shift_requests row points to exactly one shifts row, and one shifts row can have many shift_requests rows pointing to it.
- shift_requests.requested_by references people.id: each shift_requests row points to exactly one people row, and one people row can have many shift_requests rows pointing to it.
- shift_requests.target_person_id references people.id: each shift_requests row points to at most one people row, and one people row can have many shift_requests rows pointing to it.
- shift_requests.decided_by references people.id: each shift_requests row points to at most one people row, and one people row can have many shift_requests 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.
- events.week_id references weeks.id: each events row points to at most one weeks row, and one weeks row can have many events rows pointing to it.

## Tables

Also described as: staff rota database design, employee scheduling database tables, shift swap and open shift schema.

### settings

The single row of location settings: name, time zone and minimum rest between shifts. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no |  | Primary key | keeps this table to one row Check: `id = 1` |
| `location_name` | TEXT | no |  |  |  |
| `time_zone` | TEXT | no | `'America/New_York'` |  | IANA name; changing it does not rewrite shifts (they are UTC instants) |
| `min_rest_minutes` | INTEGER | no | `600` |  | Check: `min_rest_minutes BETWEEN 0 AND 1440` |

### positions

A job a shift needs, such as Barista, Kitchen or Shift lead. Implements: Position.

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

- Unique index `positions_name_unique` on (lower(name)). Position names are unique ignoring case.
- App rule (section 4): deleted only when no person, template or shift uses it.

### people

A staff member or manager, with hourly wage, weekly maximum and calendar feed token. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique | trimmed and lowercase |
| `phone` | TEXT | yes |  |  | optional free text Check: `length(phone) <= 40` |
| `is_manager` | BOOLEAN | no | `FALSE` |  |  |
| `active` | BOOLEAN | no | `TRUE` |  |  |
| `wage_cents` | INTEGER | no |  |  | per hour; managers only Check: `wage_cents >= 0` |
| `max_weekly_minutes` | INTEGER | no | `2400` |  | 2400 is 40 hours Check: `max_weekly_minutes >= 0 AND max_weekly_minutes % 15 = 0` |
| `calendar_token` | TEXT | no |  | Unique | random; the only key to the calendar feed (R7) Check: `length(calendar_token) >= 32` |

- Table check: `email = lower(email)`
- App rule (section 4): the last active manager cannot be deactivated or lose is_manager.
- App rule (R8): deactivating someone with assigned shifts that have not ended is refused.

### person_positions

One position a person holds; only holders can work or claim that position's shifts. Implements: PersonPosition.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `person_id` | TEXT | no |  | Primary key (part); References people.id |  |
| `position_id` | TEXT | no |  | Primary key (part); References positions.id |  |

- App rule (section 4): removing a position from someone with future shifts in it is refused.

### availability_entries

A recurring weekly period, on one weekday, when a person is unavailable. Implements: Availability.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | no |  | References people.id |  |
| `weekday` | INTEGER | no |  |  | 1 is Monday, 7 is Sunday Check: `weekday BETWEEN 1 AND 7` |
| `start_time` | TEXT | no |  |  | local "HH:MM" on 15-minute steps |
| `end_time` | TEXT | no |  |  | local "HH:MM"; a whole day is 00:00 to 24:00 |
| `note` | TEXT | yes |  |  | Check: `length(note) <= 200` |

- Table check: `length(start_time) = 5 AND substr(start_time, 3, 1) = ':' AND substr(start_time, 4, 2) IN ('00', '15', '30', '45')`
- Table check: `length(end_time) = 5 AND substr(end_time, 3, 1) = ':' AND substr(end_time, 4, 2) IN ('00', '15', '30', '45')`
- Table check: `start_time >= '00:00' AND end_time <= '24:00' AND start_time < end_time`

### time_off_requests

A request for a date range off, with its decision. Implements: TimeOff.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | no |  | References people.id |  |
| `start_date` | DATE | no |  |  |  |
| `end_date` | DATE | no |  |  | defaults to the start date in the form |
| `status` | TEXT | no | `'requested'` |  | Check: `status IN ('requested', 'approved', 'declined')` |
| `reason` | TEXT | yes |  |  | optional; seen only by the person and managers Check: `length(reason) <= 300` |
| `decided_by` | TEXT | yes |  | References people.id |  |
| `created_at` | TIMESTAMP | no |  |  |  |

- Table check: `end_date >= start_date`
- Table check: `(status = 'requested' AND decided_by IS NULL) OR (status <> 'requested' AND decided_by IS NOT NULL)`
- App rule: the start date cannot be in the past. App rule (R5): approval is refused while the person has assigned shifts on those dates.

### templates

A reusable shift pattern with a position and local start and end times. Implements: Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `position_id` | TEXT | no |  | References positions.id |  |
| `start_time` | TEXT | no |  |  | local "HH:MM" on 15-minute steps |
| `end_time` | TEXT | no |  |  | local "HH:MM"; at or before start_time means it ends the next day |

- Table check: `length(start_time) = 5 AND substr(start_time, 3, 1) = ':' AND substr(start_time, 4, 2) IN ('00', '15', '30', '45') AND start_time BETWEEN '00:00' AND '23:45'`
- Table check: `length(end_time) = 5 AND substr(end_time, 3, 1) = ':' AND substr(end_time, 4, 2) IN ('00', '15', '30', '45') AND end_time BETWEEN '00:00' AND '24:00'`
- Table check: `((CAST(substr(end_time, 1, 2) AS INTEGER) * 60 + CAST(substr(end_time, 4, 2) AS INTEGER)) - (CAST(substr(start_time, 1, 2) AS INTEGER) * 60 + CAST(substr(start_time, 4, 2) AS INTEGER)) + 1440) % 1440 BETWEEN 120 AND 720`

### template_weekdays

One weekday, 1 (Monday) to 7 (Sunday), that a template applies to. Implements: child table of Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `template_id` | TEXT | no |  | Primary key (part); References templates.id, on delete cascade |  |
| `weekday` | INTEGER | no |  | Primary key (part) | 1 is Monday, 7 is Sunday Check: `weekday BETWEEN 1 AND 7` |

- App rule (section 5): a template has at least one weekday.

### weeks

A Monday-to-Sunday schedule week, draft or published. Implements: Week.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `week_start` | DATE | no |  | Unique | a Monday (checked by the app) |
| `status` | TEXT | no | `'draft'` |  | Check: `status IN ('draft', 'published')` |
| `published_at` | TIMESTAMP | yes |  |  |  |
| `published_by` | TEXT | yes |  | References people.id |  |

- Table check: `(status = 'draft' AND published_at IS NULL AND published_by IS NULL) OR (status = 'published' AND published_at IS NOT NULL AND published_by IS NOT NULL)`
- App rule (R2): a week with a hard conflict, an unexplained warning or no shifts cannot be published.

### shifts

One shift in a week: position, UTC start and end, and the assigned person or none for an open shift. Implements: Shift.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `week_id` | TEXT | no |  | References weeks.id |  |
| `position_id` | TEXT | no |  | References positions.id |  |
| `starts_at` | TIMESTAMP | no |  |  | UTC instant |
| `ends_at` | TIMESTAMP | no |  |  | UTC instant |
| `person_id` | TEXT | yes |  | References people.id | NULL: open shift |
| `note` | TEXT | yes |  |  | shown to managers and in the assigned person's emails, not on the staff schedule |
| `override_note` | TEXT | yes |  |  | required when saved despite a warning (R4); cleared when saved with none |

- Table check: `ends_at > starts_at`
- Table check: `override_note IS NULL OR length(trim(override_note)) BETWEEN 5 AND 200`
- Index `shifts_person_starts` on (person_id, starts_at)
- Index `shifts_week` on (week_id)
- App rule (R1): 15-minute steps, 2 to 12 hours of real elapsed time; belongs to the week it starts in.
- App rule (section 6): hard conflicts (overlap, approved time off, position not held, inactive person) always block.

### shift_requests

A claim on an open shift, or a swap or drop of an assigned one, with its status and decision. Implements: ShiftRequest.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('claim', 'swap', 'drop')` |
| `shift_id` | TEXT | no |  | References shifts.id |  |
| `requested_by` | TEXT | no |  | References people.id |  |
| `target_person_id` | TEXT | yes |  | References people.id | swaps only: the colleague asked to take the shift |
| `reason` | TEXT | yes |  |  | drops only, required; seen by the requester and managers Check: `length(reason) <= 300` |
| `created_at` | TIMESTAMP | no |  |  | claims wait in this order |
| `status` | TEXT | no |  |  | Check: `status IN ('pending_target', 'pending_manager', 'approved', 'declined', 'cancelled')` |
| `decided_by` | TEXT | yes |  | References people.id | also set on declines, cancels and automatic declines |
| `decided_at` | TIMESTAMP | yes |  |  |  |

- Table check: `(kind = 'swap' AND target_person_id IS NOT NULL AND target_person_id <> requested_by) OR (kind <> 'swap' AND target_person_id IS NULL)`
- Table check: `(kind = 'drop' AND reason IS NOT NULL) OR (kind <> 'drop' AND reason IS NULL)`
- Table check: `status <> 'pending_target' OR kind = 'swap'`
- Table check: `(status IN ('pending_target', 'pending_manager') AND decided_by IS NULL AND decided_at IS NULL) OR (status IN ('approved', 'declined', 'cancelled') AND decided_by IS NOT NULL AND decided_at IS NOT NULL)`
- Unique index `shift_requests_one_pending_swap_or_drop` on (shift_id) where `kind IN ('swap', 'drop') AND status IN ('pending_target', 'pending_manager')`. An assigned shift has at most one pending swap or drop. Several claims may wait on one open shift.
- App rule (section 7): creating, accepting and approving are refused once the shift has started.

### events

An append-only audit entry for a change, linked to a shift or week where there is one. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `at` | TIMESTAMP | no |  |  |  |
| `actor_id` | TEXT | yes |  | References people.id | NULL when no person acted (the seed load) |
| `type` | TEXT | no |  |  |  |
| `shift_id` | TEXT | yes |  |  | no foreign key: events outlive deleted shifts |
| `week_id` | TEXT | yes |  | References weeks.id |  |
| `data` | TEXT | yes |  |  | JSON text; never holds a calendar token |

### notifications

An email in the outbox, written before it is sent, with its status. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | which email it is: one of the triggers in section 10 |
| `to_email` | TEXT | no |  |  |  |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `status` | TEXT | no | `'queued'` |  | logged when no provider is configured Check: `status IN ('queued', 'sent', 'failed', 'logged')` |
| `error` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no |  |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

## The SQL

```sql
-- Shift Scheduling database schema
-- Source: https://personalsoftwarefactory.com/blueprints/shift-scheduling (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 location's settings. Exactly one row.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY CHECK (id = 1), -- keeps this table to one row
  location_name TEXT NOT NULL,
  time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; changing it does not rewrite shifts (they are UTC instants)
  min_rest_minutes INTEGER NOT NULL DEFAULT 600 CHECK (min_rest_minutes BETWEEN 0 AND 1440)
);

-- A job a shift needs, such as Barista or Kitchen.
-- App rule (section 4): deleted only when no person, template or shift uses it.
CREATE TABLE positions (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL
);

-- Position names are unique ignoring case.
CREATE UNIQUE INDEX positions_name_unique ON positions (lower(name));

-- A person who works shifts; managers have is_manager. Never deleted, only deactivated.
-- App rule (section 4): the last active manager cannot be deactivated or lose is_manager.
-- App rule (R8): deactivating someone with assigned shifts that have not ended is refused.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE, -- trimmed and lowercase
  phone TEXT CHECK (length(phone) <= 40), -- optional free text
  is_manager BOOLEAN NOT NULL DEFAULT FALSE,
  active BOOLEAN NOT NULL DEFAULT TRUE,
  wage_cents INTEGER NOT NULL CHECK (wage_cents >= 0), -- per hour; managers only
  max_weekly_minutes INTEGER NOT NULL DEFAULT 2400 CHECK (max_weekly_minutes >= 0 AND max_weekly_minutes % 15 = 0), -- 2400 is 40 hours
  calendar_token TEXT NOT NULL UNIQUE CHECK (length(calendar_token) >= 32), -- random; the only key to the calendar feed (R7)
  CHECK (email = lower(email))
);

-- Which positions a person holds. Only holders can work or claim a position's shifts.
-- App rule (section 4): removing a position from someone with future shifts in it is refused.
CREATE TABLE person_positions (
  person_id TEXT NOT NULL REFERENCES people(id),
  position_id TEXT NOT NULL REFERENCES positions(id),
  PRIMARY KEY (person_id, position_id)
);

-- A recurring weekly period when a person is unavailable. Entries may overlap.
-- Unavailability that crosses midnight is two entries.
CREATE TABLE availability_entries (
  id TEXT PRIMARY KEY,
  person_id TEXT NOT NULL REFERENCES people(id),
  weekday INTEGER NOT NULL CHECK (weekday BETWEEN 1 AND 7), -- 1 is Monday, 7 is Sunday
  start_time TEXT NOT NULL, -- local "HH:MM" on 15-minute steps
  end_time TEXT NOT NULL, -- local "HH:MM"; a whole day is 00:00 to 24:00
  note TEXT CHECK (length(note) <= 200),
  CHECK (length(start_time) = 5 AND substr(start_time, 3, 1) = ':' AND substr(start_time, 4, 2) IN ('00', '15', '30', '45')),
  CHECK (length(end_time) = 5 AND substr(end_time, 3, 1) = ':' AND substr(end_time, 4, 2) IN ('00', '15', '30', '45')),
  CHECK (start_time >= '00:00' AND end_time <= '24:00' AND start_time < end_time)
);

-- A request for days off. Overlapping requests are allowed; requests cannot be withdrawn and a decision is final.
-- App rule: the start date cannot be in the past. App rule (R5): approval is refused while the person has assigned shifts on those dates.
CREATE TABLE time_off_requests (
  id TEXT PRIMARY KEY,
  person_id TEXT NOT NULL REFERENCES people(id),
  start_date DATE NOT NULL,
  end_date DATE NOT NULL, -- defaults to the start date in the form
  status TEXT NOT NULL DEFAULT 'requested' CHECK (status IN ('requested', 'approved', 'declined')),
  reason TEXT CHECK (length(reason) <= 300), -- optional; seen only by the person and managers
  decided_by TEXT REFERENCES people(id),
  created_at TIMESTAMP NOT NULL,
  CHECK (end_date >= start_date),
  CHECK ((status = 'requested' AND decided_by IS NULL) OR (status <> 'requested' AND decided_by IS NOT NULL))
);

-- A reusable shift pattern, such as Opening. Names need not be unique. Shifts do not refer to templates.
CREATE TABLE templates (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  position_id TEXT NOT NULL REFERENCES positions(id),
  start_time TEXT NOT NULL, -- local "HH:MM" on 15-minute steps
  end_time TEXT NOT NULL, -- local "HH:MM"; at or before start_time means it ends the next day
  CHECK (length(start_time) = 5 AND substr(start_time, 3, 1) = ':' AND substr(start_time, 4, 2) IN ('00', '15', '30', '45') AND start_time BETWEEN '00:00' AND '23:45'),
  CHECK (length(end_time) = 5 AND substr(end_time, 3, 1) = ':' AND substr(end_time, 4, 2) IN ('00', '15', '30', '45') AND end_time BETWEEN '00:00' AND '24:00'),
  -- R1: 2 to 12 hours long, wrapping past midnight when the end is at or before the start
  CHECK (((CAST(substr(end_time, 1, 2) AS INTEGER) * 60 + CAST(substr(end_time, 4, 2) AS INTEGER))
        - (CAST(substr(start_time, 1, 2) AS INTEGER) * 60 + CAST(substr(start_time, 4, 2) AS INTEGER)) + 1440) % 1440 BETWEEN 120 AND 720)
);

-- The weekdays a template applies to (the spec's weekdays list).
-- App rule (section 5): a template has at least one weekday.
CREATE TABLE template_weekdays (
  template_id TEXT NOT NULL REFERENCES templates(id) ON DELETE CASCADE,
  weekday INTEGER NOT NULL CHECK (weekday BETWEEN 1 AND 7), -- 1 is Monday, 7 is Sunday
  PRIMARY KEY (template_id, weekday)
);

-- A schedule week, Monday to Sunday, published as a whole. There is no unpublish.
-- App rule (R2): a week with a hard conflict, an unexplained warning or no shifts cannot be published.
CREATE TABLE weeks (
  id TEXT PRIMARY KEY,
  week_start DATE NOT NULL UNIQUE, -- a Monday (checked by the app)
  status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'published')),
  published_at TIMESTAMP,
  published_by TEXT REFERENCES people(id),
  CHECK ((status = 'draft' AND published_at IS NULL AND published_by IS NULL) OR (status = 'published' AND published_at IS NOT NULL AND published_by IS NOT NULL))
);

-- One shift. person_id NULL means an open shift.
-- App rule (R1): 15-minute steps, 2 to 12 hours of real elapsed time; belongs to the week it starts in.
-- App rule (section 6): hard conflicts (overlap, approved time off, position not held, inactive person) always block.
CREATE TABLE shifts (
  id TEXT PRIMARY KEY,
  week_id TEXT NOT NULL REFERENCES weeks(id),
  position_id TEXT NOT NULL REFERENCES positions(id),
  starts_at TIMESTAMP NOT NULL, -- UTC instant
  ends_at TIMESTAMP NOT NULL, -- UTC instant
  person_id TEXT REFERENCES people(id), -- NULL: open shift
  note TEXT, -- shown to managers and in the assigned person's emails, not on the staff schedule
  override_note TEXT, -- required when saved despite a warning (R4); cleared when saved with none
  CHECK (ends_at > starts_at),
  CHECK (override_note IS NULL OR length(trim(override_note)) BETWEEN 5 AND 200)
);

CREATE INDEX shifts_person_starts ON shifts (person_id, starts_at);
CREATE INDEX shifts_week ON shifts (week_id);

-- A claim on an open shift, or a swap or drop of an assigned one.
-- App rule (section 7): creating, accepting and approving are refused once the shift has started.
CREATE TABLE shift_requests (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL CHECK (kind IN ('claim', 'swap', 'drop')),
  shift_id TEXT NOT NULL REFERENCES shifts(id),
  requested_by TEXT NOT NULL REFERENCES people(id),
  target_person_id TEXT REFERENCES people(id), -- swaps only: the colleague asked to take the shift
  reason TEXT CHECK (length(reason) <= 300), -- drops only, required; seen by the requester and managers
  created_at TIMESTAMP NOT NULL, -- claims wait in this order
  status TEXT NOT NULL CHECK (status IN ('pending_target', 'pending_manager', 'approved', 'declined', 'cancelled')),
  decided_by TEXT REFERENCES people(id), -- also set on declines, cancels and automatic declines
  decided_at TIMESTAMP,
  CHECK ((kind = 'swap' AND target_person_id IS NOT NULL AND target_person_id <> requested_by) OR (kind <> 'swap' AND target_person_id IS NULL)),
  CHECK ((kind = 'drop' AND reason IS NOT NULL) OR (kind <> 'drop' AND reason IS NULL)),
  CHECK (status <> 'pending_target' OR kind = 'swap'),
  CHECK ((status IN ('pending_target', 'pending_manager') AND decided_by IS NULL AND decided_at IS NULL)
      OR (status IN ('approved', 'declined', 'cancelled') AND decided_by IS NOT NULL AND decided_at IS NOT NULL))
);

-- An assigned shift has at most one pending swap or drop. Several claims may wait on one open shift.
CREATE UNIQUE INDEX shift_requests_one_pending_swap_or_drop ON shift_requests (shift_id)
  WHERE kind IN ('swap', 'drop') AND status IN ('pending_target', 'pending_manager');

-- Audit log entry. Append-only (R9).
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  at TIMESTAMP NOT NULL,
  actor_id TEXT REFERENCES people(id), -- NULL when no person acted (the seed load)
  type TEXT NOT NULL,
  shift_id TEXT, -- no foreign key: events outlive deleted shifts
  week_id TEXT REFERENCES weeks(id),
  data TEXT -- JSON text; never holds a calendar token
);

-- An email in the outbox. Written here first, sent after the change is saved.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL, -- which email it is: one of the triggers in section 10
  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 provider is configured
  error TEXT,
  created_at TIMESTAMP NOT NULL,
  sent_at TIMESTAMP
);
```

## Seed data

Loads Harbor Café from section 14: the settings row, 3 positions, 8 people with their 10 position links, Jo's Tuesday unavailability, Kai's approved time off, 5 templates with 24 weekday rows, the published week of Oct 12 and the draft week of Oct 19 with 24 shifts each, Jo's pending swap to Lee, and the single "seed loaded" event. Times use the spec's clock (Thursday Oct 15, 2026, 09:00 local), and local times are converted to UTC at daylight time (UTC-4). The spec makes calendar tokens random, so the seed holds clearly marked placeholder tokens to replace. The outbox starts empty.

## Load it

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

## Design decisions

### Shifts are UTC instants; patterns are local clock times

Section 5 stores shift starts and ends as UTC timestamps and enters and shows them in the location's time zone, so a daylight-saving night still counts real elapsed hours (R1). Templates and availability are recurring local times, so they are "HH:MM" text with CHECKs for 15-minute steps. The build test found nothing said what changing the time zone does to existing shifts; the spec now says shifts stay at the same UTC instants, so their local times move with it. (This point comes from a gap the blueprint's build test found.)

### Template weekdays are a child table

A template applies to a list of weekdays, at least one (section 5). The build test found Template.weekdays was just "list", with no type. template_weekdays holds one row per weekday number 1 to 7 with a composite primary key, so a day can't repeat; "at least one" spans rows and stays an app rule. (This point comes from a gap the blueprint's build test found.)

### Template length is checked in the database

Templates follow R1: an end at or before the start ends the next day, and the length is 2 to 12 hours. The build test found templates had no rules for names, overnight times, length, weekdays or deleting. A CHECK turns both "HH:MM" values into minutes and requires the wrapped difference to be 120 to 720, so 22:00 to 06:00 passes and 06:00 to 06:30 is refused. Shift length uses real elapsed time across daylight saving, which needs date arithmetic, so it stays in the app. (This point comes from a gap the blueprint's build test found.)

### One request table, with columns tied to the kind

Section 5 gives claims, swaps and drops one ShiftRequest entity: only swaps have a target (someone other than the requester) and the pending_target status, and only drops have a reason, which they require. CHECKs enforce each of these, and a partial unique index on shift_id for pending swaps and drops enforces section 7's "at most one pending swap or drop" while letting several claims wait on one open shift.

### Every finished request records who and when

The build test found decided_by and decided_at were described only for decisions, not for declines by the swap target, cancels by the requester or the automatic decline of competing claims. The spec now sets both on all of them, so a CHECK requires both on approved, declined and cancelled requests and neither on pending ones. created_at, which the build also found missing, orders claims in line and the manager's Requests list. (This point comes from a gap the blueprint's build test found.)

### Events keep a shift id without a foreign key

R9 writes an event for every change and never deletes events, while section 7 lets a manager delete a shift that has no request history. A foreign key from events to shifts would block every such delete, so events.shift_id is a plain column. week_id keeps its foreign key, because weeks are never deleted.

### Conflict checks stay in the app

The hard conflicts and warnings in section 6 (overlap, approved time off, position held, active person, unavailability, rest and weekly hours) compare rows across tables and need local-time date arithmetic, so the schema leaves them to the app and names them in comments. The schema stores what they need: override_note on the shift, 5 to 200 characters after trimming (R4), and min_rest_minutes in settings.

## Example queries

### Shifts per person in the draft week

```sql
SELECT COALESCE(pe.name, 'Open') AS person, COUNT(*) AS shifts FROM shifts s JOIN weeks w ON w.id = s.week_id LEFT JOIN people pe ON pe.id = s.person_id WHERE w.week_start = '2026-10-19' GROUP BY COALESCE(pe.name, 'Open') ORDER BY person;
```

On the seed data: 9 rows, 24 shifts in all (Rosa Diaz 5, Ben Cho 4, Mia Torres 4, Open 1).

### Who an open shift is announced to

```sql
SELECT s.starts_at, po.name AS position, pe.name AS eligible FROM shifts s JOIN positions po ON po.id = s.position_id JOIN person_positions pp ON pp.position_id = s.position_id JOIN people pe ON pe.id = pp.person_id WHERE s.person_id IS NULL AND pe.active = TRUE ORDER BY s.starts_at, pe.name;
```

On the seed data: 5 rows for the open Saturday Barista shift starting 2026-10-24 12:00 UTC (08:00 local): Ana Lima, Ben Cho, Jo Park, Lee Grant, Sam Okafor.

### Hourly wage used to cost each open shift (R6)

```sql
SELECT s.id, po.name AS position, COUNT(*) AS holders, SUM(pe.wage_cents) * 1.0 / COUNT(*) AS avg_wage_cents FROM shifts s JOIN positions po ON po.id = s.position_id JOIN person_positions pp ON pp.position_id = s.position_id JOIN people pe ON pe.id = pp.person_id WHERE s.person_id IS NULL AND pe.active = TRUE GROUP BY s.id, po.name ORDER BY s.id;
```

On the seed data: 1 row (the Saturday Oct 24 Barista shift, 5 holders, 1800 cents an hour, so 8 hours cost $144.00).

### Requests that are still pending

```sql
SELECT r.kind, r.status, rq.name AS requested_by, t.name AS target, s.starts_at FROM shift_requests r JOIN shifts s ON s.id = r.shift_id JOIN people rq ON rq.id = r.requested_by LEFT JOIN people t ON t.id = r.target_person_id WHERE r.status IN ('pending_target', 'pending_manager') ORDER BY r.created_at;
```

On the seed data: 1 row (swap, pending_target, Jo Park to Lee Grant, shift starting 2026-10-17 12:00 UTC).

### Approved time off covering a date

```sql
SELECT pe.name, t.start_date, t.end_date FROM time_off_requests t JOIN people pe ON pe.id = t.person_id WHERE t.status = 'approved' AND t.start_date <= '2026-10-24' AND t.end_date >= '2026-10-24' ORDER BY pe.name;
```

On the seed data: 1 row (Kai Brooks, 2026-10-24 to 2026-10-25).

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