Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions apps/api/scripts/build-func.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execSync } from "node:child_process";
import { execSync, spawnSync } from "node:child_process";
import {
cpSync,
existsSync,
Expand Down Expand Up @@ -182,11 +182,52 @@ if (!process.env.VERCEL) {
} else if (!directDatabaseUrl) {
console.log("• no database URL at build time — skipping migrations");
} else {
const dbDir = join(repoRoot, "packages/db");
const dbEnv = { ...process.env, DATABASE_URL: directDatabaseUrl };

console.log("• applying migrations (prisma migrate deploy)...");
execSync(`${bun} x prisma migrate deploy`, {
cwd: join(repoRoot, "packages/db"),
cwd: dbDir,
stdio: "inherit",
env: { ...process.env, DATABASE_URL: directDatabaseUrl },
env: dbEnv,
});
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

bun,
[
"x",
"prisma",
"migrate",
"diff",
"--from-config-datasource",
"--to-schema",
join("prisma", "schema.prisma"),
"--exit-code",
],
{ cwd: dbDir, encoding: "utf8", env: dbEnv },
);

if (drift.status === 0) {
console.log("✓ schema matches");
} else if (drift.status === 2) {
console.log("");
console.log("!! THE PRODUCTION SCHEMA DOES NOT MATCH schema.prisma !!");
console.log(
" Every migration is recorded as applied, so `migrate deploy` will keep reporting",
);
console.log(
" nothing pending while queries fail on columns that are not there. Reconcile with",
);
console.log(
" `prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --script`.",
);
console.log("");
console.log(drift.stdout || "");
} else {
console.log(
`• could not compare the schema (${drift.stderr?.trim() || "unknown error"})`,
);
}
}
22 changes: 22 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ builds, and the pages that touch them fail. Test schema changes locally, where
worse: every preview applied its own migrations to the production database, so on
2026-08-07 the live schema ran six migrations ahead of the live code all day.

### `migrate deploy` is not proof the schema is right

The build follows the deploy with `prisma migrate diff --exit-code` against
`schema.prisma` and shouts in the build log when they disagree. **`No pending
migrations to apply` only means `_prisma_migrations` has a row for every file** —
it says nothing about what the tables actually look like.

They came apart once. A `prisma db push` shaped production from a laptop, the
migration rows were recorded as applied without their SQL ever running, and
`agentConversationAttachment` went live without its `position` column. Every deploy
reported nothing pending, for days, while `conversations.builderById` returned 500.
The tell is an object in the database that no migration defines — there was an
`agentConversationAttachment_submissionId_createdAt_idx` that appears in no
migration file, only in a `db push` of an older schema.

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

--from-config-datasource --to-schema prisma/schema.prisma --script
```

## Secrets hygiene

`.gitignore` ignores `.env` and `.env.*` with one negation for `.env.example`, so
Expand Down
Loading