FORGE-89: in-process D1 test database (FG_TEST_DB=d1) with a transaction guard #725

Merged
gmackie merged 1 commit from FORGE-89-d1-test-db into main 2026-10-12 06:41:43 +00:00
Owner

FORGE-89 (D1 Phase 4), step 2 of 5. Stacked on #721 (contains its commit until #721 merges; review the top commit).

What

  • FG_TEST_DB=d1 runs the API's DB-backed suites (the same databaseTests list the Postgres lane runs) on an in-process D1: Miniflare, i.e. Cloudflare's own D1 implementation inside workerd. Chosen over better-sqlite3 for fidelity: it enforces D1's 100-bound-parameter limit and statement-length limit, uses D1's batch semantics and error text, and authorises functions the way D1 does. Locally a query costs ~9 ms, so the lane takes ~80 s.
  • Behind the existing helpers, as asked:
    • database-test-config.ts: with FG_TEST_DB=d1, generates the STRICT SQLite DDL once (@forgegraph/db/schema-sqlite/ddl, from #721), passes it to workers, and aliases @forgegraph/db/schema → @forgegraph/db/schema-sqlite so application code under test writes through the SQLite tables. The Postgres path is unchanged.
    • helpers/test-db.ts: createTestDb() returns a drizzle d1 client over one shared Miniflare D1 (helpers/d1-test-db.ts), with execute() returning rows like the postgres-js client; the availability probe uses sqlite_schema.
  • Transaction guard: every statement passes assertNoTransactionControl, which throws D1TransactionGuardError on BEGIN / COMMIT / ROLLBACK / SAVEPOINT / RELEASE / END. Remote D1 rejects them with 7500 and local D1 versions differ (the Miniflare 5 in this repo rejects BEGIN with a Durable Objects message; older ones accept it), so the lane fails the same way production would. db.transaction() therefore fails on D1.
  • d1-known-failures.txt: the 20 files that fail on D1 today, each tagged with its cause (14 transactions, Postgres-only SQL such as extract(epoch), DISTINCT ON, TRUNCATE … CASCADE, and >100 bound parameters / statement too long). pnpm --filter @forgegraph/api test:d1 skips them and must be green; test:d1:known runs only them and prints a ::warning:: for any that now pass, so FORGE-88 removes entries as it converts code. The config rejects list entries that aren't database tests.
  • Tests: helpers/d1-test-db.test.ts (guard, unit lane) and d1-transaction-guard.database.test.ts (D1 lane only: db.transaction() fails with the guard error as its cause; a batch with a mid-batch PK violation leaves no rows). Skipped on Postgres.

Verification (local)

  • test:d1: 24 files, 136 passed, 16 skipped, 0 failed (82 s).
  • test:d1:known: 20 listed, 20 still failing, 0 now passing.
  • Postgres lane test:database (PGlite): 44 passed, 1 skipped (the D1-only file); 342 tests passed.
  • pnpm --filter @forgegraph/api typecheck: clean; oxlint clean on the new files.

Next: the non-required CI job (separate PR, coordinated with FORGE-68's ci.yml layout).

🤖 Generated with Claude Code

FORGE-89 (D1 Phase 4), step 2 of 5. **Stacked on #721** (contains its commit until #721 merges; review the top commit). ## What - `FG_TEST_DB=d1` runs the API's DB-backed suites (the same `databaseTests` list the Postgres lane runs) on an **in-process D1: Miniflare**, i.e. Cloudflare's own D1 implementation inside workerd. Chosen over better-sqlite3 for fidelity: it enforces D1's 100-bound-parameter limit and statement-length limit, uses D1's batch semantics and error text, and authorises functions the way D1 does. Locally a query costs ~9 ms, so the lane takes ~80 s. - Behind the existing helpers, as asked: - `database-test-config.ts`: with `FG_TEST_DB=d1`, generates the STRICT SQLite DDL once (`@forgegraph/db/schema-sqlite/ddl`, from #721), passes it to workers, and aliases `@forgegraph/db/schema` → `@forgegraph/db/schema-sqlite` so application code under test writes through the SQLite tables. The Postgres path is unchanged. - `helpers/test-db.ts`: `createTestDb()` returns a drizzle d1 client over one shared Miniflare D1 (`helpers/d1-test-db.ts`), with `execute()` returning rows like the postgres-js client; the availability probe uses `sqlite_schema`. - **Transaction guard**: every statement passes `assertNoTransactionControl`, which throws `D1TransactionGuardError` on BEGIN / COMMIT / ROLLBACK / SAVEPOINT / RELEASE / END. Remote D1 rejects them with 7500 and local D1 versions differ (the Miniflare 5 in this repo rejects BEGIN with a Durable Objects message; older ones accept it), so the lane fails the same way production would. `db.transaction()` therefore fails on D1. - `d1-known-failures.txt`: the 20 files that fail on D1 today, each tagged with its cause (14 transactions, Postgres-only SQL such as `extract(epoch)`, `DISTINCT ON`, `TRUNCATE … CASCADE`, and >100 bound parameters / statement too long). `pnpm --filter @forgegraph/api test:d1` skips them and must be green; `test:d1:known` runs only them and prints a `::warning::` for any that now pass, so FORGE-88 removes entries as it converts code. The config rejects list entries that aren't database tests. - Tests: `helpers/d1-test-db.test.ts` (guard, unit lane) and `d1-transaction-guard.database.test.ts` (D1 lane only: `db.transaction()` fails with the guard error as its cause; a batch with a mid-batch PK violation leaves no rows). Skipped on Postgres. ## Verification (local) - `test:d1`: 24 files, 136 passed, 16 skipped, 0 failed (82 s). - `test:d1:known`: 20 listed, 20 still failing, 0 now passing. - Postgres lane `test:database` (PGlite): 44 passed, 1 skipped (the D1-only file); 342 tests passed. - `pnpm --filter @forgegraph/api typecheck`: clean; oxlint clean on the new files. Next: the non-required CI job (separate PR, coordinated with FORGE-68's ci.yml layout). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-89: generated sqlite-core (D1) schema with a DDL parity test
All checks were successful
CI / gitleaks (pull_request) Successful in 12s
CI / go (pull_request) Successful in 1m24s
CI / storybook (pull_request) Successful in 1m55s
CI / web-build (pull_request) Successful in 2m41s
CI / checks (pull_request) Successful in 2m51s
CI / database (pull_request) Successful in 3m13s
CI / unit-web (pull_request) Successful in 5m15s
CI / unit-api (pull_request) Successful in 6m14s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 41s
4d40c5ac0b
Adds packages/db/src/schema-sqlite/, a drizzle sqlite-core schema for all 142
tables, generated from the Postgres schema's drizzle metadata by
packages/db/scripts/generate-sqlite-schema.ts (pnpm --filter @forgegraph/db
db:sqlite:generate). Translation follows the D1 design doc: pgEnum -> text +
CHECK built from the shared @forgegraph/db/enums arrays, json/jsonb/arrays ->
text json typed as the pg column, timestamptz -> integer timestamp_ms with an
epoch-ms SQL default, boolean -> integer boolean, serial seq -> integer +
deployment_event_seq_unique_idx (FORGE-85), partial/expression indexes,
checks (btrim -> trim) and FK actions kept. Relations are generated too.

schema-sqlite/ddl.ts produces the DDL via drizzle-kit and makes every table
STRICT; the D1 test lane and the Phase 5 baseline reuse it. parity.test.ts
fails when the generated files are stale and compares the DDL that SQLite
actually builds (PRAGMA table_info / index_list / foreign_key_list) against
the Postgres metadata: tables, columns, types, nullability, defaults, PKs,
unique columns, FKs and actions, indexes incl. partial/expression, checks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
FORGE-89: in-process D1 test database (FG_TEST_DB=d1) with a transaction guard
All checks were successful
CI / gitleaks (pull_request) Successful in 20s
CI / go (pull_request) Successful in 1m7s
CI / unit-api (pull_request) Successful in 4m12s
CI / storybook (pull_request) Successful in 2m42s
CI / web-build (pull_request) Successful in 2m54s
CI / checks (pull_request) Successful in 2m58s
CI / unit-web (pull_request) Successful in 6m6s
CI / database (pull_request) Successful in 4m5s
CI / ci (pull_request) Successful in 58s
0142c748d9
FG_TEST_DB=d1 runs the API's DB-backed suites against Cloudflare's own D1
implementation in workerd (Miniflare) instead of Postgres, behind the
existing helpers: database-test-config.ts generates the STRICT SQLite DDL
(@forgegraph/db/schema-sqlite/ddl) once per run and aliases
@forgegraph/db/schema to the generated sqlite-core schema, and
createTestDb() returns a drizzle d1 client over a shared Miniflare D1.

Every statement passes a guard that rejects BEGIN/COMMIT/ROLLBACK/
SAVEPOINT/RELEASE/END, because remote D1 rejects them with error 7500 and
local D1 versions differ. db.transaction() therefore fails on this lane.

d1-known-failures.txt lists the 20 test files that do not pass on D1 yet
(transactions, Postgres-only SQL, >100 bound parameters); test:d1 skips them
and is green, test:d1:known runs only them and reports any that now pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie force-pushed FORGE-89-d1-test-db from 0142c748d9
All checks were successful
CI / gitleaks (pull_request) Successful in 20s
CI / go (pull_request) Successful in 1m7s
CI / unit-api (pull_request) Successful in 4m12s
CI / storybook (pull_request) Successful in 2m42s
CI / web-build (pull_request) Successful in 2m54s
CI / checks (pull_request) Successful in 2m58s
CI / unit-web (pull_request) Successful in 6m6s
CI / database (pull_request) Successful in 4m5s
CI / ci (pull_request) Successful in 58s
to 8333114e1f
All checks were successful
CI / gitleaks (pull_request) Successful in 17s
CF executor smoke / smoke (pull_request) Successful in 23s
CI / unit-api (pull_request) Successful in 2m46s
CI / unit-web (pull_request) Successful in 3m1s
CI / storybook (pull_request) Successful in 2m8s
CI / go (pull_request) Successful in 1m24s
CI / checks (pull_request) Successful in 2m56s
CI / web-build (pull_request) Successful in 4m5s
CI / database (pull_request) Successful in 3m45s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 37s
2026-10-12 06:23:43 +00:00
Compare
Sign in to join this conversation.
No description provided.