# CLI: Database, documents and schema
Source: https://sovaria.in/docs/cli-database

Run SQL, manage tables and migrations and environments (with a sample of the data), apply app templates, check app builds and open them as pull requests, read and write documents, inspect the schema and generate types with sov.

Migrations:
```bash
# migrations/20261016_add_note.sql
#   alter table books add column note text;
#   -- migrate:down
#   alter table books drop column note;
sov db migrations --help
```

## sov db

Database (SQL) management

### sov db tables
`sov db tables [OPTIONS]`
List live relational tables in the linked project database.

### sov db sql
`sov db sql [OPTIONS] [QUERY]`
Execute SQL against the project database.
Use --interactive for a mini psql-style loop.

### sov db query
`sov db query [OPTIONS] TABLE`
Read rows with the filters in the request body: QUERY /rest/v1/<table> (same read as GET, no URL limit).

### sov db create-table
`sov db create-table [OPTIONS] NAME COLUMNS_JSON`
Create a new relational table.

### sov db as-of
`sov db as-of [OPTIONS] TABLE AT`
Show TABLE exactly as it was at a moment (sov_as_of).

### sov db schema
`sov db schema [OPTIONS]`
The schema as one short line per table: types, keys, →references, allowed values, indexes, owner rows.

### sov db plan
`sov db plan [OPTIONS] FILE`
Create or extend tables from a plan: checked, then applied as one reversible migration (sov db migrations rollback undoes it).
Columns: objects, or short form like "status text!='draft' in(draft|live)" or "customer_id →customers cascade".

Short form marks: ! required, =default, pk, uniq, idx, in(a|b), >0, →table (then cascade or setnull).

New tables get an id uuid key and created_at. Patterns: owner_rows, public_read, timestamps, soft_delete.

### sov db push
`sov db push [OPTIONS]`
Apply pending migrations (same as `sov db migrations push`, like `supabase db push`).

### sov db pull
`sov db pull [OPTIONS]`
Introspect remote SQL schema and write a migration SQL snapshot (Supabase db pull).

### sov db diff
`sov db diff [OPTIONS]`
Compare remote schema against the last local snapshot from `sov db pull`.

### sov db reset
`sov db reset [OPTIONS]`
Drop all user tables and optionally re-apply local migrations (destructive).

### sov db seed
`sov db seed [OPTIONS]`
Run seed SQL against the linked database (Supabase db seed).

### sov db sleep
`sov db sleep [OPTIONS]`
Whether the project's database and each environment's is asleep (idle databases sleep and wake on the next request: staging, branch and preview databases after 3 idle days, Free projects after 14; paid production never).

### sov db wake
`sov db wake [OPTIONS]`
Wake a sleeping database now; returns once it accepts connections (a few seconds).

### sov db restore
`sov db restore [OPTIONS]`
Restore the database to a moment (point-in-time recovery).
It is recovered beside production and checked first; a safety backup is taken; undo within 7 days (sov governance db-restore-undo). With --into-env / --into-project the backup or moment goes there instead and production stays as it is.

### sov db restore-tables
`sov db restore-tables [OPTIONS] TABLES`
Copy tables from a backup or a moment into production as restored_<table>_<stamp>, beside the live ones (live tables are not changed).
Merge with SQL afterwards, e.g. INSERT … SELECT … ON CONFLICT DO NOTHING.

### sov db tx begin
`sov db tx begin [OPTIONS]`
Begin a relational transaction and return txid.

### sov db tx commit
`sov db tx commit [OPTIONS]`
Commit an open transaction.

### sov db tx rollback
`sov db tx rollback [OPTIONS]`
Rollback an open transaction.

### sov db history list
`sov db history list [OPTIONS]`
Tables that keep history, their versions and how far back they go.

### sov db history on
`sov db history on [OPTIONS] TABLE`
Start keeping every version of TABLE's rows (read them with sov db as-of).

### sov db history off
`sov db history off [OPTIONS] TABLE`
Stop keeping history for TABLE and delete its stored versions.

### sov db replicas list
`sov db replicas list [OPTIONS]`
Replicas with status, connection string and how far behind the primary they are.

### sov db replicas add
`sov db replicas add [OPTIONS]`
Start a read replica (copies the database first; `sov db replicas list` shows when it's ready).

### sov db replicas remove
`sov db replicas remove [OPTIONS] REPLICA_ID`
Remove a replica (apps connected to it lose their connection).

### sov db column add
`sov db column add [OPTIONS] TABLE NAME`
Add a column to an existing table.

### sov db column drop
`sov db column drop [OPTIONS] TABLE NAME`
Drop a column from a table.

### sov db fk add
`sov db fk add [OPTIONS] TABLE COLUMN`
Add a foreign key between tables.

### sov db fk drop
`sov db fk drop [OPTIONS] TABLE COLUMN`
Drop a foreign key.

### sov db migrations new
`sov db migrations new [OPTIONS] NAME`
Create a new timestamped SQL migration file (Supabase-style).

### sov db migrations list
`sov db migrations list [OPTIONS]`
List remote migration log entries or local migration files.

### sov db migrations push
`sov db migrations push [OPTIONS]`
Apply pending .sql migrations through the project's migration history.
Each file is applied atomically and recorded on the server, so every teammate and CI run sees the same history. Put rollback SQL under a `-- migrate:down` line (or in `<name>.down.sql`) to make a migration reversible with `sov db migrations rollback`. A migration rolled back on the server stays rolled back: `push` skips its file unless you pass `--include-rolled-back`.

### sov db migrations rollback
`sov db migrations rollback [OPTIONS]`
Undo the most recent migration using its down SQL.

### sov db migrations pull
`sov db migrations pull [OPTIONS]`
Fetch remote migration log (schema change history) as JSON.

### sov db pitr status
`sov db pitr status [OPTIONS]`
Whether point-in-time recovery is on and the earliest and latest moment you can restore to.

### sov db pitr enable
`sov db pitr enable [OPTIONS]`
Turn on point-in-time recovery (Pro and higher).

### sov db pitr disable
`sov db pitr disable [OPTIONS]`
Stop archiving changes.
What was archived stays restorable until it leaves the window.

### sov db backups list
`sov db backups list [OPTIONS]`
Recent backups (daily, manual, before restores): id, when, size, status, until when kept.

### sov db backups now
`sov db backups now [OPTIONS]`
Take a backup now (db.manual_backups_hour / _month apply).

### sov db backups download
`sov db backups download [OPTIONS] BACKUP_ID`
Download a backup (plain SQL, gzip), streamed to a file.
Restore it anywhere with `gunzip -c FILE | psql "$DATABASE_URL"`. Owners, admins and developers only.

### sov db backups health
`sov db backups health [OPTIONS]`
Backup health: last good backup, failures since, recovery point (RPO), retention and on-demand allowance.

### sov db advisor
`sov db advisor [OPTIONS]`
Index advisor: indexes that would speed up the slowest queries, tested as hypothetical indexes first

### sov db advisor query
`sov db advisor query [OPTIONS] SQL`
Recommend indexes for one statement.

### sov db advisor apply
`sov db advisor apply [OPTIONS]`
Build a recommended index with CREATE INDEX CONCURRENTLY (no write lock); follow it with `status`.

### sov db advisor status
`sov db advisor status [OPTIONS] INDEX_NAME`
Whether an index the advisor started is building, ready, failed or invalid.

### sov db insights
`sov db insights [OPTIONS]`
Query insights: the project's statements by total time, average, calls…

### sov db insights reset
`sov db insights reset [OPTIONS]`
Clear this database's query statistics.

## sov data

Documents and SQL data plane

### sov data doc-put
`sov data doc-put [OPTIONS] COLLECTION DOCUMENT_ID DATA`
Write a plain JSON document.

### sov data doc-get
`sov data doc-get [OPTIONS] COLLECTION DOCUMENT_ID`
Read a plain JSON document.

### sov data doc-delete
`sov data doc-delete [OPTIONS] COLLECTION DOCUMENT_ID`
Delete a document.

### sov data doc-scan
`sov data doc-scan [OPTIONS]`
Scan documents by key prefix or range.

### sov data sql
`sov data sql [OPTIONS] SQL`
Run a SQL query.

### sov data table-create
`sov data table-create [OPTIONS] TABLE_NAME COLUMNS`
Create a relational table.

### sov data table-drop
`sov data table-drop [OPTIONS] TABLE_NAME`
Drop a relational table.

### sov data table-migrations
`sov data table-migrations [OPTIONS]`
List migration log entries.

## sov collections

NoSQL document collections

### sov collections list
`sov collections list [OPTIONS]`
List document collections in the linked project.

### sov collections query
`sov collections query [OPTIONS] COLLECTION`
Field query on a collection or, with --group, a collection group (same rules and row policies as the app).

### sov collections count
`sov collections count [OPTIONS] COLLECTION`
How many documents match the filters.

### sov collections aggregate
`sov collections aggregate [OPTIONS] COLLECTION`
count / sum / average on the server: sov collections aggregate orders --sum total --avg total --where '[["paid","==",true]]'.

### sov collections nearest
`sov collections nearest [OPTIONS] COLLECTION`
Vector search (Firestore findNearest): sov collections nearest items --field embedding --vector '[0.1,0.2,0.3]' --limit 5.

### sov collections delete
`sov collections delete [OPTIONS] PATH`
Delete a collection's documents, or one document, like `firebase firestore:delete [-r]`.
Without -r a document's subcollections stay (as in Firestore); with -r every collection under the path goes too, deepest first. Deletes run through the same collection rules and row policies as the app, 500 per commit.

### sov collections pipeline
`sov collections pipeline [OPTIONS] COLLECTION`
Run a document pipeline: where, aggregate, distinct, sort, offset, limit, select, remove_fields, sample.
Example: sov collections pipeline orders --stages '[{"where": ["status","==","paid"]}, {"aggregate": {"accumulators": [{"op": "sum", "field": "amount", "alias": "total"}], "groups": ["country"]}}, {"sort": [["total","desc"]]}, {"limit": 5}]'

## sov schema

Database schema introspection

### sov schema describe
`sov schema describe [OPTIONS] [TABLE]`
Describe relational tables and columns (information_schema).

## sov gen

Generate types and client artifacts

### sov gen types
`sov gen types [OPTIONS] LANG`
Generate typed models from the live SQL schema (Supabase-style).
TypeScript: supabase's `Database` type (Tables Row/Insert/Update, Views, Functions, Enums) for `createClient<Database>(url, key)`, plus one row type per table. Python: Row/Insert/Update TypedDicts. Dart classes with fromJson/toJson (Flutter), Swift Codable structs (iOS) or Kotlin @Serializable data classes.

## sov inspect

Inspect databases and project resources

### sov inspect db
`sov inspect db [OPTIONS]`
Show table row estimates and database footprint.

## sov templates

App templates: SaaS, marketplace, chat — backend + starter app

### sov templates list
`sov templates list [OPTIONS]`
The templates, what each creates and the tables it adds.

### sov templates show
`sov templates show [OPTIONS] NAME`
What a template creates, step by step (and its SQL with --sql).

### sov templates apply
`sov templates apply [OPTIONS] NAME`
Check, then apply a template to the linked project (each step with your own permissions).

### sov templates app
`sov templates app [OPTIONS] NAME`
Download the template's React starter app, with .env filled in for the linked project.

## sov builds

App builds: check schema, rules, functions and tests; open a pull request

### sov builds check
`sov builds check [OPTIONS] PATH`
Check a build against the project — nothing changes.
Exit 1 when a check fails.

### sov builds pr
`sov builds pr [OPTIONS] BUILD_ID`
Check the build again and open it as a pull request on the linked repository.

### sov builds list
`sov builds list [OPTIONS]`
Recent builds of the project.

### sov builds show
`sov builds show [OPTIONS] BUILD_ID`
One build: its files, checks and pull request.

### sov builds delete
`sov builds delete [OPTIONS] BUILD_ID`
Forget a build (an opened pull request stays on GitHub).

## sov env

Environments: staging, API versions and PR previews (schema, sample or all data)

### sov env list
`sov env list [OPTIONS]`
Production plus the project's environments: kind, database status, what it started with, URL.

### sov env create
`sov env create [OPTIONS] NAME`
Create an environment with its own database in the project's container.

### sov env copy
`sov env copy [OPTIONS] NAME`
What an environment's database started with and how its row copy went.

### sov env delete
`sov env delete [OPTIONS] NAME`
Delete an environment and its database (a snapshot is kept first).

### sov env previews
`sov env previews [OPTIONS]`
Preview environments of the linked GitHub repository's open pull requests.

## sov branches

Branches: environments with their own database, functions and policies

### sov branches list
`sov branches list [OPTIONS]`
Production, every branch and every environment: name, kind (branch, staging or API version), database status and API URL.

### sov branches get
`sov branches get [OPTIONS] NAME`
One branch: id, kind, status and its own API URL.

### sov branches create
`sov branches create [OPTIONS] NAME`
Create a branch: a copy of production's system (schema, functions, policies, settings) with its own database.
--data picks what the database starts with: the structure only (default) or a checkpoint of the data (sample or all, personal columns masked). --from starts it from another branch or environment instead of production.

### sov branches update
`sov branches update [OPTIONS] NAME`
Mark a branch as an API version (live next to production, its URL keeps working) or back to staging.
Its data, functions and policies stay as they are.

### sov branches use
`sov branches use [OPTIONS] NAME`
Run later commands (db, functions, storage, …) against this branch.

### sov branches delete
`sov branches delete [OPTIONS] NAME`
Delete a branch and its database (a snapshot is kept first).
Production can't be deleted here.

### sov branches diff
`sov branches diff [OPTIONS] NAME`
How a branch's database differs from production's (or --against another branch): migrations it has that the other doesn't, and the tables, columns, indexes and policies a deploy would add, change or drop.

### sov branches deploy
`sov branches deploy [OPTIONS] NAME`
Deploy a branch into production (or a staging environment with --to): its migrations, functions, policies and settings, checked first with nothing written (the same as Deploy in the console).
Its rows stay in the branch.

### sov branches reset
`sov branches reset [OPTIONS] NAME`
Make a branch's database again from the one it started from (its parent branch, else production), with the same data option (sample/all rows are copied again, masked as before).
A backup is taken first; its functions and policies are kept.

### sov branches restore
`sov branches restore [OPTIONS] NAME`
Put a branch's database back as it was at a moment (point-in-time, within the plan's window).
Production is never touched; a backup of the branch is taken first.

### sov branches protect
`sov branches protect [OPTIONS] NAME`
Protect a branch: it can't be deleted, reset or restored until protection is turned off (plan env.protection).

### sov branches expire
`sov branches expire [OPTIONS] NAME`
Delete a branch automatically after --days (never a protected one), or --never to keep it.

### sov branches revert
`sov branches revert [OPTIONS] AT`
Go back to a commit on one branch: policies, settings, functions and buckets are put back in that branch's own environment with your permissions; migrations and the database only when asked.
Recorded as new commits.
