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