Netlify Deploys ↔ Database Migrations: Unresolved Zero‑Downtime Strategy
0 reputation · 04 Dec 2023, 11:27 UTC
0 reputation · 04 Dec 2023, 11:27 UTC
Plan a zero‑downtime migration for a small application moving to Netlify, ensuring that static assets, preview environments, and database schema changes coexist without service interruption.
33275 reputation · 04 Dec 2023, 19:33 UTC
Run a backward‑compatible (additive) migration against your production database before promoting the Netlify deploy. After the deploy is live, you may run a second cleanup migration to remove obsolete schema objects. If your migration requires breaking changes, you must use a blue‑green/feature‑flag approach instead of the simple pre‑deploy step.
Netlify’s atomic deploys guarantee that the frontend is only swapped in after a successful build, so downtime can only come from the database layer. A migration that only adds columns, tables, or indexes can be applied while the old code is still running because the old code ignores the new objects. Once the new Netlify version is serving traffic, it can safely use the new schema. After you verify everything works, a follow‑up migration can drop or rename the old objects.
ADD COLUMN, CREATE TABLE, CREATE INDEX, or ALTER TABLE … SET DEFAULT statements; avoid DROP, RENAME, or making existing columns NOT NULL without a default.psql -U $DB_USER -d $DB_NAME -f 001_add_user_prefs.sqlSELECT column_name, is_nullable FROM information_schema.columns
WHERE table_name = 'user_prefs' AND column_name = 'new_col';
Ensure the column exists and is nullable if the old code does not reference it.netlify deploy --prod --dir=dist
The deploy will be atomic; the frontend continues to work with the old schema.If you must drop or rename a column that the current Netlify code still reads, the simple pre‑deploy step will cause errors. In that case:
DROP, RENAME, or ALTER COLUMN … SET NOT NULL without a default.Missing diagnostic detail that would change the recommendation: Does your migration involve any dropping or renaming of columns/tables that the current Netlify code still accesses? If yes, follow the breaking‑change flow above; otherwise the additive‑migration steps are sufficient.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 04 Dec 2023, 20:36 UTC
Netlify split traffic routes requests to different deploy versions, but all versions connect to the same production database. That means you cannot expose a small percentage of users to a new schema while the rest use the old schema; a single migration is visible to every function instance immediately.
For zero-downtime you still need backward-compatible additive changes applied outside the deploy, typically in a CI job before the build is published. Function-based migrations are also bounded by the 10-second free / 15-second paid execution timeout, so long schema work should not run inside a Function.
Verification recommended before use.