Skip to content

feat(server): D1 database core, SQLite migrations and test support - #249

Draft
i098 wants to merge 7 commits into
mainfrom
feat/d1-store
Draft

i098 wants to merge 7 commits into
mainfrom
feat/d1-store

Conversation

@i098

@i098 i098 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Do not merge. Merging feat/d1-store ends MySQL support in main. It is held for the production switch-over (plan section 7). Draft on purpose.

Refs #248 (slices S1a and S1b of the D1 clone plan).

Before

  • server/src/db.ts was a MySQL pool (@effect/sql-mysql2), configured by MYSQL_*.
  • server/src/migrate.ts ran MySQL DDL steps under GET_LOCK.
  • Tests needed a Docker MySQL server (tests/support/mysql-server.ts).
  • ci.yml and changelog.yml ran only for main.

After

  • dbLayer({ url }) builds a SQLite-dialect SqlClient over a DbBackend: node:sqlite (db-sqlite.ts) or the D1 gateway (db-d1.ts). SANCTUM_DB_URL replaces all MYSQL_*.
  • A transaction reads at once, buffers writes and commits in one batch behind a tx_clock guard. A lost race reruns the body (4 times, jittered). A read after a buffered write fails with SqlError. Nested transactions join the parent.
  • increment, isDuplicate, isMissingReference, isCheckViolation and sql-dialect.ts (now, addSeconds, diffSeconds, diffMicros, chunked).
  • migrate.ts applies migrations-d1/NNNN_*.sql to file or memory databases, one transaction per file, ledger table d1_migrations (same table wrangler d1 migrations apply uses).
  • createTestDatabase() returns a migrated shared-cache in-memory database; no global setup, no Docker. mysql-server.ts and pool.test.ts are deleted (the MySQL pool no longer exists).
  • ci.yml and changelog.yml also run for feat/d1-store.
  • mysql2 and @effect/sql-mysql2 removed from server/package.json.

Verification

db.test.ts (12) and migrations.test.ts (7): all pass on Node 24.22.3 (FTS5 included) and on Node 26.12.1. They ran with a temporary vitest config; server/vitest.config.ts no longer has a global setup. tsc shows no error in the files this PR owns.

Known red

  • server/migrations-d1/0001_baseline.sql belongs to slice S0 (PR to main). It is not in this branch yet. migrations.test.ts and every test that calls createTestDatabase() need it: merge main after S0 lands. I tested with a local copy of S0's baseline from fm/sanctum-d1-s0, not committed.
  • Other files still import removed MySQL exports (mysqlErrno, ER_*, hasFullText, MysqlOptions, config.mysql). Slices S2 to S11 own them.
  • Not tested: a real D1 database. The gateway is slice S2.
  • Tests of the old MySQL constraints in db.test.ts ("schema constraints") were removed; the baseline and its parity test (S0) cover the schema.

Ponytail review

One round on my own diff. Cut: the unused bigint branch in the D1 encoder. Kept: executeStream and executeValues (the Connection interface requires them); the nested-transaction shortcut is marked in code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant