FORGE-88: atomic() adapter for D1-portable writes (Phase 3 step 1) #720
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
gmackie/ForgeGraph!720
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "FORGE-88-atomic-adapter"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
FORGE-88 step 1 of 7 (FORGE-69 D1 migration, Phase 3): the
atomic()adapter every later concurrency rewrite lands on.Issue: https://tasks.gmac.io (FORGE-88). Design: docs/plans/2026-10-11-forgegraph-d1-migration.html, "Concurrency patterns" + Phase 3.
What
@forgegraph/db/atomicexportsatomic(db, (b) => [...]). On Postgres it is one transaction that executes exactly the pre-built statements, in order, with no interleaved reads. On any drizzle database withbatch(D1) it isdb.batch(). Result types are inferred per statement for both dialects.assertInBatch(b, predicate, label)aborts the whole batch. It inserts(0, label)into the new, always-emptybatch_asserttable only when the predicate is false or NULL;CHECK (ok = 1)rolls the batch back on both PG and D1. atomic() rethrowsAtomicAssertionError { code: "atomic_assertion_failed", label }.chunkByParameters()splits multi-row inserts;inList()passes long IN lists as one JSON parameter (json_eachon SQLite,jsonb_array_elements_textwith a column-type cast on PG).batch()crashes on parameterized raw statements (db.run(sql\…`)`, "reading 'bind'"), which nearly every GUARD rewrite uses.@forgegraph/db/testing/d1-sqlite: a D1 stand-in onnode:sqlitedriven through the realdrizzle-orm/d1driver. It reproduces batch atomicity, D1's 7500 rejection of BEGIN/SAVEPOINT, the 100-parameter limit, D1 bind rules and always-on FKs. No new dependency. FORGE-89's SQLite lane can reuse it.0118_batch_assert.sql: additive and idempotent, with the grant to forgegraph_dbcooper. I'll apply it to production right after merge withfg db migrate. That is required so the drift gate stays green.Not changed:
packages/api/src/ci-control/atomic.ts(FORGE-77's local copy) stays as is to avoid a cross-agent conflict; it can switch to this shared adapter in a follow-up.Tests
packages/db/src/atomic.test.ts(13, D1 shim): ordering/results, rollback on failure, guarded transition token, two concurrent transitions → exactly one effect, assertion abort + label, NULL predicate fails, parameter limit, chunked 250-row insert, json_each IN with text and integer columns, refuses started promises, D1 rejectstransaction().packages/api/src/__tests__/atomic.database.test.ts(7, Postgres): the same contract on PG, including the PG detail-derived label with two asserts in one batch.packages/api/src/__tests__/atomic.concurrency.test.ts(new, real PG): 8 connections race one guarded transition; exactly one commits, 7 fail withAtomicAssertionError, one event row.tscfor db and api clean.Remaining FORGE-88 steps
2 outbox/readiness claims + resource_locks lease; 3 BATCH/READ sites; 4 GUARD sites + bug fixes; 5 RepositoryCoordinator DO; 6 deploy route guards; 7 lint at error.
🤖 Generated with Claude Code
57955a6ab791b301d2d1