# Database and migrations

Each deployed environment of a project with the `DATABASE` capability gets
its own PostgreSQL database (Cloud SQL), reachable only by that
environment's functions. Development and production never share data.

## Declaring the database

`repobot.deploy.json` at the repository root:

```json
{ "clientOnly": false, "capabilities": ["AUTH", "DATABASE", "EMAIL"], "authMethods": ["email-code"] }
```

The full-stack starter (`--template full-stack`) and `repobot-saas` already declare it.

## Changing the schema: migrations

The SQL files are the schema; nothing creates tables implicitly.

1. Add `firebase/functions/migrations/<YYYYMMDDTHHMMSS>__<what>.sql`, for
   example `20261125T120000__create_note.sql`:

   ```sql
   create table note (
       id text primary key,
       owner_user_id text not null,
       title text not null,
       body text not null default '',
       row_created_at timestamptz not null default now(),
       row_updated_at timestamptz not null default now()
   );
   ```

2. Mirror it in Drizzle under `firebase/functions/src/Data/<Domain>/` and
   register the id prefix in `src/Data/TablePrefix.ts`. CI fails when the
   SQL and the Drizzle mirror disagree.
3. Commit and deploy. Every deploy that touches backend files runs the
   migrations that have not run yet, in filename order, each in its own
   transaction, and records them in the `schema_migrations` table. The
   deploy's `MIGRATING` stage is this step; a failing migration fails the
   deploy and leaves the database as it was before that file.

Rules: never edit or delete a migration that has run anywhere (write a new
one); there are no down-migrations; prefer additive changes. The starter's
`docs/migrations-and-db.md` and `docs/adding-a-domain.md` have the full
recipe, from SQL to service, GraphQL and page.

Locally, the starter runs Postgres with `npm run dev:up` (Docker, or
`DB_MODE=embedded` without Docker) and applies migrations with
`npm run migrate`.

## Looking at a deployed database

```bash
spaceboy db tables --project prj_... --target development
spaceboy db migrations --project prj_...
spaceboy db query "select id, title from note order by row_created_at desc limit 5" --project prj_...
spaceboy db query "delete from note where title = 'test'" --project prj_... --write
```

- Owners and admins only. The query runs with the environment's own
  database role, a 10-second statement limit and a 500-row result cap
  (`truncated: true` when cut).
- Statements that change data are refused without `--write`. Every
  statement is recorded in the organization's audit log.
- Use migrations, not `db query`, for schema changes: a table created by
  hand is unknown to the migration ledger and to the Drizzle drift check.

## Not available

- No direct connection string: the database is not reachable from outside
  the platform, so `psql` and ORMs on your machine cannot connect.
- No CLI backup, restore or export. Deleting an environment
  (`spaceboy undeploy`) deletes its database permanently.
