# Knowledge base database schema

Source: https://personalsoftwarefactory.com/schemas/knowledge-base
Updated: 2026-12-04

> Fifteen tables. The design is carried by cards, which hold one question, one owner and a review interval, plus card_versions for every publish and flags for reports that a card is wrong. Whether a card is Verified is never stored: it is computed from the owner, open flags, the edit state and the review date, so it can't go stale. Alternate questions and tags are child tables with a position column whose range caps them at 5 and 8 per card.

- 15 tables, 5 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 126 seed rows from the blueprint
- The blueprint's test build passed 31 of 31 acceptance checks

Files: [knowledge-base.sql](https://personalsoftwarefactory.com/schemas/knowledge-base.sql) · [knowledge-base-seed.sql](https://personalsoftwarefactory.com/schemas/knowledge-base-seed.sql) · [the Internal Knowledge Base blueprint](https://personalsoftwarefactory.com/blueprints/knowledge-base)

## Relationships

- people.team_id references teams.id: each people row points to exactly one teams row, and one teams row can have many people rows pointing to it.
- collection_teams.collection_id references collections.id: each collection_teams row points to exactly one collections row, and one collections row can have many collection_teams rows pointing to it. On delete: cascade.
- collection_teams.team_id references teams.id: each collection_teams row points to exactly one teams row, and one teams row can have many collection_teams rows pointing to it.
- collection_editors.collection_id references collections.id: each collection_editors row points to exactly one collections row, and one collections row can have many collection_editors rows pointing to it. On delete: cascade.
- collection_editors.person_id references people.id: each collection_editors row points to exactly one people row, and one people row can have many collection_editors rows pointing to it.
- cards.collection_id references collections.id: each cards row points to exactly one collections row, and one collections row can have many cards rows pointing to it.
- cards.owner_id references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
- cards.verified_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
- cards.needs_verification_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
- cards.created_by references people.id: each cards row points to exactly one people row, and one people row can have many cards rows pointing to it.
- cards.updated_by references people.id: each cards row points to exactly one people row, and one people row can have many cards rows pointing to it.
- cards.archived_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
- card_questions.card_id references cards.id: each card_questions row points to exactly one cards row, and one cards row can have many card_questions rows pointing to it. On delete: cascade.
- card_tags.card_id references cards.id: each card_tags row points to exactly one cards row, and one cards row can have many card_tags rows pointing to it. On delete: cascade.
- card_versions.card_id references cards.id: each card_versions row points to exactly one cards row, and one cards row can have many card_versions rows pointing to it.
- card_versions.author_id references people.id: each card_versions row points to exactly one people row, and one people row can have many card_versions rows pointing to it.
- flags.card_id references cards.id: each flags row points to exactly one cards row, and one cards row can have many flags rows pointing to it.
- flags.person_id references people.id: each flags row points to exactly one people row, and one people row can have many flags rows pointing to it.
- flags.resolved_by references people.id: each flags row points to at most one people row, and one people row can have many flags rows pointing to it.
- follows.card_id references cards.id: each follows row points to exactly one cards row, and one cards row can have many follows rows pointing to it. On delete: cascade.
- follows.person_id references people.id: each follows row points to exactly one people row, and one people row can have many follows rows pointing to it.
- card_events.card_id references cards.id: each card_events row points to exactly one cards row, and one cards row can have many card_events rows pointing to it. On delete: cascade.
- card_events.actor_id references people.id: each card_events row points to at most one people row, and one people row can have many card_events rows pointing to it.
- notifications.person_id references people.id: each notifications row points to exactly one people row, and one people row can have many notifications rows pointing to it.
- notifications.card_id references cards.id: each notifications row points to exactly one cards row, and one cards row can have many notifications rows pointing to it.

## Tables

Also described as: internal wiki database design, knowledge management system database tables, FAQ database schema.

### settings

The one row holding the company name, the team time zone and when the daily job last ran. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no |  | Primary key | Check: `id = 1` |
| `company_name` | TEXT | no |  |  | Check: `length(company_name) BETWEEN 1 AND 60` |
| `time_zone` | TEXT | no | `'America/New_York'` |  | IANA name; "today" and every date shown use this zone |
| `last_daily_run_at` | TIMESTAMP | yes |  |  | written only by the scheduled job |

### teams

A team; each person belongs to one, and collections can be limited to teams. Implements: Team.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | Check: `length(name) BETWEEN 1 AND 40` |

- Unique index `teams_name_unique` on (lower(name))

### people

A person who can sign in, with their team, admin flag, active flag and Slack user ID. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | Check: `length(name) BETWEEN 1 AND 80` |
| `email` | TEXT | no |  | Unique | stored lowercase Check: `email = lower(email)` |
| `team_id` | TEXT | no |  | References teams.id |  |
| `is_admin` | BOOLEAN | no | `FALSE` |  |  |
| `active` | BOOLEAN | no | `TRUE` |  |  |
| `slack_user_id` | TEXT | yes |  | Unique | set by Link Slack accounts or by hand |
| `slack_lookup_error` | TEXT | yes |  |  | the Slack API error from the last failed lookup |

- App rule (R15): at least one active admin must always exist.

### collections

A group of cards visible to everyone or to chosen teams. Implements: Collection.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | Check: `length(name) BETWEEN 1 AND 60` |
| `description` | TEXT | yes |  |  | Check: `length(description) <= 300` |
| `visibility` | TEXT | no |  |  | Check: `visibility IN ('everyone', 'teams')` |

- Unique index `collections_name_unique` on (lower(name))
- App rule (R3): every editor must be active and able to see the collection.

### collection_teams

A team that can see a collection whose visibility is teams. Implements: CollectionTeam.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `collection_id` | TEXT | no |  | Primary key (part); References collections.id, on delete cascade |  |
| `team_id` | TEXT | no |  | Primary key (part); References teams.id | a team used by a collection cannot be deleted (R15) |

- App rule: a collection with visibility teams needs at least one row here.

### collection_editors

A person who can create and edit cards in a collection. Implements: CollectionEditor.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `collection_id` | TEXT | no |  | Primary key (part); References collections.id, on delete cascade |  |
| `person_id` | TEXT | no |  | Primary key (part); References people.id |  |

### cards

One question and its answer, with its owner, review interval, last verification and status. Implements: Card.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | shown as KB-101 Check: `number >= 101` |
| `collection_id` | TEXT | no |  | References collections.id | a collection with cards cannot be deleted |
| `status` | TEXT | no | `'draft'` |  | Check: `status IN ('draft', 'published', 'archived')` |
| `title` | TEXT | no |  |  | written as the question people ask Check: `length(title) BETWEEN 5 AND 120` |
| `body` | TEXT | no | `''` |  | Markdown Check: `length(body) <= 5000` |
| `owner_id` | TEXT | yes |  | References people.id | required to publish |
| `review_interval_days` | INTEGER | yes | `90` |  | NULL means does not expire Check: `review_interval_days IN (30, 90, 180, 365)` |
| `verified_at` | TIMESTAMP | yes |  |  |  |
| `verified_by` | TEXT | yes |  | References people.id |  |
| `verified_until` | DATE | yes |  |  | date of verified_at in the team zone plus review_interval_days; NULL when it does not expire |
| `needs_verification` | TEXT | yes |  |  | NULL means empty Check: `needs_verification IN ('not_verified', 'edited', 'restored')` |
| `needs_verification_by` | TEXT | yes |  | References people.id |  |
| `needs_verification_at` | TIMESTAMP | yes |  |  |  |
| `version` | INTEGER | no | `0` |  | 0 until first published, then the current version number |
| `created_by` | TEXT | no |  | References people.id |  |
| `created_at` | TIMESTAMP | no |  |  |  |
| `updated_by` | TEXT | no |  | References people.id | the author of the current version (the last save for drafts) |
| `updated_at` | TIMESTAMP | no |  |  |  |
| `archived_by` | TEXT | yes |  | References people.id |  |
| `archived_at` | TIMESTAMP | yes |  |  |  |
| `reminder_sent_for` | DATE | yes |  |  | the verified_until value the review_due notice was sent for |
| `overdue_sent_for` | DATE | yes |  |  | the verified_until value the review_overdue notice was sent for |

- Table check: `status = 'draft' OR owner_id IS NOT NULL`
- Table check: `status = 'draft' OR length(body) >= 1`
- Table check: `(status = 'draft' AND version = 0) OR (status <> 'draft' AND version >= 1)`
- Table check: `verified_at IS NOT NULL OR (verified_by IS NULL AND verified_until IS NULL)`
- 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.

### card_questions

One alternate phrasing of a card's question, in display order. Implements: child table of Card.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `card_id` | TEXT | no |  | Primary key (part); References cards.id, on delete cascade |  |
| `position` | INTEGER | no |  | Primary key (part) | the 1 to 5 range caps a card at 5 questions Check: `position BETWEEN 1 AND 5` |
| `question` | TEXT | no |  |  | Check: `length(question) BETWEEN 3 AND 120` |

### card_tags

One tag on a card, in display order. Implements: child table of Card.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `card_id` | TEXT | no |  | Primary key (part); References cards.id, on delete cascade |  |
| `position` | INTEGER | no |  | Primary key (part) | the 1 to 8 range caps a card at 8 tags Check: `position BETWEEN 1 AND 8` |
| `tag` | TEXT | no |  |  | Check: `length(tag) BETWEEN 2 AND 30 AND tag = lower(tag)` |

- Unique together: `card_id`, `tag`
- App rule (R4): tags use only lowercase letters, digits and hyphens.

### card_versions

A published version of a card, written on every publish and never changed. Implements: CardVersion.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `card_id` | TEXT | no |  | References cards.id |  |
| `number` | INTEGER | no |  |  | Check: `number >= 1` |
| `title` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `questions` | TEXT | no | `''` |  | the alternate questions as published, one per line, in order |
| `tags` | TEXT | no | `''` |  | the tags as published, one per line, in order |
| `change_note` | TEXT | no | `''` |  | empty for version 1, 3 to 200 characters after that |
| `author_id` | TEXT | no |  | References people.id |  |
| `published_at` | TIMESTAMP | no |  |  |  |

- Unique together: `card_id`, `number`
- Table check: `(number = 1 AND change_note = '') OR (number > 1 AND length(change_note) BETWEEN 3 AND 200)`

### flags

A report that a card is wrong, open until verifying, archiving or withdrawing resolves it. Implements: Flag.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `card_id` | TEXT | no |  | References cards.id |  |
| `person_id` | TEXT | no |  | References people.id | who flagged |
| `reason` | TEXT | no |  |  | Check: `length(reason) BETWEEN 5 AND 500` |
| `created_at` | TIMESTAMP | no |  |  |  |
| `status` | TEXT | no | `'open'` |  | Check: `status IN ('open', 'resolved')` |
| `resolved_at` | TIMESTAMP | yes |  |  |  |
| `resolved_by` | TEXT | yes |  | References people.id |  |
| `resolution` | TEXT | yes |  |  | Check: `resolution IN ('verified', 'archived', 'withdrawn')` |

- Table 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)`
- Unique index `flags_one_open_per_person` on (card_id, person_id) where `status = 'open'`
- App rule (R9): the owner of the card cannot flag it; only the person who flagged can withdraw it.

### follows

A person following a card to hear about new versions. Implements: Follow.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `card_id` | TEXT | no |  | Primary key (part); References cards.id, on delete cascade |  |
| `person_id` | TEXT | no |  | Primary key (part); References people.id |  |
| `created_at` | TIMESTAMP | no |  |  |  |

### card_events

One action on a card, append-only, shown as the card's Activity. Implements: CardEvent.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `card_id` | TEXT | no |  | References cards.id, on delete cascade |  |
| `actor_id` | TEXT | yes |  | References people.id | NULL for the system |
| `created_at` | TIMESTAMP | no |  |  |  |
| `type` | TEXT | no |  |  | Check: `type IN ('created', 'published', 'updated', 'verified', 'flagged', 'flag_withdrawn', 'owner_changed', 'archived', 'restored')` |
| `data` | TEXT | no | `'{}'` |  | a JSON object stored as text: version number, old and new owner, flags resolved |

### search_logs

One submitted search: the terms, the number of results and the time, never who searched. Implements: SearchLog.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `query` | TEXT | no |  |  | the search terms joined by single spaces Check: `length(query) BETWEEN 1 AND 200` |
| `result_count` | INTEGER | no |  |  | results shown, at most 20 Check: `result_count BETWEEN 0 AND 20` |
| `created_at` | TIMESTAMP | no |  |  |  |

- Index `search_logs_created_at` on (created_at)

### notifications

One email in the outbox with its kind, recipient, status and send attempts. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('verify_request', 'card_updated', 'flagged', 'flag_resolved', 'review_due', 'review_overdue')` |
| `person_id` | TEXT | no |  | References people.id | the recipient |
| `to_email` | TEXT | no |  |  |  |
| `card_id` | TEXT | no |  | References cards.id |  |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  | plain text |
| `status` | TEXT | no | `'queued'` |  | Check: `status IN ('queued', 'sent', 'logged', 'failed')` |
| `attempts` | INTEGER | no | `0` |  | each send attempt adds 1; logging is not an attempt; retried until 5 Check: `attempts >= 0` |
| `last_error` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no |  |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

- Index `notifications_status` on (status)

## The SQL

```sql
-- 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);
```

## Seed data

Loads all of section 15: the settings row, 5 teams, 11 people, 4 collections with their team and editor rows, 12 cards with their 7 alternate questions and 24 tags, 13 versions, 2 flags, 2 follows, 28 events and 7 search log rows. The outbox is empty, as the spec says. Times are converted from 10:00 New York time to UTC. The keys inside each event's data object (version, verified, flags_resolved) are our naming; the spec lists what data records but not its key names.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/knowledge-base.sql https://personalsoftwarefactory.com/schemas/knowledge-base-seed.sql | sqlite3 knowledge_base.db
# PostgreSQL
createdb knowledge_base && curl -s https://personalsoftwarefactory.com/schemas/knowledge-base.sql https://personalsoftwarefactory.com/schemas/knowledge-base-seed.sql | psql -v ON_ERROR_STOP=1 knowledge_base
```

## Design decisions

### Trust is computed, not stored

There is no verified or trust column. Section 6 and R12 say trust is worked out every time from the owner, open flags, needs_verification and the review date, in that order of rules. A stored flag would stay Verified the day after verified_until passes, or after the owner is deactivated, because nothing writes to the card on those days.

### Verification fields are kept apart from the edit state

verified_at, verified_by and verified_until record the last verification, while needs_verification (edited, not_verified or restored, NULL for empty) records why the card is waiting since. Section 6 needs both: KB-104 was verified on 3 Aug and then edited by Sam, and the trust text names the editor and the owner. Clearing verified_at on an edit would lose the review date that the scheduled job's markers compare against.

### Alternate questions and tags are child tables

Section 5 allows 0 to 5 alternate questions and 0 to 8 tags, and section 8 searches both as separate fields. card_questions and card_tags store one item per row with a per-item length check, a unique tag per card, and a position column checked to 1 to 5 or 1 to 8, which caps the count in the database. The tag character rule (lowercase letters, digits and hyphens) has no portable pattern check, so only lowercase and length are enforced in SQL.

### Versions keep a text snapshot of questions and tags

A CardVersion is written once and never changed (R6), so its questions and tags are stored as text, one item per line, as they were when published. The seed follows the rule added after the build test found that the spec gave only the old body: earlier versions keep the card's current title, questions and tags and differ only in the body. (This point comes from a gap the blueprint's build test found.)

### One open flag per person per card is a partial unique index

Section 5 allows at most one open flag per person per card, but a person may flag the same card again after a flag is resolved. A unique index on (card_id, person_id) WHERE status = 'open' enforces exactly that, while a plain unique constraint would block the second flag forever.

### Verify now is one event, not two

card_events stores one row per action with a JSON object in a text column. The build test found that publishing with Verify now could be read as two events; R19 now says it is one published or updated event whose data records the verification and the number of flags resolved, and the seed's KB-101 version 2 event does that. (This point comes from a gap the blueprint's build test found.)

### Deleting a draft cascades, nothing else does

Only drafts can be deleted, and section 7 says the card, its events and follows go with it, so those foreign keys use ON DELETE CASCADE. card_versions and flags have no cascade because only published cards have them. Collections and teams that are still used can't be deleted (R15), which the plain foreign keys from cards, people and collection_teams enforce.

## Example queries

### Verification score by collection

```sql
SELECT col.name AS collection,
  COUNT(*) AS published_cards,
  SUM(CASE WHEN o.active = TRUE
    AND (o.is_admin = TRUE OR EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id))
    AND NOT EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open')
    AND c.needs_verification IS NULL
    AND (c.review_interval_days IS NULL OR c.verified_until >= '2026-10-14') THEN 1 ELSE 0 END) AS verified_cards,
  (200 * SUM(CASE WHEN o.active = TRUE
    AND (o.is_admin = TRUE OR EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id))
    AND NOT EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open')
    AND c.needs_verification IS NULL
    AND (c.review_interval_days IS NULL OR c.verified_until >= '2026-10-14') THEN 1 ELSE 0 END) + COUNT(*)) / (2 * COUNT(*)) AS score_percent
FROM cards c
JOIN collections col ON col.id = c.collection_id
JOIN people o ON o.id = c.owner_id
WHERE c.status = 'published'
GROUP BY col.name
ORDER BY col.name;
```

On the seed data, with today as 2026-10-14: 4 rows; IT & Accounts has 5 published cards, 2 verified, 40%.

### Needs review, grouped by owner

```sql
SELECT owner, card, title, reason FROM (
  SELECT o.name || CASE WHEN o.active = FALSE THEN ' (inactive)' ELSE '' END AS owner,
    c.number,
    'KB-' || c.number AS card,
    c.title,
    CASE
      WHEN o.active = FALSE OR (o.is_admin = FALSE AND NOT EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id)) THEN 'Needs a new owner'
      WHEN EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open') THEN 'Flagged'
      WHEN c.needs_verification IS NOT NULL THEN 'Waiting for verification: ' || c.needs_verification
      WHEN c.review_interval_days IS NOT NULL AND c.verified_until < '2026-10-14' THEN 'Review overdue'
    END AS reason
  FROM cards c
  JOIN people o ON o.id = c.owner_id
  WHERE c.status = 'published'
) needs_review
WHERE reason IS NOT NULL
ORDER BY owner, number;
```

On the seed data: 4 rows; Kai Brooks (inactive) owns KB-112, which needs a new owner.

### Searches with no results in the last 30 days

```sql
SELECT query, COUNT(*) AS searches
FROM search_logs
WHERE result_count = 0
  AND created_at >= '2026-09-14 13:00:00'
GROUP BY query
ORDER BY COUNT(*) DESC, MAX(created_at) DESC;
```

On the seed data, with now as 2026-10-14 13:00 UTC: 3 rows; parking pass with 3 searches comes first.

### Recently changed, for one reader

```sql
SELECT 'KB-' || c.number AS card, c.title, v.number AS version, a.name AS author,
  CASE WHEN v.number = 1 THEN 'Published' ELSE v.change_note END AS note
FROM card_versions v
JOIN cards c ON c.id = v.card_id
JOIN collections col ON col.id = c.collection_id
JOIN people a ON a.id = v.author_id
WHERE c.status = 'published'
  AND (col.visibility = 'everyone'
       OR EXISTS (SELECT 1 FROM collection_teams ct JOIN people me ON me.team_id = ct.team_id
                  WHERE ct.collection_id = col.id AND me.id = 'p-jo'))
ORDER BY v.published_at DESC
LIMIT 10;
```

On the seed data, for Jo Park: 10 rows; the first is KB-104 version 2 by Sam Okafor, Added the $500 yearly limit.

### Cards due an overdue notice

```sql
SELECT 'KB-' || c.number AS card, o.name AS owner, o.email, c.verified_until
FROM cards c
JOIN people o ON o.id = c.owner_id
WHERE c.status = 'published'
  AND c.review_interval_days IS NOT NULL
  AND o.active = TRUE
  AND c.verified_until < '2026-10-14'
  AND (c.overdue_sent_for IS NULL OR c.overdue_sent_for <> c.verified_until)
ORDER BY c.number;
```

On the seed data: 1 row; KB-103, owned by Ben Cho, due 2026-10-04.

## What was not tested

- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.

## The blueprint

This schema is section 5 of the [Internal Knowledge Base blueprint](https://personalsoftwarefactory.com/blueprints/knowledge-base), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/knowledge-base/blueprint.md
