db.init() replays SCHEMA_SQL + ~129 migrations on every fresh in-memory DB (~40ms each), which is minutes of pure setup across thousands of DB-backed tests. Add a test-only migrated-schema snapshot: migrate ONE in-memory DB per test file, serialize it, and deserialize a fresh copy per test instead of re-migrating. Each test still gets a brand-new, fully-isolated in-memory DB; only the migration cost is amortized. - sqlite-adapter: expose serialize()/deserialize() (node:sqlite + bun) - db.ts: setInMemoryTemplateSnapshot() hook (test-only, null in prod) + serializeSnapshot(); constructor deserializes the snapshot for in-memory DBs so init() short-circuits migrate()+compat at v129 - store-test-helpers: install/clearInMemoryDbSnapshot harness - dashboard: db-snapshot-helper mirror (core __tests__ is cross-package) - convert agent-store, mission-store, workflow-routes suites Measured (raw db.init(): 43ms -> 5ms, 8x): - agent-store 13.12s -> 3.32s - mission-store 17.62s -> 5.69s (min of 3) - workflow-routes tests 4.38s -> 2.79s (min of 5; not init-dominated) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
133 lines
4.4 KiB
TypeScript
133 lines
4.4 KiB
TypeScript
/**
|
|
* SQLite adapter that picks the runtime's native SQLite at construction time:
|
|
* - Bun runtime → `bun:sqlite` (built-in, no native module dance)
|
|
* - Node runtime → `node:sqlite` (Node 22+ built-in)
|
|
*
|
|
* Exports a `DatabaseSync` class with the subset of node:sqlite's API that the
|
|
* fn codebase actually uses: `prepare`, `exec`, `close`, and prepared
|
|
* statement methods `all`, `get`, `run`.
|
|
*
|
|
* The adapter exists because Bun's --compile bundler does not implement
|
|
* `node:sqlite` (require returns undefined silently; import throws), so a
|
|
* standalone Bun binary cannot use the same module that plain `node` uses.
|
|
*/
|
|
|
|
import { createRequire } from "node:module";
|
|
import { assertOutsideRealFusionPath } from "./test-safety.js";
|
|
|
|
const isBun = typeof (globalThis as { Bun?: unknown }).Bun !== "undefined";
|
|
|
|
// Use createRequire so the bundler does not statically trace these specifiers.
|
|
// Bun's bundler will skip require() calls whose argument it cannot resolve at
|
|
// build time, which keeps `node:sqlite` from being eagerly pulled into the
|
|
// compiled binary (where it would fail to resolve at runtime).
|
|
const requireFromHere = createRequire(import.meta.url);
|
|
|
|
export interface SqliteRunResult {
|
|
changes: number | bigint;
|
|
lastInsertRowid: number | bigint;
|
|
}
|
|
|
|
export interface SqliteStatement {
|
|
all(...params: unknown[]): unknown[];
|
|
get(...params: unknown[]): unknown;
|
|
run(...params: unknown[]): SqliteRunResult;
|
|
}
|
|
|
|
interface RawStatement {
|
|
all: (...params: unknown[]) => unknown[];
|
|
get: (...params: unknown[]) => unknown;
|
|
run: (...params: unknown[]) => { changes: number | bigint; lastInsertRowid: number | bigint };
|
|
}
|
|
|
|
interface RawDatabase {
|
|
exec(sql: string): void;
|
|
prepare(sql: string): RawStatement;
|
|
close(): void;
|
|
// Optional snapshot API (node:sqlite ≥ 22.x, bun:sqlite). Both runtimes
|
|
// expose `serialize()` → Uint8Array and `deserialize(buf)` that replaces the
|
|
// open database's contents in place. Used only by the test snapshot harness.
|
|
serialize?: () => Uint8Array;
|
|
deserialize?: (data: Uint8Array) => void;
|
|
}
|
|
|
|
type DatabaseCtor = new (path: string) => RawDatabase;
|
|
|
|
let cachedCtor: DatabaseCtor | null = null;
|
|
|
|
function loadDatabaseCtor(): DatabaseCtor {
|
|
if (cachedCtor) return cachedCtor;
|
|
|
|
if (isBun) {
|
|
const mod = requireFromHere("bun:sqlite") as { Database: DatabaseCtor };
|
|
cachedCtor = mod.Database;
|
|
} else {
|
|
const mod = requireFromHere("node:sqlite") as { DatabaseSync: DatabaseCtor };
|
|
cachedCtor = mod.DatabaseSync;
|
|
}
|
|
return cachedCtor;
|
|
}
|
|
|
|
/**
|
|
* Drop-in replacement for `node:sqlite`'s `DatabaseSync`. Backed by
|
|
* `bun:sqlite` under Bun and `node:sqlite` under Node.
|
|
*/
|
|
export class DatabaseSync {
|
|
private impl: RawDatabase;
|
|
|
|
constructor(path: string) {
|
|
assertOutsideRealFusionPath(path, "SQLite database open");
|
|
const Ctor = loadDatabaseCtor();
|
|
this.impl = new Ctor(path);
|
|
}
|
|
|
|
exec(sql: string): void {
|
|
this.impl.exec(sql);
|
|
}
|
|
|
|
close(): void {
|
|
this.impl.close();
|
|
}
|
|
|
|
/**
|
|
* FNXC:CoreTests 2026-06-25-16:30:
|
|
* Snapshot the entire database into a byte buffer. Backs the test-only
|
|
* migrated-DB snapshot harness so the 129-migration init() runs once per
|
|
* test file instead of once per test. Throws if the runtime lacks the API.
|
|
*/
|
|
serialize(): Uint8Array {
|
|
if (typeof this.impl.serialize !== "function") {
|
|
throw new Error("SQLite runtime does not support serialize()");
|
|
}
|
|
return this.impl.serialize();
|
|
}
|
|
|
|
/**
|
|
* FNXC:CoreTests 2026-06-25-16:30:
|
|
* Replace this (in-memory) database's contents with a previously serialized
|
|
* snapshot. Restores a fully-migrated schema without replaying migrations.
|
|
*/
|
|
deserialize(data: Uint8Array): void {
|
|
if (typeof this.impl.deserialize !== "function") {
|
|
throw new Error("SQLite runtime does not support deserialize()");
|
|
}
|
|
this.impl.deserialize(data);
|
|
}
|
|
|
|
prepare(sql: string): SqliteStatement {
|
|
const stmt = this.impl.prepare(sql);
|
|
// Both node:sqlite and bun:sqlite expose the same .all/.get/.run shape.
|
|
// Normalize `get` to return undefined (not null) when no row matches, and
|
|
// pass run() through unchanged — both runtimes already produce the same
|
|
// { changes, lastInsertRowid } shape.
|
|
return {
|
|
all: (...params: unknown[]) => stmt.all(...params),
|
|
get: (...params: unknown[]) => {
|
|
const row = stmt.get(...params);
|
|
return row ?? undefined;
|
|
},
|
|
run: (...params: unknown[]) => stmt.run(...params),
|
|
};
|
|
}
|
|
}
|