Skip to content

Database

Drizzle schemas live in packages/db/src/schema. Generated SQL migrations live in packages/db/migrations. After editing the schema, generate and verify the migration locally.

终端窗口
pnpm --filter @dshu/db db:generate
pnpm --filter @dshu/api db:migrate:local
pnpm --filter @dshu/db db:check

Deployment migrates the target database before deploying Workers. Keep migrations compatible with existing Workers. Deploy code that stops using a column before dropping it. Run large backfills in batches and review the generated SQL in the PR.

Deployment runs PRAGMA optimize after migrations to update planner statistics. Run it after manual index changes too, then inspect the actual query with EXPLAIN QUERY PLAN, rows read, and SQL duration. An index existing does not prove that the plan or performance meets its budget. See D1 planner maintenance.

  1. Confirm the environment, database, and point in time. Pause sync jobs and admin operations that write to that database.

  2. For Preview, look up the bookmark with pnpm --filter @dshu/api exec wrangler d1 time-travel info DB --env preview --timestamp "$UTC_TIMESTAMP". Retention depends on the Cloudflare plan. See D1 Time Travel.

  3. Export the current state in a maintenance window. Remote exports can interrupt database queries. For Preview:

    终端窗口
    pnpm --filter @dshu/scripts export-backup --env preview

    Use --out "$BACKUP_PATH" to choose a destination. The default is the ignored .wrangler/backups/ directory, with file mode 0600. Existing files are preserved. Remote exports use Cloudflare’s sqlite3_cli_only format to include FTS5 data for inspection with the SQLite CLI. Use Time Travel for in-place D1 recovery.

  4. Validate the recovered tables and cursor. Reconcile the projection, KV indexes, and public responses before resuming writes. Rebuild indexes if needed.

Upstream sync can recover some plugin metadata. User accounts, reviews, and audit records require a backup. A Production restore has yet to be rehearsed.