Skip to content

Database Migrations

Every schema change goes through a Drizzle migration. Never edit the D1 schema directly, and never edit a migration that has already been applied.

Change the Drizzle schema ─▶ generate ─▶ review the SQL ─▶ test locally ─▶ apply remotely
Terminal window
pnpm --filter @kenresoft-cms/database generate # drizzle-kit generate
pnpm --filter @kenresoft-cms/database migrate:local
pnpm --filter @kenresoft-cms/database migrate:remote

Migrations live in packages/database/migrations as sequential NNNN_*.sql files. The database binding is DB, and the commands read the root wrangler.toml.

pnpm run setup applies remote migrations on first install, and pnpm run update applies new ones before redeploying the Workers, so new code never runs against an old schema. Wrangler records which migrations have run, so applying is safe to repeat.

Some migrations copy or transform existing data. Read the changelog before updating a production deployment.