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:generatepnpm --filter @dshu/api db:migrate:localpnpm --filter @dshu/db db:checkMigration rules
Section titled “Migration rules”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.
Backup and recovery
Section titled “Backup and recovery”-
Confirm the environment, database, and point in time. Pause sync jobs and admin operations that write to that database.
-
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. -
Export the current state in a maintenance window. Remote exports can interrupt database queries. For Preview:
终端窗口 pnpm --filter @dshu/scripts export-backup --env previewUse
--out "$BACKUP_PATH"to choose a destination. The default is the ignored.wrangler/backups/directory, with file mode0600. Existing files are preserved. Remote exports use Cloudflare’ssqlite3_cli_onlyformat to include FTS5 data for inspection with the SQLite CLI. Use Time Travel for in-place D1 recovery. -
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.