← Back to Pull requests

docs: baseline a migration history that is behind the schema #8933

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 25, 2026 14:30 UTC
Author

TL;DR

The docs never covered a migration history that is behind the schema, which is what makes prisma migrate deploy create only some of the tables on a new database. This PR adds that workflow to the Prisma ORM 6 and 7 baselining pages and replaces > redirection with migrate diff --output, which already exists and avoids the Windows PowerShell encoding problem.

Linear: https://linear.app/prisma-company/issue/DR-9703

Drifted history: detect it, add a catch-up migration, verify

Problem. When tables are created with db push, the schema and the database agree but the prisma/migrations folder cannot recreate the database. migrate deploy on an empty database reports success and leaves those tables missing, and migrate status says Database schema is up to date! on the drifted database.

Fix. A new section, "Baselining a migration history that is behind your schema", on orm/v7/prisma-migrate/workflows/baselining and the orm/v6 copy. It compares schema with database and history with schema using migrate diff --exit-code, writes the missing SQL as a catch-up migration, marks it applied on existing databases with migrate resolve --applied, and repeats the parity check. Each page uses its own version's flag names.

Entry points. The Prisma Postgres import guide, its v7 copy and the Supabase guide link to the section from their "Prisma ORM 7 or earlier" step, and the migrate diff reference links to it.

PowerShell encoding: use --output, not >

Problem. The baselining commands redirected migrate diff --script into migration.sql. Windows PowerShell 5.1 writes redirected output as UTF-16LE with a BOM, and PostgreSQL rejects a file that starts with a BOM.

Fix. The 12 commands that redirected into prisma/migrations/0_init/migration.sql now use --output. A short section documents the symptoms and the repair, and the migrate diff reference gains a "Write the script to a file" example.

Also fixed

The two Prisma ORM 6 baselining commands used --to-schema, which Prisma ORM 6.19.1 rejects with unknown or unexpected option. They now use --to-schema-datamodel.

Validation

All commands were run against temporary Prisma Postgres databases (npx create-db) on macOS with Node 24.11.1, once with prisma@6.19.1 and once with prisma@7.10.1.

  • Missing tables reproduced: history with 1 of 5 tables, migrate deploy on an empty database created only User and exited 0.

  • Detection: history against schema exits 2 and lists the 4 missing tables; schema against the drifted database exits 0.

  • Catch-up flow: after resolve --applied on the old database and migrate deploy on the new one, both report No difference detected. and migrate status is clean. A pg_dump and pg_restore copy carried _prisma_migrations with it.

  • Shadow database: Prisma ORM 6 accepts a second Prisma Postgres database through --shadow-database-url. Prisma ORM 7 rejects one in datasource.shadowDatabaseUrl with P3025, so the v7 page uses prisma dev.

  • Encoding: --output wrote plain UTF-8 with no BOM. A file with EF BB BF prepended fails in db execute --file (syntax error at or near the BOM character) and in migrate deploy (P3018, code 42601); a UTF-16LE file fails with string contains embedded null; psql 17.6 accepts the UTF-8 BOM file. Recovery with resolve --rolled-back plus a regenerated file was run end to end.

  • Not run: Windows PowerShell itself. The page cites Microsoft's about_Character_Encoding for what > writes.

  • Checks: lint:spellcheck, lint:links, lint:versions and test:llm-markdown pass, and the edited pages return 200 on a local dev server with the new anchors present.
  • Follow-ups

  • Other Prisma ORM 6 pages still use --to-schema or --from-schema (squashing, down migrations, patching, Cloudflare D1).

  • Other pages still redirect migrate diff with > (down.sql, forward.sql, D1 migrations).

  • P3025 for two distinct Prisma Postgres databases and P1001 from db execute --file on a UTF-16 file look like tooling issues worth filing.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Changes after review

  • No workarounds for our own tools. apps/docs/AGENTS.md forbids them, so the page no longer says to avoid a Prisma Postgres shadow database because of P3025, and no longer mentions P1001 from db execute --file. The v7 page describes the shadow database by what it must be and shows the local one from prisma dev. A Prisma Postgres shadow database is left out until the guard is fixed.

  • Both are tool defects, now filed. I reproduced the false P3025 again with two fresh Prisma Postgres databases on 7.10.1. TML-3976 covers it and notes the P1001 case.

  • The replace-the-history fallback is gone. It left old rows in _prisma_migrations and dropped custom SQL from archived migrations, and it was not tested for those cases. The section covers only the catch-up migration.

  • Smaller fixes: dotenv/config in the config example, a cleanup step that removes shadowDatabaseUrl before the variable is unset, and a one-line --output example that also runs in PowerShell.
  • Summary by CodeRabbit

    Documentation
    Expanded baselining guidance for Prisma ORM 6 and 7, including how to identify and resolve cases where migration history doesn’t include all schema changes.
    Clarified how to create catch-up migrations for existing databases and apply them to new databases.
    Updated migration examples to use --output for saving SQL files, with guidance on Windows encoding issues and recovery from migration errors.
    * Added notes about tables created with prisma db push that may be missing from new databases deployed with prisma migrate deploy.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: ankur-arch Review duration: 10225m
    All Vouch review confidence checks passed
    Residual risk 0.40 = Change Risk (0.75) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #8933"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 40.1% · Moderate risk
    40.1%
    Repository