Skip to content

TML-3727: A database records whether db update may move it, and db migrate, db update and db sign obey it - #30926

Merged
wmadden merged 111 commits into
mainfrom
tml-3727-policies
Nov 17, 2026
Merged

wmadden merged 111 commits into
mainfrom
tml-3727-policies

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes TML-3727.

A ref can now say how its database may change, and the database records it:

// migrations/app/refs/production.json
{
  "hash": "4b1c…e09a",
  "invariants": [],
  "policies": { "change": ["migrate"] }
}
$ prisma db migrate --to production --db $PROD
✘ [MIGRATION.POLICY_MISMATCH] The ref's policies differ from the database's
  why: production declares change: migrate; the database has change: migrate, reconcile.
→ Make the ref match the database, or have whoever owns the database apply the ref's policies.

$ prisma db migrate --to production --permit-policy-change --db $PROD
✔ Policies updated: change: migrate

$ prisma db update --db $PROD
✘ [MIGRATION.CHANGE_POLICY_FORBIDS] This database's change policy permits only migrations
  why: The marker records change: migrate, so db update may not move it.
→ Author a migration with prisma migration plan, then apply it with prisma db migrate.

The decision

Every database records two policies in its application space's marker, under meta.policies:

  • a change policy: may it be moved by walking the migration graph (migrate: db migrate, a Composer deploy), by reconciling it to a contract (reconcile: db update, db sign), or both;
  • a data protection policy: dataLoss and accessWidening, each …-forbidden, …-with-consent or …-permitted. This PR stores and compares these; enforcing them is the next slice (TML-3728).

A marker with no policies takes the defaults, which are today's behaviour, so existing databases are unaffected. The decision record is ADR 320.

How it works

Refs declare, the marker records. A ref may declare any of the settings. db migrate --to <ref> and db update --to <ref> compare each declared setting with the marker before any DDL. A difference fails with MIGRATION.POLICY_MISMATCH. With --permit-policy-change, the run is checked against the ref's policies and writes them into the marker when it succeeds: inside the run's transaction on Postgres, SQLite and MySQL, and in the application marker's compare-and-swap on MongoDB. A run with nothing else pending writes only the policies. The flag needs --to <ref> and is refused with CLI.INVALID_ARGUMENTS without one; a ref that declares nothing is accepted and changes nothing. No error or advice offers the flag unless the user gave it in the command being repeated. Every command that rewrites a ref file now keeps the fields it did not change, so invariants and policies are never erased.

Enforcement. db update and db sign need reconcile; db migrate needs migrate. A refusal is MIGRATION.CHANGE_POLICY_FORBIDS. A database with no marker has no policy, so adoption with db sign and db init are never refused.

Concurrency. The runner compares the policies the command checked with the marker before it applies the first space: under its lock on Postgres and MySQL, inside its exclusive transaction on SQLite, and on MongoDB again in the application marker's write, which matches on the policy settings rather than their stored form. A concurrent change fails the run with MIGRATION.MARKER_CAS_FAILURE, whose reason says what moved.

The off-graph error follows the policy. MIGRATION.MARKER_MISMATCH (from db migrate) and the migration status warning now give only the recovery the change policy permits: db update where reconcile is allowed, otherwise authoring migrations. migration status --to <ref> reports policy differences without failing.

Printed commands work as printed. Advice that repeats a run (db migrate, db update, db sign) repeats the flags the user gave, including --to <ref> and --permit-policy-change. When a refusal comes from the ref's own declaration, the error names the ref and prints the command that works.

Nothing goes in the ledger. The ledger is the journal of applied migrations (ADR 228); a policy change is not one.

Corrupt policies. A meta.policies value that cannot be read is CONTRACT.MARKER_ROW_CORRUPT, advising the database's owner to correct it and never to delete the marker. A ref file with an invalid policies value fails as MIGRATION.INVALID_REF_FILE, naming the file and the field.

Where the code is

  • Framework: the policy types, value lists, meta key, parser, comparison and the runner's structured failure in packages/1-framework/1-core/framework-components/src/control/database-policies.ts; refs keep their fields in packages/1-framework/3-tooling/migration/src/refs.ts. Database policies are a new framework concept; this was agreed as an exception to the framework-domain rule.
  • Control API and CLI: the checks in migrate, dbUpdate and db sign, the flag, both error codes, migration status, db migrate --show.
  • Targets: marker writes and the under-lock comparison on Postgres, SQLite, MySQL (whose marker upsert no longer resets meta) and MongoDB.
  • Docs: ADR 320, status notes on ADRs 021, 218 and 307, the glossary, the Migration System doc, the error reference, the prisma-8 skill, and upgrade notes for extension authors and the control API's callers.

Tests

Integration journeys run on Postgres, SQLite, MySQL and MongoDB: a mismatching ref fails before any DDL; with the flag the policies and migrations land in one run; a policy-only run writes the marker and no ledger row; a failed run leaves the policies unchanged; db update and db sign are refused on a migrations-only database; a ref without policies records none; corrupt policies and invalid ref files fail with their own errors.

Alternatives considered

  • Better error and skill text only. Rejected: prose is what failed in the incident that started this project, where an agent proposed resetting a shared development database.
  • A single "protected" flag. Rejected: a database whose data matters can still be one that may be reconciled.
  • The marker names its ref, and the ref holds the rules. Rejected: the database could not be read without a checkout.
  • A deploy always applies its ref's policies. Rejected: it would override the policy it enforces.
  • A ledger row for each policy change, or a separate db policy set command. Rejected: the ledger holds applied migrations, and a pipeline can only deploy what is committed.

Agent: eratosthenes-31, aslan-59

Summary by CodeRabbit

  • New Features
    • Database change and data-protection policies can be declared in refs and recorded with the database. Data-protection settings are compared but are not yet enforced.
    • db migrate and db update check ref policies against database settings. Use --permit-policy-change to approve applying declared policy changes; policy restrictions also apply to db sign.
    • Migration status reports policy differences in text and JSON output, and warns when recorded policies cannot be read.
    • Updating refs preserves existing policies and invariants.
  • Bug Fixes
    • Policy errors provide clearer explanations and recovery guidance, including options shaped by the database’s change policy.
    • Concurrent policy changes are detected before operations proceed.

wmadden-electric and others added 30 commits October 30, 2026 14:13
…e ledger row and writes policies on success

The project spec no longer appends a ledger row for a policy change; the ledger is the journal of applied migrations. Policies are written when a run succeeds, so a failed MongoDB deploy never leaves a loosened policy behind. The decisions the orchestrating agent made, rather than Will, are listed with their reasons.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…icy; glossary and skill document them

The decision record states the two policies, the ref-declares / marker-records split, the agreement rule with --permit-policy-change, the defaults and the alternatives. ADR 021 and ADR 218 point at it. The prisma-8 skill explains the policies, both new error codes and the flag, and tells agents never to loosen a policy to get a deploy past a refusal.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…s, policiesOf and policyDifferences

A database's marker can record a change policy and a data protection
policy in meta.policies. policiesOf reads them, fills absent settings
from DEFAULT_POLICIES, and treats an invalid value as an unreadable
marker. policyDifferences compares a ref's declaration with the
policies in force, setting by setting. Runner per-space options gain
databasePolicies, which the marker update records.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
A ref file may carry a policies field, validated when the ref is read;
an unknown key or value fails as MIGRATION.INVALID_REF_FILE. advanceRef
points a ref at a new hash and keeps its invariants and policies.
migration ref set, ref advancement after db init, db update and
db migrate, and db sign's extension-space refs all go through it, so
none of them erases what a person wrote. ref set reports the
invariants and policies it wrote.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…nd no marker write loses meta

The runner's per-space options take databasePolicies. When given, the
marker write at the end of the run sets meta.policies, inside the run's
transaction, so a failed run leaves the recorded policies as they were.
A run with no migrations and policies to record still writes the
marker, and writes no ledger entry.

initMarker's upsert used to overwrite meta with {} when a row existed;
now it leaves meta alone unless it records policies, and updateMarker
does the same.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…no longer resets meta

The MySQL runner passes databasePolicies to the marker write it makes
in the transaction that also writes the ledger. The upsert used to
set meta to '{}' on every write; now it keeps meta and sets
meta.policies only when it records policies. The compare-and-swap
update reads meta with the locked row and writes it back with the new
policies. A run with no migrations and policies to record writes the
marker and no ledger entry.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…update

The Mongo runner passes databasePolicies to the marker write it makes
after the last migration of a space, so a run that fails part way
leaves the recorded policies as they were. updateMarker sets
meta.policies and leaves the rest of meta; initMarker records them on
a first run. A run with no migrations and policies to record writes the
marker and no ledger entry.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
RunMigrationInputs gains databasePolicies. runMigration passes them in
the application space's runner options only, since a ref declares the
policies of the application space.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ent to a policy change

MigrateOptions replaces refHash, refInvariants and refName with ref,
the target --to named: its hash and invariants, and for a ref its name
and the policies it declares. It gains permitPolicyChange. DbUpdateOptions
gains the same two fields. db migrate and db update --to pass the
entry they resolved.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…builders and reference entries

POLICY_MISMATCH says, setting by setting, how a ref's declared policies
differ from the database's, and that the ref or the database must
change. CHANGE_POLICY_FORBIDS says the marker's change policy does not
permit the command, and gives the one recovery it does permit: author a
migration, or run db update.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… ref's policies with consent

Before planning, executeMigrate reads each space's policies from its
marker. A ref whose declared policies differ refuses the run with
MIGRATION.POLICY_MISMATCH unless permitPolicyChange consents; with it,
the run records the ref's declarations laid over the policies in force.
A space whose change policy lacks migrate refuses with
MIGRATION.CHANGE_POLICY_FORBIDS. When nothing is pending but policies
are to be recorded, the runner runs the application space with no
migrations so the marker update records them; the result says
policiesUpdated and its summary names the settings. An invalid
meta.policies is reported as an unreadable marker of its space.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… and applies alike

A space whose change policy lacks reconcile refuses db update with
MIGRATION.CHANGE_POLICY_FORBIDS, and a ref whose declared policies
differ refuses it with MIGRATION.POLICY_MISMATCH unless consent is
given; both before the planner runs, so a dry run shows the same
refusal an apply would. With consent, the apply records the policies
and its summary names the settings.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signing moves a marker without a migration, so a space whose marker
records a change policy without reconcile refuses db sign with
MIGRATION.CHANGE_POLICY_FORBIDS before anything is introspected or
written. A space with no marker is adopted as before.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The flag consents to writing the ref's declared policies into the
database. db migrate checks the policies before it plans, as the
control API does, so a refusal comes before any other work, and its
--json result carries policiesUpdated. Test markers now carry meta, as
every marker the database returns does.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…e policy permits

The error takes each space's change policy in force. When it includes
reconcile, the next actions are the two db update commands; the
"Unless this database changes only through migrations" choice and the
"Otherwise plan a migration" one are gone. When it does not, the one
next action is to author migrations back onto the graph and deploy
them. meta gains change. db migrate, db migrate --show and the
control API's migrate read the policies before they plan.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…rom the database's

With a database, after the applied and pending report, a differences
block lists each setting the ref declares that differs from the
marker's, and the summary line ends "; policies differ from <ref>".
--json carries policyDifferences. The exit code is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…rs record databasePolicies

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
An apply that recorded the ref's policies returns the settings it
changed, so Composer and --json readers see the same field on both.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
db migrate --to <ref> refuses a ref whose policies differ before any
migration runs, applies it with --permit-policy-change, and records
policies with no migration to run and no ledger entry. migration status
lists the differing settings. A database that changes only through
migrations refuses db update, its dry run and db sign; a database with
no marker is still signed; db update --advance-ref and migration ref set
keep a ref's policies and invariants; a database with no policies
behaves as before. The journeys are shared helpers each target's file
runs.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…and the off-graph advice follows the change policy

A run with --permit-policy-change whose migration fails rolls back, and
the marker keeps the policies it recorded before. A database whose change
policy permits only migrations, with a marker left by a deleted migration,
gets MIGRATION.MARKER_MISMATCH with the single migrations-only recovery.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The shared journeys run on MongoDB: db update, its dry run and db sign
are refused on a migrations-only database, a database with no marker is
signed, ref writers keep policies and invariants, and a database with no
policies behaves as before. A run whose second migration fails leaves
the recorded policies as they were.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The shared journeys run against MYSQL_URL and skip when it is not set.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Policies belong to the database and live on the application space's marker; refs passed to the control API carry a required name; a corrupt meta.policies is CONTRACT.MARKER_ROW_CORRUPT; change holds at least one strategy; a policy-only run writes no ledger row; the runner compares the checked policies under its lock; re-run advice repeats the flags the user gave.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…kspace root

The test package names the workspace packages, and the single-import-root
check refuses a test file that also names a published one. The journey
config imports @internal/mongo/config.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…e Migration System doc describe the policies

Main took ADR numbers 309 to 313 while this branch was open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…er, and the runner compares them under its lock

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…, a result-returning parser, and the meta key

The policy types, the defaults, the comparison and the overlay of a
declaration move to framework-components/control/database-policies.ts
and say "database policies" in their names. Each value list is one
constant array the unions and the arktype schemas derive from, with a
type test that the schemas infer the interfaces. A change policy holds
at least one strategy. databasePoliciesOf returns a Result rather than
throwing a half-built error; the framework also exports the meta key,
the stored form, markerHoldsDatabasePolicies for a runner's check under
its lock, and MarkerDestination, the type of what a marker write
records. Runner options gain expectedDatabasePolicies, and a space to
sign gains the same. Refs take the schema and gain NamedRef.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…lock, and Postgres merges meta in the statement

Each runner now decides the marker write and the ledger write apart:
the marker is written when anything changed or policies are given, and
a ledger row only when a migration edge was applied, so a db update
that only records policies writes no row. After reading the marker
under its lock (inside the transaction on SQLite, under GET_LOCK on
MySQL), a runner given expectedDatabasePolicies fails with
MIGRATION.MARKER_CAS_FAILURE when the marker no longer records them.
MongoDB checks before its first operation and narrows the marker's
compare-and-swap to the policies it read. db sign does the same in
each family's signSpaces.

Postgres sets meta.policies with jsonb_set in the update, never by
writing back a meta it read. SQLite and MySQL merge inside their
exclusive transaction. Every target stores the framework's serialised
form under its meta key, and the family and target signatures take the
framework's MarkerDestination.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… refusals that say where the policy came from

The policies are the database's: they are read once from the
application space's marker, and an invalid meta.policies is raised once
as CONTRACT.MARKER_ROW_CORRUPT with the space and the marker location.
The control API is the only place that checks them; db migrate no longer
runs its own check, and db migrate --show previews the refusal the run
would raise. A refusal caused by a ref's declaration under
--permit-policy-change names the ref and its strategies.

MigrateOptions and DbUpdateOptions take ref: NamedRef, with a required
name; migrate takes targetHash for a --to that names a contract, and
dbUpdate fails as an internal error when its ref names another contract
than the one it reconciles to. TargetRef is gone. The results carry
databasePoliciesUpdated, and a difference names the ref file's key
path. Re-run advice repeats --permit-policy-change when the user gave
it. DbSignOptions takes the invocation, so its refusal repeats --config.
The runners receive the policies the check used, to compare under their
lock. One formatter prints policy values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
CONTRACT.MARKER_ROW_CORRUPT names an invalid meta.policies as a cause.
CHANGE_POLICY_FORBIDS and POLICY_MISMATCH describe the database-wide
policy on the application space's marker, the ref-declared wording and
the key-path settings. The app upgrade note covers NamedRef, targetHash,
databasePoliciesUpdated and dbSign's invocation; the extension note
covers MarkerDestination, expectedDatabasePolicies and the Mongo
compare-and-swap argument.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…te fields

Callers joined the path from the message, which repeated it as arktype writes it. The parser now returns the problem without the path, and the ref reader and the marker reader each join them.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The advice was one schema line of about 400 characters. When a key of policies is wrong, the fix now names it and, for a wrong value, lists the allowed values, such as Set policies.change[0] to "migrate" or "reconcile". A key a declaration does not have gets Remove policies.<key>. Other invalid ref files get a short shape.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… the way to apply the migration

"then deploy it" named no command. The advice now says to apply the authored migration with db migrate, with the --config and --db the command was given.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Approved PR #30483 takes 319.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…an, which MDX needs

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…fter merging main

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The ADR index conflicted: main added ADR 319 on the line where this branch added ADR 320. Both are kept.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The SQL family's package.json conflicted: main moved every workspace dependency to 8.0.0-rc.24 where this branch added @internal/errors. The dependency is kept at the new version.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… repeats them

The advice after the policies moved during db sign printed plain db sign, which signs the emitted contract rather than the --contract the user named. The command now builds --contract, --advance-ref and --no-advance-ref once, passes them as rerunArgs, and its own retry commands use the same list.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…not its hash

db migrate --to production --permit-policy-change on a database that permits only db update advised db update --to <hash>, which records none of the policies the user consented to. The advice now names the ref and repeats --permit-policy-change only when the user gave it, for db update and for the db migrate that applies an authored migration.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… runs and records none

The control API tests for migrate and dbUpdate, and the shared journey on every target, now run the flag against a policy-less ref, so a check tightened to "a ref with policies" would fail.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… of its own options

A direct migrate or dbUpdate call was told to pass --to <ref> and saw meta.command db migrate, a command it never ran. The CLI commands keep the flag wording and meta.command; the control API names permitPolicyChange and its target or ref option, with the same code and the to key.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…nations the engine cannot see

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
policyIssueFix matched the declaration's keys by hand. Its fixes are now records typed over DATABASE_POLICIES_KEYS and DATA_PROTECTION_KEYS, so a new key does not compile until it has its advice. The change advice now says its items are "migrate" or "reconcile", which no longer reads as both being required.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… and that the flag needs a ref

No error or advice offers the flag unless the user gave it in the command being repeated. Without a ref the flag fails with CLI.INVALID_ARGUMENTS; a ref that declares nothing is accepted and changes nothing.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…iew and QA rounds

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-prisma__orm that referenced this pull request Nov 17, 2026
…son still gives json, and sql.String(n) is now sql.Varchar(n) (prisma#31065)

```prisma
model Event {
  id      Int    @id
  payload Json                  // a json column, as before
  search  Json(storage: jsonb)  // a jsonb column
  code    sql.Varchar(12)       // was sql.String(12)
}
```

## What changes for a user

**`Json` is a convenience type on PostgreSQL.** It has one option,
`storage`:

| Written | Explicit type | Column |
| --- | --- | --- |
| `Json` or `Json(storage: json)` | `PlainJson` | `json` |
| `Json(storage: jsonb)` | `Jsonb` | `jsonb` |

- No schema, contract or database changes. A field written `Json` keeps
its `json` column. `pnpm fixtures:check` shows no difference.
- `contract infer` and `contract print` write a `json` column as `Json`
and a `jsonb` column as `Json(storage: jsonb)`. They wrote `Jsonb`
before. `Jsonb` remains valid.
- The Prisma 7 reader writes Prisma 7's `Json`, which was `jsonb`, as
`Json(storage: jsonb)`, and `Json @db.Json` as `Json`.
- In a TypeScript contract, `type.Json()` and `type.Json({ storage:
'jsonb' })` are typed as the explicit type they choose. The column
helper `jsonColumn` is renamed `plainJsonColumn`, with no alias.
- The reference docs say what a `json` column cannot do (equality
filters, indexes, unique constraints) and that `Json(storage: jsonb)`
can. A new integration test shows the index and unique limits against
PostgreSQL.

**`sql.String(n)` is renamed `sql.Varchar(n)`** on PostgreSQL, SQLite
and MySQL. `sql.String` stays for one release line as a deprecated
alias: in PSL it warns `PSL_DEPRECATED_SCALAR_NAME` and emits the same
contract; in TypeScript it still compiles.

Both renames have upgrade instructions with detection patterns in
`upgrade-instructions/pending/postgres-json-convenience-type/`.

## Why

- **`Json` defaults to `json`.** `Json` is the exact name of a
PostgreSQL type, so it must not mean a different one. A user who wants
`jsonb` says so.
- **`PlainJson` exists because a convenience type and an explicit type
cannot share a name.** A type choice names its explicit type in the same
namespace. Users meet `PlainJson` in hover text, the type reference, and
the refusal of `@db.Json`. The tools never write it.
- **`sql.String` shared a name with the `String` convenience type and
meant something else** (a `varchar(n)` column). The namespace is now
`sql.Varchar(n)` and `sql.Char(n)`.

## ADRs

- ADR 316 gains `Json`, the rule for naming an explicit type whose
natural name is a convenience name, the reason `BigInt` is kept as its
own concept, and the `sql.Varchar` rename. ADR 257 and ADR 279 are
amended to match.
- Main had two ADRs numbered 316. The one about an extension pack's
extension and the search path is now ADR 321, in its own commit
(`f1f5d2a83b`). ADR 320 is taken by prisma#30926.

## Also in this pull request

`projects/scalar-convenience-types/spec.md` and `plan.md` hold the
design for the rest of the project: the same eight convenience types on
MongoDB, SQLite and MySQL, each in a later pull request.

## Verification

- `/drive-code-review` (system design and code passes), two rounds; the
second is clean on the pushed tip, with all acceptance criteria for this
change passing.
- Manual QA with the real CLI against a local PostgreSQL: emit,
refusals, `db init`, `contract infer`, `contract print` round-trip, `db
verify`, a TypeScript contract, and the Prisma 7 reader.
- No source file under `packages/1-framework` changes.

## Known gaps, tracked separately

- `Json @unique` or an index on a `Json` field fails only when the
migration runs, with PostgreSQL's own message, which does not mention
`Json(storage: jsonb)`. TML-3833 covers a clear error.
- The TypeScript compiler suggests `jsonbColumn` for the removed
`jsonColumn`. The upgrade instruction names the right replacement.

## Alternatives considered

- **Default `Json` to `jsonb`.** Built in prisma#30966 and closed: it changed
every existing `Json` column and made a PostgreSQL type name mean
another type.
- **No convenience type for `Json`.** Leaves no way to say "a JSON
value" and choose its storage, which every other scalar now has.
- **Name the explicit type `JsonJson`**, by the value-suffix rule. It
says nothing.
- **Rename `sql.String` inside the SQLite pull request.** PostgreSQL is
the released target it affects, so it ships here.

Closes TML-3789.

Agent: genghis-36


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* PostgreSQL `Json` now supports `json` storage by default and `jsonb`
via an explicit storage option. The explicit `json` type is named
`PlainJson`.
* Variable-length SQL text types are now named `sql.Varchar`;
`sql.String` remains available as a deprecated alias.
* The PostgreSQL untyped JSON column helper is now named
`plainJsonColumn`.

* **Documentation**
* Updated reference materials and migration guidance to explain the new
names, storage choices, and JSON limitations, including unsupported
equality filtering, indexing, and unique constraints for `json`.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Keeps both sides in the MongoDB runner and the ref operations. The advice that tells the user to resolve the cause and run db migrate again repeats the flags the user gave.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden
wmadden enabled auto-merge November 17, 2026 22:43
The ADR index keeps both new steering-list entries.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden
wmadden added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit 7163eaa Nov 17, 2026
25 checks passed
@wmadden
wmadden deleted the tml-3727-policies branch November 17, 2026 23: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.

2 participants