Skip to content

feat(db): phase 1a — native code-KG schema + 15→16 migration#203

Merged
edheltzel merged 1 commit into
mainfrom
worktree-phase-1a-kg-schema-migration
Jun 27, 2026
Merged

feat(db): phase 1a — native code-KG schema + 15→16 migration#203
edheltzel merged 1 commit into
mainfrom
worktree-phase-1a-kg-schema-migration

Conversation

@edheltzel

Copy link
Copy Markdown
Owner

Summary

Lands the native code knowledge graph schema as Phase 1a (epic #196). This is the first of three Phase 1 PRs; 1b (indexer command) and 1c (scout integration) build directly on this schema.

Three files changed, all additive — no existing behavior altered.

Changes

src/db/schema.ts

  • Appended code_files, code_nodes, code_edges CREATE TABLE IF NOT EXISTS to CREATE_TABLES (fresh-install + existing-install idempotent path via initDb, G1/G7 precedent)
  • Added code_nodes entry to FTS_SCHEMA — external-content FTS5 over (name, qualified_name, signature), byte-aligned with the house pattern. Auto-wires recall repair/doctor via FTS_SOURCES with zero extra code (G6)
  • Appended 6 indexes to CREATE_INDEXES, including a partial index on unresolved edges (WHERE dst_id IS NULL) that makes the resolution re-link pass cheap

src/db/migrations.ts

  • Appended MIGRATIONS[15] — minimal 15→16 capability gate. Body slices FTS DDL from FTS_SCHEMA.code_nodes (G8 no-drift idiom, same pattern as 14→15). Does NOT open its own transaction (runner provides it). Does NOT re-CREATE base tables (initDb's CREATE_TABLES already guarantees them on every init)

tests/db/migrations.test.ts + tests/db/schema.test.ts

  • New code-KG schema migration (15 to 16) describe block with 4 tests:
    • Fresh DB lands at version 16 with all 4 objects (3 tables + FTS) — mutation guard
    • v8 fixture migrates cleanly through 16 (per §9-C1 — the live DB is at v8, not v15)
    • code_edges carries no project column (§9-C2)
    • FK semantics: src_id CASCADE, dst_id SET NULL — exercised with real inserts/deletes (§9-C3)
  • Updated 4 hardcoded MIGRATIONS.length mutation guards from 15 → 16

§9 RedTeam corrections implemented

Correction Applied
C1 — test uses sub-15 fixture (v8) ✓ v8 fixture, not v15→16
C2 — code_edges has no project column ✓ omitted; comment explains scope via src_id
C3 — resolved predicate is dst_id IS NOT NULL ✓ DDL comment + FK test use this predicate
C6 — comment describes one try/catch, not per-statement ✓ tightened in migration body

Verification

bun run build and bun test both green. Pre-existing failures in opencode-integration and pi-integration (labeled [RED] — unimplemented hardening tests unrelated to schema/migration) are unchanged.

 1187 pass
 10 fail  ← all 10 are pre-existing [RED] tests in opencode/pi-integration
Ran 1197 tests across 97 files

The new describe block contributes 11 tests (37 total in migrations.test.ts, up from 26).

Closes #199

…, #199)

Add code_files / code_nodes / code_edges tables and code_nodes_fts
external-content FTS5 index to the schema. Append MIGRATIONS[15]
as a minimal capability gate (user_version >= 16 = "native KG
present"). Tests cover fresh-install, v8-through-16 upgrade path,
FK cascade/SET NULL semantics, and no-project-column on edges.

- §9-C2: code_edges carries no project column; scope inherits via src_id
- §9-C3: resolved predicate is dst_id IS NOT NULL, never provenance
- §9-C1: migration test uses v8 fixture (live DB is at v8, not v15)
- §9-C6: migration comment describes one try/catch (not per-statement)
- G8: migration slices FTS DDL from FTS_SCHEMA.code_nodes (no drift)
- G6: FTS_SCHEMA entry auto-wires repair/doctor with zero extra code

Closes #199
@edheltzel edheltzel merged commit 06c9078 into main Jun 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Phase 1a: code-KG schema + 15→16 migration

1 participant