fix(api): warn when deployed schema drifts from schema.prisma - #88
fix(api): warn when deployed schema drifts from schema.prisma#88github-actions[bot] wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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>
| Reconciling is one command, and it is worth reading before running: | ||
|
|
||
| ```sh | ||
| DATABASE_URL="…" bunx prisma migrate diff \ |
There was a problem hiding this comment.
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>
Production served
The column agentConversationAttachment.position does not existfor days while everydeploy logged
No pending migrations to apply. Both statements were true:_prisma_migrationshad afinished 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 wasagentConversationAttachment_submissionId_createdAt_idx, an index that exists in the database and inno migration file.
migrate deploycan never detect this: it compares the migrations directory to atable of names, never to the schema.
migrate diff --exit-codedoes compare them, so the build now runs it straight aftermigrate deployand 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 diffagainst it now reportsNo difference detected.Summary by cubic
Warns during API build when the deployed database schema drifts from
packages/db/prisma/schema.prisma, catching casesprisma migrate deploycan miss. Adds a post-deploy diff check and docs on how to reconcile.prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --exit-codeaftermigrate deployinapps/api/scripts/build-func.mjs.docs/setup.mdto explain whymigrate deployisn’t proof of schema correctness and include the reconciliation command.Written for commit 6bf96d7. Summary will update on new commits.