Repository navigation
Shape the intent-hints project: the planner refuses destructive changes unless the schema states the intent - #30557
wmadden-electric wants to merge 9 commits into
Conversation
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (5)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including ⚙️ Run configurationConfiguration used: Repository: prisma/orm/.coderabbit.yml Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-postgres
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
size-limit report 📦
|
… not only renames Rename the folder to projects/intent-hints, adopt the deprecated/deleted lifecycle vocabulary from the recovered Contract-Driven DB Update design, add the migration plan refusal and the deleted tombstone as slices, and record the verification of that design against today's code. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
Numbered rules for the attribute grammar, the contract section, resolution against the origin, rename application in the planners, stated drops, consent in migration plan, reporting, tests and docs, after one review pass against the code. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
…tion Combined table-and-column renames fold into one renameTable call with a columns map; stripOperations keeps statedIntent and target; toOps takes the lowerer; consumed-hint text is rendered where the family instance is available; advice keys on the op id prefix; stated-drop closure reads the adjusted origin; the consistency check becomes a family capability; ownership gains ownerOf; corrected class, facade, file and union names. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
… names, index drops are widening, ignored hints warn Four operator rulings: no composite rename call, a working schema that each facade call advances instead; constraint companion names come from the destination contract and the origin's actual name; DropIndexCall is widening on both targets; a hint on a table control policy protects is ignored with a warning and the old table's drop goes through consent. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ts, checks and policies are widening Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Shelved on 2026-10-05. After discussion with Serhii, stating planner intent through the contract source ( |
… with --rename, instead of dropping it (prisma#30638) ## At a glance A user renames the `Profile` model to `User` and its `name` field to `fullName` in the contract source, then says so on the command line: ```text $ prisma migration plan --name tidy --rename Profile:User --rename User.name:User.fullName ✔ Planned 6 operation(s) migrations/app/20261007T1003_tidy ├─ Rename table "Profile" to "User" ├─ Rename primary key "Profile_pkey" to "User_pkey" on "User" ├─ Rename index "Profile_handle_idx_b5b249e4" to "User_handle_idx_b5b249e4" on "User" ├─ Rename column "User"."name" to "fullName" ├─ Rename unique constraint "Profile_name_key" to "User_fullName_key" on "User" └─ Rename index "Profile_name_handle_idx_f7a67607" to "User_fullName_handle_idx_46490beb" on "User" Statements applied ├─ rename model "Profile" to "User" (3 operations) └─ rename field "User.name" to "User.fullName" (3 operations) ``` The written `migration.ts` holds `...this.renameTable(...)` and `...this.renameColumn(...)`, the calls a user could write by hand, and re-running it writes the same `ops.json`. `prisma db update` takes the same statements and does the same to a live database, with no consent prompt. Before this change the same edit planned `dropTable "Profile"` plus `createTable "User"`, and the rows were gone after one consent prompt. ## The decision The user states a rename on `migration plan` and `db update` as `--rename <old>:<new>`, naming models and fields as the contract source names them. The planner acts on the statement and never guesses: a change with no statement is planned as the diff shows it. - **Resolution in the framework, storage in the family.** The CLI parses each statement and resolves it against the origin contract and the destination contract into domain coordinates (namespace, model, field). The SQL family maps each resolved statement to a storage effect (a table rename, a column rename, or nothing when storage did not change) and applies it, in order, to a working copy of the origin schema. The Postgres and SQLite planners emit the rename, then rename every constraint and index named after the old table or column to the name the destination contract gives it, then run the usual diff on the renamed schema. - **An unusable statement is an error, and nothing is written.** `MIGRATION.STATEMENT_INVALID` (syntax), `MIGRATION.STATEMENT_UNRESOLVED` (the names do not match the two contracts) and `MIGRATION.STATEMENT_ORIGIN_UNKNOWN` (no origin contract) each say what was searched, what was found, and the statement to type instead. A statement the planner cannot carry out is a `statementRefused` conflict under `MIGRATION.PLANNING_FAILED`, carrying the statement as `refusedStatement`. - **`db update` finds the old contract through the snapshot of its marker hash**, and only when statements are given; without them nothing changes. Running the same statements twice fails the second time and says the rename already happened. - **The plan reports what each statement did.** The planner result carries `appliedStatements`, one per statement, each with the resolved statement and the positions of the operations it produced in the plan. Both commands print them under `Statements applied` and in `--json`. - **Drops that lose no data are widening.** Dropping an index, a unique or foreign-key constraint, a check, a row-level-security policy, a default or a native enum type, and disabling row-level security, no longer need consent on Postgres and SQLite. A SQLite rename rebuilds indexes, and would otherwise always ask. - **MongoDB refuses statements in this release.** Before this change it would have accepted `--rename` and planned a drop and create. The refusal says how to keep the documents by hand. ## How it fits together - `packages/1-framework/3-tooling/cli/src/control-api/statements/` — parse and resolve statements. The control API takes one ordered list of `{ verb, text }`, so later verbs and an interactive prompt feed the same entry point. - `packages/1-framework/1-core/framework-components/src/control/migration-statements.ts` — `ResolvedMigrationStatement`, `AppliedMigrationStatement`, `describeMigrationStatement`. The planner options gain `statements` and `origin`; the success result gains `appliedStatements`. - `packages/2-sql/9-family/src/core/migrations/statement-planning.ts` — storage effects and the ordered application against a target's working schema. - `packages/3-targets/3-targets/{postgres,sqlite}/src/core/migrations/` — `working-schema.ts`, `renameColumn` (new, beside `renameTable`), companion constraint and index renames, planner wiring. The working schema and destination-driven companion names come from the shelved prisma#30570, without its contract-source hints. - `packages/1-framework/3-tooling/migration/src/aggregate/` — only the application space receives the origin contract and statements. - Identifiers: the shared Postgres and SQLite `qualifyTable` helpers now escape `"` in names. Review found that a table name containing a quote reached rename SQL unescaped through a statement; the probe and the fix are in the hostile-name tests on PGlite and `node:sqlite`. ## What this does not do yet `--delete` and the refusal of data loss by default (the next slice), `--convert` and `--backfill`, renames of namespaces, enum values and value-object fields, a model moved to another namespace (refused), a model that keeps its name but changes `@@map` (refused, with the hand-written route), and statements on MongoDB. ## Verification - Journeys on Postgres and SQLite: `test/integration/test/cli-journeys/rename-statements-migration.e2e.test.ts` and `.sqlite.e2e.test.ts` rename a model and a field through `migration plan` + `db migrate` and through `db update`, on tables with rows, a unique, a foreign key from another table, a secondary index, and on Postgres a check and a policy. They assert the database state directly, that a further plan is empty, that `db verify --schema-only` is clean, that re-running `migration.ts` is byte-identical, and that a second `db update` with the same statements fails. - A manual QA run against real databases (Postgres through `@prisma/dev`, SQLite, MongoDB) covered ten scenarios; its findings are fixed and the commands re-run. - Upgrade instructions for both audiences are in `upgrade-instructions/pending/migration-statement-renames/`. ## Alternatives considered - **Intent in the contract source** (`@@hint(was:)`, prisma#30557 and prisma#30570). Shelved as unintuitive; it also had to survive until every environment consumed it. - **Detecting renames, or asking in a prompt whether a drop is a rename.** Rejected: detection guesses, and a prompt cannot be answered by a script or an agent. A prompt that asks the user to state the intent, without guessing, is the planned next step on top of this entry point. - **Storage names on the command line** (`--rename-table`). Rejected: the command line speaks the contract's vocabulary, and the same statement must mean the same thing on every family. - **Putting the schema into Postgres operation ids** so a statement could point at its operations by id. Rejected during review: the ledger keeps each migration's hash, and re-emitting an existing migration with new ids would drop its applied mark. Statements point at operations by position instead. Refs: TML-3475 Agent: turing-38 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added repeatable `--rename old:new` options to `migration plan` and `db update` for model and field renames. PostgreSQL and SQLite plans can include corresponding table, column, and related-object renames. * Plans and update results now report applied rename statements and their associated operations. * Added validation and actionable errors for invalid, unresolved, or unsupported rename statements. MongoDB plans report rename statements they cannot apply. * **Behavior Changes** * Drops of non-data-bearing objects and disabling row-level security are classified as widening operations on SQL targets. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
At a glance
A user renames the
Profilemodel toUser, renames itsfirst_namecolumn, and removes theLegacymodel for good, and says all three in the schema:After this project,
prisma migration planwrites a migration that renames the table, the column, and every constraint and index named after them, and dropsLegacy.prisma db updatedoes the same against a live database. Without thedeletedhint, both commands refuse to dropLegacy. Once an environment is updated the hints match nothing and do nothing, so the user may delete them whenever they like.Today the rename plans as a drop and a create, and the rows are gone. The only route is a hand-written migration, and only for tables. The drop is planned and relies on a consent prompt in
db update, and on nothing at all inmigration plan.What this PR changes
This PR adds no code. It lands the shaping artifacts for the project under
projects/intent-hints/:spec.md: purpose, the decisions settled in design discussion, cross-cutting requirements, project definition of done.plan.md: four stacked slices with their Linear issues.design-notes.md: the reasoning behind each decision, the recovered design the project builds on and its verification against today's code, and the alternatives rejected.README.md: the index.Why intent, not inference
The migration planner diffs two states: the destination contract and an origin schema, which is a contract snapshot for
migration planand an introspected database fordb update. A renamed model looks exactly like a dropped table plus a created one, and a removed model looks the same as one the user forgot. The planner refuses to guess, because two unrelated tables can share columns and a wrong guess carries rows into the wrong table.The subsystem docs have described
@hint(was: "old_name")as the answer since the first ADRs, and a design document deleted before the release candidates, "Contract-Driven DB Update", went further:db updateas a safe path on any environment, with destructive operations refused by default and an active, deprecated, deleted lifecycle stated in the authoring layer. The recent rename-table work (#30331) added the operation and athis.renameTable(...)call for hand-written migrations, and left the hint as the follow-up. This project builds the mechanism from that recovered design, verified against the code as it is now.The decisions
The spec records these; the design notes record why.
db updatealready does, through its consent prompt.migration plangains the same refusal, so an ordinary plan never writes a drop the user has not stated. Command-level consent stays as the fallback for a change no hint can express.wasfor a rename,deletedfor a confirmed removal, anddeprecatedreserved for an object the application no longer needs but the database may keep. Hints that supply an expression, such as a cast for a type change, are declared future arguments of the same attribute and not built here.washolds the old storage name, what@@mapor@mapwould have said. The planner resolves it against the origin schema alone and never opens an older contract, so the contract stays self-contained and the hint works fordb update, which has no older contract to consult.deletedmodel is a tombstone. The block stays in the schema, because a drop is absence and absence cannot carry an attribute. It contributes nothing to the contract but its hints entry.contract.jsonoutside the hashed sections and are stripped from snapshots and migration directories. The storage hash of a contract with and without hints is identical.deprecated, namespace moves, enum value renames and named-index renames are follow-ons. The documented dev-only stance ondb updateis left alone.The slices
migration planrefuses destructive operations by default, with the same consent model asdb update, and both refusals name the hint that would state the intent.deletedon models and fields.Alternatives considered
wasname the old model rather than the old table. Rejected because resolving it requires the previous contract, whichdb updatedoes not have, and the contract must be self-contained.dropas the whole removal story. Rejected because a rolling deploy may still have the old app version reading a column when the new contract drops it. The two-phase vocabulary is adopted whole now so nothing is renamed later.db updateusers would see the warning forever and learn to delete hints early, which is the data-loss case.contract.json. Rejected as a second artifact every emitter and copy step must carry.Refs: TML-3421
Agent: scylla-59
🤖 Generated with Claude Code