-- Status Page database schema -- Source: https://personalsoftwarefactory.com/blueprints/status-page (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), page_name TEXT NOT NULL, company_name TEXT NOT NULL, -- every public update is signed with it logo TEXT, -- image URL or file path time_zone TEXT NOT NULL DEFAULT 'America/Chicago', -- IANA name; used for days in uptime bars, emails and admin screens support_url TEXT, from_email TEXT, email_domain TEXT, -- the only domain team members sign in with page_url TEXT -- the public address used in every email and feed link ); -- A person on the team who can sign in to /admin. CREATE TABLE team_members ( id TEXT PRIMARY KEY, name TEXT NOT NULL, -- never shown to visitors (R11) email TEXT NOT NULL UNIQUE, role TEXT NOT NULL CHECK (role IN ('admin', 'responder')), active BOOLEAN NOT NULL DEFAULT TRUE -- only active members can sign in ); -- A heading on the public page that components are listed under. Deleted only when empty. CREATE TABLE component_groups ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, position INTEGER NOT NULL -- display order ); -- A saved starting point for an incident: title, first message and component statuses. CREATE TABLE templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL, title TEXT NOT NULL, -- the incident title it fills in message TEXT NOT NULL -- the message of the first update ); -- A part of the product shown on the page with its current status. -- App rule (R6): status is always the worst of the latest status each open incident set for it and -- under_maintenance for each in-progress maintenance affecting it, or operational. -- App rule (section 13): a component used by any incident, maintenance or template can only be hidden, not deleted. CREATE TABLE components ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, description TEXT, -- shown on the public page group_id TEXT REFERENCES component_groups(id), -- a group with components cannot be deleted position INTEGER NOT NULL, -- display order within its group status TEXT NOT NULL DEFAULT 'operational' CHECK (status IN ('operational', 'degraded_performance', 'partial_outage', 'major_outage', 'under_maintenance')), created_at TIMESTAMP NOT NULL, -- uptime counts from here when it is inside the 90-day window hidden BOOLEAN NOT NULL DEFAULT FALSE, -- hidden components keep their history and do not count toward the banner automation_token TEXT UNIQUE, -- secret for the monitor webhook (R10); generated by the app auto_publish BOOLEAN NOT NULL DEFAULT FALSE, -- publish automation incidents at once instead of leaving a draft template_id TEXT REFERENCES templates(id) -- used by automation; NULL gives the title "Payments: major outage" ); -- A component a template sets, and the status it sets it to. Only maintenance sets under_maintenance. CREATE TABLE template_components ( template_id TEXT NOT NULL REFERENCES templates(id) ON DELETE CASCADE, component_id TEXT NOT NULL REFERENCES components(id), status TEXT NOT NULL CHECK (status IN ('operational', 'degraded_performance', 'partial_outage', 'major_outage')), PRIMARY KEY (template_id, component_id) ); -- An incident: a draft until published, then open until resolved. Never reopened. -- App rule (R4): status moves forward only (investigating, identified, monitoring, resolved); steps can be skipped. -- App rule: only drafts can be deleted, and the number of a deleted draft is not reused. CREATE TABLE incidents ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number >= 1), -- shown as INC-0013; given when the draft is created title TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'investigating', 'identified', 'monitoring', 'resolved')), impact TEXT NOT NULL DEFAULT 'minor' CHECK (impact IN ('none', 'minor', 'major', 'critical')), started_at TIMESTAMP, -- set when it is published resolved_at TIMESTAMP, postmortem TEXT CHECK (length(postmortem) <= 20000), -- Markdown; resolved incidents only postmortem_at TIMESTAMP, -- when the postmortem was last added or edited created_by TEXT REFERENCES team_members(id), -- NULL when automation opened it related_to TEXT REFERENCES incidents(id), -- an earlier incident this one follows on from CHECK ((status = 'draft' AND started_at IS NULL) OR (status <> 'draft' AND started_at IS NOT NULL)), CHECK ((status = 'resolved' AND resolved_at IS NOT NULL) OR (status <> 'resolved' AND resolved_at IS NULL)), CHECK (postmortem IS NULL OR status = 'resolved') ); -- One update on an incident. The text of a draft is its first update with published_at NULL. -- App rule (R5): an admin can correct the message after publishing; the original stays in the activity log. CREATE TABLE incident_updates ( id TEXT PRIMARY KEY, incident_id TEXT NOT NULL REFERENCES incidents(id) ON DELETE CASCADE, -- cascade only matters for deleted drafts status TEXT NOT NULL CHECK (status IN ('investigating', 'identified', 'monitoring', 'resolved')), message TEXT NOT NULL CHECK (length(message) BETWEEN 10 AND 2000), notify BOOLEAN NOT NULL, -- email subscribers of the affected components of the incident (R9) published_at TIMESTAMP, -- NULL only for the text of a draft author_id TEXT REFERENCES team_members(id) -- NULL for automation; never shown publicly ); -- A component status set by one incident update (the component_statuses list of the update). CREATE TABLE incident_update_components ( update_id TEXT NOT NULL REFERENCES incident_updates(id) ON DELETE CASCADE, component_id TEXT NOT NULL REFERENCES components(id), status TEXT NOT NULL CHECK (status IN ('operational', 'degraded_performance', 'partial_outage', 'major_outage')), PRIMARY KEY (update_id, component_id) ); -- A scheduled maintenance window (Maintenance in the spec), numbered M-0001. -- App rule (R7): the every-minute job starts and completes it on time and sends a reminder 1 hour before. CREATE TABLE maintenance_windows ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number >= 1), -- shown as M-0003 title TEXT NOT NULL, message TEXT, -- optional starts_at TIMESTAMP NOT NULL, ends_at TIMESTAMP NOT NULL, status TEXT NOT NULL DEFAULT 'scheduled' CHECK (status IN ('scheduled', 'in_progress', 'completed', 'cancelled')), notify BOOLEAN NOT NULL, CHECK (ends_at > starts_at) ); -- A component a maintenance window affects. App rule: at least one per window. CREATE TABLE maintenance_components ( maintenance_id TEXT NOT NULL REFERENCES maintenance_windows(id) ON DELETE CASCADE, component_id TEXT NOT NULL REFERENCES components(id), PRIMARY KEY (maintenance_id, component_id) ); -- One change of the status of a component. Append-only; uptime and the 90-day bars come only from this log (R2). CREATE TABLE status_changes ( id TEXT PRIMARY KEY, component_id TEXT NOT NULL REFERENCES components(id), status TEXT NOT NULL CHECK (status IN ('operational', 'degraded_performance', 'partial_outage', 'major_outage', 'under_maintenance')), at TIMESTAMP NOT NULL, incident_id TEXT REFERENCES incidents(id), maintenance_id TEXT REFERENCES maintenance_windows(id), actor TEXT NOT NULL, -- a team member id, 'automation' or 'schedule' CHECK (incident_id IS NULL OR maintenance_id IS NULL) ); CREATE INDEX status_changes_component_at ON status_changes (component_id, at); -- An email address subscribed to updates. Removing a subscriber deletes the row. -- App rule (R8): double opt-in; the confirmation link expires 7 days after confirm_sent_at. CREATE TABLE subscribers ( id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase all_components BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE: every component, and subscriber_components is empty confirm_token TEXT UNIQUE, -- generated by the app confirm_sent_at TIMESTAMP, confirmed_at TIMESTAMP, -- only confirmed subscribers are emailed manage_token TEXT UNIQUE, -- for the manage and one-click unsubscribe links; generated by the app unsubscribed_at TIMESTAMP, created_ip TEXT ); -- A component a subscriber follows when all_components is FALSE (component_ids in the spec). CREATE TABLE subscriber_components ( subscriber_id TEXT NOT NULL REFERENCES subscribers(id) ON DELETE CASCADE, component_id TEXT NOT NULL REFERENCES components(id), PRIMARY KEY (subscriber_id, component_id) ); -- One entry in the activity log. Append-only (R12). CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT NOT NULL, -- a team member id, 'schedule', 'automation', 'visitor' or 'outbox' type TEXT NOT NULL, data TEXT NOT NULL DEFAULT '{}' -- a JSON object stored as text ); -- One email in the outbox, written before it is sent. Subscriber emails keep only the address, so removing a subscriber leaves them. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, headers TEXT, -- extra headers such as List-Unsubscribe, as text created_at TIMESTAMP NOT NULL, incident_id TEXT REFERENCES incidents(id), maintenance_id TEXT REFERENCES maintenance_windows(id), attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 3), -- at most 3 send attempts per email status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), error TEXT, CHECK (incident_id IS NULL OR maintenance_id IS NULL) ); CREATE INDEX notifications_status ON notifications (status);