-- Inspections database schema -- Source: https://personalsoftwarefactory.com/blueprints/inspections (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, time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; dates and due times are in this zone action_due_days_high INTEGER NOT NULL DEFAULT 1, -- days after the completion date action_due_days_normal INTEGER NOT NULL DEFAULT 7 ); -- A person who signs in: admin, manager or inspector. -- App rule (R8): deactivating or demoting a site's manager needs another manager for their sites first. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, -- the sign-in identity role TEXT NOT NULL CHECK (role IN ('admin', 'manager', 'inspector')), active BOOLEAN NOT NULL ); -- A store or site. -- App rule (R8): the manager is an active person with the manager role; a site with open actions can't be removed. CREATE TABLE sites ( id TEXT PRIMARY KEY, name TEXT NOT NULL, address TEXT, manager_id TEXT NOT NULL REFERENCES people(id) ); -- One site a person works at (child table of Person.site_ids). Removing a site takes it off everyone's list. CREATE TABLE person_sites ( person_id TEXT NOT NULL REFERENCES people(id), site_id TEXT NOT NULL REFERENCES sites(id) ON DELETE CASCADE, PRIMARY KEY (person_id, site_id) ); -- One version of a checklist template. Each version is its own row, keyed by id and version (R1). CREATE TABLE templates ( id TEXT NOT NULL, version INTEGER NOT NULL CHECK (version >= 1), name TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('draft', 'published', 'archived')), -- draft: unpublished edit; archived: older version or retired template PRIMARY KEY (id, version) ); CREATE UNIQUE INDEX templates_one_published ON templates (id) WHERE status = 'published'; -- one current version per template CREATE UNIQUE INDEX templates_one_draft ON templates (id) WHERE status = 'draft'; -- One section of a template version, in order (child table of Template.sections). CREATE TABLE template_sections ( template_id TEXT NOT NULL, version INTEGER NOT NULL, position INTEGER NOT NULL, -- section order title TEXT, -- optional; the signature sits in a final section titled "Sign-off" PRIMARY KEY (template_id, version, position), FOREIGN KEY (template_id, version) REFERENCES templates(id, version) ); -- One question in a template version (child table of the section's questions). -- App rule (section 5): the signature question is in a final Sign-off section and can never be not applicable. CREATE TABLE template_questions ( template_id TEXT NOT NULL, version INTEGER NOT NULL, id TEXT NOT NULL, -- kept across versions: a repeat failure is the same question id section_position INTEGER NOT NULL, position INTEGER NOT NULL, -- order within the section text TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('yes_no', 'rating', 'number', 'text', 'photo', 'signature')), weight NUMERIC NOT NULL DEFAULT 1 CHECK (weight >= 0), -- points; 0 is allowed min NUMERIC, -- number questions: fails below min max NUMERIC, -- number questions: fails above max photo_required_on_fail BOOLEAN NOT NULL DEFAULT FALSE, high_priority BOOLEAN NOT NULL DEFAULT FALSE, -- its action is due action_due_days_high days later PRIMARY KEY (template_id, version, id), FOREIGN KEY (template_id, version, section_position) REFERENCES template_sections(template_id, version, position), CHECK (kind <> 'number' OR min IS NOT NULL OR max IS NOT NULL), -- a number question needs a min, a max or both CHECK (kind = 'number' OR (min IS NULL AND max IS NULL)), CHECK (min IS NULL OR max IS NULL OR min <= max) ); CREATE UNIQUE INDEX template_questions_one_signature ON template_questions (template_id, version) WHERE kind = 'signature'; -- at most one per template -- A recurring inspection at a site. Paused, never deleted. -- template_id names a template, not one version, so it has no foreign key: the version is chosen when each inspection starts. CREATE TABLE schedules ( id TEXT PRIMARY KEY, template_id TEXT NOT NULL, site_id TEXT REFERENCES sites(id) ON DELETE SET NULL, -- removing a site pauses its schedules (R8) every TEXT NOT NULL CHECK (every IN ('daily', 'weekly', 'monthly')), weekday INTEGER CHECK (weekday BETWEEN 1 AND 7), -- weekly: 1 = Monday, 7 = Sunday day_of_month INTEGER CHECK (day_of_month BETWEEN 1 AND 31), -- monthly: 29 to 31 fall on a shorter month's last day due_time TEXT NOT NULL CHECK (due_time LIKE '__:__'), -- HH:MM company time, kept across daylight-saving changes assignee_id TEXT REFERENCES people(id), -- empty when the assignee is inactive active BOOLEAN NOT NULL, -- false = paused CHECK ((every = 'daily' AND weekday IS NULL AND day_of_month IS NULL) OR (every = 'weekly' AND weekday IS NOT NULL AND day_of_month IS NULL) OR (every = 'monthly' AND day_of_month IS NOT NULL AND weekday IS NULL)) ); -- One inspection: scheduled (created by the hourly job) or started by hand. -- App rule (section 7): a due scheduled inspection not completed by due_at plus 12 hours becomes missed; hand-started ones never are. -- App rule (R2): completing needs every question answered or not applicable, required photos and the signature; completed inspections are read-only. CREATE TABLE inspections ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (number LIKE 'I-%'), -- I-0001 template_id TEXT NOT NULL, template_version INTEGER, -- the version published when it was started; empty while due site_id TEXT REFERENCES sites(id) ON DELETE SET NULL, -- empty once the site is removed ("Removed site") inspector_id TEXT REFERENCES people(id), -- whoever started it; while due, the schedule's assignee or empty schedule_id TEXT REFERENCES schedules(id), -- empty for inspections started by hand due_at TIMESTAMP, started_at TIMESTAMP, completed_at TIMESTAMP, status TEXT NOT NULL CHECK (status IN ('due', 'in_progress', 'completed', 'missed', 'site_closed')), score_percent INTEGER CHECK (score_percent BETWEEN 0 AND 100), -- empty when every scorable question is not applicable signature TEXT, -- the signer's typed name (an optional drawing is stored as a file reference) FOREIGN KEY (template_id, template_version) REFERENCES templates(id, version), CHECK ((status = 'completed' AND completed_at IS NOT NULL AND started_at IS NOT NULL AND template_version IS NOT NULL) OR (status <> 'completed' AND completed_at IS NULL AND score_percent IS NULL)), CHECK (status <> 'due' OR started_at IS NULL), CHECK (status <> 'in_progress' OR started_at IS NOT NULL), CHECK (status NOT IN ('missed', 'site_closed') OR schedule_id IS NOT NULL) -- only scheduled inspections are missed or site closed ); CREATE INDEX inspections_site_idx ON inspections (site_id, template_id, completed_at); CREATE INDEX inspections_due_idx ON inspections (status, due_at); -- One answer to one question of an inspection. CREATE TABLE answers ( inspection_id TEXT NOT NULL REFERENCES inspections(id), question_id TEXT NOT NULL, -- template_questions.id in the inspection's template version (app rule: no foreign key, the version lives on the inspection) value TEXT, -- yes_no: 'yes' or 'no'; rating: '1' to '5'; number: decimal text; text: the text; empty for photo and signature not_applicable BOOLEAN NOT NULL DEFAULT FALSE, failed BOOLEAN NOT NULL DEFAULT FALSE, -- calculated by section 6 when saved: No, a rating of 2 or below, or a number outside min to max note TEXT, PRIMARY KEY (inspection_id, question_id), CHECK (NOT (not_applicable = TRUE AND failed = TRUE)) ); -- One photo attached to an answer (child table of Answer.photos). Stored privately. CREATE TABLE answer_photos ( inspection_id TEXT NOT NULL, question_id TEXT NOT NULL, position INTEGER NOT NULL, file_key TEXT NOT NULL, -- reference to the private file (up to 10 MB), served only to people who can see the inspection PRIMARY KEY (inspection_id, question_id, position), FOREIGN KEY (inspection_id, question_id) REFERENCES answers(inspection_id, question_id) ); -- One corrective action, created for every failed answer when its inspection is completed. -- App rule (section 6): a repeat failure is worked out, not stored: the same question id failed at the same site in the previous completed inspection from the same template. -- App rule (R4): overdue from the day after due_on in company time. CREATE TABLE actions ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (number LIKE 'A-%'), -- A-0001 inspection_id TEXT NOT NULL, question_id TEXT NOT NULL, site_id TEXT REFERENCES sites(id) ON DELETE SET NULL, title TEXT NOT NULL, -- the question text priority TEXT NOT NULL CHECK (priority IN ('high', 'normal')), assignee_id TEXT NOT NULL REFERENCES people(id), -- the site's manager at completion; reassignable to an active person at the site due_on DATE NOT NULL, status TEXT NOT NULL CHECK (status IN ('open', 'done')), done_note TEXT CHECK (LENGTH(done_note) BETWEEN 1 AND 2000), done_at TIMESTAMP, FOREIGN KEY (inspection_id, question_id) REFERENCES answers(inspection_id, question_id), CHECK ((status = 'done' AND done_note IS NOT NULL AND done_at IS NOT NULL) OR (status = 'open' AND done_note IS NULL AND done_at IS NULL)) ); CREATE INDEX actions_open_idx ON actions (status, site_id, due_on); -- One photo of the fix (child table of Action.done_photos); required for high-priority actions, up to 5. CREATE TABLE action_photos ( action_id TEXT NOT NULL REFERENCES actions(id), position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), file_key TEXT NOT NULL, -- reference to the private file, up to 10 MB PRIMARY KEY (action_id, position) ); -- One append-only event: every change, each job run and each export. Never edited or deleted (R10). CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT REFERENCES people(id), -- empty for the hourly job type TEXT NOT NULL, data TEXT -- JSON text, e.g. what a job run created, marked missed and emailed ); CREATE INDEX events_at_idx ON events (at); -- One outgoing email in the outbox, listed newest first by number. CREATE TABLE notifications ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (number LIKE 'N-%'), -- N-0001 created_at TIMESTAMP NOT NULL, kind TEXT NOT NULL, -- action created, due today, overdue, inspection missed or repeat failure (section 10) to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged = no email provider configured error TEXT );