-- Training Manual database schema -- Source: https://personalsoftwarefactory.com/blueprints/training-manual (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 for the company. 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; due dates and verification days use it due_soon_days INTEGER NOT NULL DEFAULT 2, -- the due-soon reminder goes this many days before due_on (R8) verification_default_days INTEGER NOT NULL DEFAULT 180, -- default verify_every_days for new subjects email_domain TEXT NOT NULL -- sign-in and new people must use it ); -- Someone who works at the company. An editor is anyone who owns a subject; there is no editor flag. -- App rule (section 4): the last active admin cannot remove their own admin flag or deactivate themselves; manager chains cannot loop. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, -- signatures must match it, ignoring case and extra spaces email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase is_admin BOOLEAN NOT NULL DEFAULT FALSE, manager_id TEXT REFERENCES people(id), -- a manager team is their direct reports only (R8) start_date DATE NOT NULL, -- people who start later are due start_date + due_days (R1) active BOOLEAN NOT NULL DEFAULT TRUE, CHECK (manager_id IS NULL OR manager_id <> id) ); -- A job role, such as Barista. CREATE TABLE roles ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE ); -- A role a person holds. CREATE TABLE person_roles ( person_id TEXT NOT NULL REFERENCES people(id), role_id TEXT NOT NULL REFERENCES roles(id), PRIMARY KEY (person_id, role_id) ); -- A process or policy people are trained on. -- App rule (R6): a published subject is due for verification on verified_at + verify_every_days, in the company time zone. -- App rule (section 4): only admins create subjects and choose the owner. CREATE TABLE subjects ( id TEXT PRIMARY KEY, title TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('process', 'policy')), owner_id TEXT NOT NULL REFERENCES people(id), status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'published', 'archived')), assign_to_all BOOLEAN NOT NULL DEFAULT FALSE, -- every active person open_to_everyone BOOLEAN NOT NULL DEFAULT FALSE, -- readable by all, assigned or not due_days INTEGER NOT NULL, -- days to complete after assignment verify_every_days INTEGER, verified_at TIMESTAMP, verified_by TEXT REFERENCES people(id), version INTEGER CHECK (version >= 1), -- policies only: the current published version pass_mark INTEGER CHECK (pass_mark BETWEEN 0 AND 100), -- percent, optional; only with a quiz CHECK (kind = 'policy' OR version IS NULL) ); -- One step of a subject, in order. A deleted step leaves the reader and the progress count. CREATE TABLE steps ( id TEXT PRIMARY KEY, subject_id TEXT NOT NULL REFERENCES subjects(id), position INTEGER NOT NULL, title TEXT NOT NULL, body TEXT NOT NULL, -- Markdown created_at TIMESTAMP NOT NULL, -- steps added later count as unread (R5) deleted_at TIMESTAMP -- reads of a deleted step stay in history under its title ); -- One multiple-choice quiz question of a subject. -- App rule (section 5): a question has 2 to 5 options. CREATE TABLE questions ( id TEXT PRIMARY KEY, subject_id TEXT NOT NULL REFERENCES subjects(id), position INTEGER NOT NULL, text TEXT NOT NULL, correct_option INTEGER NOT NULL CHECK (correct_option BETWEEN 1 AND 5) -- the position of the correct option ); -- One answer option of a question. CREATE TABLE question_options ( question_id TEXT NOT NULL REFERENCES questions(id), position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), text TEXT NOT NULL, PRIMARY KEY (question_id, position) ); -- A subject assigned to a role. Everyone with the role gets a person assignment, now and later (R1). CREATE TABLE role_assignments ( subject_id TEXT NOT NULL REFERENCES subjects(id), role_id TEXT NOT NULL REFERENCES roles(id), PRIMARY KEY (subject_id, role_id) ); -- A subject assigned to one person, with the date it is due. -- App rule (R1): due_on = assignment date + due_days, or start date + due_days for people who start later; changing due_days leaves existing due dates. -- App rule (section 7): a re-signing version moves due_on to publish date + due_days for people whose signature counted, if that is later. CREATE TABLE person_assignments ( subject_id TEXT NOT NULL REFERENCES subjects(id), person_id TEXT NOT NULL REFERENCES people(id), assigned_at TIMESTAMP NOT NULL, due_on DATE NOT NULL, -- a calendar date in the company time zone; overdue from the day after PRIMARY KEY (subject_id, person_id) ); -- A person reading a step. Append-only (R9). CREATE TABLE step_reads ( person_id TEXT NOT NULL REFERENCES people(id), step_id TEXT NOT NULL REFERENCES steps(id), read_at TIMESTAMP NOT NULL, PRIMARY KEY (person_id, step_id, read_at) ); -- One finished quiz attempt. Append-only (R9); the best score counts. CREATE TABLE quiz_attempts ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), subject_id TEXT NOT NULL REFERENCES subjects(id), at TIMESTAMP NOT NULL, answers TEXT NOT NULL, -- the chosen option position for each question, comma-separated in question order score_percent INTEGER NOT NULL CHECK (score_percent BETWEEN 0 AND 100), -- correct / questions, rounded down passed BOOLEAN NOT NULL -- score_percent >= pass_mark, or any finished attempt when there is no pass mark ); -- One published version of a policy, with the text people signed. CREATE TABLE policy_versions ( subject_id TEXT NOT NULL REFERENCES subjects(id), version INTEGER NOT NULL CHECK (version >= 1), published_at TIMESTAMP NOT NULL, change_note TEXT, requires_resign BOOLEAN NOT NULL, -- everyone assigned must sign again body TEXT NOT NULL, -- the full text of the version steps, kept so every signature can show it PRIMARY KEY (subject_id, version) ); -- A person signing one version of a policy. Never edited or deleted (R4). -- App rule (section 7): the typed name must match the person name, ignoring case and extra spaces; signing again while a signature counts is refused. CREATE TABLE signatures ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), subject_id TEXT NOT NULL, version INTEGER NOT NULL, typed_name TEXT NOT NULL, signed_at TIMESTAMP NOT NULL, FOREIGN KEY (subject_id, version) REFERENCES policy_versions(subject_id, version) ); -- One line of the append-only activity log. An export also writes one, with its filters and row count (R10). CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT REFERENCES people(id), -- NULL for the system type TEXT NOT NULL, -- the spec gives no fixed list data TEXT -- JSON text ); -- One email in the outbox, listed newest first. CREATE TABLE notifications ( id TEXT PRIMARY KEY, created_at TIMESTAMP NOT NULL, kind TEXT NOT NULL, -- the spec gives no fixed list 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')), error TEXT );