Migrations

Creating and applying database migrations

Migrations

Creating Migrations

  1. Update Prisma schema (prisma/schema.prisma)
  2. Generate migration SQL
  3. Review migration file
  4. Apply to database

Migration Files

Located in migrations/ directory:

  • Named: NNNN_description.sql
  • Example: 0031_add_approval_system.sql

Applying Migrations

Local:

wrangler d1 migrations apply DB --local

Production:

wrangler d1 migrations apply DB --remote

Safe Migrations

  • Use CREATE TABLE IF NOT EXISTS
  • Use ALTER TABLE with checks
  • Add columns with DEFAULT values
  • Use CREATE INDEX IF NOT EXISTS