-- Bug Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/bug-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 for the team. 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 60), time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; days, weeks and shown times use it target_s1_hours INTEGER DEFAULT 24 CHECK (target_s1_hours BETWEEN 1 AND 8760), -- NULL means no fix target target_s2_hours INTEGER DEFAULT 168 CHECK (target_s2_hours BETWEEN 1 AND 8760), target_s3_hours INTEGER DEFAULT 720 CHECK (target_s3_hours BETWEEN 1 AND 8760), target_s4_hours INTEGER CHECK (target_s4_hours BETWEEN 1 AND 8760), -- empty by default reopen_window_days INTEGER NOT NULL DEFAULT 30 CHECK (reopen_window_days BETWEEN 1 AND 365), form_enabled BOOLEAN NOT NULL DEFAULT TRUE, -- the public report form is on last_bug_number INTEGER NOT NULL DEFAULT 100 -- R2: a new bug takes last_bug_number + 1 in the same transaction ); -- Someone who can sign in. People are deactivated, never deleted. -- App rule (R16): there is always at least one active admin and one active triager; a person assigned to open bugs cannot 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 is_admin BOOLEAN NOT NULL DEFAULT FALSE, is_triager BOOLEAN NOT NULL DEFAULT FALSE, is_qa BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); -- A part of the product a bug belongs to. Inactive areas stay on existing bugs. CREATE TABLE areas ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 2 AND 40), active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX areas_name_uniq ON areas (LOWER(name)); -- unique ignoring case -- One reported bug, from a team member or the public form. -- App rule (R1, section 6): who may move a bug from which status to which is checked by the app. -- App rule (section 7): fix_due_at = created_at + the target hours for the severity; recalculated on a severity change, which also clears overdue_notified_at. -- App rule (section 8): duplicate_of_id always points at a bug that is not itself a duplicate. -- App rule (R17): version goes up by 1 on every action on this bug except Comment, Watch and Unwatch. CREATE TABLE bugs ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE, -- shown as BUG-104; never reused (R2) version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1), title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 5 AND 120), area_id TEXT REFERENCES areas(id), -- may be empty until triage severity TEXT NOT NULL CHECK (severity IN ('s1', 's2', 's3', 's4')), priority TEXT CHECK (priority IN ('p1', 'p2', 'p3', 'p4')), -- empty until triage status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed', 'verified', 'wont_fix', 'cannot_reproduce', 'duplicate')), steps TEXT NOT NULL CHECK (LENGTH(steps) BETWEEN 10 AND 5000), expected TEXT CHECK (LENGTH(expected) <= 2000), -- optional actual TEXT NOT NULL CHECK (LENGTH(actual) BETWEEN 1 AND 2000), -- what happened environment TEXT CHECK (LENGTH(environment) <= 200), -- where it happened; required on team reports (see the table check) link TEXT CHECK (LENGTH(link) <= 2000 AND link LIKE 'https://%'), -- optional screenshot, recording or page source TEXT NOT NULL CHECK (source IN ('team', 'form')), reporter_id TEXT REFERENCES people(id), -- team reports only reporter_name TEXT CHECK (LENGTH(reporter_name) BETWEEN 1 AND 100), -- form reports only reporter_email TEXT CHECK (LENGTH(reporter_email) <= 254 AND reporter_email = LOWER(reporter_email)), -- form reports only, stored lowercase assignee_id TEXT REFERENCES people(id), -- at most one assignee duplicate_of_id TEXT REFERENCES bugs(id), -- set exactly while status is duplicate review_link TEXT CHECK (review_link LIKE 'https://%'), -- optional, from Send to review fix_note TEXT CHECK (LENGTH(fix_note) BETWEEN 10 AND 2000), -- set by Mark fixed, cleared by Reopen fixed_by_id TEXT REFERENCES people(id), -- the latest fix fixed_at TIMESTAMP, fix_due_at TIMESTAMP, -- NULL when the severity has no target overdue_notified_at TIMESTAMP, -- one overdue notice per due time triaged_at TIMESTAMP, closed_at TIMESTAMP, -- set exactly while the bug is closed reopen_count INTEGER NOT NULL DEFAULT 0 CHECK (reopen_count >= 0), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, -- time of its latest event CHECK ((source = 'team' AND reporter_id IS NOT NULL AND reporter_name IS NULL AND reporter_email IS NULL) OR (source = 'form' AND reporter_id IS NULL AND reporter_name IS NOT NULL AND reporter_email IS NOT NULL)), CHECK (source = 'form' OR (environment IS NOT NULL AND LENGTH(environment) >= 2)), -- R3 CHECK ((status = 'duplicate' AND duplicate_of_id IS NOT NULL) OR (status <> 'duplicate' AND duplicate_of_id IS NULL)), CHECK (duplicate_of_id IS NULL OR duplicate_of_id <> id), CHECK ((status IN ('verified', 'wont_fix', 'cannot_reproduce', 'duplicate') AND closed_at IS NOT NULL) OR (status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed') AND closed_at IS NULL)), CHECK (status NOT IN ('triaged', 'in_progress', 'in_review', 'fixed', 'verified') OR (priority IS NOT NULL AND area_id IS NOT NULL)), -- R4 CHECK (status NOT IN ('in_progress', 'in_review', 'fixed') OR assignee_id IS NOT NULL), -- R6: only the assignee does the work CHECK (status NOT IN ('fixed', 'verified') OR (fix_note IS NOT NULL AND fixed_by_id IS NOT NULL AND fixed_at IS NOT NULL)) -- R7 ); CREATE INDEX bugs_status_idx ON bugs (status); CREATE INDEX bugs_duplicate_of_idx ON bugs (duplicate_of_id); -- A person who gets notifications about a bug. CREATE TABLE watchers ( bug_id TEXT NOT NULL REFERENCES bugs(id), person_id TEXT NOT NULL REFERENCES people(id), PRIMARY KEY (bug_id, person_id) ); -- A comment on a bug. Comments cannot be edited or deleted in v1. CREATE TABLE comments ( id TEXT PRIMARY KEY, bug_id TEXT NOT NULL REFERENCES bugs(id), author_id TEXT NOT NULL REFERENCES people(id), body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 5000), created_at TIMESTAMP NOT NULL ); -- One line of the activity log of a bug. Append-only. -- App rule (R15): events are never edited or deleted. CREATE TABLE events ( id TEXT PRIMARY KEY, seq INTEGER NOT NULL UNIQUE, -- increasing; orders events written at the same moment bug_id TEXT NOT NULL REFERENCES bugs(id), actor_id TEXT REFERENCES people(id), -- NULL for the system and for form reports type TEXT NOT NULL CHECK (type IN ('reported', 'edited', 'triaged', 'severity_changed', 'priority_changed', 'assigned', 'unassigned', 'started', 'sent_to_review', 'review_returned', 'fixed', 'verified', 'reopened', 'wont_fix', 'cannot_reproduce', 'marked_duplicate', 'duplicate_added', 'unmarked_duplicate', 'commented', 'overdue')), data TEXT, -- JSON text: from/to values, changed field names, comment_id, fix note, bug numbers created_at TIMESTAMP NOT NULL ); CREATE INDEX events_bug_idx ON events (bug_id, created_at, seq); -- One email in the outbox, written in the same transaction as the action. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('new_bug', 'acknowledgement', 'assigned', 'ready_to_verify', 'verified', 'fixed_for_reporter', 'reopened', 'closed', 'duplicate', 'comment', 'overdue', 'weekly_summary')), person_id TEXT REFERENCES people(id), -- NULL for outside reporters to_email TEXT NOT NULL, bug_id TEXT REFERENCES bugs(id), -- NULL for weekly summaries subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), -- the job retries until 5 attempts error TEXT, created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); -- A weekly summary that has been queued, so a second run that week sends nothing. CREATE TABLE summary_runs ( week_start DATE PRIMARY KEY, -- the Monday the summarised week starts on created_at TIMESTAMP NOT NULL ); -- One accepted public-form report, for the rate limit (R19). The job deletes rows older than 24 hours. CREATE TABLE form_attempts ( id TEXT PRIMARY KEY, -- row id; the spec lists only ip_hash and created_at ip_hash TEXT NOT NULL, -- HMAC-SHA256 of the IP address with FORM_IP_SECRET; the IP itself is never stored created_at TIMESTAMP NOT NULL ); CREATE INDEX form_attempts_ip_idx ON form_attempts (ip_hash, created_at);