FORGE-85: classifyDbError() for Postgres and SQLite constraint errors #706

Merged
gmackie merged 1 commit from FORGE-85-db-errors into main 2026-10-12 06:33:54 +00:00
Owner

Part 4 of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #703.

  • packages/db/src/errors.ts: classifyDbError(err) → { kind: "unique" | "foreign_key", dialect, constraint?, columns? }. Postgres: SQLSTATE 23505/23503 with constraint_name (postgres-js) or constraint (pg). SQLite/D1: "UNIQUE constraint failed: t.a, t.b" (columns), "UNIQUE constraint failed: index 'name'" (expression indexes), "FOREIGN KEY constraint failed" (no name), including D1's D1_ERROR: …: SQLITE_CONSTRAINT wrapper. Walks drizzle's DrizzleQueryError.cause chain. Helpers isUniqueViolation(err, constraint?), isForeignKeyViolation(err).
  • Replaced: pipeline/auto-dispatch.ts, routers/gcp.ts, lib/cron-eval.ts, fg/deploy/route.ts, fg/deploy/[id]/rollback/route.ts, fg/targets/route.ts. (ci-control/drizzle-store.ts is FORGE-77's.)

Behaviour note: gcp.ts, cron-eval.ts and the rollback route checked only the top-level err.code. drizzle 1.0 throws DrizzleQueryError with the driver error on .cause, so those three never matched a wrapped violation; they now do (existing binding returned / window skipped / 409 instead of a 500). auto-dispatch and targets already walked cause and are unchanged.

Verification

  • packages/db/src/errors.test.ts (8): real node:sqlite errors (column, expression index, FK), D1 messages, postgres-js / node-postgres shapes, DrizzleQueryError wrapping, cause loops.
  • packages/api/src/__tests__/db-error-classification.database.test.ts: real drizzle errors from Postgres 16 (unique with constraint name; FK). The FK case is skipped on local PGlite, which doesn't enforce FKs; CI runs real Postgres.
  • Existing: web targets/deploy/cron/lib suites (571 tests), auto-dispatch, rollback-callback, readiness, e2e-pipeline.

🤖 Generated with Claude Code

Part 4 of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #703. - `packages/db/src/errors.ts`: `classifyDbError(err)` → `{ kind: "unique" | "foreign_key", dialect, constraint?, columns? }`. Postgres: SQLSTATE 23505/23503 with `constraint_name` (postgres-js) or `constraint` (pg). SQLite/D1: "UNIQUE constraint failed: t.a, t.b" (columns), "UNIQUE constraint failed: index 'name'" (expression indexes), "FOREIGN KEY constraint failed" (no name), including D1's `D1_ERROR: …: SQLITE_CONSTRAINT` wrapper. Walks drizzle's `DrizzleQueryError.cause` chain. Helpers `isUniqueViolation(err, constraint?)`, `isForeignKeyViolation(err)`. - Replaced: pipeline/auto-dispatch.ts, routers/gcp.ts, lib/cron-eval.ts, fg/deploy/route.ts, fg/deploy/[id]/rollback/route.ts, fg/targets/route.ts. (ci-control/drizzle-store.ts is FORGE-77's.) Behaviour note: gcp.ts, cron-eval.ts and the rollback route checked only the top-level `err.code`. drizzle 1.0 throws `DrizzleQueryError` with the driver error on `.cause`, so those three never matched a wrapped violation; they now do (existing binding returned / window skipped / 409 instead of a 500). auto-dispatch and targets already walked `cause` and are unchanged. Verification - `packages/db/src/errors.test.ts` (8): real `node:sqlite` errors (column, expression index, FK), D1 messages, postgres-js / node-postgres shapes, DrizzleQueryError wrapping, cause loops. - `packages/api/src/__tests__/db-error-classification.database.test.ts`: real drizzle errors from Postgres 16 (unique with constraint name; FK). The FK case is skipped on local PGlite, which doesn't enforce FKs; CI runs real Postgres. - Existing: web targets/deploy/cron/lib suites (571 tests), auto-dispatch, rollback-callback, readiness, e2e-pipeline. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-85: classifyDbError() for Postgres and SQLite constraint errors
Some checks failed
CI / gitleaks (pull_request) Successful in 8s
CI / unit-api (pull_request) Successful in 1m18s
CI / go (pull_request) Successful in 57s
CI / storybook (pull_request) Successful in 1m55s
CI / web-build (pull_request) Successful in 1m57s
CI / database (pull_request) Successful in 1m54s
CI / checks (pull_request) Successful in 3m53s
CI / unit-web (pull_request) Successful in 3m43s
CI / ci (pull_request) Has been cancelled
ee16d942ec
packages/db/src/errors.ts recognises Postgres 23505/23503 (with the
constraint name from postgres-js or node-postgres) and SQLite/D1
"UNIQUE constraint failed" / "FOREIGN KEY constraint failed" messages
(columns, or the index name for expression indexes), searching drizzle's
DrizzleQueryError cause chain. isUniqueViolation(err, constraint?) and
isForeignKeyViolation(err) wrap it.

Replaces the hand-written checks in auto-dispatch.ts, routers/gcp.ts,
cron-eval.ts, fg/deploy/route.ts, fg/deploy/[id]/rollback/route.ts and
fg/targets/route.ts. The gcp, cron-eval and rollback checks read only the
top-level error's code, which drizzle 1.0 moves to .cause, so those three
now catch the wrapped violations they were written for.

Tests: unit tests on real node:sqlite errors and Postgres/D1 shapes; a DB
lane test on real drizzle errors from Postgres.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie force-pushed FORGE-85-db-errors from ee16d942ec
Some checks failed
CI / gitleaks (pull_request) Successful in 8s
CI / unit-api (pull_request) Successful in 1m18s
CI / go (pull_request) Successful in 57s
CI / storybook (pull_request) Successful in 1m55s
CI / web-build (pull_request) Successful in 1m57s
CI / database (pull_request) Successful in 1m54s
CI / checks (pull_request) Successful in 3m53s
CI / unit-web (pull_request) Successful in 3m43s
CI / ci (pull_request) Has been cancelled
to 71aba48e0b
All checks were successful
CI / gitleaks (pull_request) Successful in 9s
CI / storybook (pull_request) Successful in 1m12s
CI / unit-api (pull_request) Successful in 1m23s
CI / checks (pull_request) Successful in 2m16s
CI / unit-web (pull_request) Successful in 1m59s
CI / go (pull_request) Successful in 1m15s
CI / database (pull_request) Successful in 2m2s
CI / web-build (pull_request) Successful in 3m51s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 40s
2026-10-12 04:48:20 +00:00
Compare
gmackie force-pushed FORGE-85-db-errors from 71aba48e0b
All checks were successful
CI / gitleaks (pull_request) Successful in 9s
CI / storybook (pull_request) Successful in 1m12s
CI / unit-api (pull_request) Successful in 1m23s
CI / checks (pull_request) Successful in 2m16s
CI / unit-web (pull_request) Successful in 1m59s
CI / go (pull_request) Successful in 1m15s
CI / database (pull_request) Successful in 2m2s
CI / web-build (pull_request) Successful in 3m51s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 40s
to 8453fef377
All checks were successful
CI / gitleaks (pull_request) Successful in 13s
CF executor smoke / smoke (pull_request) Successful in 17s
CI / unit-api (pull_request) Successful in 3m30s
CI / storybook (pull_request) Successful in 1m32s
CI / unit-web (pull_request) Successful in 4m28s
CI / go (pull_request) Successful in 57s
CI / checks (pull_request) Successful in 3m42s
CI / database (pull_request) Successful in 3m18s
CI / web-build (pull_request) Successful in 4m37s
CI / ci (pull_request) Successful in 50s
2026-10-12 05:26:05 +00:00
Compare
gmackie force-pushed FORGE-85-db-errors from 8453fef377
All checks were successful
CI / gitleaks (pull_request) Successful in 13s
CF executor smoke / smoke (pull_request) Successful in 17s
CI / unit-api (pull_request) Successful in 3m30s
CI / storybook (pull_request) Successful in 1m32s
CI / unit-web (pull_request) Successful in 4m28s
CI / go (pull_request) Successful in 57s
CI / checks (pull_request) Successful in 3m42s
CI / database (pull_request) Successful in 3m18s
CI / web-build (pull_request) Successful in 4m37s
CI / ci (pull_request) Successful in 50s
to 2a2c04da40
All checks were successful
CF executor smoke / smoke (pull_request) Successful in 21s
CI / gitleaks (pull_request) Successful in 20s
CI / unit-api (pull_request) Successful in 3m8s
CI / storybook (pull_request) Successful in 1m57s
CI / go (pull_request) Successful in 1m9s
CI / unit-web (pull_request) Successful in 4m24s
CI / checks (pull_request) Successful in 2m27s
CI / database (pull_request) Successful in 2m38s
CI / web-build (pull_request) Successful in 4m35s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 58s
2026-10-12 06:14:40 +00:00
Compare
gmackie changed target branch from FORGE-85-time-cutoffs to main 2026-10-12 06:33:17 +00:00
Sign in to join this conversation.
No description provided.