Skip to content

Multi-region schema check (Drizzle + Prisma) - #1

Merged
felipeg48 merged 2 commits into
mainfrom
feature/multiregion-check
Nov 24, 2026
Merged

felipeg48 merged 2 commits into
mainfrom
feature/multiregion-check

Conversation

@felipeg48

Copy link
Copy Markdown
Contributor

Faithful port of the Spring starter's startup multi-region locality check (com.cockroachlabs.data.multiregion) to the TypeScript library, for the Drizzle and Prisma adapters. Raw pg has no model metadata, so it is out of scope.

What's new

  • Declarative locality marker (none existed before):
    • Drizzle: crdbLocality(table, { locality: 'GLOBAL' | 'REGIONAL_BY_ROW', regionColumn? }) — a sidecar WeakMap keyed by the drizzle table object; schema enumerated via drizzle-orm is/Table/getTableName.
    • Prisma: PrismaLocalityConfig sidecar ({ Model: { locality, regionColumn? } }); models enumerated via the client's _runtimeDataModel (no doc-comment parsing).
  • Shared check core in @cockroachdb/core (multiregion.ts): live-schema introspection, the diff, the report, and fix-up SQL (reuses ddl.ts setPrimaryRegion/addRegion/setLocality*/ident).
  • Trigger: opt-in await verifyMultiregionSchema(poolOrClient, schemaOrConfig, { mode }).

Faithful semantics (mirrors Spring)

  • Modes none | warn | validate, default warn. A contradiction (global-on-RBR, RBR-on-global, both markers, or entities disagreeing on one table) fails startup even in warn; none is off.
  • Reads only, via SHOW + information_schema (never crdb_internal). Skips quietly when not CockroachDB. Both directions: declared-vs-table, and table-is-MR-but-entity-unmarked. Not-multi-region-but-markers is reported once with ALTER DATABASE SET PRIMARY REGION / ADD REGION.

Tests

  • Unit (core/multiregion.test.ts): every diff branch, all contradiction paths, identifier quoting, regionalByRowColumn parsing, report formatting, and a teeth-check that contradictions fail in warn.
  • Integration (pg/multiregion.integration.test.ts): gated on CRDB_URL; CI runs it in the integration job. Tolerates fresh-schema timing by polling SHOW TABLES.

Notes

  • One intentional deviation from Spring byte-output: fix-up DDL reuses ddl.ts, which always-quotes identifiers ("table") and uses 'region' literals — valid, keyword-safe CockroachDB, not Spring's conditional quoting.
  • Not wired into CrdbPool.connect (left opt-in via the verify function).
  • No version bumps (lead handles release).

Port the Spring starter's startup locality check to TypeScript. Adds a
declarative locality marker per adapter and a shared check core.

- core/multiregion.ts: live-schema introspection (SHOW DATABASES /
  SHOW REGIONS FROM CLUSTER / SHOW TABLES / information_schema.columns,
  no crdb_internal), diff, report, and fix-up SQL reusing ddl.ts.
  Modes none|warn|validate (default warn); a contradiction fails even in
  warn; none and non-CockroachDB skip. Reads only.
- drizzle: crdbLocality(table, { locality, regionColumn? }) marker +
  verifyMultiregionSchema(pool, schema, { mode }).
- prisma: PrismaLocalityConfig sidecar + verifyMultiregionSchema(client,
  config, { mode }), enumerating models via the runtime data model.
- Unit tests for every diff branch, contradictions, and quoting; a gated
  live integration test; CI runs it with CRDB_URL.
Bump core, drizzle, prisma, pg, ai, testing, continuum to 0.2.0 and inter-package
refs to ^0.2.0 so the single-version Pages feed stays resolvable (caret 0.1.0 cannot
match 0.2.0). Bump examples/ideas-board pins (pg/ai/continuum/testing) to ^0.2.0 in
lockstep so the workspace resolves. cdc unchanged (no cross-package deps).
@felipeg48
felipeg48 force-pushed the feature/multiregion-check branch from 970bd90 to 2b2dbec Compare November 24, 2026 20:26
@felipeg48
felipeg48 merged commit a614b92 into main Nov 24, 2026
2 checks passed
@felipeg48
felipeg48 deleted the feature/multiregion-check branch November 24, 2026 20:27
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