-- NPS Surveys database schema -- Source: https://personalsoftwarefactory.com/blueprints/nps-surveys (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. id is fixed at 1 so the table can never hold a second row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), company_name TEXT NOT NULL, from_name TEXT, -- sender name on survey emails from_email TEXT, -- sender address on survey emails question TEXT NOT NULL, -- the 0 to 10 question follow_up_question TEXT NOT NULL, -- asked on the page that opens after a score is clicked send_time TEXT NOT NULL DEFAULT '10:00' CHECK (length(send_time) = 5 AND substr(send_time, 3, 1) = ':'), -- HH:MM in the company time zone, weekdays only daily_send_limit INTEGER NOT NULL DEFAULT 100 CHECK (daily_send_limit >= 1), throttle_days INTEGER NOT NULL DEFAULT 90 CHECK (throttle_days >= 1), -- no contact is surveyed twice within this many days company_domain TEXT, -- addresses on this domain are internal and never surveyed sending_enabled BOOLEAN NOT NULL DEFAULT FALSE, -- off: survey emails are held in the outbox time_zone TEXT NOT NULL, -- IANA name; send days, months and due dates use this zone product_name TEXT NOT NULL -- named in the survey subject, e.g. Quick question from Tally ); -- A staff member who signs in: an admin or a member. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, -- the import matches account_owner_email against it role TEXT NOT NULL CHECK (role IN ('admin', 'member')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- A customer who can be surveyed. Imported by CSV, upserted by email, never deleted. -- App rule (R3): a contact is due only if not unsubscribed, not on company_domain, and never surveyed or last surveyed more than throttle_days ago. -- App rule (section 13): three failed sends in a row unsubscribe the contact with the reason bounced. CREATE TABLE contacts ( id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase name TEXT NOT NULL, company TEXT, plan TEXT, -- free text, e.g. Pro or Starter account_owner_id TEXT REFERENCES people(id), -- NULL when the contact has no owner created_at TIMESTAMP NOT NULL, unsubscribed_at TIMESTAMP, -- set once; the contact is never surveyed again unsubscribe_reason TEXT, -- e.g. bounced last_surveyed_at TIMESTAMP, -- NULL if never surveyed; held surveys count CHECK (unsubscribe_reason IS NULL OR unsubscribed_at IS NOT NULL) ); CREATE INDEX contacts_due ON contacts (last_surveyed_at) WHERE unsubscribed_at IS NULL; -- One survey email to one contact, with the token its score links carry. -- App rule (section 7): the daily send runs once per company date, on weekdays at or after send_time, to at most daily_send_limit due contacts. CREATE TABLE survey_sends ( id TEXT PRIMARY KEY, contact_id TEXT NOT NULL REFERENCES contacts(id), token TEXT NOT NULL UNIQUE, -- random; score links are /s// sent_on DATE NOT NULL, -- the company date of the send status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'held')) -- sent also when the email was only logged ); CREATE INDEX survey_sends_contact ON survey_sends (contact_id, sent_on); CREATE INDEX survey_sends_sent_on ON survey_sends (sent_on, status); -- One answer to one survey email: the score, its category and the comment. -- App rule (R1, section 7): the score and comment can change only within 24 hours of first_clicked_at. -- App rule (R5): a detractor response creates a follow-up for the account owner. CREATE TABLE responses ( id TEXT PRIMARY KEY, send_id TEXT NOT NULL UNIQUE REFERENCES survey_sends(id), -- at most one response per send contact_id TEXT NOT NULL REFERENCES contacts(id), plan TEXT, -- copied from the contact at the first click, so segment reports never move account_owner_id TEXT REFERENCES people(id), -- copied from the contact at the first click score INTEGER NOT NULL CHECK (score BETWEEN 0 AND 10), category TEXT NOT NULL CHECK (category IN ('promoter', 'passive', 'detractor')), comment TEXT, first_clicked_at TIMESTAMP NOT NULL, -- decides the month and segment of the response last_changed_at TIMESTAMP NOT NULL, CHECK ((score >= 9 AND category = 'promoter') OR (score IN (7, 8) AND category = 'passive') OR (score <= 6 AND category = 'detractor')), CHECK (last_changed_at >= first_clicked_at) ); CREATE INDEX responses_first_click ON responses (first_clicked_at); -- A tag for comment themes. Renaming a tag keeps it on its responses. -- App rule (section 4): only admins create and rename tags. CREATE TABLE tags ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE ); -- A tag applied to a response. Stores the response tags list as Tag ids. CREATE TABLE response_tags ( response_id TEXT NOT NULL REFERENCES responses(id) ON DELETE CASCADE, tag_id TEXT NOT NULL REFERENCES tags(id), PRIMARY KEY (response_id, tag_id) ); CREATE INDEX response_tags_tag ON response_tags (tag_id); -- The follow-up on a detractor response, owned by one person. -- App rule (R5): due 2 business days after the first click date; a score of 7 or more cancels an open or contacted follow-up, and a score back under 7 reopens it with the same due date. -- App rule (R6): open, contacted, resolved and no_reply can move to contacted, resolved or no_reply, each with a note; only the system sets cancelled, and a cancelled follow-up is never updated. CREATE TABLE follow_ups ( id TEXT PRIMARY KEY, response_id TEXT NOT NULL UNIQUE REFERENCES responses(id), -- one follow-up per response, reopened rather than recreated owner_id TEXT NOT NULL REFERENCES people(id), status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'contacted', 'resolved', 'no_reply', 'cancelled')), due_on DATE NOT NULL -- overdue when open or contacted and before today ); CREATE INDEX follow_ups_owner ON follow_ups (owner_id, status); -- One note on a follow-up. Stores the follow-up notes list; notes are never edited or deleted. CREATE TABLE follow_up_notes ( id TEXT PRIMARY KEY, follow_up_id TEXT NOT NULL REFERENCES follow_ups(id), at TIMESTAMP NOT NULL, by TEXT NOT NULL REFERENCES people(id), -- the person who wrote the note text TEXT NOT NULL ); CREATE INDEX follow_up_notes_follow_up ON follow_up_notes (follow_up_id, at); -- One change, written by every action, including each day of sending. Append-only. -- App rule (R10): events are never edited or deleted. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT NOT NULL, -- a person id, a customer token, or the job type TEXT NOT NULL, -- e.g. daily_send, score_changed, follow_up_resolved data TEXT -- details of the change as JSON text ); -- One email in the outbox. Every email is written here first, then sent. -- App rule (section 10): the sending switch holds customer surveys only; staff alerts and the digest always go. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, -- which trigger in section 10, e.g. survey, detractor_alert, weekly_digest to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, headers TEXT, -- extra email headers as JSON text, e.g. List-Unsubscribe status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged', 'held')), -- held when sending_enabled is off error TEXT );