-- Internal Knowledge Base database schema -- Source: https://personalsoftwarefactory.com/blueprints/knowledge-base (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 CHECK (length(company_name) BETWEEN 1 AND 60), time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; "today" and every date shown use this zone last_daily_run_at TIMESTAMP -- written only by the scheduled job ); -- A team. Each person belongs to exactly one. CREATE TABLE teams ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 40) ); CREATE UNIQUE INDEX teams_name_unique ON teams (lower(name)); -- unique ignoring case -- A person who can sign in. People are deactivated, never deleted. -- App rule (R15): at least one active admin must always exist. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase team_id TEXT NOT NULL REFERENCES teams(id), is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE, slack_user_id TEXT UNIQUE, -- set by Link Slack accounts or by hand slack_lookup_error TEXT -- the Slack API error from the last failed lookup ); -- A collection of cards, visible to everyone or to chosen teams. -- App rule (R3): every editor must be active and able to see the collection. CREATE TABLE collections ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 60), description TEXT CHECK (length(description) <= 300), visibility TEXT NOT NULL CHECK (visibility IN ('everyone', 'teams')) ); CREATE UNIQUE INDEX collections_name_unique ON collections (lower(name)); -- unique ignoring case -- A team that can see a collection whose visibility is teams. -- App rule: a collection with visibility teams needs at least one row here. CREATE TABLE collection_teams ( collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE, team_id TEXT NOT NULL REFERENCES teams(id), -- a team used by a collection cannot be deleted (R15) PRIMARY KEY (collection_id, team_id) ); -- A person who can edit the cards in a collection. Admins edit every collection without a row here. CREATE TABLE collection_editors ( collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE, person_id TEXT NOT NULL REFERENCES people(id), PRIMARY KEY (collection_id, person_id) ); -- A card: one question and its answer. Trust (Verified or Unverified, with a reason) is computed, never stored (R12). -- App rule (R5): number is assigned in order from 101 and never reused, even after a draft is deleted. -- App rule (R4, R10): the owner must be an active editor of the collection or an admin when set. -- App rule (section 7): status changes follow the lifecycle table; only drafts can be deleted. CREATE TABLE cards ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number >= 101), -- shown as KB-101 collection_id TEXT NOT NULL REFERENCES collections(id), -- a collection with cards cannot be deleted status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'published', 'archived')), title TEXT NOT NULL CHECK (length(title) BETWEEN 5 AND 120), -- written as the question people ask body TEXT NOT NULL DEFAULT '' CHECK (length(body) <= 5000), -- Markdown owner_id TEXT REFERENCES people(id), -- required to publish review_interval_days INTEGER DEFAULT 90 CHECK (review_interval_days IN (30, 90, 180, 365)), -- NULL means does not expire verified_at TIMESTAMP, verified_by TEXT REFERENCES people(id), verified_until DATE, -- date of verified_at in the team zone plus review_interval_days; NULL when it does not expire needs_verification TEXT CHECK (needs_verification IN ('not_verified', 'edited', 'restored')), -- NULL means empty needs_verification_by TEXT REFERENCES people(id), needs_verification_at TIMESTAMP, version INTEGER NOT NULL DEFAULT 0, -- 0 until first published, then the current version number created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, updated_by TEXT NOT NULL REFERENCES people(id), -- the author of the current version (the last save for drafts) updated_at TIMESTAMP NOT NULL, archived_by TEXT REFERENCES people(id), archived_at TIMESTAMP, reminder_sent_for DATE, -- the verified_until value the review_due notice was sent for overdue_sent_for DATE, -- the verified_until value the review_overdue notice was sent for CHECK (status = 'draft' OR owner_id IS NOT NULL), CHECK (status = 'draft' OR length(body) >= 1), CHECK ((status = 'draft' AND version = 0) OR (status <> 'draft' AND version >= 1)), CHECK (verified_at IS NOT NULL OR (verified_by IS NULL AND verified_until IS NULL)) ); -- An alternate phrasing of the question on a card (0 to 5 per card, in display order). CREATE TABLE card_questions ( card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- the 1 to 5 range caps a card at 5 questions question TEXT NOT NULL CHECK (length(question) BETWEEN 3 AND 120), PRIMARY KEY (card_id, position) ); -- A tag on a card (0 to 8 per card, in display order). -- App rule (R4): tags use only lowercase letters, digits and hyphens. CREATE TABLE card_tags ( card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 8), -- the 1 to 8 range caps a card at 8 tags tag TEXT NOT NULL CHECK (length(tag) BETWEEN 2 AND 30 AND tag = lower(tag)), PRIMARY KEY (card_id, position), UNIQUE (card_id, tag) ); -- A published version of a card, written on every publish and never changed or deleted (R6). CREATE TABLE card_versions ( id TEXT PRIMARY KEY, card_id TEXT NOT NULL REFERENCES cards(id), number INTEGER NOT NULL CHECK (number >= 1), title TEXT NOT NULL, body TEXT NOT NULL, questions TEXT NOT NULL DEFAULT '', -- the alternate questions as published, one per line, in order tags TEXT NOT NULL DEFAULT '', -- the tags as published, one per line, in order change_note TEXT NOT NULL DEFAULT '', -- empty for version 1, 3 to 200 characters after that author_id TEXT NOT NULL REFERENCES people(id), published_at TIMESTAMP NOT NULL, UNIQUE (card_id, number), CHECK ((number = 1 AND change_note = '') OR (number > 1 AND length(change_note) BETWEEN 3 AND 200)) ); -- A flag saying a card is wrong or out of date. An open flag makes the card Unverified. -- App rule (R9): the owner of the card cannot flag it; only the person who flagged can withdraw it. CREATE TABLE flags ( id TEXT PRIMARY KEY, card_id TEXT NOT NULL REFERENCES cards(id), person_id TEXT NOT NULL REFERENCES people(id), -- who flagged reason TEXT NOT NULL CHECK (length(reason) BETWEEN 5 AND 500), created_at TIMESTAMP NOT NULL, status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'resolved')), resolved_at TIMESTAMP, resolved_by TEXT REFERENCES people(id), resolution TEXT CHECK (resolution IN ('verified', 'archived', 'withdrawn')), CHECK ((status = 'open' AND resolved_at IS NULL AND resolution IS NULL) OR (status = 'resolved' AND resolved_at IS NOT NULL AND resolution IS NOT NULL)) ); CREATE UNIQUE INDEX flags_one_open_per_person ON flags (card_id, person_id) WHERE status = 'open'; -- at most one open flag per person per card -- A person following a card, told when a new version is published with "Notify followers". CREATE TABLE follows ( card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE, person_id TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, PRIMARY KEY (card_id, person_id) ); -- One action on a card. Append-only; rows go only when a draft is deleted with its card (R19). CREATE TABLE card_events ( id TEXT PRIMARY KEY, card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE, actor_id TEXT REFERENCES people(id), -- NULL for the system created_at TIMESTAMP NOT NULL, type TEXT NOT NULL CHECK (type IN ('created', 'published', 'updated', 'verified', 'flagged', 'flag_withdrawn', 'owner_changed', 'archived', 'restored')), data TEXT NOT NULL DEFAULT '{}' -- a JSON object stored as text: version number, old and new owner, flags resolved ); -- One submitted search. Never stores who searched (R14). Rows older than 90 days are deleted by the daily job. CREATE TABLE search_logs ( id TEXT PRIMARY KEY, query TEXT NOT NULL CHECK (length(query) BETWEEN 1 AND 200), -- the search terms joined by single spaces result_count INTEGER NOT NULL CHECK (result_count BETWEEN 0 AND 20), -- results shown, at most 20 created_at TIMESTAMP NOT NULL ); CREATE INDEX search_logs_created_at ON search_logs (created_at); -- One email in the outbox. Written in the same transaction as its action and sent after it commits (R17). CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('verify_request', 'card_updated', 'flagged', 'flag_resolved', 'review_due', 'review_overdue')), person_id TEXT NOT NULL REFERENCES people(id), -- the recipient to_email TEXT NOT NULL, card_id TEXT NOT NULL REFERENCES cards(id), subject TEXT NOT NULL, body TEXT NOT NULL, -- plain text status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), -- each send attempt adds 1; logging is not an attempt; retried until 5 last_error TEXT, created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); CREATE INDEX notifications_status ON notifications (status);