refactor: split db/index.ts into focused modules#170
Merged
Conversation
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).
a83e605 to
f1a6527
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 linessrc/db/index.tsreplaced with a 5-line re-export barrel; all downstream callers are unchangedCLAUDE.mdupdated to reflect current architecture:log.ts,format.ts,tools.ts,install/,learn/,profiles/modules documented; CLI commands section added; phase table extended to phase 9Test plan
bun test— 657 pass, 0 fail (same as before)bun run typecheck— cleanbun run build— clean, dist rebuilt