# scribase schema

Works with Scribase schema files — the typed model-definition language that
compiles to Postgres DDL with RLS policies baked in.

## Subcommands

### `schema compile <file> [--out FILE]`

Compiles a schema file to Postgres SQL. Validates the schema and reports any
errors or warnings.

```sh
scribase schema compile schema.scribase
scribase schema compile schema.scribase --out schema.sql
```

Without `--out`, the SQL is printed to stdout. With `--out`, it is written to
the given file.

### `schema test <file>`

Runs the Aegis policy tests embedded in the schema file. Tests are declared
inline with the schema and verify RLS policy behavior against synthetic
authenticated contexts.

```sh
scribase schema test schema.scribase
```

Output shows each test, its expected outcome, and pass/fail status. A non-zero
exit code means at least one test failed.

## Schema file format

A `.scribase` file defines models (tables) with typed columns, constraints, and
RLS policies in a concise declarative syntax. Example:

```scribase
model orders {
  id         uuid primary key default gen_random_uuid()
  user_id    uuid not null references auth.users
  total      numeric(12,2) not null check (total > 0)
  created_at timestamptz not null default now()

  policy "owner can read" on select
    using (auth.uid() = user_id)

  policy "owner can insert" on insert
    with check (auth.uid() = user_id)
}
```

The compiler generates:
- `CREATE TABLE` with columns and constraints
- `ALTER TABLE ENABLE ROW LEVEL SECURITY`
- `CREATE POLICY` for each declared policy
- `GRANT` statements for application roles

## Notes

- Run `schema test` in CI to catch policy regressions before deployment.
- `schema compile` can be used to preview the SQL before sending it to the server.
- The Scribase schema language is a superset of what `scribase import` can read back from an existing database.

See [Aegis Policy Compiler](https://docs.scribase.com/docs/aegis.md) for the full policy testing reference.
