Skip to content

refactor: split db/index.ts into focused modules#170

Merged
sakebomb merged 6 commits into
mainfrom
refactor/split-db-module
Apr 25, 2026
Merged

refactor: split db/index.ts into focused modules#170
sakebomb merged 6 commits into
mainfrom
refactor/split-db-module

Conversation

@sakebomb
Copy link
Copy Markdown
Owner

Summary

  • src/db/index.ts (899 lines) split into 5 focused files: types.ts, schema.ts, chunking.ts, queries.ts, analytics.ts — each under 335 lines
  • src/db/index.ts replaced with a 5-line re-export barrel; all downstream callers are unchanged
  • CLAUDE.md updated to reflect current architecture: log.ts, format.ts, tools.ts, install/, learn/, profiles/ modules documented; CLI commands section added; phase table extended to phase 9

Test plan

  • bun test — 657 pass, 0 fail (same as before)
  • bun run typecheck — clean
  • bun run build — clean, dist rebuilt

Sakebomb added 4 commits April 24, 2026 20:32
db/index.ts (899 lines) split into:
- db/types.ts — all TypeScript interfaces
- db/schema.ts — DDL, migrations, connection management
- db/chunking.ts — chunkText, sanitizeFtsQuery
- db/queries.ts — core CRUD operations
- db/analytics.ts — aggregation/reporting queries
- db/index.ts — thin re-export barrel (all callers unchanged)

CLAUDE.md updated to reflect current architecture: new src/log.ts,
format.ts, tools.ts, install/, learn/, profiles/ modules; CLI commands
section added; phase table extended through phase 9.
commands.ts (901 lines) split into:
- profiles/shared.ts      — constants, validation, dir helpers, manifest fetch/verify
- profiles/cmd-local.ts   — list, remove, feed, check (no network required)
- profiles/cmd-catalog.ts — install, update, seed, info, available (manifest network ops)
- profiles/cmd-test.ts    — testProfile, cmdTest (diagnostic subcommand)
- profiles/commands.ts    — dispatcher + re-exports (67 lines)

All external imports unchanged; tests import from commands.ts as before.
PRAGMA wal_checkpoint(TRUNCATE) zeros the WAL file. On Bun 1.3.13 /
GitHub Actions runners, subsequent writes into a fresh-from-zero WAL
are not visible to new connections (received 15 rows instead of 30).
An explicit FULL checkpoint before close() did not help either.

The other three concurrent-DB tests pass because they never truncate.
SQLite WAL guarantees committed frames are visible to any connection
that opens after the writer closes — no manual checkpointing needed.
Replace the checkpoint-then-truncate approach with plain sequential
writes (db1 closes, db2 opens on the existing WAL, appends, closes).
@sakebomb sakebomb force-pushed the refactor/split-db-module branch from a83e605 to f1a6527 Compare April 25, 2026 01:34
Sakebomb added 2 commits April 24, 2026 21:51
Without the explicit TRUNCATE before closeDb(), db1's 15 rows live only
in the WAL file.  Whether db2 (or dbVerify) can read them depends on
SQLite's WAL recovery behaviour, which is reliable locally but flaky on
Ubuntu CI.  The TRUNCATE checkpoint materialises all rows into the main DB
file so Phase 2 starts from a clean, deterministic state.

Reverts the checkpoint removal from f1a6527.
Bun 1.3.13 appears to share sub-module state (the `instance` singleton in
schema.ts) across parallel test workers when imported via a barrel re-export.
This caused getDb(dbPath) in Phase 1 to return a stale connection from
another worker, writing to the wrong database and producing 15 rows instead
of 30.

Two defences:
1. beforeEach() calls closeDb() to guarantee a null singleton at the start
   of every test regardless of cross-worker leakage.
2. Phase 1 of the two-writers test now uses openSecondConnection (raw
   new Database) instead of getDb (singleton), so its writes are always
   directed to the correct dbPath file.
@sakebomb sakebomb merged commit be795bd into main Apr 25, 2026
3 checks passed
@sakebomb sakebomb deleted the refactor/split-db-module branch April 25, 2026 02:57
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.

1 participant