Replies: 7 comments 4 replies
|
This is not really something Prisma can safely solve by skipping migrations from another branch. After running For disposable development data, the normal workflow is: npx prisma migrate resetand then restore predictable development data through a seed script. If the development data must be preserved independently on each branch, the practical solution is to use one development database per branch rather than switching incompatible migration histories against the same database. For example: Then each branch uses its own # ft/some-enhancement
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/app_dev_some_enhancement"
npx prisma migrate dev
# ft/some-other-enhancement
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/app_dev_some_other_enhancement"
npx prisma migrate devWith Prisma 7, this can remain a normal environment-variable change because the URL is read by import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
datasource: {
url: env("DATABASE_URL"),
},
});If a new feature branch should begin with the current development data, create its database from a dump of the base branch database before running the feature migration: createdb app_dev_some_enhancement
pg_dump --format=custom app_dev_main > app_dev_main.dump
pg_restore --no-owner --dbname=app_dev_some_enhancement app_dev_main.dump
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/app_dev_some_enhancement"
npx prisma migrate devThat gives each branch its own schema state, migration table, and preserved local data, without fighting Prisma's migration history checks. I would avoid trying to manually delete rows from So the two sane workflows are:
|
|
This is a real pain point with Prisma's migration workflow. There are a few strategies that work well in practice: Strategy 1: One dev database per feature branch (cleanest)The most friction-free solution is to use a separate database per branch. With PostgreSQL this is easy: # In your .env or .env.local per branch
DATABASE_URL="postgresql://user:pass@localhost:5432/myapp_ft_some_enhancement"With tools like dotenv-cli + per-branch Services like Neon (branching databases) or PlanetScale (schema branches) make this even smoother if you're not running locally. Strategy 2: Reset and re-migrate on switchIf you want to keep a single DB, the workflow is: # Leaving ft/some-enhancement, going back to main:
npx prisma migrate reset --skip-seed # resets DB to baseline
git checkout main
# Going to ft/some-other-enhancement:
git checkout ft/some-other-enhancement
npx prisma migrate dev # applies only this branch's migrations
Strategy 3: Shadow database + manual baselineFor preserving data, this is more complex but possible:
This is scriptable with a small shell helper. Strategy 4:
|
|
Any non-AI response from people who either maintain Prisma or actually manage this use case in a professional environment, please ? |
|
There is no built-in "stash a migration" feature, and a flag to skip another branch's migrations isn't coming, because the conflict isn't in the files, it's in the database. When In practice the workflow that actually holds up day to day, and what I run, is one dev database per branch. A local Postgres database is basically free to create, and since // prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: { path: "prisma/migrations" },
datasource: {
url: env("DATABASE_URL"),
},
});The piece that makes this painless is wiring # .envrc
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/myapp_$(git branch --show-current | tr '/-' '__')"Now createdb "myapp_$(git branch --show-current | tr '/-' '__')"
npx prisma migrate devEach branch gets its own migration history and its own data, no drift, no resets, and merging a feature to pg_dump --format=custom myapp_main > /tmp/base.dump
createdb myapp_ft_some_enhancement
pg_restore --no-owner --dbname=myapp_ft_some_enhancement /tmp/base.dumpIf the dev data is genuinely disposable, the simpler path is a single database plus git checkout ft/some-other-enhancement
npx prisma migrate reset # drops, replays this branch's migrations, reseedsTwo things to skip:
One more tactical note: if you create a migration but want to inspect or tweak the SQL before it touches your DB, Per-branch databases is the one that scales to a team without anyone thinking about drift. If that sorts it for you, mark it as the answer. |
|
This is a real pain point when working with Prisma migrations across feature branches. Here are two solid approaches: Option 1: Use a separate database per branch (recommended)The cleanest solution is giving each feature branch its own dev database. With Docker this is trivial: # .env.branch (gitignored, set per branch)
DATABASE_URL="postgresql://user:pass@localhost:5432/mydb_$(git branch --show-current)"Each branch gets its own isolated database — zero drift by design. Option 2: Reset + re-migrate on branch switchAdd a git #!/bin/bash
IS_BRANCH_CHECKOUT=$3
if [ "$IS_BRANCH_CHECKOUT" = "1" ]; then
echo "Branch switched — resetting Prisma dev database..."
npx prisma migrate reset --force --skip-seed
npx prisma migrate dev
fiThis automatically rolls the database back to match the current branch schema after every checkout. You lose dev data, but for a feature database that is usually fine. Option 3:
|
|
There is no fully drift-free way to switch between branches with different Prisma migrations while keeping one shared dev database in place. The database state and the checked-out migration history have to agree, otherwise Prisma is correct to report drift. The safest pattern is to avoid sharing the same mutable dev database across feature branches. What I usually recommend is one of these workflows:
For local development, create a database name from the Git branch: export DATABASE_URL="postgresql://user:pass@localhost:5432/myapp_$(git branch --show-current | tr "/-" "__")"Then each branch gets its own migration history and data. You can run: npx prisma migrate dev
npx prisma db seedwithout affecting another branch. This is the cleanest option if you switch branches often.
If the data can be recreated, treat the local database as disposable: npx prisma migrate resetand keep a good seed script. This gives the least drift, but it does not preserve manual local data.
If you need to preserve local data, take a branch snapshot before switching: pg_dump "$DATABASE_URL" > .db-snapshots/feature-a.sqlThen restore when coming back: dropdb myapp_dev
createdb myapp_dev
psql myapp_dev < .db-snapshots/feature-a.sqlThis works, but it is slower and more operationally heavy than one DB per branch.
A flag that skips migrations from another branch sounds convenient, but it would be dangerous because the database would no longer represent the migration history that Prisma sees in the current checkout. For example:
At that point, the correct fix is not to ignore the drift, but to use a database whose state matches the branch. My practical recommendation:
A simple workflow could be: BRANCH_DB="myapp_$(git branch --show-current | tr "/-" "__")"
createdb "$BRANCH_DB" 2>/dev/null || true
export DATABASE_URL="postgresql://user:pass@localhost:5432/$BRANCH_DB"
npx prisma migrate dev
npx prisma db seedThis keeps Prisma, the migration table, and the actual schema aligned for each branch. It also avoids the hidden risk of accidentally generating a migration from a database state that belongs to another branch. Did this resolve it? Feel free to mark as answer if so. |
|
What you're running into is that your dev database is one shared bit of mutable state, but your migrations are per branch. When you jump from branch A (whose migration is applied to the DB) over to branch B (which doesn't have that migration in its There's no way to keep a single database in sync with a bunch of diverging branches at once, but you've got a few options depending on how much you care about the existing data: Option A, a database per branch (best if you work on features in parallel). Point Option B, just reset when you switch (simplest). prisma migrate resetThat re-applies only the current branch's migrations from scratch and runs your seed. You lose any ad hoc data, but a decent I'd go Option A if you're constantly hopping between feature branches, since a DB per branch kills the whole class of problem. Otherwise Hope that helps. If it does, selecting it as the answer would be great. |
Uh oh!
There was an error while loading. Please reload this page.
Question
When working on multiple features at 1 branch per feature, each with a different database change, how to switch between branches without being blocked by drift issues while preserving the dev database's data ?
How to reproduce (optional)
mainwith a dev database in sync with schema ;ft/some-enhancement, make schema changes and runprisma migrate devsuccessfully ;main;ft/some-other-enhancement, make schema changes and runprisma migrate dev, which will fail with the drift detected error.Expected behavior (optional)
This is expected of course, but ideally I'd like a way to "stash" the migration created in
ft/some-enhancementso the database gets back to its original state as I go back tomainand I can make a drift-free migration onft/some-other-enhancement. Alternatively, a flag to skip migrations from a different branch when generating new ones.Information about Prisma Schema, Client Queries and Environment (optional)
OS: Mac v26
Database: PostgreSQL v18
Node.js version: 24.12.0
prisma : 7.7.0
@prisma/client : 7.7.0
Operating System : darwin
Architecture : x64
Node.js : v24.12.0
TypeScript : 6.0.2
Query Compiler : enabled
PSL : @prisma/prisma-schema-wasm 7.6.0-1.75cbdc1eb7150937890ad5465d861175c6624711
Schema Engine : schema-engine-cli 75cbdc1eb7150937890ad5465d861175c6624711 (at node_modules/@prisma/engines/schema-engine-darwin)
Default Engines Hash : 75cbdc1eb7150937890ad5465d861175c6624711
Studio : 0.27.3
Thanks
All reactions