FORGE-89: generated sqlite-core (D1) schema with a DDL parity test #721

Merged
gmackie merged 1 commit from FORGE-89-sqlite-schema into main 2026-10-12 06:40:54 +00:00
Owner

FORGE-89 (D1 Phase 4, parent FORGE-69), step 1 of 5: the sqlite-core schema and its parity test.

What

  • packages/db/src/schema-sqlite/: drizzle sqlite-core tables for all 142 Postgres tables, plus enum compat exports (fooEnum.enumValues), constants, type re-exports and relations, so the module exports exactly the same names as @forgegraph/db/schema.
  • Generated, not hand-kept: packages/db/scripts/generate-sqlite-schema.ts (pnpm --filter @forgegraph/db db:sqlite:generate, db:sqlite:check). It reads drizzle's own table metadata (getTableConfig, relations), so it handles this schema's standalone-call style that scripts/pg-to-d1.mjs cannot, and it fails on anything it can't translate (unknown column types, Postgres-only SQL in checks/indexes, unlisted default functions, serial columns without a unique-index entry).
  • Translation rules (design doc "Schema translation rules"): pgEnum → text({ enum }) + CHECK (col IN (...)) from the shared @forgegraph/db/enums arrays; json/jsonb/arrays → text({ mode: "json" }) typed as the pg column; timestamptz → integer({ mode: "timestamp_ms" }) with an epoch-ms SQL default for defaultNow(); boolean → integer({ mode: "boolean" }); serial deployment_events.seq → integer + deployment_event_seq_unique_idx (same name as FORGE-85 #712; skipped once #712 adds it on Postgres); numeric/real → real; the 7 partial and 3 expression indexes, checks (btrim → trim) and all 255 FKs with their actions are kept.
  • schema-sqlite/ddl.ts: drizzle-kit DDL with every table made STRICT (wrong storage classes fail at write time; PK columns are NOT NULL). The D1 test lane (next PR) and the Phase 5 baseline reuse it.
  • schema-sqlite/parity.test.ts (runs in the existing unit-web lane): fails if the generated files are stale, then loads the DDL into a real SQLite (node:sqlite) and compares PRAGMA table_info / index_list / index_xinfo / foreign_key_list and the stored CHECKs against the Postgres metadata: tables, columns, storage types, nullability, DB defaults, PKs, unique columns, FKs + actions, indexes (unique, columns, expression text, partial predicate) and checks. Also checks CHECK/STRICT/NOT NULL enforcement on a real row.
  • Adds tsx (packages/db) and miniflare (packages/api, same version wrangler already pins; used by the next PR) to devDependencies; lockfile edited for the two importers only.

Verification

  • pnpm --filter @forgegraph/db test: 250 passed (148 parity cases).
  • pnpm --filter @forgegraph/db typecheck: clean. db:sqlite:check: up to date.
  • Mutating a generated column (dropping NOT NULL, removing a partial-index predicate) fails both the freshness test and the deployments DDL parity case.
  • All 527 generated statements load into Miniflare's local D1 (workerd) as STRICT tables.

Follow-ups in this issue: D1 test DB behind the test helpers (FG_TEST_DB=d1), a non-required CI lane with a known-failures list, the remote D1 feature probe, and the staging cron.

🤖 Generated with Claude Code

FORGE-89 (D1 Phase 4, parent FORGE-69), step 1 of 5: the sqlite-core schema and its parity test. ## What - `packages/db/src/schema-sqlite/`: drizzle sqlite-core tables for all 142 Postgres tables, plus enum compat exports (`fooEnum.enumValues`), constants, type re-exports and relations, so the module exports exactly the same names as `@forgegraph/db/schema`. - **Generated, not hand-kept**: `packages/db/scripts/generate-sqlite-schema.ts` (`pnpm --filter @forgegraph/db db:sqlite:generate`, `db:sqlite:check`). It reads drizzle's own table metadata (`getTableConfig`, relations), so it handles this schema's standalone-call style that `scripts/pg-to-d1.mjs` cannot, and it fails on anything it can't translate (unknown column types, Postgres-only SQL in checks/indexes, unlisted default functions, serial columns without a unique-index entry). - Translation rules (design doc "Schema translation rules"): pgEnum → `text({ enum })` + `CHECK (col IN (...))` from the shared `@forgegraph/db/enums` arrays; json/jsonb/arrays → `text({ mode: "json" })` typed as the pg column; timestamptz → `integer({ mode: "timestamp_ms" })` with an epoch-ms SQL default for `defaultNow()`; boolean → `integer({ mode: "boolean" })`; serial `deployment_events.seq` → integer + `deployment_event_seq_unique_idx` (same name as FORGE-85 #712; skipped once #712 adds it on Postgres); numeric/real → real; the 7 partial and 3 expression indexes, checks (`btrim` → `trim`) and all 255 FKs with their actions are kept. - `schema-sqlite/ddl.ts`: drizzle-kit DDL with every table made `STRICT` (wrong storage classes fail at write time; PK columns are NOT NULL). The D1 test lane (next PR) and the Phase 5 baseline reuse it. - `schema-sqlite/parity.test.ts` (runs in the existing unit-web lane): fails if the generated files are stale, then loads the DDL into a real SQLite (`node:sqlite`) and compares PRAGMA `table_info` / `index_list` / `index_xinfo` / `foreign_key_list` and the stored CHECKs against the Postgres metadata: tables, columns, storage types, nullability, DB defaults, PKs, unique columns, FKs + actions, indexes (unique, columns, expression text, partial predicate) and checks. Also checks CHECK/STRICT/NOT NULL enforcement on a real row. - Adds `tsx` (packages/db) and `miniflare` (packages/api, same version wrangler already pins; used by the next PR) to devDependencies; lockfile edited for the two importers only. ## Verification - `pnpm --filter @forgegraph/db test`: 250 passed (148 parity cases). - `pnpm --filter @forgegraph/db typecheck`: clean. `db:sqlite:check`: up to date. - Mutating a generated column (dropping NOT NULL, removing a partial-index predicate) fails both the freshness test and the `deployments` DDL parity case. - All 527 generated statements load into Miniflare's local D1 (workerd) as STRICT tables. Follow-ups in this issue: D1 test DB behind the test helpers (FG_TEST_DB=d1), a non-required CI lane with a known-failures list, the remote D1 feature probe, and the staging cron. 🤖 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>
gmackie force-pushed FORGE-89-sqlite-schema from 4d40c5ac0b
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
to a29afd2f36
All checks were successful
CI / gitleaks (pull_request) Successful in 20s
CF executor smoke / smoke (pull_request) Successful in 23s
CI / unit-api (pull_request) Successful in 2m37s
CI / unit-web (pull_request) Successful in 3m59s
CI / storybook (pull_request) Successful in 2m29s
CI / web-build (pull_request) Successful in 2m54s
CI / go (pull_request) Successful in 1m8s
CI / checks (pull_request) Successful in 3m3s
CI / database (pull_request) Successful in 3m41s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 39s
2026-10-12 06:23:53 +00:00
Compare
Sign in to join this conversation.
No description provided.