Canonical page: [Schema and SQL Access](https://cloudflavor.io/aemon/docs/schema/)
Source snapshot: https://github.com/cloudflavor/aemon/blob/a85ee1d024b81c6373acd86379fe12f15c26a7a5/docs/schema.md

# Schema and SQL Access

The schema is **not** embedded in the binary: it lives in numbered files under
`migrations/`, applied in order by a `pragma user_version` runner. Adding a column means
adding `migrations/0002_*.sql`, never editing shipped SQL.

```sql
-- migrations/0001_init.sql
docs(id, name unique, library, version, release, kind, source, ingested_at)
pages(id, doc_id, path, title, kind)
entries(id, doc_id, page_id, title, kind, path, ord, body, summary)
entries_fts  -- FTS5 external content over entries(body, title, kind), porter unicode61
rerank_cache, expand_cache, enrich_state, prune_state
```

`kind` is `page` for body chunks and `symbol:<Builtin|Function|Guide|Parameter|Variable|
Word|...>` for searchIndex rows. `title` is the heading the chunk sits under, so results
read like `Getting Started`, not like a filename.

`docs` carries the provenance fields (`library`, `version`, `release`, `source`) plus
`license` and `attribution`, which `aemon sources` reads back out.

## Querying directly

```sql
-- top matches with highlighted snippets
select d.name, e.title, snippet(entries_fts, 0, '[', ']', '…', 20)
  from entries_fts join entries e on e.id = entries_fts.rowid
  join docs d on d.id = e.doc_id
 where entries_fts match 'borrow checker' limit 5;

-- every chunk of one page, in document order
select ord, title, body from entries where path = 'std/borrowck.html' order by ord;

-- the symbol index only
select title, path from entries where kind like 'symbol:%' and title = 'sort_by';
```

Search ranks with `bm25(entries_fts, 1.0, 5.0, 1.0)` — body weight 1, title weight 5 —
and retries with `OR` when the strict `AND` query returns nothing. Full syntax is FTS5's;
`entries_fts` is an external-content table, so `insert`/`delete` go through `entries`.

Read-only access is safe while a query is running: `aemon serve` opens workers
read-only, and `sqlite3 "file:$HOME/.local/share/aemon/aemon.db?mode=ro"` works too.
Corpus mutations go through the CLI, which owns migrations.

## Maintenance

```sh
aemon compact --dedupe --vacuum    # junk/dup clean-up + FTS rebuild
aemon audit                        # corpus shape
aemon stats --json                 # per-corpus counts and freshness
```

`compact --vacuum` rewrites the file and needs free disk equal to the database size.
