Skip to content

TML-3477: migration plan asks before a lossy type change, and writes slots to fill only for --convert and --backfill - #30695

Merged
wmadden-electric merged 119 commits into
mainfrom
tml-3477-convert-backfill
Oct 14, 2026
Merged

wmadden-electric merged 119 commits into
mainfrom
tml-3477-convert-backfill

Conversation

@wmadden-electric

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

Copy link
Copy Markdown
Contributor

A user changes User.age from String to Int on Postgres:

$ prisma migration plan --name age-to-int
✖ [CLI.CONSENT_REQUIRED] 1 subject needs a statement, and the session is not interactive.
  why: Alter type of "User"."age" to int4 would change the type of field "User.age" and can change or lose its values.
→ Pass --convert User.age
→ Pass --delete User.age

$ prisma migration plan --name age-to-int --convert User.age

migration.ts then has one slot to fill:

this.alterColumnType({
  table: 'User',
  column: 'age',
  type: 'int4',
  using: placeholder('User.age:convert'),
}),

The user writes using: 'trim("age")::integer', runs node migrations/app/<dir>/migration.ts, and applies it with prisma db migrate.

Until now, migration plan wrote a migration with placeholders on its own for every lossy type change and every required field added to an existing table. A user who did not want them got a migration they could not apply until they edited it. On SQLite, db update could not add a required field to a table with rows at all.

The decision

migration plan writes a slot to fill only when a flag asks for it. Two new flags ask:

  • --convert User.age answers a lossy type change with your own conversion.
  • --backfill User.email fills the existing rows of a field that became required with your own values.

Everything else is decided without a placeholder:

Change in the schema What happens
Type change that loses values, such as String → Int Refused until answered: --convert, or --delete
Type change that keeps every value, such as Int → String or VarChar(10) → VarChar(20) Planned, nothing asked
--delete on a type change The database's own cast runs. A value it cannot cast fails the migration, nothing changes, and the advice names --convert
Required field added to an existing table Existing rows get a temporary value ('', 0, …). The output says so and names --backfill
Optional field made required SET NOT NULL. If rows hold NULL, the failure counts them and says what to do
--convert or --backfill on db update Refused: db update has no file to fill. The error prints the migration plan command

Postgres and SQLite behave the same.

How it works, step by step

  1. Questions know why data is lost. Each data-loss entry says drop or typeChange. A type-change question offers convert and delete; a drop question offers rename and delete. --convert and --delete on the same field are refused as conflicting.
  2. Statements reach the planners. ResolvedMigrationStatement gains kind: rename | convert | backfill. Convert and backfill reach the planner strategies as destination coordinates. A converted change is classed data, so it is not asked about again.
  3. Postgres. alterColumnType takes plain options, { table, column, type, using? }; using holds the convert slot, named User.age:convert. Its check after applying compares the column's type text, derived from type the same way by the planner and in migration.ts, so a re-run reproduces ops.json. The temporary value is a real call, addColumnWithTemporaryDefault, instead of an empty rawSql(...).
  4. SQLite. A table rebuild takes a copy expression per column. --convert puts its slot (User.age:convert) there, and a key that names no column is refused; the temporary value is a copy expression too. SQLite does not cast on a rebuild, so a rebuild of a retyped column without --convert checks every value's storage class and fails naming the field if one does not fit.
  5. SQL values. using, temporaryDefault and copyExpressions take a string or a sql template value, like every other SQL text in migration.ts. Their text is checked with each database's quoting rules.
  6. Failures say what to fix. When a column cannot become required, the error counts the rows without a value (TML-3517). A backfill whose run leaves rows without a value fails counting them and naming its step, on both databases. Each failure prints advice that ends in an applied migration when followed as printed.

Upgrade notes

  • App: a lossy type change now asks; a new required field gets a temporary value without asking, listed in the JSON output as temporaryValues (always present, empty when none); --convert and --backfill exist.
  • Hand-written migrations: alterColumnType takes { table, column, type, using? }. A call with the old options fails naming the shape it now takes. The upgrade instructions show the rewrite.

How it was checked

Built in six steps, each reviewed by someone running the code on PGlite and SQLite. Then an architecture review and a code review of the whole change, and a manual QA run that followed every printed piece of advice to its end. A second reviewer re-checked every QA fix. Plans on every pair of the repository's SQLite contract fixtures match main's, apart from the new loss and label fields on data-loss entries. Before opening this PR, every integration and e2e file that plans or applies on Postgres or SQLite with a file this branch or main changed was run.

Follow-ups filed from review and QA: TML-3562 (SQLite Decimal gets '' as its temporary value, because it shares the text codec), TML-3563, TML-3564, TML-3565.

Alternatives considered

  • Keep writing placeholders on their own. Users who did not want them could not apply the migration until they edited it.
  • --delete on a type change drops and re-adds the column. Will chose to run the database's cast instead, so values that convert are kept.
  • Ask before giving a new required field a temporary value. The project spec decided against a question here: the output names the value and --backfill.
  • Compare only the type's OID after alterColumnType. A change of length alone, such as VarChar(10) → VarChar(20), would look already applied, and the runner would skip it.

Closes TML-3477 and TML-3517.

Agent: gauss-84

…-change delete wording

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…s, classes and variants

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…questions know a type change from a drop

migration plan takes --convert and --backfill, resolves them against the two contracts, and hands the SQL planners their columns on the strategy context. No strategy reads them yet. Each dataLoss entry says whether it is a drop or a typeChange; a type change question offers convert and delete on migration plan and delete only on db update. A typed convert re-plans like a typed rename. db update refuses both statements with MIGRATION.STATEMENT_NEEDS_MIGRATION_FILE before it plans. A delete that answers a type change is reported as deleting the values of the field.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… user text, and speaks of fields, not renames

The db update refusal prints migration plan with every statement of the run that command accepts, each quoted for a shell. Convert and backfill refusals keep the user text as written, and an unknown model, namespace or value object gets advice for a field statement instead of the rename advice. The error reference covers convert and backfill under STATEMENT_INVALID and STATEMENT_UNRESOLVED.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…terColumnType takes plain options

A lossy type change on Postgres no longer gets an automatic data transform. Without a statement it is the direct destructive alter, which migration plan asks about. With --convert it is one alterColumnType classed data whose using is placeholder(Model.field:using); it is not a data loss, so a typed convert resolves its question. alterColumnType takes { schema, table, column, type, using?, operationClass? }. The type its postcheck compares is worked out from type through the data types of the stack, by one function the planner and the migration.ts facade share; a type no data type reads fails with MIGRATION.COLUMN_TYPE_UNREADABLE. Upgrade instructions cover hand-written calls.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…meters as widenings, and its advice names db migrate

A type change drops each check that names the column before the alter and adds it back after, so a list column whose element check Postgres stored with the old type can change type. A larger varchar, varbit or fractional-seconds cap, varchar to text, and a numeric with room for every digit are widenings, planned without a question. alterColumnType defaults its class to data when it has a using, and a type written as "public"."Mood" is checked as format_type prints it. The db update refusal says to apply with db migrate. The upgrade instructions move to a pending fragment.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ses data

Each dataLoss entry of the dry-run JSON now carries loss, drop or typeChange, as the control API already reports it.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…e temporary default becomes a real migration call

Without --backfill, a new required field on an existing table gets the temporary default under migration plan as under db update, and an optional field made required is a direct SET NOT NULL. With --backfill, the planner writes the nullable add, a data transform to fill in and SET NOT NULL, or the data transform and SET NOT NULL. The temporary default and the add to an empty table render as this.addColumnWithTemporaryDefault and this.addColumnToEmptyTable, new methods of the Postgres migration class, so running the written migration.ts reproduces ops.json.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…umn calls take a column name and type

The backfill and NULL-filling slots are now <Model.field>:check and <Model.field>:run, like the convert slot; the data transform keeps its name. addColumnWithTemporaryDefault and addColumnToEmptyTable take { schema, table, column, type }, so a column can no longer ask for something the call ignores, and an unsafe temporaryDefault is refused with the call named.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…e through the table rebuild

recreateTable takes copyExpressions, the SQL each named column is copied from. --convert copies the column through placeholder(Model.field:using), and the rebuild is a data change. --backfill adds a new required field nullable, or takes an optional field made required, through a data transform slot, Model.field:run, before the tightening rebuild. Without a flag, a required field added to a table with rows is copied from a temporary value under migration plan and db update, unless it is in a key or foreign key. Fill values come from the codec hook, then the datetime and json codecs, then the data type. A copy expression in migration.ts that is not one SQL expression is refused with MIGRATION.COPY_EXPRESSION_INVALID, naming the call.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ds conversion and copy expressions, and data-loss questions keep the call's label

A new required field whose values come from a value set gets no temporary value on Postgres or SQLite, since no one value stands for every member. On SQLite such a field, and one in a key, is carried through the table rebuild with nothing to copy from, so the rebuild applies to an empty table and fails on NOT NULL when the table has rows.

Postgres alterColumnType using and SQLite recreateTable copyExpressions share checkSqlExpressionText, which skips quoted literals and identifiers and refuses a semicolon, a comment, dollar quoting, unbalanced quotes or parentheses, and a top-level keyword such as FROM or UNION, with MIGRATION.SQL_EXPRESSION_INVALID naming the call.

Each data-loss entry carries the label of the planned call that loses the data, so a question still names its operation while a slot of the plan is unfilled.

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…k, IS DISTINCT FROM is accepted, and the advice says to wrap a refused keyword in parentheses

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… lossy type change, and a Postgres cast that fails names --convert

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…its new type cannot hold, naming --convert

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…lete and the plan command with --from, a missing cast says so, and SQLite names the value it could not convert

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ck a value, and the advice says how to give them one

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…the failed migration's statements, plans from the database's contract, and names every field that needs a backfill

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…he database can be reached, and never creates one to read the marker

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ions, docs for convert, backfill and the new advice, and an app upgrade fragment

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…version slot

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…bounded marker read for the refusal, and docs that say when the advice has --from

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…olumn's indexes itself, so no index is created before the column exists

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…the default fills the existing rows

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…nd its message says only what it checks

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ne builder and starts from --from when the database's contract is known, so the temporary-value advice plans as printed

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…e migration.ts it writes with node before db migrate

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…it failed, and every error of a conversion step gets the conversion advice on both targets

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ommands journey

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…amework type names no family's language

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…copy expression fails on its own or the broken constraint names a converted column

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…rtedColumns, which writes the conversion check and marks the copy, so a hand-written rebuild that converts gets the conversion advice

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… the commands whose flags include dbFlag

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ill as a conversion, and the retyped columns in castColumns as a cast

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ns, and a failed cast check says the migration.ts has no conversion for the field and to write one there

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…emporaryValues, so copyExpressions holds only conversions and a temporary value gets no type-change check

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
… any --delete of the field

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…mn the old table has or that a copy expression also fills

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden
wmadden enabled auto-merge October 13, 2026 14:40
@wmadden
wmadden added this pull request to the merge queue Oct 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 13, 2026
@wmadden-electric
wmadden-electric added this pull request to the merge queue Oct 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 13, 2026
…try's label and loss, which every entry now carries

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden-electric
wmadden-electric added this pull request to the merge queue Oct 14, 2026
Merged via the queue into main with commit 07a7bc0 Oct 14, 2026
36 of 39 checks passed
@wmadden-electric
wmadden-electric deleted the tml-3477-convert-backfill branch October 14, 2026 06:42
kpostekk pushed a commit to kpostekk/orm that referenced this pull request Oct 14, 2026
…slots to fill only for --convert and --backfill (prisma#30695)

A user changes `User.age` from `String` to `Int` on Postgres:

```text
$ prisma migration plan --name age-to-int
✖ [CLI.CONSENT_REQUIRED] 1 subject needs a statement, and the session is not interactive.
  why: Alter type of "User"."age" to int4 would change the type of field "User.age" and can change or lose its values.
→ Pass --convert User.age
→ Pass --delete User.age

$ prisma migration plan --name age-to-int --convert User.age
```

`migration.ts` then has one slot to fill:

```ts
this.alterColumnType({
  table: 'User',
  column: 'age',
  type: 'int4',
  using: placeholder('User.age:convert'),
}),
```

The user writes `using: 'trim("age")::integer'`, runs `node
migrations/app/<dir>/migration.ts`, and applies it with `prisma db
migrate`.

Until now, `migration plan` wrote a migration with placeholders on its
own for every lossy type change and every required field added to an
existing table. A user who did not want them got a migration they could
not apply until they edited it. On SQLite, `db update` could not add a
required field to a table with rows at all.

## The decision

`migration plan` writes a slot to fill only when a flag asks for it. Two
new flags ask:

- `--convert User.age` answers a lossy type change with your own
conversion.
- `--backfill User.email` fills the existing rows of a field that became
required with your own values.

Everything else is decided without a placeholder:

| Change in the schema | What happens |
| --- | --- |
| Type change that loses values, such as `String` → `Int` | Refused
until answered: `--convert`, or `--delete` |
| Type change that keeps every value, such as `Int` → `String` or
`VarChar(10)` → `VarChar(20)` | Planned, nothing asked |
| `--delete` on a type change | The database's own cast runs. A value it
cannot cast fails the migration, nothing changes, and the advice names
`--convert` |
| Required field added to an existing table | Existing rows get a
temporary value (`''`, `0`, …). The output says so and names
`--backfill` |
| Optional field made required | `SET NOT NULL`. If rows hold NULL, the
failure counts them and says what to do |
| `--convert` or `--backfill` on `db update` | Refused: `db update` has
no file to fill. The error prints the `migration plan` command |

Postgres and SQLite behave the same.

## How it works, step by step

1. **Questions know why data is lost.** Each data-loss entry says `drop`
or `typeChange`. A type-change question offers `convert` and `delete`; a
drop question offers `rename` and `delete`. `--convert` and `--delete`
on the same field are refused as conflicting.
2. **Statements reach the planners.** `ResolvedMigrationStatement` gains
`kind: rename | convert | backfill`. Convert and backfill reach the
planner strategies as destination coordinates. A converted change is
classed `data`, so it is not asked about again.
3. **Postgres.** `alterColumnType` takes plain options, `{ table,
column, type, using? }`; `using` holds the convert slot, named
`User.age:convert`. Its check after applying compares the column's type
text, derived from `type` the same way by the planner and in
`migration.ts`, so a re-run reproduces `ops.json`. The temporary value
is a real call, `addColumnWithTemporaryDefault`, instead of an empty
`rawSql(...)`.
4. **SQLite.** A table rebuild takes a copy expression per column.
`--convert` puts its slot (`User.age:convert`) there, and a key that
names no column is refused; the temporary value is a copy expression
too. SQLite does not cast on a rebuild, so a rebuild of a retyped column
without `--convert` checks every value's storage class and fails naming
the field if one does not fit.
5. **SQL values.** `using`, `temporaryDefault` and `copyExpressions`
take a string or a `sql` template value, like every other SQL text in
`migration.ts`. Their text is checked with each database's quoting
rules.
6. **Failures say what to fix.** When a column cannot become required,
the error counts the rows without a value (TML-3517). A backfill whose
`run` leaves rows without a value fails counting them and naming its
step, on both databases. Each failure prints advice that ends in an
applied migration when followed as printed.

## Upgrade notes

- **App:** a lossy type change now asks; a new required field gets a
temporary value without asking, listed in the JSON output as
`temporaryValues` (always present, empty when none); `--convert` and
`--backfill` exist.
- **Hand-written migrations:** `alterColumnType` takes `{ table, column,
type, using? }`. A call with the old options fails naming the shape it
now takes. The upgrade instructions show the rewrite.

## How it was checked

Built in six steps, each reviewed by someone running the code on PGlite
and SQLite. Then an architecture review and a code review of the whole
change, and a manual QA run that followed every printed piece of advice
to its end. A second reviewer re-checked every QA fix. Plans on every
pair of the repository's SQLite contract fixtures match `main`'s, apart
from the new `loss` and `label` fields on data-loss entries. Before
opening this PR, every integration and e2e file that plans or applies on
Postgres or SQLite with a file this branch or `main` changed was run.

Follow-ups filed from review and QA: TML-3562 (SQLite `Decimal` gets
`''` as its temporary value, because it shares the text codec),
TML-3563, TML-3564, TML-3565.

## Alternatives considered

- **Keep writing placeholders on their own.** Users who did not want
them could not apply the migration until they edited it.
- **`--delete` on a type change drops and re-adds the column.** Will
chose to run the database's cast instead, so values that convert are
kept.
- **Ask before giving a new required field a temporary value.** The
project spec decided against a question here: the output names the value
and `--backfill`.
- **Compare only the type's OID after `alterColumnType`.** A change of
length alone, such as `VarChar(10)` → `VarChar(20)`, would look already
applied, and the runner would skip it.

Closes TML-3477 and TML-3517.

Agent: gauss-84

---------

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
kpostekk pushed a commit to kpostekk/orm that referenced this pull request Oct 14, 2026
…slots to fill only for --convert and --backfill (prisma#30695)

A user changes `User.age` from `String` to `Int` on Postgres:

```text
$ prisma migration plan --name age-to-int
✖ [CLI.CONSENT_REQUIRED] 1 subject needs a statement, and the session is not interactive.
  why: Alter type of "User"."age" to int4 would change the type of field "User.age" and can change or lose its values.
→ Pass --convert User.age
→ Pass --delete User.age

$ prisma migration plan --name age-to-int --convert User.age
```

`migration.ts` then has one slot to fill:

```ts
this.alterColumnType({
  table: 'User',
  column: 'age',
  type: 'int4',
  using: placeholder('User.age:convert'),
}),
```

The user writes `using: 'trim("age")::integer'`, runs `node
migrations/app/<dir>/migration.ts`, and applies it with `prisma db
migrate`.

Until now, `migration plan` wrote a migration with placeholders on its
own for every lossy type change and every required field added to an
existing table. A user who did not want them got a migration they could
not apply until they edited it. On SQLite, `db update` could not add a
required field to a table with rows at all.

## The decision

`migration plan` writes a slot to fill only when a flag asks for it. Two
new flags ask:

- `--convert User.age` answers a lossy type change with your own
conversion.
- `--backfill User.email` fills the existing rows of a field that became
required with your own values.

Everything else is decided without a placeholder:

| Change in the schema | What happens |
| --- | --- |
| Type change that loses values, such as `String` → `Int` | Refused
until answered: `--convert`, or `--delete` |
| Type change that keeps every value, such as `Int` → `String` or
`VarChar(10)` → `VarChar(20)` | Planned, nothing asked |
| `--delete` on a type change | The database's own cast runs. A value it
cannot cast fails the migration, nothing changes, and the advice names
`--convert` |
| Required field added to an existing table | Existing rows get a
temporary value (`''`, `0`, …). The output says so and names
`--backfill` |
| Optional field made required | `SET NOT NULL`. If rows hold NULL, the
failure counts them and says what to do |
| `--convert` or `--backfill` on `db update` | Refused: `db update` has
no file to fill. The error prints the `migration plan` command |

Postgres and SQLite behave the same.

## How it works, step by step

1. **Questions know why data is lost.** Each data-loss entry says `drop`
or `typeChange`. A type-change question offers `convert` and `delete`; a
drop question offers `rename` and `delete`. `--convert` and `--delete`
on the same field are refused as conflicting.
2. **Statements reach the planners.** `ResolvedMigrationStatement` gains
`kind: rename | convert | backfill`. Convert and backfill reach the
planner strategies as destination coordinates. A converted change is
classed `data`, so it is not asked about again.
3. **Postgres.** `alterColumnType` takes plain options, `{ table,
column, type, using? }`; `using` holds the convert slot, named
`User.age:convert`. Its check after applying compares the column's type
text, derived from `type` the same way by the planner and in
`migration.ts`, so a re-run reproduces `ops.json`. The temporary value
is a real call, `addColumnWithTemporaryDefault`, instead of an empty
`rawSql(...)`.
4. **SQLite.** A table rebuild takes a copy expression per column.
`--convert` puts its slot (`User.age:convert`) there, and a key that
names no column is refused; the temporary value is a copy expression
too. SQLite does not cast on a rebuild, so a rebuild of a retyped column
without `--convert` checks every value's storage class and fails naming
the field if one does not fit.
5. **SQL values.** `using`, `temporaryDefault` and `copyExpressions`
take a string or a `sql` template value, like every other SQL text in
`migration.ts`. Their text is checked with each database's quoting
rules.
6. **Failures say what to fix.** When a column cannot become required,
the error counts the rows without a value (TML-3517). A backfill whose
`run` leaves rows without a value fails counting them and naming its
step, on both databases. Each failure prints advice that ends in an
applied migration when followed as printed.

## Upgrade notes

- **App:** a lossy type change now asks; a new required field gets a
temporary value without asking, listed in the JSON output as
`temporaryValues` (always present, empty when none); `--convert` and
`--backfill` exist.
- **Hand-written migrations:** `alterColumnType` takes `{ table, column,
type, using? }`. A call with the old options fails naming the shape it
now takes. The upgrade instructions show the rewrite.

## How it was checked

Built in six steps, each reviewed by someone running the code on PGlite
and SQLite. Then an architecture review and a code review of the whole
change, and a manual QA run that followed every printed piece of advice
to its end. A second reviewer re-checked every QA fix. Plans on every
pair of the repository's SQLite contract fixtures match `main`'s, apart
from the new `loss` and `label` fields on data-loss entries. Before
opening this PR, every integration and e2e file that plans or applies on
Postgres or SQLite with a file this branch or `main` changed was run.

Follow-ups filed from review and QA: TML-3562 (SQLite `Decimal` gets
`''` as its temporary value, because it shares the text codec),
TML-3563, TML-3564, TML-3565.

## Alternatives considered

- **Keep writing placeholders on their own.** Users who did not want
them could not apply the migration until they edited it.
- **`--delete` on a type change drops and re-adds the column.** Will
chose to run the database's cast instead, so values that convert are
kept.
- **Ask before giving a new required field a temporary value.** The
project spec decided against a question here: the output names the value
and `--backfill`.
- **Compare only the type's OID after `alterColumnType`.** A change of
length alone, such as `VarChar(10)` → `VarChar(20)`, would look already
applied, and the runner would skip it.

Closes TML-3477 and TML-3517.

Agent: gauss-84

---------

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signed-off-by: Krystian Postek <[email protected]>
kpostekk pushed a commit to kpostekk/orm that referenced this pull request Oct 14, 2026
…slots to fill only for --convert and --backfill (prisma#30695)

A user changes `User.age` from `String` to `Int` on Postgres:

```text
$ prisma migration plan --name age-to-int
✖ [CLI.CONSENT_REQUIRED] 1 subject needs a statement, and the session is not interactive.
  why: Alter type of "User"."age" to int4 would change the type of field "User.age" and can change or lose its values.
→ Pass --convert User.age
→ Pass --delete User.age

$ prisma migration plan --name age-to-int --convert User.age
```

`migration.ts` then has one slot to fill:

```ts
this.alterColumnType({
  table: 'User',
  column: 'age',
  type: 'int4',
  using: placeholder('User.age:convert'),
}),
```

The user writes `using: 'trim("age")::integer'`, runs `node
migrations/app/<dir>/migration.ts`, and applies it with `prisma db
migrate`.

Until now, `migration plan` wrote a migration with placeholders on its
own for every lossy type change and every required field added to an
existing table. A user who did not want them got a migration they could
not apply until they edited it. On SQLite, `db update` could not add a
required field to a table with rows at all.

## The decision

`migration plan` writes a slot to fill only when a flag asks for it. Two
new flags ask:

- `--convert User.age` answers a lossy type change with your own
conversion.
- `--backfill User.email` fills the existing rows of a field that became
required with your own values.

Everything else is decided without a placeholder:

| Change in the schema | What happens |
| --- | --- |
| Type change that loses values, such as `String` → `Int` | Refused
until answered: `--convert`, or `--delete` |
| Type change that keeps every value, such as `Int` → `String` or
`VarChar(10)` → `VarChar(20)` | Planned, nothing asked |
| `--delete` on a type change | The database's own cast runs. A value it
cannot cast fails the migration, nothing changes, and the advice names
`--convert` |
| Required field added to an existing table | Existing rows get a
temporary value (`''`, `0`, …). The output says so and names
`--backfill` |
| Optional field made required | `SET NOT NULL`. If rows hold NULL, the
failure counts them and says what to do |
| `--convert` or `--backfill` on `db update` | Refused: `db update` has
no file to fill. The error prints the `migration plan` command |

Postgres and SQLite behave the same.

## How it works, step by step

1. **Questions know why data is lost.** Each data-loss entry says `drop`
or `typeChange`. A type-change question offers `convert` and `delete`; a
drop question offers `rename` and `delete`. `--convert` and `--delete`
on the same field are refused as conflicting.
2. **Statements reach the planners.** `ResolvedMigrationStatement` gains
`kind: rename | convert | backfill`. Convert and backfill reach the
planner strategies as destination coordinates. A converted change is
classed `data`, so it is not asked about again.
3. **Postgres.** `alterColumnType` takes plain options, `{ table,
column, type, using? }`; `using` holds the convert slot, named
`User.age:convert`. Its check after applying compares the column's type
text, derived from `type` the same way by the planner and in
`migration.ts`, so a re-run reproduces `ops.json`. The temporary value
is a real call, `addColumnWithTemporaryDefault`, instead of an empty
`rawSql(...)`.
4. **SQLite.** A table rebuild takes a copy expression per column.
`--convert` puts its slot (`User.age:convert`) there, and a key that
names no column is refused; the temporary value is a copy expression
too. SQLite does not cast on a rebuild, so a rebuild of a retyped column
without `--convert` checks every value's storage class and fails naming
the field if one does not fit.
5. **SQL values.** `using`, `temporaryDefault` and `copyExpressions`
take a string or a `sql` template value, like every other SQL text in
`migration.ts`. Their text is checked with each database's quoting
rules.
6. **Failures say what to fix.** When a column cannot become required,
the error counts the rows without a value (TML-3517). A backfill whose
`run` leaves rows without a value fails counting them and naming its
step, on both databases. Each failure prints advice that ends in an
applied migration when followed as printed.

## Upgrade notes

- **App:** a lossy type change now asks; a new required field gets a
temporary value without asking, listed in the JSON output as
`temporaryValues` (always present, empty when none); `--convert` and
`--backfill` exist.
- **Hand-written migrations:** `alterColumnType` takes `{ table, column,
type, using? }`. A call with the old options fails naming the shape it
now takes. The upgrade instructions show the rewrite.

## How it was checked

Built in six steps, each reviewed by someone running the code on PGlite
and SQLite. Then an architecture review and a code review of the whole
change, and a manual QA run that followed every printed piece of advice
to its end. A second reviewer re-checked every QA fix. Plans on every
pair of the repository's SQLite contract fixtures match `main`'s, apart
from the new `loss` and `label` fields on data-loss entries. Before
opening this PR, every integration and e2e file that plans or applies on
Postgres or SQLite with a file this branch or `main` changed was run.

Follow-ups filed from review and QA: TML-3562 (SQLite `Decimal` gets
`''` as its temporary value, because it shares the text codec),
TML-3563, TML-3564, TML-3565.

## Alternatives considered

- **Keep writing placeholders on their own.** Users who did not want
them could not apply the migration until they edited it.
- **`--delete` on a type change drops and re-adds the column.** Will
chose to run the database's cast instead, so values that convert are
kept.
- **Ask before giving a new required field a temporary value.** The
project spec decided against a question here: the output names the value
and `--backfill`.
- **Compare only the type's OID after `alterColumnType`.** A change of
length alone, such as `VarChar(10)` → `VarChar(20)`, would look already
applied, and the runner would skip it.

Closes TML-3477 and TML-3517.

Agent: gauss-84

---------

Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signed-off-by: Krystian Postek <[email protected]>
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