Have you ever pushed a feature branch that needs database migrations, only to realize your preview deployment can’t actually test it?
We ran into this exact problem when adding a new table migration to a feature PR – the preview showed the UI but crashed on every query.
The dilemma:
Run migrations in a shared preview database? Breaks other developers’ PRs
Skip migrations? Can’t test your feature properly
Use production data? Absolutely not
The “obvious” solution is database branching services like Neon or PlanetScale. They’re excellent products, but they come with tradeoffs: vendor lock-in, another service to manage and pay for, migration complexity if you already have a Postgres database.
There’s a Simpler Way
What if I told you Postgres has had a built-in solution for 20+ years? Schemas.
Not database schemas in the abstract sense. Literal PostgreSQL schemas. Namespaces within a single database.
{ "scripts": { "build": "npm run migrate && next build", "migrate": "prisma migrate deploy" } }
Enter fullscreen mode
Exit fullscreen mode
The magic here: when your ORM runs migrations with ?schema=pr_15, it automatically creates the schema if it doesn’t exist (depending on the ORM), then runs migrations inside it.
// src/lib/schema-utils.ts export function getSchemaName(): string { // Production override if (process.env.DB_SCHEMA) { return process.env.DB_SCHEMA; } // PR number (preferred) if (process.env.VERCEL_GIT_PULL_REQUEST_ID) { return `pr_${process.env.VERCEL_GIT_PULL_REQUEST_ID}`; } // Local dev default return 'public'; }
Clone it, deploy it to Vercel, open some PRs, and see isolated schemas in action.
Final Thoughts
Sometimes the best solution isn’t the newest service – it’s the 20-year-old feature hiding in plain sight.
Postgres schemas aren’t perfect for this use case (they were designed for multi-tenancy, not preview environments), but they’re good enough and work everywhere.
If you’re building on Vercel/Netlify/Railway with an existing Postgres database, give this approach a try. You might find it’s exactly the level of isolation you need, without the operational overhead of another service.
For further actions, you may consider blocking this person and/or reporting abuse
Залишити відповідь