Skip to content

fix(api): warn when deployed schema drifts from schema.prisma - #88

Open
github-actions[bot] wants to merge 1 commit into
mainfrom
lewis/schema-drift-check
Open

fix(api): warn when deployed schema drifts from schema.prisma#88
github-actions[bot] wants to merge 1 commit into
mainfrom
lewis/schema-drift-check

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Production served The column agentConversationAttachment.position does not exist for days while every
deploy logged No pending migrations to apply. Both statements were true: _prisma_migrations had a
finished row for 20260805220000_conversation_attachments, and the column it creates was not there.

Production had been shaped by prisma db push, not by the migrations — the giveaway was
agentConversationAttachment_submissionId_createdAt_idx, an index that exists in the database and in
no migration file. migrate deploy can never detect this: it compares the migrations directory to a
table of names, never to the schema.

migrate diff --exit-code does compare them, so the build now runs it straight after migrate deploy
and prints the drift plus the command to reconcile. It only warns — failing the deploy would block
shipping the fix for the drift.

The production database has already been reconciled by hand (24 statements, one transaction, all
affected tables empty); migrate diff against it now reports No difference detected.


Summary by cubic

Warns during API build when the deployed database schema drifts from packages/db/prisma/schema.prisma, catching cases prisma migrate deploy can miss. Adds a post-deploy diff check and docs on how to reconcile.

  • Bug Fixes
    • Run prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --exit-code after migrate deploy in apps/api/scripts/build-func.mjs.
    • Log a clear warning and print the diff when exit code is 2 (build does not fail).
    • Update docs/setup.md to explain why migrate deploy isn’t proof of schema correctness and include the reconciliation command.

Written for commit 6bf96d7. Summary will update on new commits.

Review in cubic

@vercel

vercel Bot commented Aug 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
crm-agent Ready Ready Preview Aug 7, 2026 7:37pm
crm-api Ready Ready Preview Aug 7, 2026 7:37pm
crm-app Ready Ready Preview Aug 7, 2026 7:37pm

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/setup.md">

<violation number="1" location="docs/setup.md:134">
P3: The reconcile command uses `--to-schema prisma/schema.prisma`, which only resolves when run from inside `packages/db` — the repo root has no `prisma/schema.prisma` and no `prisma.config.ts`, so a self-hoster copying this into the quick-start context (root) gets a schema/file-not-found error. The section never states the working directory. Consider noting that this must be run from `packages/db` (or using the full `packages/db/prisma/schema.prisma` path and the config), so the command is reproducible.</violation>
</file>

<file name="apps/api/scripts/build-func.mjs">

<violation number="1" location="apps/api/scripts/build-func.mjs:197">
P2: The drift diff output is captured with `spawnSync` and all defaults, which caps both stdout and stderr at Node's 1 MiB `maxBuffer`. This check is specifically meant to surface exactly the case where the two schemas disagree — the human-readable summary printed via `drift.stdout` grows with the number of differing tables/columns. If a real drift produces output across the 1 MiB limit, `spawnSync` throws a `RangeError: stdout maxBuffer length exceeded` and the whole build script dies, turning a by-design non-fatal warning into a broken deploy. Consider raising `maxBuffer` (and, optionally, explicitly treating a spawn failure so it still only warns).</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

console.log("✓ migrations applied");

console.log("• checking the deployed schema against schema.prisma...");
const drift = spawnSync(

@cubic-dev-ai cubic-dev-ai Bot Aug 7, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The drift diff output is captured with spawnSync and all defaults, which caps both stdout and stderr at Node's 1 MiB maxBuffer. This check is specifically meant to surface exactly the case where the two schemas disagree — the human-readable summary printed via drift.stdout grows with the number of differing tables/columns. If a real drift produces output across the 1 MiB limit, spawnSync throws a RangeError: stdout maxBuffer length exceeded and the whole build script dies, turning a by-design non-fatal warning into a broken deploy. Consider raising maxBuffer (and, optionally, explicitly treating a spawn failure so it still only warns).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/scripts/build-func.mjs, line 197:

<comment>The drift diff output is captured with `spawnSync` and all defaults, which caps both stdout and stderr at Node's 1 MiB `maxBuffer`. This check is specifically meant to surface exactly the case where the two schemas disagree — the human-readable summary printed via `drift.stdout` grows with the number of differing tables/columns. If a real drift produces output across the 1 MiB limit, `spawnSync` throws a `RangeError: stdout maxBuffer length exceeded` and the whole build script dies, turning a by-design non-fatal warning into a broken deploy. Consider raising `maxBuffer` (and, optionally, explicitly treating a spawn failure so it still only warns).</comment>

<file context>
@@ -182,11 +182,52 @@ if (!process.env.VERCEL) {
 	console.log("✓ migrations applied");
+
+	console.log("• checking the deployed schema against schema.prisma...");
+	const drift = spawnSync(
+		bun,
+		[
</file context>
Fix with cubic

Comment thread docs/setup.md
Reconciling is one command, and it is worth reading before running:

```sh
DATABASE_URL="…" bunx prisma migrate diff \

@cubic-dev-ai cubic-dev-ai Bot Aug 7, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The reconcile command uses --to-schema prisma/schema.prisma, which only resolves when run from inside packages/db — the repo root has no prisma/schema.prisma and no prisma.config.ts, so a self-hoster copying this into the quick-start context (root) gets a schema/file-not-found error. The section never states the working directory. Consider noting that this must be run from packages/db (or using the full packages/db/prisma/schema.prisma path and the config), so the command is reproducible.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At docs/setup.md, line 134:

<comment>The reconcile command uses `--to-schema prisma/schema.prisma`, which only resolves when run from inside `packages/db` — the repo root has no `prisma/schema.prisma` and no `prisma.config.ts`, so a self-hoster copying this into the quick-start context (root) gets a schema/file-not-found error. The section never states the working directory. Consider noting that this must be run from `packages/db` (or using the full `packages/db/prisma/schema.prisma` path and the config), so the command is reproducible.</comment>

<file context>
@@ -113,6 +113,28 @@ builds, and the pages that touch them fail. Test schema changes locally, where
+Reconciling is one command, and it is worth reading before running:
+
+```sh
+DATABASE_URL="…" bunx prisma migrate diff \
+  --from-config-datasource --to-schema prisma/schema.prisma --script
+```
</file context>
Fix with cubic

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.

1 participant