-- Customer Feedback Inbox database schema -- Source: https://personalsoftwarefactory.com/blueprints/feedback-inbox (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 team member who can sign in. CREATE TABLE people ( 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. CREATE TABLE settings ( id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row company_name TEXT NOT NULL CHECK (LENGTH(company_name) <= 60), time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name follow_up_template TEXT NOT NULL CHECK (LENGTH(follow_up_template) <= 2000) -- may use {contact_first_name}, {theme_name}, {company_name} ); -- A customer account. -- App rule (section 12): archived customers can't vote, receive new captures or join a new follow-up list. CREATE TABLE customers ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 80), -- unique ignoring case (index below) mrr_cents INTEGER NOT NULL DEFAULT 0 CHECK (mrr_cents >= 0), archived BOOLEAN NOT NULL DEFAULT FALSE ); CREATE UNIQUE INDEX customers_name_unique ON customers (LOWER(name)); -- One email domain of a customer (Customer.domains in the spec). A customer may have none. CREATE TABLE customer_domains ( domain TEXT PRIMARY KEY CHECK (domain = LOWER(domain) AND domain NOT LIKE '%@%'), -- e.g. acme.example; belongs to at most one customer customer_id TEXT NOT NULL REFERENCES customers(id) ); CREATE INDEX customer_domains_customer ON customer_domains (customer_id); -- A theme: a request or problem that feedback is grouped under. Never deleted. -- App rule (R7): promotion needs at least one linked feedback and an active owner. CREATE TABLE themes ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(TRIM(name)) BETWEEN 3 AND 80), -- unique ignoring case and surrounding spaces (index below) description TEXT CHECK (description IS NULL OR LENGTH(description) <= 1000), created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, promoted_at TIMESTAMP, -- null until promoted to the roadmap; never un-promoted owner_id TEXT REFERENCES people(id), -- required once promoted status TEXT CHECK (status IN ('under_review', 'planned', 'in_progress', 'shipped', 'not_planned')), -- null until promoted CHECK (promoted_at IS NULL OR owner_id IS NOT NULL), CHECK ((promoted_at IS NULL AND status IS NULL) OR (promoted_at IS NOT NULL AND status IS NOT NULL)) ); CREATE UNIQUE INDEX themes_name_unique ON themes (LOWER(TRIM(name))); -- One piece of feedback: a customer's own words. Never edited after capture (R1). -- App rule (R4): received_at is never in the future; the customer is not archived at capture. CREATE TABLE feedback ( id TEXT PRIMARY KEY, customer_id TEXT REFERENCES customers(id), -- null: Needs customer source TEXT NOT NULL CHECK (source IN ('email', 'call', 'ticket', 'chat', 'in_app', 'other')), from_name TEXT CHECK (from_name IS NULL OR LENGTH(from_name) <= 100), from_email TEXT CHECK (from_email IS NULL OR (from_email = LOWER(from_email) AND from_email LIKE '%_@_%')), -- stored lowercase quote TEXT NOT NULL CHECK (LENGTH(TRIM(quote)) BETWEEN 10 AND 3000), -- never rewritten received_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, captured_by TEXT REFERENCES people(id), -- null for inbound external_id TEXT UNIQUE CHECK (external_id IS NULL OR LENGTH(external_id) <= 200), -- set by the inbound endpoint state TEXT NOT NULL DEFAULT 'inbox' CHECK (state IN ('inbox', 'linked', 'dismissed')), theme_id TEXT REFERENCES themes(id), -- one theme at most (R2) dismiss_reason TEXT CHECK (dismiss_reason IS NULL OR LENGTH(dismiss_reason) BETWEEN 5 AND 300), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CHECK ((state = 'linked' AND theme_id IS NOT NULL) OR (state <> 'linked' AND theme_id IS NULL)), -- theme only when linked CHECK ((state = 'dismissed' AND dismiss_reason IS NOT NULL) OR (state <> 'dismissed' AND dismiss_reason IS NULL)), -- reason only when dismissed CHECK (state <> 'linked' OR customer_id IS NOT NULL) -- R3: evidence needs an account ); CREATE INDEX feedback_theme ON feedback (theme_id); CREATE INDEX feedback_customer ON feedback (customer_id); -- One customer account's vote for a theme. A vote never adds evidence or MRR. CREATE TABLE votes ( theme_id TEXT NOT NULL REFERENCES themes(id), customer_id TEXT NOT NULL REFERENCES customers(id), recorded_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (theme_id, customer_id) -- one vote per account per theme (R6) ); -- One status decision on a promoted theme. Append-only. -- App rule (R8): made by the theme owner or an admin. CREATE TABLE status_changes ( id TEXT PRIMARY KEY, theme_id TEXT NOT NULL REFERENCES themes(id), from_status TEXT CHECK (from_status IN ('under_review', 'planned', 'in_progress', 'shipped', 'not_planned')), -- null for the promotion to_status TEXT NOT NULL CHECK (to_status IN ('under_review', 'planned', 'in_progress', 'shipped', 'not_planned')), note TEXT NOT NULL CHECK (LENGTH(note) BETWEEN 5 AND 500), actor_id TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CHECK (from_status IS NULL OR from_status <> to_status) -- R8: a different status ); CREATE INDEX status_changes_theme ON status_changes (theme_id, created_at); -- One account to tell about one shipment of a theme. -- App rule (R9): created when the theme enters shipped; pending ones become cancelled when it leaves shipped. -- App rule (R10): only the theme owner or an admin marks one emailed or contacted, once. CREATE TABLE follow_ups ( id TEXT PRIMARY KEY, theme_id TEXT NOT NULL REFERENCES themes(id), shipped_change_id TEXT NOT NULL REFERENCES status_changes(id), -- the change that moved the theme to shipped customer_id TEXT NOT NULL REFERENCES customers(id), status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'emailed', 'contacted', 'cancelled')), done_by TEXT REFERENCES people(id), done_at TIMESTAMP, note TEXT CHECK (note IS NULL OR LENGTH(note) <= 300), -- how they were contacted UNIQUE (shipped_change_id, customer_id), CHECK (status NOT IN ('emailed', 'contacted') OR (done_by IS NOT NULL AND done_at IS NOT NULL)), CHECK (status <> 'contacted' OR COALESCE(LENGTH(note), 0) >= 3) -- R10: Mark contacted needs a note ); -- One entry in the activity log. Append-only, never updated or deleted (R15). -- feedback_id has no foreign key on purpose: R14 deletes feedback, and its feedback_deleted event keeps the id. CREATE TABLE events ( id TEXT PRIMARY KEY, actor_id TEXT REFERENCES people(id), -- null for inbound and the system feedback_id TEXT, theme_id TEXT REFERENCES themes(id), type TEXT NOT NULL CHECK (type IN ('captured', 'inbound_received', 'customer_assigned', 'linked', 'unlinked', 'dismissed', 'restored', 'theme_created', 'vote_added', 'vote_removed', 'promoted', 'status_changed', 'owner_changed', 'follow_up_emailed', 'follow_up_contacted', 'feedback_deleted', 'customer_changed')), data TEXT, -- JSON text; cancelled follow-ups are listed in the status_changed event that left shipped created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX events_theme ON events (theme_id, created_at); -- One email in the outbox, written before it is sent. -- App rule (section 10): failed sends are retried up to 5 times after the first attempt; pending for 15 minutes counts as failed. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, to_email TEXT NOT NULL, person_id TEXT REFERENCES people(id), -- null for customer contacts theme_id TEXT REFERENCES themes(id), subject TEXT NOT NULL, body TEXT NOT NULL, -- plain text status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), -- logged: no email provider configured error TEXT, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, sent_at TIMESTAMP );