-- Help Desk database schema -- Source: https://personalsoftwarefactory.com/blueprints/help-desk (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 support team member who answers tickets. Agents are deactivated, never deleted. -- App rule (R6): deactivating an agent who still has tickets that are not solved or closed is refused. CREATE TABLE agents ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); -- The single settings row for the one company and support address. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only company_name TEXT NOT NULL, support_email TEXT NOT NULL, -- the address customers write to and replies come from time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; used for report weeks target_urgent_minutes INTEGER NOT NULL DEFAULT 60 CHECK (target_urgent_minutes BETWEEN 15 AND 10080), target_high_minutes INTEGER NOT NULL DEFAULT 240 CHECK (target_high_minutes BETWEEN 15 AND 10080), target_normal_minutes INTEGER NOT NULL DEFAULT 480 CHECK (target_normal_minutes BETWEEN 15 AND 10080), target_low_minutes INTEGER NOT NULL DEFAULT 1440 CHECK (target_low_minutes BETWEEN 15 AND 10080), auto_close_days INTEGER NOT NULL DEFAULT 4 CHECK (auto_close_days BETWEEN 1 AND 28), -- days after solved_at acknowledgement_template TEXT, -- placeholders: {requester_first_name}, {ticket_number}, {subject} last_ticket_number INTEGER NOT NULL DEFAULT 1000 -- counter for ticket numbers; deleting tickets never lowers it ); -- A customer who wrote in by email or the web form. Requesters never sign in. CREATE TABLE requesters ( id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase name TEXT CHECK (LENGTH(name) <= 100), -- optional created_at TIMESTAMP NOT NULL ); -- One support request with one assignee and a status. -- App rule (R6): assignee_id must point to an active agent. -- App rule (R1, R2): a ticket never returns to new; agents can never set new or closed; only the job closes tickets. -- App rule (R5): first_response_due_at = created_at + the target for the priority; recalculated on a priority change only before the first response. -- App rule (R13): version goes up by 1 on every change. CREATE TABLE tickets ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number >= 1001), -- taken from settings.last_ticket_number + 1, never reused subject TEXT NOT NULL CHECK (LENGTH(subject) BETWEEN 1 AND 150), requester_id TEXT NOT NULL REFERENCES requesters(id) ON DELETE CASCADE, -- R14 removes a requester's tickets channel TEXT NOT NULL CHECK (channel IN ('email', 'web', 'agent')), status TEXT NOT NULL CHECK (status IN ('new', 'open', 'pending', 'on_hold', 'solved', 'closed')), priority TEXT NOT NULL DEFAULT 'normal' CHECK (priority IN ('low', 'normal', 'high', 'urgent')), assignee_id TEXT REFERENCES agents(id), -- at most one assignee follow_up_of TEXT REFERENCES tickets(id), -- the closed ticket this one follows up (R3) created_at TIMESTAMP NOT NULL, first_response_at TIMESTAMP, -- first public reply by an agent first_response_due_at TIMESTAMP NOT NULL, solved_at TIMESTAMP, -- set when solved, cleared when reopened, kept when closed closed_at TIMESTAMP, updated_at TIMESTAMP NOT NULL, version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1), CHECK (status <> 'new' OR (assignee_id IS NULL AND first_response_at IS NULL)), -- R1: assignment or a first reply makes it open CHECK (status NOT IN ('solved', 'closed') OR assignee_id IS NOT NULL), -- R4: solving needs an owner CHECK ((status IN ('solved', 'closed') AND solved_at IS NOT NULL) OR (status NOT IN ('solved', 'closed') AND solved_at IS NULL)), CHECK ((status = 'closed' AND closed_at IS NOT NULL) OR (status <> 'closed' AND closed_at IS NULL)) ); CREATE INDEX tickets_status_idx ON tickets (status); CREATE INDEX tickets_assignee_idx ON tickets (assignee_id); CREATE INDEX tickets_requester_idx ON tickets (requester_id); -- One entry in a ticket's thread: a requester email, a public reply or an internal note. -- App rule (R7): internal notes are never emailed or included in anything the requester receives. CREATE TABLE messages ( id TEXT PRIMARY KEY, ticket_id TEXT NOT NULL REFERENCES tickets(id) ON DELETE CASCADE, kind TEXT NOT NULL CHECK (kind IN ('requester', 'public_reply', 'internal_note')), author_agent_id TEXT REFERENCES agents(id), -- set for public replies and notes, empty for requester messages body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 20000), -- plain text, shown as text, never as HTML email_message_id TEXT UNIQUE, -- Message-ID header of the email received or sent; used for threading (R11) and duplicates (R12) created_at TIMESTAMP NOT NULL, CHECK ((kind = 'requester' AND author_agent_id IS NULL) OR (kind <> 'requester' AND author_agent_id IS NOT NULL)), CHECK (kind <> 'internal_note' OR email_message_id IS NULL) -- internal notes are not emails ); CREATE INDEX messages_ticket_idx ON messages (ticket_id, created_at); -- A saved reply an agent can insert into the reply box. It never sends on its own (R9). CREATE TABLE canned_replies ( id TEXT PRIMARY KEY, title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 80), body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 5000), -- placeholders: {requester_first_name}, {ticket_number}, {agent_first_name} created_by TEXT NOT NULL REFERENCES agents(id), updated_at TIMESTAMP NOT NULL ); CREATE UNIQUE INDEX canned_replies_title_uniq ON canned_replies (LOWER(title)); -- unique ignoring case -- One append-only history entry. Never edited or deleted, except by R14. -- App rule (R15): each action writes the events listed in section 6. CREATE TABLE events ( id TEXT PRIMARY KEY, ticket_id TEXT REFERENCES tickets(id) ON DELETE CASCADE, -- empty for requester_deleted actor_agent_id TEXT REFERENCES agents(id), -- empty for the requester and the system type TEXT NOT NULL CHECK (type IN ('created', 'requester_replied', 'replied', 'note_added', 'assigned', 'status_changed', 'priority_changed', 'reopened', 'follow_up_created', 'closed', 'requester_deleted')), data TEXT, -- JSON text with from/to values; requester_deleted stores only the ticket count created_at TIMESTAMP NOT NULL ); CREATE INDEX events_ticket_idx ON events (ticket_id, created_at); -- One outgoing email in the outbox. Written first, then sent. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('acknowledgement', 'public_reply', 'assignment')), to_email TEXT NOT NULL, ticket_id TEXT REFERENCES tickets(id) ON DELETE CASCADE, -- R14 removes the outbox rows of deleted tickets subject TEXT NOT NULL, body TEXT NOT NULL, message_id_header TEXT NOT NULL, -- a new Message-ID for every email in_reply_to_header TEXT, -- newest Message-ID among the requester's emails, the acknowledgement and earlier replies (R8) status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), -- logged = no email provider configured error TEXT, attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 6), -- 6 attempts in all (section 10) created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); CREATE INDEX notifications_status_idx ON notifications (status); -- One public support form submission, kept for the rate limit (5 per IP hash per hour). CREATE TABLE form_submissions ( id TEXT PRIMARY KEY, ip_hash TEXT NOT NULL, -- SHA-256 of the IP address with a server secret; the IP itself is never stored created_at TIMESTAMP NOT NULL ); CREATE INDEX form_submissions_ip_idx ON form_submissions (ip_hash, created_at);