Repository navigation
Studio reads the migration ledger from the _prisma schema, and falls back to prisma_contract - #1606
Merged
Merged
Conversation
…ontract Prisma ORM 8.0.0 renames the schema that holds its own tables from prisma_contract to _prisma, the first time a command that changes the database runs. Until then a database still has prisma_contract, so the Migrations view, its navigation item and readRecordedSchema now read _prisma when it holds a ledger and prisma_contract otherwise. The choice is made once per load from the existing checks: introspection for the Migrations view, and one to_regclass probe over both schemas for readRecordedSchema. The ledger queries become functions of one of the two fixed schema names. The demo seed writes _prisma. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The schema constants, the choice between _prisma and prisma_contract and the ledger query builders move to data/prisma-tables-schema.ts, which is not a build entry, so they are not published. The public data/migrations module keeps readRecordedSchema, parseLedgerRows and the types. The readRecordedSchema probe columns now say which schema they describe (current_schema_has_ledger and so on). New tests cover the hooks: the row probe and the ledger load query the schema detection chose and carry it in their query keys. Another covers _prisma having a ledger without a contract store while prisma_contract has both. The demo seed's in-place upgrade looked for old layouts in _prisma, which nothing ever wrote; the demo database is fresh each run, so it is removed. The architecture doc says why the prisma_contract fallback exists and when it is removed. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
|
Compute preview deployed with Prisma Composer. Stage: |
wmadden
approved these changes
Nov 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
At a glance
Studio's Migrations view shows the migrations Prisma ORM 8 has applied to a PostgreSQL database. It reads two tables the ORM keeps for itself in that database:
ledger, one row per migration applied, andcontract, which stores the contract (the ORM's description of the database's structure) before and after each one. Today Studio looks for them in one schema:The next Prisma ORM release after
8.0.0-rc.25renames that schema to_prisma. It is not published yet. Once it is, Studio as it is today finds noprisma_contract.ledgerin a renamed database and hides the Migrations view.With this change Studio first works out which schema holds the ledger, then runs the same queries against it:
_prisma.ledger_prismaprisma_contract.ledgerprisma_contract_prismaThis removes three exports from a published entry point.
@prisma/studio-core/data/migrationsno longer exportsLEDGER_QUERY,LEDGER_QUERY_WITHOUT_CONTRACTandLEDGER_PROBE_QUERY. Details are below.Decision
Studio reads
_prisma, and falls back toprisma_contractwhen_prismahas no ledger.The ORM renames the schema itself, the first time a command that changes the database (
prisma db migrate,db update,db initordb sign) runs with the release that has the rename. Until a database's owner has run one, the database still hasprisma_contract, with the same tables and columns. So once that ORM release is out, real databases of both forms exist, and Studio has to show the history of both.This repository's rules forbid compatibility fallbacks unless one is asked for. This one is asked for by the ORM work that does the rename, for the reason above, and
Architecture/migrations-view.mdnow records it as a deliberate exception. It is removed when Studio stops supporting databases written by a Prisma ORM 8 release candidate. No date is set for that, because Studio has no stated policy on it yet.Order of releases. This can merge and be released now: on every database that exists today it reads
prisma_contract, exactly as before. It should be released before the ORM release that renames the schema, or users of that release lose the Migrations view until they update Studio. The same holds for Console (prisma/pdp-control-plane), which callsreadRecordedSchemafrom this package: it needs the Studio release with this change before the ORM release ships.A database has both schemas only when something went wrong, for example when an older ORM command-line tool is run against a database that was already renamed, which creates a fresh, empty
prisma_contractbeside_prisma._prismais the one the current ORM writes to, so it wins.Why the ORM renames the schema
Every schema, table or collection Prisma ORM 8 puts at the top level of a user's database will have a name that begins
_prisma, so a user can tell from the name that it is Prisma's and not to be edited by hand. On PostgreSQL that is the schema; the tables inside it,ledgerandcontract, keep their names and columns. The name cannot change after8.0.0without a major version, so it changes now. The ORM work is Linear ticket TML-3978, "Prisma 8 keeps its own database tables up to date in each release, under names that begin_prisma".How it works
Paths are from the repository root.
data/prisma-tables-schema.ts, holds the two schema names, the rule that picks one (the first of_prisma,prisma_contractthat has aledgertable), and the ledger queries as functions of the schema. The schema is always one of those two constants, so nothing a user supplies reaches the SQL.ui/hooks/use-migrations.ts) apply the rule to the list of tables Studio has already loaded for the database, so the choice is made once per load of that list. Two queries then use the chosen schema: a cheap one that asks whether the ledger has any rows, which decides whether the navigation item appears, and the one that loads the ledger. The schema is part of both cache keys, so a result cached for one schema is never shown for the other.readRecordedSchema(data/migrations.ts) returns the contract of the latest applied migration; Console uses it to compare a database with its recorded structure. It already asked PostgreSQL in one query whether the two tables exist. That query now asks about both schemas, and the same rule picks one.contracttable is newer than the ledger, so Studio already handled a schema that hasledgerand nocontract: it shows the list of migrations without the before-and-after comparison. That still holds per schema. If_prismahas a ledger and no contract table whileprisma_contracthas both, Studio reads_prismaonly; it never joins one schema's ledger to the other's contracts.Incidental: the demo seed (
demo/ppg-dev/seed-migrations.ts) now writes_prisma, since it stands for a current database. Its code for upgrading an older demo database in place is deleted. The demo database is created fresh on every run, so that code had nothing to upgrade.The removed exports
@prisma/studio-core/data/migrationsexported three SQL strings:LEDGER_QUERY,LEDGER_QUERY_WITHOUT_CONTRACTandLEDGER_PROBE_QUERY. Each named one schema, so none can be correct for both forms of database. They are removed and nothing public replaces them; the queries are now internal.readRecordedSchema,parseLedgerRowsand the types are unchanged.A code search of the
prismaorganisation on GitHub finds the three names only in this repository. Console imports this entry point, but usesreadRecordedSchema, not the constants. I did not search outside the organisation.The changeset is a minor bump, which is how this package, at
0.35.0, marks a breaking change.How it was checked
data/migrations.postgres.test.tsruns every query against a real PostgreSQL (PGlite) for each schema, and one case has both schemas and asserts_prismais read.data/migrations.test.tscoversreadRecordedSchemafor each schema, and the case where_prismahas a ledger and no contract table whileprisma_contracthas both.ui/hooks/use-migrations.queries.test.tsxrenders the hooks with a database adapter that records SQL. It asserts that both queries go to the detected schema only, and that the schema is in the cache keys.tsc --noEmit, and ESLint on the changed files. The whole suite andcheck:exportsare left to CI.The demo, in a browser.
AGENTS.mdasks for a browser check of the demo on UI changes. I checked this pull request's own preview deployment (thedemoservice that thedeploycheck builds from this branch, whose database is seeded into_prisma): the Migrations item is in the navigation, the view lists all 20 seeded migrations, selecting one draws its before-and-after comparison, and the browser console shows no errors. I could not runpnpm demo:ppglocally: on the machine this was built on, the demo fails while seeding its database (bind message supplies N parameters, but prepared statement "" requires 0), and it fails the same way with the seed file frommain, so this change did not cause it. I have not found the cause and have filed nothing for it.Alternatives considered
_prisma, and release Studio at the same moment as the ORM. Rejected: the rename happens per database, when its owner next runs a command that changes it, not at the release. A database last changed by a release candidate would lose its Migrations view until then._prismaand retry againstprisma_contracton an error. Rejected: Studio already knows which tables exist, and an error can mean something else.Agent: laozi-26
🤖 Generated with Claude Code