docs/agents/database.md

Database

Database

Source of truth

  • The database schema source of truth is the declarative files in apps/supabase/schemas/ (numbered SQL files).
  • To understand structure, read the relevant declarative schema file first.

Migrations (apps/supabase)

  • Migrations are generated from the declarative schemas and must be reviewed.
  • Never hand-write migrations from scratch.
  • Generate via declarative schemas, then review and patch the generated migration as needed (RLS/policies/views/grants/etc.).

Where to run

Run these from apps/supabase.

npx supabase start --workdir ..             # Start local Supabase
pnpm seed                                   # Seed database
pnpm apply-migrations                       # Apply pending migrations
pnpm create-migration <name>               # Generate new migration

Declarative workflow

  1. Edit the appropriate file in supabase/schemas/.
  2. Generate migration: pnpm create-migration .
  3. Apply migration: pnpm apply-migrations.

Diff caveats to verify and patch

  • DML (insert/update/delete) not captured.
  • View ownership/grants, security_invoker, materialized views.
  • Altered view column types may not recreate views.
  • RLS/alter policy, column privileges.
  • Schema privileges, comments, partitions.
  • alter publication ... add table ..., domains.
  • Duplicate grants from default privileges.