Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 7 min read

My Prisma Migrations Say 'Applied by Hand.' My Build Never Runs Them.

Yesterday I published a guide on my site about changing a database schema while people are using the app: expand, write both, backfill, switch reads, contract. It ends with a seven-question checklist. As I have been doin

Yesterday I published a guide on my site about changing a database schema while people are using the app: expand, write both, backfill, switch reads, contract. It ends with a seven-question checklist. As I have been doing with every guide lately, I held my own code up to it before telling anyone else to follow it.

The code is VoiceDash, my white-label client portal for voice AI agencies. It runs on Next.js, Prisma and Postgres (Supabase). It has six migration files in prisma/migrations. That is a small enough history to read every line, which is exactly what I did.

The six migrations

Date Migration What it does
2026-03-17 init 24 tables
2026-03-17 add_trial_ends_at one nullable column on Workspace
2026-09-28 add_agent_calls_synced_until one nullable column on Agent
2026-09-28 conversation_unique_per_agent swaps the unique index on Conversation
2026-10-05 add_custom_domains new CustomDomain table
2026-10-10 client_billing 2 enums, 3 tables, 7 columns

Most of these are the boring kind of change the guide recommends: add something nullable or defaulted, and old code never notices. The interesting parts are in how they reached the database, and in what the schema still carries that nothing uses.

Finding 1: my build never runs a migration

Three of the six files open with a comment like this one:

-- IF NOT EXISTS: this is applied by hand to the live database before deploying,
-- so a later `prisma migrate deploy` must be a no-op.
ALTER TABLE "Agent" ADD COLUMN IF NOT EXISTS "callsSyncedUntil" TIMESTAMP(3);

That "later prisma migrate deploy" does not exist anywhere. The build script is:

"build": "prisma generate && next build"

The only GitHub workflow in the repo is a keepalive ping for Supabase. The README tells you to run npx prisma migrate deploy in production, and was last edited in April. So in practice the process is: I open a SQL session against the live database, paste the migration, then push the code.

Doing it by hand is not automatically wrong. It is how I got the ordering right in one case (more below). But my guide says migrations should "run through the same pipeline in every environment", and mine run through me.

The real cost shows up in the sixth file. client_billing has no IF NOT EXISTS anywhere and no comment about how it was applied. CREATE TYPE "BillingPricingModel" fails the second time you run it. So the honest answer to "if I ran prisma migrate deploy against production today, what would happen?" is: it depends on which of these six rows Prisma's _prisma_migrations table already knows about, and I cannot tell you that from the repo. That uncertainty is the finding. A migration history is only useful if it is the history.

Finding 2: the one I did right was the one that scared me

The conversation_unique_per_agent migration is the change I wrote about in My Unique Index Decided Which Customer Owned Each Phone Call. Calls used to be unique on platformCallId alone. Now they are unique per (agentId, platformCallId), so the same Retell agent connected in two workspaces shows its calls in both.

The old webhook code did this:

await prisma.conversation.upsert({
  where: { platformCallId: call.call_id },
  // ...
});

That where only works while a unique index on platformCallId exists. Drop the index while that code is still serving webhooks and every call that ends in that window can fail to save, because Prisma turns that upsert into an ON CONFLICT ("platformCallId") that Postgres rejects once no unique index matches it.

The commit message for that change says:

It is applied in two steps around the deploy: the new indexes first, then dropping the old unique index once no running code depends on it.

That is expand and contract, done in the right order. The new composite index went in first, which old code ignores. Then the code switched to where: { agentId_platformCallId: ... }. Then, and only then, the old index went away. The new code works with both indexes present; it just cannot store a second copy of a call until the old one is gone.

The backfill was handled the way the guide says, too, mostly by accident of the existing design. The migration resets every agent's sync watermark:

UPDATE "Agent" SET "callsSyncedUntil" = NULL;

That does not copy any data itself. It makes each agent's normal background sync re-read its Retell history from the start, in pages, under a time budget, upserting on the new composite key. Batched, idempotent, separate from the deploy. I did not design the sync as a backfill tool, but that is what it turned out to be.

Two things I would still change about it:

  • The file bundles both steps. I applied it in two halves by hand, but the file runs top to bottom. Anyone replaying migrations into a fresh environment gets the drop immediately after the create. Harmless on an empty database, wrong as documentation of what actually happened in production.
  • CREATE UNIQUE INDEX without CONCURRENTLY. Conversation is the table every call webhook writes to. A plain index build blocks writes for as long as it runs. At my current row count that was short, but no lock_timeout was set either, so if the build had queued behind a long query, the webhooks would have queued behind it. Worth knowing if you copy my pattern: CONCURRENTLY combined with IF NOT EXISTS has a trap. If a concurrent build fails partway, Postgres leaves an INVALID index behind, and a re-run with IF NOT EXISTS sees the name and happily does nothing.

Finding 3: I expand and never contract

My guide's step 5 says the contract step "is the one teams rush." I do not rush it. I skip it.

Workspace.customDomain. The init migration gave every workspace a single customDomain column with a unique index. In October I built custom domains properly, as a separate CustomDomain table with per-client domains (write-up here). The old column is still in the schema. Two API routes still write to it. When I grepped for readers, the only one is the route returning the value it just wrote. The settings component that called that route is not imported anywhere.

So I have a column that can be written, has a unique constraint that can reject writes, and affects nothing. The next person to read the schema (me, in six months) has to work out which of customDomain and customDomains is real.

PhoneNumber. In April I removed the phone numbers feature: pages, routes, components and the Prisma model, in one commit. No migration came with it. The init migration creates a PhoneNumber table, and nothing in the migration history ever drops it. So the table is almost certainly still in production, holding whatever rows it had in April, with foreign keys to Agent and Client. And the schema file and the migration history now disagree. The next time a tool diffs them, it will want to generate a DROP TABLE "PhoneNumber", bundled into whatever unrelated migration happens to be next. That is the one place I least want a destructive statement to appear by surprise.

The fix for both is the step I wrote the guide around: a deliberate contract migration, on its own, after a backup.

The other app: no schema to migrate, so no migrations at all

Upwork Scout, my AI job alert product, runs on Firestore. The guide says to version documents and convert every old shape in one place. Upwork Scout does neither:

const users = userSnaps.docs.map((d) => ({ uid: d.id, ...(d.data() as UserDoc) }));

That as UserDoc is a claim, not a check. Every document written since launch is trusted to match today's type. I covered what that does to the filters in My Job Filters Fail Open. So far it has only grown by optional fields, the one change where skipping versioning works. The first rename will not.

Scoring myself against the checklist

  1. Recent backup, known restore? When I graded my backups I could not confirm point-in-time recovery from the repo, and nothing in the repo answers it now.
  2. Works with the code currently running? Yes, for all five changes after init.
  3. Locks and lock timeout? One non-concurrent index on the busiest table, no timeout anywhere.
  4. Backfill batched and separate? Yes, the one time it mattered.
  5. Every reader of the old shape found? For the index swap, yes. For customDomain and PhoneNumber, I found them by writing this post.
  6. Each deploy rollable on its own? Yes, up to the contract step, which is the point.
  7. Rehearsed on production-sized data? Nothing in the repo shows that it was.

What I am changing, none of it shipped yet:

  • prisma migrate deploy runs as a step before the build, so the migration history and the database stop being two separate records.
  • Confirm what _prisma_migrations actually contains in production, and mark the hand-applied ones with prisma migrate resolve --applied if they are missing.
  • A contract migration that drops Workspace.customDomain and PhoneNumber, after the old route and component are deleted, after a backup.
  • SET lock_timeout at the top of every hand-written migration, and CONCURRENTLY on indexes for Conversation.

The full process, with the Postgres lock list and the Firestore side, is in the original guide on my site: Changing the database schema on a live app without downtime.

Question for anyone running Prisma on a small product: do you let your build run migrate deploy, or do you apply migrations as a separate step you trigger yourself? I have seen good arguments both ways, and right now I am doing neither on purpose.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.