-- Employee Onboarding database schema -- Source: https://personalsoftwarefactory.com/blueprints/employee-onboarding (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. -- A person at the company: a team member, a manager, a buddy or a new hire. Deactivated, never deleted. -- App rule (section 5): manager_id must be active and must not create a reporting loop; changing it never changes existing onboardings. -- App rule (R12): people who own open tasks on upcoming or in-progress onboardings, and the current IT and people owners, can't be deactivated. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 100), email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase title TEXT CHECK (LENGTH(title) <= 100), -- optional manager_id TEXT REFERENCES people(id), is_people_team BOOLEAN NOT NULL, active BOOLEAN NOT NULL, start_date DATE, -- set for new hires; they cannot sign in before it CHECK (manager_id <> id) -- never the person themself ); -- The single settings row. Comes after people because it names the IT and people owners. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only company_name TEXT NOT NULL, time_zone TEXT NOT NULL, -- IANA name; "today" and every date are in this zone it_owner_id TEXT NOT NULL REFERENCES people(id), -- receives it-role tasks; must be active (R12) people_owner_id TEXT NOT NULL REFERENCES people(id), -- receives people-role tasks; must be active (R12) digest_hour INTEGER NOT NULL DEFAULT 8 CHECK (digest_hour BETWEEN 0 AND 23) -- team time ); -- A checklist template: one base template per onboarding, plus any add-ons. Deactivated, never deleted. -- App rule (R6): a base template always has at least one task. CREATE TABLE templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 80), kind TEXT NOT NULL CHECK (kind IN ('base', 'add_on')), active BOOLEAN NOT NULL -- inactive templates can't be picked for a new onboarding ); CREATE UNIQUE INDEX templates_name_uniq ON templates (LOWER(name)); -- unique ignoring case -- One task in a template. CREATE TABLE template_tasks ( id TEXT PRIMARY KEY, template_id TEXT NOT NULL REFERENCES templates(id), title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 120), description TEXT CHECK (LENGTH(description) <= 1000), -- optional owner_role TEXT NOT NULL CHECK (owner_role IN ('manager', 'it', 'people', 'buddy', 'new_hire')), offset_days INTEGER NOT NULL CHECK (offset_days BETWEEN -60 AND 120), -- 0 is the start date; negative is before it is_check_in BOOLEAN NOT NULL, -- check-ins collect a private note when completed position INTEGER NOT NULL, -- task order within the template CHECK (is_check_in = FALSE OR owner_role IN ('manager', 'people')) -- only they may read the note ); CREATE INDEX template_tasks_template_idx ON template_tasks (template_id, position); -- One new hire's onboarding. -- App rule (R3): creation is refused when a task needs a buddy and none is chosen, or the new hire has no active manager. -- App rule (R9): completed automatically when every task is done or skipped; a cancelled onboarding is read-only. CREATE TABLE onboardings ( id TEXT PRIMARY KEY, new_hire_id TEXT NOT NULL REFERENCES people(id), manager_id TEXT NOT NULL REFERENCES people(id), -- copied from the new hire at creation; decides who reads check-in notes buddy_id TEXT REFERENCES people(id), start_date DATE NOT NULL, title TEXT, -- copied from the new hire's title at creation, e.g. "Account Executive" status TEXT NOT NULL CHECK (status IN ('upcoming', 'in_progress', 'completed', 'cancelled')), cancel_reason TEXT CHECK (LENGTH(cancel_reason) BETWEEN 3 AND 200), -- seen only by the people team and the manager created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK (buddy_id <> new_hire_id), -- the new hire can't be their own buddy CHECK ((status = 'cancelled' AND cancel_reason IS NOT NULL) OR (status <> 'cancelled' AND cancel_reason IS NULL)) ); CREATE UNIQUE INDEX onboardings_one_live ON onboardings (new_hire_id) WHERE status IN ('upcoming', 'in_progress'); -- one live onboarding per person (section 7) -- One template name copied onto an onboarding (child table of Onboarding.template_names). CREATE TABLE onboarding_templates ( onboarding_id TEXT NOT NULL REFERENCES onboardings(id), position INTEGER NOT NULL, -- 1 is the base template, then add-ons in name order template_name TEXT NOT NULL, -- copied name; later renames never change it PRIMARY KEY (onboarding_id, position) ); -- One task on an onboarding, copied once from a template (section 7). -- App rule (R2): due_date = start_date + offset_days, moved to the Friday before when it falls on a weekend; open tasks are recalculated when the start date moves. -- App rule (R8): check-in notes are visible only to the onboarding's manager and the people team, never in emails, events or exports. CREATE TABLE tasks ( id TEXT PRIMARY KEY, onboarding_id TEXT NOT NULL REFERENCES onboardings(id), title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 120), description TEXT CHECK (LENGTH(description) <= 1000), owner_role TEXT NOT NULL CHECK (owner_role IN ('manager', 'it', 'people', 'buddy', 'new_hire')), owner_id TEXT NOT NULL REFERENCES people(id), -- resolved from owner_role at creation (R3) offset_days INTEGER NOT NULL CHECK (offset_days BETWEEN -60 AND 120), due_date DATE NOT NULL, is_check_in BOOLEAN NOT NULL, position INTEGER NOT NULL, -- the order the task was copied in status TEXT NOT NULL CHECK (status IN ('open', 'done', 'skipped')), done_at TIMESTAMP, done_by TEXT REFERENCES people(id), skip_reason TEXT CHECK (LENGTH(skip_reason) BETWEEN 3 AND 200), check_in_note TEXT CHECK (LENGTH(check_in_note) BETWEEN 5 AND 2000), -- kept when a check-in is reopened CHECK (is_check_in = FALSE OR owner_role IN ('manager', 'people')), CHECK (status <> 'open' OR (done_at IS NULL AND done_by IS NULL AND skip_reason IS NULL)), -- reopening clears them CHECK (status <> 'done' OR (done_at IS NOT NULL AND done_by IS NOT NULL AND skip_reason IS NULL)), CHECK (status <> 'skipped' OR skip_reason IS NOT NULL), -- skipping needs a reason (R4) CHECK (is_check_in = TRUE OR check_in_note IS NULL), -- notes on check-ins only CHECK (is_check_in = FALSE OR status <> 'done' OR check_in_note IS NOT NULL) -- R8: completing a check-in needs a note ); CREATE INDEX tasks_onboarding_idx ON tasks (onboarding_id, position); CREATE INDEX tasks_owner_idx ON tasks (owner_id, status, due_date); -- One append-only activity entry on an onboarding. Never edited or deleted. -- App rule (R13): template, people and settings changes are not logged as events. CREATE TABLE events ( id TEXT PRIMARY KEY, onboarding_id TEXT NOT NULL REFERENCES onboardings(id), actor_id TEXT REFERENCES people(id), -- empty for the system type TEXT NOT NULL CHECK (type IN ('created', 'start_date_moved', 'task_done', 'task_reopened', 'task_skipped', 'task_reassigned', 'cancelled', 'completed', 'reminded')), data TEXT, -- JSON text, e.g. old and new start date; never a check-in note or cancel reason created_at TIMESTAMP NOT NULL ); CREATE INDEX events_onboarding_idx ON events (onboarding_id, created_at); -- One outgoing email in the outbox. -- App rule (R9): cancelling an onboarding deletes its pending and failed rows. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('digest', 'pre_start', 'assigned', 'cancelled')), to_email TEXT NOT NULL, person_id TEXT NOT NULL REFERENCES people(id), onboarding_id TEXT REFERENCES onboardings(id), subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('pending', 'sent', 'logged', 'failed')), -- logged = no email provider configured error TEXT, created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); CREATE INDEX notifications_status_idx ON notifications (status, created_at); -- One daily digest already queued for a person; a person never gets two on the same day. CREATE TABLE digest_runs ( person_id TEXT NOT NULL REFERENCES people(id), date DATE NOT NULL, -- team date PRIMARY KEY (person_id, date) );