When to Use Migrations
Use pnpm db:push for a fresh or disposable development database when you do
not need migration history. If you want migration history from the start,
initialize an empty database with pnpm db:generate and pnpm db:migrate.
Production databases should use migration history. For an existing database
that was initialized with db:push, verify a baseline against its current
schema before switching migration tracking on; do not apply a generated full
initial migration as though the database were empty.
Generate and Apply a Migration
- Update the active Drizzle schema. Keep
schema.tsandschema-pg.tsaligned if your project supports both providers. - Run
pnpm db:generateto compare the schema with the latest snapshot in your project's migration history. - Review the generated SQL under
src/lib/db/migrations/for Turso/SQLite orsrc/lib/db/migrations-pg/for PostgreSQL. Add explicit SQL for data transformations when required. - Run
pnpm db:migrateagainst the selected database. - Commit the SQL and snapshot with the schema change.
The first migration can be generated in your project from its selected schema. Generated migrations and Drizzle snapshots form your project's migration history and anchor later schema diffs.
Provider Selection
DB_PROVIDER selects the schema and output directory in drizzle.config.ts:
| Provider | Schema | Migration directory |
|---|---|---|
| Turso / SQLite (default) | src/lib/db/schema.ts | src/lib/db/migrations/ |
| PostgreSQL | src/lib/db/schema-pg.ts | src/lib/db/migrations-pg/ |
Generate migrations for each provider your project supports, using the
corresponding DB_PROVIDER value and configuration.
Review Data Changes
Generated SQL is a proposal based on the difference between Drizzle snapshots. Review table and column removals, type changes, constraints, and index changes before applying it to a database with data. A rename or data transformation may need a custom migration so existing values survive.
Rollback Strategy
Before applying a migration, review its SQL and take a database backup when the change can remove or rewrite data. Drizzle does not generate a safe reverse migration automatically. If an applied migration causes a problem, prefer a new forward migration that restores the intended schema or data. Keep applied migration files append-only; do not edit one to repair a database that has already recorded it as applied.
For production recovery, restore and validate a backup against an isolated database before switching traffic. See the deployment backup and restore runbook.
Schema Conflicts During Upgrades
When an upstream schema change overlaps with your project changes, resolve the schema conflict first and generate a migration from the merged schema:
- Merge upstream and project-owned table and column changes in the active schema. Preserve both sides' intent and include any required data transformation.
- Run
pnpm db:generateand inspect the generated SQL for unexpected drops, type changes, or data loss. - Apply and verify the migration in a development or staging database before
applying it to production with
pnpm db:migrate.
For the source merge and conflict-resolution process, see the Upgrading guide.