-- Testimonial Wall database schema -- Source: https://personalsoftwarefactory.com/blueprints/testimonial-wall (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. -- Text lengths are counted after trimming. Tokens are at least 24 URL-safe characters from a secure random generator. -- A staff member. Customers never have an account. -- App rule (section 10): nobody can deactivate themselves or change their own role. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(trim(name)) >= 1), email TEXT NOT NULL UNIQUE, -- lowercase active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP NOT NULL, role TEXT NOT NULL CHECK (role IN ('member', 'moderator', 'admin')), CHECK (email = lower(email)) ); -- Company settings. Exactly one row. -- App rule (R15): changing permission_text adds 1 to permission_version and writes a settings_changed event. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- keeps this table to one row company_name TEXT NOT NULL CHECK (length(trim(company_name)) BETWEEN 1 AND 80), wall_title TEXT NOT NULL CHECK (length(trim(wall_title)) BETWEEN 1 AND 80), form_intro TEXT NOT NULL CHECK (length(trim(form_intro)) BETWEEN 1 AND 500), permission_text TEXT NOT NULL CHECK (length(trim(permission_text)) BETWEEN 20 AND 500), -- the sentence next to the permission checkbox permission_version INTEGER NOT NULL DEFAULT 1 CHECK (permission_version >= 1), form_open BOOLEAN NOT NULL DEFAULT TRUE, time_zone TEXT NOT NULL DEFAULT 'America/New_York' -- IANA name, for dates in emails and on public pages ); -- A personal link that asks one customer for a testimonial, pre-filled with what they already said. -- App rule (R17): the link works only while open and before expires_at, even when the form is closed. CREATE TABLE invitations ( id TEXT PRIMARY KEY, token TEXT NOT NULL UNIQUE CHECK (length(token) >= 24), -- the /share/ link created_by_id TEXT NOT NULL REFERENCES people(id), name TEXT NOT NULL CHECK (length(trim(name)) BETWEEN 2 AND 80), email TEXT NOT NULL, company TEXT, -- optional, pre-fills the form job_title TEXT, -- optional, pre-fills the form prefill_quote TEXT CHECK (length(trim(prefill_quote)) <= 1000), -- the words the customer already used source TEXT NOT NULL CHECK (source IN ('email', 'call', 'chat', 'other')), source_note TEXT CHECK (length(trim(source_note)) <= 300), -- private status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'completed', 'cancelled', 'expired')), created_at TIMESTAMP NOT NULL, -- the invitation email is queued then expires_at TIMESTAMP NOT NULL, -- created_at plus 14 days reminded_at TIMESTAMP, -- set once, by the scheduled job after 72 hours completed_at TIMESTAMP, testimonial_id TEXT UNIQUE, -- the testimonial it produced; no foreign key because testimonials.invitation_id points back here CHECK (status <> 'completed' OR (completed_at IS NOT NULL AND testimonial_id IS NOT NULL)) ); -- A customer's testimonial. Customer wording and consent fields are never edited by staff (R3). -- App rule (R12): status checks and updates happen in one transaction. CREATE TABLE testimonials ( id TEXT PRIMARY KEY, submitted_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, name TEXT NOT NULL CHECK (length(trim(name)) BETWEEN 2 AND 80), email TEXT NOT NULL CHECK (length(trim(email)) <= 254), -- never public company TEXT CHECK (length(trim(company)) <= 100), job_title TEXT CHECK (length(trim(job_title)) <= 100), -- labelled "Role" on the form quote TEXT NOT NULL CHECK (length(trim(quote)) BETWEEN 20 AND 1000), -- plain text, line breaks kept, never rendered as HTML rating INTEGER CHECK (rating BETWEEN 1 AND 5), -- NULL means no rating, never zero stars consent BOOLEAN NOT NULL, -- TRUE only when the customer ticked the box (R1, R2) consent_text TEXT, -- the exact sentence agreed to; kept after a withdrawal as evidence consent_version INTEGER, consent_at TIMESTAMP, withdrawn_at TIMESTAMP, status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'published', 'withdrawn')), featured BOOLEAN NOT NULL DEFAULT FALSE, source TEXT NOT NULL CHECK (source IN ('form', 'invitation')), invitation_id TEXT UNIQUE REFERENCES invitations(id), -- an invitation produces at most one testimonial (R17) manage_token TEXT NOT NULL UNIQUE CHECK (length(manage_token) >= 24), -- the customer's private /t/ link review_note TEXT CHECK (length(trim(review_note)) <= 300), -- private; required (5 or more) on reject reviewed_by_id TEXT REFERENCES people(id), reviewed_at TIMESTAMP, published_at TIMESTAMP, -- the most recent publish CHECK ((source = 'invitation') = (invitation_id IS NOT NULL)), -- consent evidence is complete when given, and empty when never given CHECK (consent = FALSE OR (consent_text IS NOT NULL AND consent_version IS NOT NULL AND consent_at IS NOT NULL)), CHECK (consent = TRUE OR status = 'withdrawn' OR (consent_text IS NULL AND consent_version IS NULL AND consent_at IS NULL)), -- R4, R5: approved and published need consent. R8: withdrawn means consent FALSE, withdrawn_at set, not featured CHECK (status NOT IN ('approved', 'published') OR consent = TRUE), CHECK ((status = 'withdrawn') = (withdrawn_at IS NOT NULL)), CHECK (status <> 'withdrawn' OR (consent = FALSE AND consent_at IS NOT NULL)), CHECK (status NOT IN ('approved', 'published', 'rejected') OR (reviewed_by_id IS NOT NULL AND reviewed_at IS NOT NULL)), CHECK (status <> 'rejected' OR (review_note IS NOT NULL AND length(trim(review_note)) >= 5)), CHECK (status <> 'published' OR published_at IS NOT NULL), CHECK (featured = FALSE OR status = 'published') -- R7: only a published testimonial can be featured ); -- R7: at most one testimonial is featured. CREATE UNIQUE INDEX testimonials_one_featured ON testimonials (featured) WHERE featured = TRUE; CREATE INDEX testimonials_status_submitted ON testimonials (status, submitted_at); -- Append-only history. Never copies the customer's quote, email or name. -- No foreign keys to testimonials or invitations: R14's deleted event keeps the old id after both rows are gone. CREATE TABLE events ( id TEXT PRIMARY KEY, testimonial_id TEXT, invitation_id TEXT, created_at TIMESTAMP NOT NULL, actor_type TEXT NOT NULL CHECK (actor_type IN ('person', 'customer', 'system')), actor_id TEXT REFERENCES people(id), -- set when actor_type is person type TEXT NOT NULL CHECK (type IN ('submitted', 'approved', 'rejected', 'published', 'unpublished', 'featured', 'unfeatured', 'withdrawn', 'deleted', 'invitation_created', 'invitation_completed', 'invitation_reminded', 'invitation_cancelled', 'invitation_expired', 'settings_changed')), note TEXT, -- a review or withdrawal note data TEXT, -- JSON text, such as old and new permission_version CHECK ((actor_type = 'person') = (actor_id IS NOT NULL)) ); CREATE INDEX events_testimonial ON events (testimonial_id, created_at); -- An email in the outbox. Written first, then sent; failed or stuck rows are retried by the hourly job. -- R14 removes a deleted testimonial's outbox rows, so both references cascade. CREATE TABLE notifications ( id TEXT PRIMARY KEY, to_email TEXT NOT NULL, person_id TEXT REFERENCES people(id), -- set for staff recipients testimonial_id TEXT REFERENCES testimonials(id) ON DELETE CASCADE, -- NULL for invitation and invitation_reminder invitation_id TEXT REFERENCES invitations(id) ON DELETE CASCADE, reply_to TEXT, -- the invitation creator's email, for invitation emails kind TEXT NOT NULL CHECK (kind IN ('submission_received', 'new_submission', 'invitation', 'invitation_reminder', 'published', 'withdrawal_confirmed', 'withdrawn_alert')), subject TEXT NOT NULL, body TEXT NOT NULL, -- plain text status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), -- logged when no provider is configured created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP, error TEXT ); -- One saved form submission, for rate limiting (R16: at most 5 per IP hash per 60 minutes). Pruned after 24 hours. CREATE TABLE form_attempts ( id TEXT PRIMARY KEY, ip_hash TEXT NOT NULL, -- SHA-256 of the IP plus a server secret; never the raw IP created_at TIMESTAMP NOT NULL ); CREATE INDEX form_attempts_ip_hash ON form_attempts (ip_hash, created_at);