---
title: "Database"
url: "https://preview-docs.dshuniverse.com/en/ops/database/"
lang: "en"
updated: "2026-11-20"
license: "CC-BY-4.0"
licenseScope: "DSH Universe plugin data"
---

# 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.

```bash
pnpm --filter @dshu/db db:generate
pnpm --filter @dshu/api db:migrate:local
pnpm --filter @dshu/db db:check
```

## 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](https://developers.cloudflare.com/d1/sql-api/sql-statements/#pragma-optimize).

## Backup and recovery

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](https://developers.cloudflare.com/d1/reference/time-travel/).

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

   ```bash
   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](https://developers.cloudflare.com/changelog/post/2026-11-12-d1-export-virtual-tables/) 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.
