-- 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 );