-- Client Onboarding Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/client-onboarding-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. -- The single settings row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only company_name TEXT NOT NULL CHECK (LENGTH(company_name) BETWEEN 1 AND 100), -- shown on the client progress page time_zone TEXT NOT NULL DEFAULT 'UTC', -- IANA name; "today" is the current date in this zone last_digest_on DATE -- set by the scheduled job ); -- A team member who can own projects and tasks. Deactivated, never deleted. -- App rule (R16): people who own open tasks or projects that are active or on hold can't be deactivated; nobody deactivates themselves; the last active admin stays. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 100), title TEXT CHECK (LENGTH(title) <= 100), -- optional, e.g. "Customer success lead" email TEXT NOT NULL CHECK (LENGTH(email) <= 254), is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX people_email_uniq ON people (LOWER(email)); -- unique, compared case-insensitively -- A reusable onboarding checklist. Editing it never changes existing projects (R9). -- App rule (R10): a template starts projects only when it is active and has at least one task in each of kickoff, setup and training. CREATE TABLE templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 80), description TEXT CHECK (LENGTH(description) <= 500), active BOOLEAN NOT NULL DEFAULT TRUE, -- inactive templates are hidden from New project created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL -- compared at create time to refuse a stale preview (section 6) ); CREATE UNIQUE INDEX templates_name_uniq ON templates (LOWER(name)); -- unique case-insensitively -- One checklist item in a template. CREATE TABLE template_tasks ( id TEXT PRIMARY KEY, template_id TEXT NOT NULL REFERENCES templates(id), stage TEXT NOT NULL CHECK (stage IN ('kickoff', 'setup', 'training')), -- never live position INTEGER NOT NULL, -- order within the stage title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 160), description TEXT CHECK (LENGTH(description) <= 1000), default_owner_id TEXT REFERENCES people(id), -- empty means "the project owner" offset_days INTEGER NOT NULL CHECK (offset_days BETWEEN 0 AND 365), -- due this many calendar days after the start date visible_to_client BOOLEAN NOT NULL DEFAULT TRUE -- false marks the task Internal ); CREATE INDEX template_tasks_template_idx ON template_tasks (template_id, stage, position); -- One client engagement moving through kickoff, setup, training and live. -- App rule (R3, R4): a handoff moves one stage forward, only when every task in the current stage is done or skipped. -- App rule (R5): live and cancelled are final; only comments (and, for live, the client link) change them. CREATE TABLE projects ( id TEXT PRIMARY KEY, client_name TEXT NOT NULL CHECK (LENGTH(client_name) BETWEEN 1 AND 80), name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 100), -- e.g. "Customer portal" template_id TEXT NOT NULL REFERENCES templates(id), template_name TEXT NOT NULL, -- copy of the template's name at creation owner_id TEXT NOT NULL REFERENCES people(id), -- the current project owner created_by_id TEXT NOT NULL REFERENCES people(id), start_date DATE NOT NULL, stage TEXT NOT NULL CHECK (stage IN ('kickoff', 'setup', 'training', 'live')), status TEXT NOT NULL CHECK (status IN ('active', 'on_hold', 'cancelled')), -- a live project keeps status active stage_started_at TIMESTAMP NOT NULL, -- set at creation and at every handoff on_hold_since TIMESTAMP, -- set while on hold, cleared on resume hold_reason TEXT CHECK (LENGTH(hold_reason) BETWEEN 3 AND 1000), cancelled_at TIMESTAMP, cancel_reason TEXT CHECK (LENGTH(cancel_reason) BETWEEN 3 AND 1000), live_at TIMESTAMP, -- set by the handoff to live created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, CHECK ((status = 'on_hold' AND on_hold_since IS NOT NULL AND hold_reason IS NOT NULL) OR (status <> 'on_hold' AND on_hold_since IS NULL AND hold_reason IS NULL)), CHECK ((status = 'cancelled' AND cancelled_at IS NOT NULL AND cancel_reason IS NOT NULL) OR (status <> 'cancelled' AND cancelled_at IS NULL AND cancel_reason IS NULL)), CHECK ((stage = 'live' AND live_at IS NOT NULL AND status = 'active') OR (stage <> 'live' AND live_at IS NULL)) ); CREATE UNIQUE INDEX projects_client_name_uniq ON projects (LOWER(client_name), LOWER(name)) WHERE status <> 'cancelled'; -- unique among projects that are not cancelled CREATE INDEX projects_owner_idx ON projects (owner_id); -- A project's copy of a template task, or a task added to one project. Never deleted; skipped instead (R11). -- App rule (R6): due_date is never before the project's start_date. -- App rule (R2): a task is completed or reopened only in the current stage of an active project. CREATE TABLE tasks ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), template_task_id TEXT REFERENCES template_tasks(id) ON DELETE SET NULL, -- empty for tasks added by hand; cleared if the template task is removed stage TEXT NOT NULL CHECK (stage IN ('kickoff', 'setup', 'training')), position INTEGER NOT NULL, -- order within the stage title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 160), description TEXT CHECK (LENGTH(description) <= 1000), visible_to_client BOOLEAN NOT NULL, owner_id TEXT NOT NULL REFERENCES people(id), due_date DATE NOT NULL, status TEXT NOT NULL CHECK (status IN ('open', 'done', 'skipped')), skip_reason TEXT CHECK (LENGTH(skip_reason) BETWEEN 3 AND 1000), -- required when skipped, cleared on unskip closed_at TIMESTAMP, -- set when done or skipped, cleared when reopened or unskipped closed_by_id TEXT REFERENCES people(id), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, CHECK ((status = 'skipped' AND skip_reason IS NOT NULL) OR (status <> 'skipped' AND skip_reason IS NULL)), CHECK ((status = 'open' AND closed_at IS NULL AND closed_by_id IS NULL) OR (status <> 'open' AND closed_at IS NOT NULL AND closed_by_id IS NOT NULL)) ); CREATE INDEX tasks_project_idx ON tasks (project_id, stage, position); CREATE INDEX tasks_owner_idx ON tasks (owner_id, status); -- One stage change in a project's history. CREATE TABLE handoffs ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), from_stage TEXT NOT NULL CHECK (from_stage IN ('kickoff', 'setup', 'training')), to_stage TEXT NOT NULL CHECK (to_stage IN ('setup', 'training', 'live')), from_owner_id TEXT NOT NULL REFERENCES people(id), to_owner_id TEXT NOT NULL REFERENCES people(id), note TEXT CHECK (LENGTH(note) <= 1000), -- optional by_id TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK ((from_stage = 'kickoff' AND to_stage = 'setup') OR (from_stage = 'setup' AND to_stage = 'training') OR (from_stage = 'training' AND to_stage = 'live')) -- R4: exactly one stage forward ); CREATE INDEX handoffs_created_idx ON handoffs (created_at); -- One activity log entry. Append-only: never updated or deleted (R14). CREATE TABLE events ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), task_id TEXT REFERENCES tasks(id), actor_id TEXT REFERENCES people(id), -- empty for system actions created_at TIMESTAMP NOT NULL, type TEXT NOT NULL CHECK (type IN ('project_created', 'project_updated', 'owner_changed', 'task_added', 'task_updated', 'task_completed', 'task_reopened', 'task_skipped', 'task_unskipped', 'dates_shifted', 'handed_off', 'put_on_hold', 'resumed', 'cancelled', 'reassigned', 'commented', 'link_created', 'link_revoked')), comment TEXT, -- comment text, handoff note, or skip, hold or cancel reason data TEXT -- JSON text, e.g. old and new due date, from and to owner, days shifted ); CREATE INDEX events_project_idx ON events (project_id, created_at); -- A read-only progress link for the client. Views set last_viewed_at and write no event. -- App rule (section 12): the link stops working when revoked, when the project is cancelled, or more than 30 days after go-live. CREATE TABLE client_links ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), token TEXT NOT NULL UNIQUE, -- at least 128 random bits, URL-safe; never logged created_by_id TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, revoked_at TIMESTAMP, last_viewed_at TIMESTAMP ); CREATE UNIQUE INDEX client_links_one_active ON client_links (project_id) WHERE revoked_at IS NULL; -- one active link per project -- One outgoing email in the outbox. CREATE TABLE notifications ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), project_id TEXT REFERENCES projects(id), -- empty for digests subject TEXT NOT NULL, body TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('project_created', 'stage_started', 'handed_off', 'owner_changed', 'task_assigned', 'went_live', 'on_hold', 'resumed', 'cancelled', 'commented', 'digest')), 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 BETWEEN 0 AND 5), -- up to 5 attempts in total created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP, error TEXT ); CREATE INDEX notifications_status_idx ON notifications (status, created_at);