Skip to content

fix(migrations): read Prisma ORM's tables from the prisma schema, not _prisma - #1615

Merged
wmadden-electric merged 4 commits into
mainfrom
read-prisma-schema-without-underscore
Nov 29, 2026
Merged

wmadden-electric merged 4 commits into
mainfrom
read-prisma-schema-without-underscore

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

At a glance

Against a PostgreSQL database that Prisma ORM 8.0 has migrated, Studio 0.36.0's Migrations view shows nothing: it looks for the ORM's tables in a schema the ORM does not use. This changes where Studio looks:

Studio 0.36.0        _prisma          → prisma_contract
This change          prisma           → prisma_contract

Background: where Prisma ORM keeps its own tables

Prisma ORM keeps a few tables for itself in every PostgreSQL database it manages. Studio's Migrations view reads two of them: the ledger, one row per migration applied, and the contract store, which holds the description of the database's structure before and after each migration, so the view can show what a migration changed.

The schema that holds them has had three names:

ORM version Schema
Prisma ORM 8 release candidates prisma_contract
An unreleased change to the ORM, and preview builds of it _prisma
Prisma ORM 8.0 prisma

Studio 0.36.0 was released ahead of the ORM change and read _prisma. Before the ORM change shipped, the ORM dropped the leading underscore, so 8.0 uses prisma (prisma/orm#31242). The first command of 8.0 that changes a release candidate's database renames prisma_contract to prisma in place, so a database has one or the other, never both, unless something else created the second.

The decision

Studio reads the ledger from prisma when that schema has Prisma's ledger, and otherwise from prisma_contract. A schema has Prisma's ledger when it has a table or view named ledger with the columns migration_hash and destination_core_hash, which Studio reads. When both schemas have one, prisma wins, because it is where 8.0 puts the tables.

Studio no longer reads _prisma. No released ORM created it. A developer who tried a preview build of the ORM change has a database with _prisma: Studio shows no history for it, and running a command of any later ORM release does not move it. Such a database is a scratch database; recreate it.

An application's own prisma schema

An application may have a schema of its own named prisma. It counts as Prisma's only when it has a ledger table with those columns, so an application's prisma schema, or an application table that happens to be named prisma.ledger, is ignored and Studio falls back to prisma_contract. Studio checks this in the system catalogue, so a role that may not use an application's prisma schema still gets the fallback rather than an error.

The ORM itself stops before it renames prisma_contract when a non-empty prisma already exists, so such a database keeps its history in prisma_contract, which is where Studio then reads it.

Changes

  • data/prisma-tables-schema.ts: the schema name is prisma, and one function, choosePrismaTablesSchema, decides between prisma and prisma_contract. Both places that need the answer use it: the query that reads the history (readRecordedSchema), and the check that decides whether to show the Migrations item in the navigation.
  • The probe for the tables reads the system catalogue, not to_regclass, which raises an error for a schema the role may not use.
  • The demo seed creates the tables the way ORM 8.0 does.
  • Architecture/migrations-view.md and FEATURES.md name prisma and say when a schema counts as Prisma's.
  • A patch changeset. The public API does not change.

How it was checked

  • Unit and PGlite tests:
    • one names the schema prisma literally, so changing the name back fails it;
    • Studio prefers prisma when both schemas have a ledger;
    • an application's prisma schema without Prisma's ledger falls back to prisma_contract, against a real database and in the navigation check;
    • a role that may use only prisma_contract still reads from it.
  • On the tip: the touched test files (60 tests), tsc --noEmit on the project, and ESLint and Prettier on the touched files pass.
  • On this pull request's preview deployment, the demo database has only the schema prisma (with signatures, ledger and contract), and the Migrations view lists all 20 seeded migrations and draws the before-and-after diagram, with no console errors.

Release order

Release this Studio patch before Prisma ORM 8.0. Until it is out, Studio's Migrations view is empty for a database 8.0 has migrated. Console reads the same tables through Studio, so it moves to this release too.

Alternatives considered

  • Keep reading _prisma as a third fallback. Rejected: only preview builds created it, and each fallback is code Studio carries until it is removed.
  • Treat prisma as Prisma's whenever it has a table named ledger. Rejected: an application's own ledger table, a common name, would hide the history.
  • Take the schema name from an ORM package, or ask the database which schema the ORM uses. Not done: Studio does not depend on the ORM's packages, and the database records the ORM's state inside the very schema Studio is looking for.

Agent: laozi-26

🤖 Generated with Claude Code

wmadden-electric and others added 3 commits November 29, 2026 08:16
Prisma ORM 8.0 names the schema for its own tables prisma, without the
underscore. No ORM release ever created _prisma, so Studio stops reading
it. Studio reads prisma when it holds a ledger table and falls back to
prisma_contract, the name in the ORM 8 release candidates.

A schema named prisma that an application created for its own tables is
not read as Prisma's unless it holds a ledger table; new tests cover
that for readRecordedSchema and for the Migrations detection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
The ORM release that names the schema prisma keeps the applied contract
hashes in a signatures table and creates no marker table. The demo seed
now creates signatures with that release's columns and fills it from
the fixture's marker rows. Studio reads neither table.

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

An application can own a schema named prisma, and the ORM then keeps
its tables in prisma_contract. Two things made such a database lose its
history in Studio:

- readRecordedSchema probed with to_regclass, which raises an error when
  the role may not use the prisma schema, so the read failed before the
  fallback. It now reads the same facts from pg_catalog, as introspection
  does.
- A table of the application's own named ledger made prisma win. A
  schema now counts as Prisma's only when its ledger table has the
  migration_hash and destination_core_hash columns. The check lives in
  choosePrismaTablesSchema, so readRecordedSchema and the Migrations
  detection share it.

New PGlite tests cover both cases, and one writes the name prisma
literally so that changing the constant fails it. The architecture doc
says what makes a schema count as Prisma's, and two of its sentences
are corrected.

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

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary by CodeRabbit

  • Bug Fixes

    • The migrations view now detects Prisma’s migration tables in the prisma schema, preferring it when both supported schemas contain a valid ledger.
    • Detection checks for Prisma’s expected ledger columns, helping avoid mistaking an application-owned prisma schema for Prisma’s tables.
    • If no valid ledger is found in prisma, Studio falls back to prisma_contract. Limited schema access is handled more gracefully.
  • Documentation

    • Updated migration-view and architecture documentation to reflect the schema detection behavior.

Walkthrough

Schema detection now identifies Prisma tables by checking for the required ledger columns and prefers prisma over prisma_contract when both qualify. Recorded migration reads use PostgreSQL catalog queries to inspect configured schemas. Demo migration seeding now creates and populates the Prisma tables in prisma, including signatures.

Merge Risk: Minimal · up to edf79

Studio's Migrations view now reads Prisma's tables from prisma and falls back to prisma_contract. The tests cover the main selection and fallback cases. No merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Studio's Migrations view now reads Prisma ORM's tables from the prisma schema instead of _prisma. It matches the changeset, which switches the schema name, adds le…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and on-topic. It explains that Studio's Migrations view looked for Prisma ORM tables in _prisma, while Prisma ORM 8.0 uses prisma. It describes the new selection rule b…
Full details: Title check

Explanation

The title names the main change: Studio's Migrations view now reads Prisma ORM's tables from the prisma schema instead of _prisma. It matches the changeset, which switches the schema name, adds ledger-column detection, and falls back to prisma_contract. The title is short, single-sentence, and specific.

Full details: Description check

Explanation

The description is specific and on-topic. It explains that Studio's Migrations view looked for Prisma ORM tables in _prisma, while Prisma ORM 8.0 uses prisma. It describes the new selection rule between prisma and prisma_contract, the ledger column check, the catalogue-based probe, the demo seed and documentation updates, the test coverage, and the release order. This matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Simplify code

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: f3f4d7ef-15d1-4479-9b2d-6715b7376010
Commits

Reviewing files that changed from the base of the PR and between 05f5db2 and edf79db.

Files selected for processing: 4

  • Architecture/migrations-view.md
  • FEATURES.md
  • data/migrations.postgres.test.ts
  • data/migrations.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

Compute preview deployed with Prisma Composer.

Stage: read-prisma-schema-without-underscore
Service: demo
Preview: https://uuhcdrml4x9dgn1k45mqgut8.sin.prisma.build

…a's tables

readRecordedSchema counted regular and partitioned tables, while the
introspection behind the Migrations view counts regular tables and
views, so the two could choose different schemas. The probe now counts
regular tables and views, with a test of a prisma.ledger view.

The architecture doc names data/prisma-tables-schema.ts as the internal
module, and FEATURES.md says when a prisma schema counts as Prisma's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden-electric
wmadden-electric marked this pull request as draft November 28, 2026 22:06
@wmadden-electric
wmadden-electric marked this pull request as ready for review November 29, 2026 00:16
@wmadden-electric
wmadden-electric merged commit 7ddc80c into main Nov 29, 2026
2 checks passed
@wmadden-electric
wmadden-electric deleted the read-prisma-schema-without-underscore branch November 29, 2026 01:04
wmadden-electric added a commit to prisma/orm that referenced this pull request Nov 29, 2026
…ma schema yet

Studio 0.36.0 reads _prisma, which no release of Prisma uses. The release that reads prisma is the one after prisma/studio#1615 merges; until then the Migrations view is empty on an upgraded database.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
wmadden-electric added a commit to prisma/orm that referenced this pull request Nov 29, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
wmadden-electric added a commit to prisma/orm that referenced this pull request Nov 29, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[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