# Bug tracking system database schema

Source: https://personalsoftwarefactory.com/schemas/bug-tracker
Updated: 2026-12-05

> Ten tables. The bugs table carries the design: one row per bug with a severity, a priority set at triage, a status from a fixed list of nine, a fix-due time and a pointer to the bug it duplicates, with table checks that keep the reporter, closed time, fix fields and duplicate pointer consistent with the status. An append-only events table is the activity log and the source of the weekly summary counts.

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

Files: [bug-tracker.sql](https://personalsoftwarefactory.com/schemas/bug-tracker.sql) · [bug-tracker-seed.sql](https://personalsoftwarefactory.com/schemas/bug-tracker-seed.sql) · [the Bug Tracker blueprint](https://personalsoftwarefactory.com/blueprints/bug-tracker)

## Relationships

- bugs.area_id references areas.id: each bugs row points to at most one areas row, and one areas row can have many bugs rows pointing to it.
- bugs.reporter_id references people.id: each bugs row points to at most one people row, and one people row can have many bugs rows pointing to it.
- bugs.assignee_id references people.id: each bugs row points to at most one people row, and one people row can have many bugs rows pointing to it.
- bugs.duplicate_of_id references bugs.id: each bugs row points to at most one another bugs row, and one bugs row can have many bugs rows pointing to it.
- bugs.fixed_by_id references people.id: each bugs row points to at most one people row, and one people row can have many bugs rows pointing to it.
- watchers.bug_id references bugs.id: each watchers row points to exactly one bugs row, and one bugs row can have many watchers rows pointing to it.
- watchers.person_id references people.id: each watchers row points to exactly one people row, and one people row can have many watchers rows pointing to it.
- comments.bug_id references bugs.id: each comments row points to exactly one bugs row, and one bugs row can have many comments rows pointing to it.
- comments.author_id references people.id: each comments row points to exactly one people row, and one people row can have many comments rows pointing to it.
- events.bug_id references bugs.id: each events row points to exactly one bugs row, and one bugs row can have many events rows pointing to it.
- events.actor_id references people.id: each events row points to at most one people row, and one people row can have many events rows pointing to it.
- notifications.person_id references people.id: each notifications row points to at most one people row, and one people row can have many notifications rows pointing to it.
- notifications.bug_id references bugs.id: each notifications row points to at most one bugs row, and one bugs row can have many notifications rows pointing to it.

## Tables

Also described as: issue tracker database design, bug tracker database tables, defect tracking data model.

### settings

The single row with the company name, time zone, fix targets per severity, reopen window, form switch and the bug number counter. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no |  | Primary key | one row only Check: `id = 1` |
| `company_name` | TEXT | no |  |  | Check: `LENGTH(company_name) BETWEEN 1 AND 60` |
| `time_zone` | TEXT | no | `'America/New_York'` |  | IANA name; days, weeks and shown times use it |
| `target_s1_hours` | INTEGER | yes | `24` |  | NULL means no fix target Check: `target_s1_hours BETWEEN 1 AND 8760` |
| `target_s2_hours` | INTEGER | yes | `168` |  | Check: `target_s2_hours BETWEEN 1 AND 8760` |
| `target_s3_hours` | INTEGER | yes | `720` |  | Check: `target_s3_hours BETWEEN 1 AND 8760` |
| `target_s4_hours` | INTEGER | yes |  |  | empty by default Check: `target_s4_hours BETWEEN 1 AND 8760` |
| `reopen_window_days` | INTEGER | no | `30` |  | Check: `reopen_window_days BETWEEN 1 AND 365` |
| `form_enabled` | BOOLEAN | no | `TRUE` |  | the public report form is on |
| `last_bug_number` | INTEGER | no | `100` |  | R2: a new bug takes last_bug_number + 1 in the same transaction |

### people

Someone who can sign in, with admin, triager and QA flags. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | Check: `LENGTH(name) BETWEEN 1 AND 100` |
| `email` | TEXT | no |  | Unique | stored lowercase Check: `email = LOWER(email)` |
| `is_admin` | BOOLEAN | no | `FALSE` |  |  |
| `is_triager` | BOOLEAN | no | `FALSE` |  |  |
| `is_qa` | BOOLEAN | no | `FALSE` |  |  |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- App rule (R16): there is always at least one active admin and one active triager; a person assigned to open bugs cannot be deactivated.

### areas

A part of the product, such as Web app or Payments. Implements: Area.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | Check: `LENGTH(name) BETWEEN 2 AND 40` |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Unique index `areas_name_uniq` on (LOWER(name))

### bugs

One reported bug with its status, severity, priority, reporter, assignee, fix and due times. Implements: Bug.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | shown as BUG-104; never reused (R2) |
| `version` | INTEGER | no | `1` |  | Check: `version >= 1` |
| `title` | TEXT | no |  |  | Check: `LENGTH(title) BETWEEN 5 AND 120` |
| `area_id` | TEXT | yes |  | References areas.id | may be empty until triage |
| `severity` | TEXT | no |  |  | Check: `severity IN ('s1', 's2', 's3', 's4')` |
| `priority` | TEXT | yes |  |  | empty until triage Check: `priority IN ('p1', 'p2', 'p3', 'p4')` |
| `status` | TEXT | no | `'new'` |  | Check: `status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed', 'verified', 'wont_fix', 'cannot_reproduce', 'duplicate')` |
| `steps` | TEXT | no |  |  | Check: `LENGTH(steps) BETWEEN 10 AND 5000` |
| `expected` | TEXT | yes |  |  | optional Check: `LENGTH(expected) <= 2000` |
| `actual` | TEXT | no |  |  | what happened Check: `LENGTH(actual) BETWEEN 1 AND 2000` |
| `environment` | TEXT | yes |  |  | where it happened; required on team reports (see the table check) Check: `LENGTH(environment) <= 200` |
| `link` | TEXT | yes |  |  | optional screenshot, recording or page Check: `LENGTH(link) <= 2000 AND link LIKE 'https://%'` |
| `source` | TEXT | no |  |  | Check: `source IN ('team', 'form')` |
| `reporter_id` | TEXT | yes |  | References people.id | team reports only |
| `reporter_name` | TEXT | yes |  |  | form reports only Check: `LENGTH(reporter_name) BETWEEN 1 AND 100` |
| `reporter_email` | TEXT | yes |  |  | form reports only, stored lowercase Check: `LENGTH(reporter_email) <= 254 AND reporter_email = LOWER(reporter_email)` |
| `assignee_id` | TEXT | yes |  | References people.id | at most one assignee |
| `duplicate_of_id` | TEXT | yes |  | References bugs.id | set exactly while status is duplicate |
| `review_link` | TEXT | yes |  |  | optional, from Send to review Check: `review_link LIKE 'https://%'` |
| `fix_note` | TEXT | yes |  |  | set by Mark fixed, cleared by Reopen Check: `LENGTH(fix_note) BETWEEN 10 AND 2000` |
| `fixed_by_id` | TEXT | yes |  | References people.id | the latest fix |
| `fixed_at` | TIMESTAMP | yes |  |  |  |
| `fix_due_at` | TIMESTAMP | yes |  |  | NULL when the severity has no target |
| `overdue_notified_at` | TIMESTAMP | yes |  |  | one overdue notice per due time |
| `triaged_at` | TIMESTAMP | yes |  |  |  |
| `closed_at` | TIMESTAMP | yes |  |  | set exactly while the bug is closed |
| `reopen_count` | INTEGER | no | `0` |  | Check: `reopen_count >= 0` |
| `created_at` | TIMESTAMP | no |  |  |  |
| `updated_at` | TIMESTAMP | no |  |  | time of its latest event |

- Table check: `(source = 'team' AND reporter_id IS NOT NULL AND reporter_name IS NULL AND reporter_email IS NULL) OR (source = 'form' AND reporter_id IS NULL AND reporter_name IS NOT NULL AND reporter_email IS NOT NULL)`
- Table check: `source = 'form' OR (environment IS NOT NULL AND LENGTH(environment) >= 2)`
- Table check: `(status = 'duplicate' AND duplicate_of_id IS NOT NULL) OR (status <> 'duplicate' AND duplicate_of_id IS NULL)`
- Table check: `duplicate_of_id IS NULL OR duplicate_of_id <> id`
- Table check: `(status IN ('verified', 'wont_fix', 'cannot_reproduce', 'duplicate') AND closed_at IS NOT NULL) OR (status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed') AND closed_at IS NULL)`
- Table check: `status NOT IN ('triaged', 'in_progress', 'in_review', 'fixed', 'verified') OR (priority IS NOT NULL AND area_id IS NOT NULL)`
- Table check: `status NOT IN ('in_progress', 'in_review', 'fixed') OR assignee_id IS NOT NULL`
- Table check: `status NOT IN ('fixed', 'verified') OR (fix_note IS NOT NULL AND fixed_by_id IS NOT NULL AND fixed_at IS NOT NULL)`
- Index `bugs_status_idx` on (status)
- Index `bugs_duplicate_of_idx` on (duplicate_of_id)
- App rule (R1, section 6): who may move a bug from which status to which is checked by the app.
- App rule (section 7): fix_due_at = created_at + the target hours for the severity; recalculated on a severity change, which also clears overdue_notified_at.
- App rule (section 8): duplicate_of_id always points at a bug that is not itself a duplicate.
- App rule (R17): version goes up by 1 on every action on this bug except Comment, Watch and Unwatch.

### watchers

A person who gets notifications about one bug. Implements: Watcher.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `bug_id` | TEXT | no |  | Primary key (part); References bugs.id |  |
| `person_id` | TEXT | no |  | Primary key (part); References people.id |  |

### comments

A comment on a bug; never edited or deleted. Implements: Comment.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `bug_id` | TEXT | no |  | References bugs.id |  |
| `author_id` | TEXT | no |  | References people.id |  |
| `body` | TEXT | no |  |  | Check: `LENGTH(body) BETWEEN 1 AND 5000` |
| `created_at` | TIMESTAMP | no |  |  |  |

### events

One line of a bug's append-only activity log. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `seq` | INTEGER | no |  | Unique | increasing; orders events written at the same moment |
| `bug_id` | TEXT | no |  | References bugs.id |  |
| `actor_id` | TEXT | yes |  | References people.id | NULL for the system and for form reports |
| `type` | TEXT | no |  |  | Check: `type IN ('reported', 'edited', 'triaged', 'severity_changed', 'priority_changed', 'assigned', 'unassigned', 'started', 'sent_to_review', 'review_returned', 'fixed', 'verified', 'reopened', 'wont_fix', 'cannot_reproduce', 'marked_duplicate', 'duplicate_added', 'unmarked_duplicate', 'commented', 'overdue')` |
| `data` | TEXT | yes |  |  | JSON text: from/to values, changed field names, comment_id, fix note, bug numbers |
| `created_at` | TIMESTAMP | no |  |  |  |

- Index `events_bug_idx` on (bug_id, created_at, seq)
- App rule (R15): events are never edited or deleted.

### notifications

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

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('new_bug', 'acknowledgement', 'assigned', 'ready_to_verify', 'verified', 'fixed_for_reporter', 'reopened', 'closed', 'duplicate', 'comment', 'overdue', 'weekly_summary')` |
| `person_id` | TEXT | yes |  | References people.id | NULL for outside reporters |
| `to_email` | TEXT | no |  |  |  |
| `bug_id` | TEXT | yes |  | References bugs.id | NULL for weekly summaries |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `status` | TEXT | no | `'queued'` |  | Check: `status IN ('queued', 'sent', 'logged', 'failed')` |
| `attempts` | INTEGER | no | `0` |  | the job retries until 5 attempts Check: `attempts >= 0` |
| `error` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no |  |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

### summary_runs

A week whose summary has been queued, so it is sent once. Implements: SummaryRun.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `week_start` | DATE | no |  | Primary key | the Monday the summarised week starts on |
| `created_at` | TIMESTAMP | no |  |  |  |

### form_attempts

One accepted public-form report, keyed by a hash of the IP, for the rate limit. Implements: FormAttempt.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key | row id; the spec lists only ip_hash and created_at |
| `ip_hash` | TEXT | no |  |  | HMAC-SHA256 of the IP address with FORM_IP_SECRET; the IP itself is never stored |
| `created_at` | TIMESTAMP | no |  |  |  |

- Index `form_attempts_ip_idx` on (ip_hash, created_at)

## The SQL

```sql
-- Bug Tracker database schema
-- Source: https://personalsoftwarefactory.com/blueprints/bug-tracker (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 for the team.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only
  company_name TEXT NOT NULL CHECK (LENGTH(company_name) BETWEEN 1 AND 60),
  time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; days, weeks and shown times use it
  target_s1_hours INTEGER DEFAULT 24 CHECK (target_s1_hours BETWEEN 1 AND 8760), -- NULL means no fix target
  target_s2_hours INTEGER DEFAULT 168 CHECK (target_s2_hours BETWEEN 1 AND 8760),
  target_s3_hours INTEGER DEFAULT 720 CHECK (target_s3_hours BETWEEN 1 AND 8760),
  target_s4_hours INTEGER CHECK (target_s4_hours BETWEEN 1 AND 8760), -- empty by default
  reopen_window_days INTEGER NOT NULL DEFAULT 30 CHECK (reopen_window_days BETWEEN 1 AND 365),
  form_enabled BOOLEAN NOT NULL DEFAULT TRUE, -- the public report form is on
  last_bug_number INTEGER NOT NULL DEFAULT 100 -- R2: a new bug takes last_bug_number + 1 in the same transaction
);

-- Someone who can sign in. People are deactivated, never deleted.
-- App rule (R16): there is always at least one active admin and one active triager; a person assigned to open bugs cannot be deactivated.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 100),
  email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), -- stored lowercase
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  is_triager BOOLEAN NOT NULL DEFAULT FALSE,
  is_qa BOOLEAN NOT NULL DEFAULT FALSE,
  active BOOLEAN NOT NULL DEFAULT TRUE
);

-- A part of the product a bug belongs to. Inactive areas stay on existing bugs.
CREATE TABLE areas (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 2 AND 40),
  active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE UNIQUE INDEX areas_name_uniq ON areas (LOWER(name)); -- unique ignoring case

-- One reported bug, from a team member or the public form.
-- App rule (R1, section 6): who may move a bug from which status to which is checked by the app.
-- App rule (section 7): fix_due_at = created_at + the target hours for the severity; recalculated on a severity change, which also clears overdue_notified_at.
-- App rule (section 8): duplicate_of_id always points at a bug that is not itself a duplicate.
-- App rule (R17): version goes up by 1 on every action on this bug except Comment, Watch and Unwatch.
CREATE TABLE bugs (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE, -- shown as BUG-104; never reused (R2)
  version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1),
  title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 5 AND 120),
  area_id TEXT REFERENCES areas(id), -- may be empty until triage
  severity TEXT NOT NULL CHECK (severity IN ('s1', 's2', 's3', 's4')),
  priority TEXT CHECK (priority IN ('p1', 'p2', 'p3', 'p4')), -- empty until triage
  status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed', 'verified', 'wont_fix', 'cannot_reproduce', 'duplicate')),
  steps TEXT NOT NULL CHECK (LENGTH(steps) BETWEEN 10 AND 5000),
  expected TEXT CHECK (LENGTH(expected) <= 2000), -- optional
  actual TEXT NOT NULL CHECK (LENGTH(actual) BETWEEN 1 AND 2000), -- what happened
  environment TEXT CHECK (LENGTH(environment) <= 200), -- where it happened; required on team reports (see the table check)
  link TEXT CHECK (LENGTH(link) <= 2000 AND link LIKE 'https://%'), -- optional screenshot, recording or page
  source TEXT NOT NULL CHECK (source IN ('team', 'form')),
  reporter_id TEXT REFERENCES people(id), -- team reports only
  reporter_name TEXT CHECK (LENGTH(reporter_name) BETWEEN 1 AND 100), -- form reports only
  reporter_email TEXT CHECK (LENGTH(reporter_email) <= 254 AND reporter_email = LOWER(reporter_email)), -- form reports only, stored lowercase
  assignee_id TEXT REFERENCES people(id), -- at most one assignee
  duplicate_of_id TEXT REFERENCES bugs(id), -- set exactly while status is duplicate
  review_link TEXT CHECK (review_link LIKE 'https://%'), -- optional, from Send to review
  fix_note TEXT CHECK (LENGTH(fix_note) BETWEEN 10 AND 2000), -- set by Mark fixed, cleared by Reopen
  fixed_by_id TEXT REFERENCES people(id), -- the latest fix
  fixed_at TIMESTAMP,
  fix_due_at TIMESTAMP, -- NULL when the severity has no target
  overdue_notified_at TIMESTAMP, -- one overdue notice per due time
  triaged_at TIMESTAMP,
  closed_at TIMESTAMP, -- set exactly while the bug is closed
  reopen_count INTEGER NOT NULL DEFAULT 0 CHECK (reopen_count >= 0),
  created_at TIMESTAMP NOT NULL,
  updated_at TIMESTAMP NOT NULL, -- time of its latest event
  CHECK ((source = 'team' AND reporter_id IS NOT NULL AND reporter_name IS NULL AND reporter_email IS NULL)
      OR (source = 'form' AND reporter_id IS NULL AND reporter_name IS NOT NULL AND reporter_email IS NOT NULL)),
  CHECK (source = 'form' OR (environment IS NOT NULL AND LENGTH(environment) >= 2)), -- R3
  CHECK ((status = 'duplicate' AND duplicate_of_id IS NOT NULL) OR (status <> 'duplicate' AND duplicate_of_id IS NULL)),
  CHECK (duplicate_of_id IS NULL OR duplicate_of_id <> id),
  CHECK ((status IN ('verified', 'wont_fix', 'cannot_reproduce', 'duplicate') AND closed_at IS NOT NULL)
      OR (status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed') AND closed_at IS NULL)),
  CHECK (status NOT IN ('triaged', 'in_progress', 'in_review', 'fixed', 'verified') OR (priority IS NOT NULL AND area_id IS NOT NULL)), -- R4
  CHECK (status NOT IN ('in_progress', 'in_review', 'fixed') OR assignee_id IS NOT NULL), -- R6: only the assignee does the work
  CHECK (status NOT IN ('fixed', 'verified') OR (fix_note IS NOT NULL AND fixed_by_id IS NOT NULL AND fixed_at IS NOT NULL)) -- R7
);
CREATE INDEX bugs_status_idx ON bugs (status);
CREATE INDEX bugs_duplicate_of_idx ON bugs (duplicate_of_id);

-- A person who gets notifications about a bug.
CREATE TABLE watchers (
  bug_id TEXT NOT NULL REFERENCES bugs(id),
  person_id TEXT NOT NULL REFERENCES people(id),
  PRIMARY KEY (bug_id, person_id)
);

-- A comment on a bug. Comments cannot be edited or deleted in v1.
CREATE TABLE comments (
  id TEXT PRIMARY KEY,
  bug_id TEXT NOT NULL REFERENCES bugs(id),
  author_id TEXT NOT NULL REFERENCES people(id),
  body TEXT NOT NULL CHECK (LENGTH(body) BETWEEN 1 AND 5000),
  created_at TIMESTAMP NOT NULL
);

-- One line of the activity log of a bug. Append-only.
-- App rule (R15): events are never edited or deleted.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  seq INTEGER NOT NULL UNIQUE, -- increasing; orders events written at the same moment
  bug_id TEXT NOT NULL REFERENCES bugs(id),
  actor_id TEXT REFERENCES people(id), -- NULL for the system and for form reports
  type TEXT NOT NULL CHECK (type IN ('reported', 'edited', 'triaged', 'severity_changed', 'priority_changed', 'assigned', 'unassigned', 'started', 'sent_to_review', 'review_returned', 'fixed', 'verified', 'reopened', 'wont_fix', 'cannot_reproduce', 'marked_duplicate', 'duplicate_added', 'unmarked_duplicate', 'commented', 'overdue')),
  data TEXT, -- JSON text: from/to values, changed field names, comment_id, fix note, bug numbers
  created_at TIMESTAMP NOT NULL
);
CREATE INDEX events_bug_idx ON events (bug_id, created_at, seq);

-- One email in the outbox, written in the same transaction as the action.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL CHECK (kind IN ('new_bug', 'acknowledgement', 'assigned', 'ready_to_verify', 'verified', 'fixed_for_reporter', 'reopened', 'closed', 'duplicate', 'comment', 'overdue', 'weekly_summary')),
  person_id TEXT REFERENCES people(id), -- NULL for outside reporters
  to_email TEXT NOT NULL,
  bug_id TEXT REFERENCES bugs(id), -- NULL for weekly summaries
  subject TEXT NOT NULL,
  body TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')),
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), -- the job retries until 5 attempts
  error TEXT,
  created_at TIMESTAMP NOT NULL,
  sent_at TIMESTAMP
);

-- A weekly summary that has been queued, so a second run that week sends nothing.
CREATE TABLE summary_runs (
  week_start DATE PRIMARY KEY, -- the Monday the summarised week starts on
  created_at TIMESTAMP NOT NULL
);

-- One accepted public-form report, for the rate limit (R19). The job deletes rows older than 24 hours.
CREATE TABLE form_attempts (
  id TEXT PRIMARY KEY, -- row id; the spec lists only ip_hash and created_at
  ip_hash TEXT NOT NULL, -- HMAC-SHA256 of the IP address with FORM_IP_SECRET; the IP itself is never stored
  created_at TIMESTAMP NOT NULL
);
CREATE INDEX form_attempts_ip_idx ON form_attempts (ip_hash, created_at);
```

## Seed data

Loads everything section 14 seeds: the settings row, 8 people, 4 areas, the 14 bugs (9 open, 5 closed), 23 watchers, the 4 comments and the 48 events their histories imply, with team times (EDT) converted to UTC. Placeholders: the spec leaves the steps, what happened and where it happened text of seeded bugs to the builder, so steps, actual and environment hold one fixed placeholder each, the same on every row (environment stays empty on Erin Walsh's form report, where it is optional); expected is optional and left empty. Event data is left empty because the spec names what it carries but not its JSON shape. No notifications, summary runs or form attempts are seeded, as the spec says.

## Load it

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

## Design decisions

### Bug numbers come from a counter on the settings row

R2 says a new bug takes Settings.last_bug_number + 1 and stores it back in the same transaction, so numbers are sequential and never reused. bugs.number is UNIQUE, which also stops two concurrent reports from getting the same number. MAX(number) + 1 would hand out a number twice under load.

### The status decides which other columns must be set

Section 5 says closed_at is set exactly while a bug is closed, R4 requires a priority and an area before triage, R6 lets only the assignee do the work and R7 makes Mark fixed set fixed_by, fixed_at and the fix note. Each of these is a table CHECK on bugs written so a NULL cannot slip through. The transitions themselves (who may move a bug from which status) stay in the app.

### Team and form reporters share one table, with a check

Section 5 gives team reports a reporter_id and form reports a name and email instead. A CHECK makes those two shapes the only ones allowed, and another makes where it happened required, with at least 2 characters, on team reports only, as R3 and R19 say. Outside reporters are not people rows, because they never sign in.

### Duplicates point at one canonical bug

duplicate_of_id references bugs and is set exactly while the status is duplicate. Section 8 moves every duplicate of D to C when D itself becomes a duplicate, so chains never form and the reports count is 1 plus a single COUNT of rows pointing at the bug. That move is a multi-row update, so it stays an app rule.

### Fix targets are nullable hours, due time is stored

Section 7 gives each severity a target of 1 to 8,760 hours or none (S4 has none by default), so the four target columns are nullable with a range CHECK. fix_due_at is stored on the bug, because changing a target in Settings must not move existing due times; a severity change recalculates it from created_at.

### A version column for stale screens

R17 sends the version a person saw with every action and saves nothing if it changed. version starts at 1 and the app adds 1 inside the same transaction as the update. Comments, watching and the side effects of Mark duplicate on other bugs leave it alone.

### Events are the source of the weekly counts

Section 12 counts Fixed, Verified, Closed without a fix and Reopened from events in the week, not from the current status, because a bug can be fixed, reopened and fixed again in one week. events.seq orders events written at the same moment, and data is JSON stored as TEXT, because JSON types are not portable.

## Example queries

### Board: open bugs by column

```sql
SELECT 'BUG-' || b.number AS bug, b.status, UPPER(b.severity) AS sev, COALESCE(UPPER(b.priority), '-') AS pri, COALESCE(p.name, 'Unassigned') AS assignee, 1 + (SELECT COUNT(*) FROM bugs d WHERE d.duplicate_of_id = b.id) AS reports FROM bugs b LEFT JOIN people p ON p.id = b.assignee_id WHERE b.status IN ('new', 'triaged', 'in_progress', 'in_review', 'fixed') ORDER BY CASE b.status WHEN 'new' THEN 1 WHEN 'triaged' THEN 2 WHEN 'in_progress' THEN 3 WHEN 'in_review' THEN 4 ELSE 5 END, CASE WHEN b.priority IS NULL THEN 9 ELSE 0 END, b.priority, b.severity, b.created_at;
```

On the seed data: 9 rows; New has BUG-114 (S1) above BUG-113, and BUG-104 shows 2 reports.

### Overdue bugs at the seed time

```sql
SELECT 'BUG-' || b.number AS bug, b.title, UPPER(b.severity) AS sev, COALESCE(p.name, 'Unassigned') AS assignee, b.fix_due_at FROM bugs b LEFT JOIN people p ON p.id = b.assignee_id WHERE b.status IN ('new', 'triaged', 'in_progress', 'in_review') AND b.fix_due_at < '2026-10-13 14:00:00' ORDER BY b.fix_due_at;
```

On the seed data at Tue 13 Oct 2026, 10:00 EDT: 2 rows, BUG-102 (due 2026-09-30 14:00 UTC, Kofi Mensah) then BUG-104 (Theo Lindqvist).

### Waiting for verification, oldest fix first

```sql
SELECT 'BUG-' || b.number AS bug, b.title, f.name AS fixed_by, b.fixed_at FROM bugs b JOIN people f ON f.id = b.fixed_by_id WHERE b.status = 'fixed' ORDER BY b.fixed_at;
```

On the seed data: 3 rows, BUG-107 (Theo Lindqvist), BUG-106 (Kofi Mensah), BUG-108 (Grace Kim).

### Weekly summary counts for Mon 5 to Sun 11 Oct

```sql
SELECT (SELECT COUNT(*) FROM bugs WHERE created_at >= '2026-10-05 04:00:00' AND created_at < '2026-10-12 04:00:00') AS reported, (SELECT COUNT(DISTINCT bug_id) FROM events WHERE type = 'fixed' AND created_at >= '2026-10-05 04:00:00' AND created_at < '2026-10-12 04:00:00') AS fixed, (SELECT COUNT(DISTINCT bug_id) FROM events WHERE type = 'verified' AND created_at >= '2026-10-05 04:00:00' AND created_at < '2026-10-12 04:00:00') AS verified, (SELECT COUNT(DISTINCT bug_id) FROM events WHERE type IN ('wont_fix', 'cannot_reproduce', 'marked_duplicate') AND created_at >= '2026-10-05 04:00:00' AND created_at < '2026-10-12 04:00:00') AS closed_without_fix, (SELECT COUNT(DISTINCT bug_id) FROM events WHERE type = 'reopened' AND created_at >= '2026-10-05 04:00:00' AND created_at < '2026-10-12 04:00:00') AS reopened;
```

On the seed data: 1 row, Reported 6, Fixed 2, Verified 1, Closed without a fix 3, Reopened 1, matching the summary example in section 12.

## 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 [Bug Tracker blueprint](https://personalsoftwarefactory.com/blueprints/bug-tracker), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/bug-tracker/blueprint.md
