You're reading docs for Nextly Alpha. APIs may change between releases.
Guides
Production migrations
Ship database schema changes to production safely with the Nextly migration CLI. Forward-only model, GitHub Actions, Vercel build step, and other-platform patterns.
Nextly's schema-evolution model splits responsibilities cleanly between development and production:
Local development:next dev + Nextly's HMR pipeline auto-applies schema changes as you edit nextly.config.ts.
Production: schema changes are committed as .sql files in your repo. A CI/CD step (or your laptop) runs nextly migrate against the production database before the new app code is deployed.
The deployed Next.js app never touches the schema. No boot-time auto-apply, no advisory locks, no race conditions across serverless cold-starts. This page covers the three common deploy patterns. Pick whichever fits your stack.
After you edit nextly.config.ts, generate a .sql file capturing the change. Commit it to git.
nextly migrate:check
CI gate. Verifies migration file integrity and config drift. Does NOT connect to a database.
nextly migrate
Applies pending .sql files to the target database. Run from CI or your laptop.
nextly migrate:status
Shows applied / pending / failed migrations with their checksums and durations.
nextly migrate:fresh
Wipes all tables and re-applies every migration. Local-dev only. Confirmation required.
Global flags inherited from the root nextly command: --config <path> (custom nextly.config.ts path), --cwd <path>, --verbose, -q/--quiet.
There is no migrate:rollback or migrate:down. Forward-only is by design: rollback for live production data is intrinsically lossy. To undo an applied change, write a NEW corrective migration that reverses it.
Most teams should use this. The migration runs on a CI machine that explicitly has your prod DB credentials; if it fails, the Vercel deploy never happens. Old code keeps serving traffic on the old schema.
nextly migrate:check verifies file integrity + no uncommitted schema changes.
nextly migrate applies pending .sql files against the prod DB.
Vercel deploys the new app code.
If step 2 or 3 fails, step 4 doesn't run.
Setting NEXTLY_APPLIED_BY is optional but recommended. It populates the applied_by column in nextly_migrations so post-incident debugging can answer "which CI job applied this row?" When unset, the runtime falls back to GITHUB_ACTOR -> USER -> hostname.
The default scaffold ships this build script in package.json:
{ "scripts": { "build": "nextly migrate && next build" }}
Configure DATABASE_URL as a Vercel environment variable (available at build time). Every Vercel deploy applies pending migrations before compiling Next.js.
Trade-offs:
Zero CI setup. Push to GitHub, Vercel does everything.
Multiple Vercel deploys can race if you push twice in rapid succession (e.g. preview + production, or two PRs merging close together). Nextly's filename UNIQUE constraint catches double-insert; the second build will exit non-zero.
The Vercel build machine needs prod DB access from its IP range (check with your DB host).
Do NOT put nextly migrate in startCommand. startCommand runs on every container start and would race across replicas, which is precisely the failure mode this guide is designed to avoid.
Don't call nextly migrate from your deployed app's runtime. It's a CLI tool, not a runtime API. Nextly enforces this with an ESLint rule for code in init/, route-handler/, dispatcher/, api/, actions/, direct-api/, routeHandler.ts, and next.ts.
Don't edit applied migration files. The hash check catches this and aborts with MIGRATION_TAMPERED. To change something already applied, write a new corrective migration.
Don't run nextly migrate concurrently (e.g. two CI jobs racing). Nextly does not take an advisory lock; the filename UNIQUE constraint on nextly_migrations will catch double-insert, but the second instance will exit non-zero. Single-instance operator responsibility.
The failure row is recorded with status='failed' and structured error_json containing the SQL state, message, and (on MySQL once F15 ships) the failing statement.
Inspect with nextly migrate:status --verbose. The error JSON tells you what broke.
Either fix the SQL (revert + regenerate via migrate:create) or fix the underlying database state.
Re-run nextly migrate. The retry path deletes the prior failed row inside the same transaction, so the unique-filename constraint doesn't block re-application.
For MySQL specifically, partial state is possible because MySQL DDL auto-commits per statement. Manual cleanup may be needed; see MySQL caveats for the recovery playbook.
The forward-only model means you don't roll back -- you write a new migration that fixes the previous one. Examples:
Accidentally added a NOT NULL column without a default and the deploy failed -> write a new migration that backfills the column then re-applies the constraint.
Renamed a column too aggressively and broke a downstream service -> write a new migration that adds the old column name back as a copy until consumers are updated.
Need to drop a table you added in a recent migration -> write a new migration with DROP TABLE.
The audit trail in nextly_migrations keeps both rows, so post-incident review can see exactly what was applied when, and by which CI job.