# Tutorial: prove your table is private before you ship
Source: https://sovaria.in/docs/tutorial-secure-a-table

Take the Quickstart notes table, check who can read and change it with a simulation, catch a risky policy before it goes live, and make the check part of your deploys.

**What you will build:** a repeatable check that proves anonymous visitors can't read or change your `notes` table, and that signed-in users only reach their own notes — run by hand, on a draft change, and before every deploy.

**Why:** row rules are easy to get almost right. A single `using (true)` makes every note public, and nothing in your app will show it until someone looks. A simulation looks for you, as every kind of caller, without touching data.

## Prerequisites

- The project and `notes` table from the [Quickstart](https://sovaria.in/docs/quickstart) (with row-level security enabled and forced).
- Any plan: Free includes 2 real simulation runs a day (5 a month); single-request checks use one per 5 ([Simulate access before you ship](https://sovaria.in/docs/simulation), [Plan limits](https://sovaria.in/docs/plan-limits)).
- The CLI (Python 3.11+): `npm install -g @sovaria/cli` or `pip install sovaria-cli`, then `sov login`.

## 1. Run a simulation for the table

Terminal:
```bash
sov sim run --resource public.notes
```

**What to look for:** for **Not signed in** (`__guest__`), read, add, update and delete should all be *Denied (expected)*. For **Signed-in user**, they should be *Allowed* — the row rule then limits them to the user's own notes. The summary ends with the number of findings; there should be no critical ones.

The same result is in the console under **Simulation → Access → Request flow**: pick **Step by step**, choose *Not signed in*, then the **Read** chip on `notes`, and press **Simulate this request** to see the path stop at the policy check.

## 2. Preview a risky change

Suppose someone proposes this to "fix" an empty list on a public page:

risky.sql:
```sql
create policy "anyone reads notes" on notes
  for select using (true);
```

Try it without applying it. The script runs with every check and is rolled back; the answer lists what it would add — here, a new row policy on `notes`.

Terminal:
```bash
sov sim change --sql-file risky.sql
```

Because `using (true)` applies to everyone, including visitors, this policy would make every note readable without signing in. Don't apply it; if a page needs public content, put that content in a separate table with its own rules.

## 3. Check every deploy

`--fail-on critical` exits with status 1 when a run has critical findings, so it can stop a pipeline. In CI the CLI reads its token from `SOVARIA_ACCESS_TOKEN` (store it as a CI secret; use a team account set aside for CI) and takes the organization and project as flags.

.github/workflows/deploy.yml:
```bash
jobs:
  check-access:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-python@v5
        with: { python-version: '3.12' }
      - run: pip install sovaria-cli
      - run: sov sim run --org "$ORG_ID" --project "$PROJECT_ID" --fail-on critical
        env:
          SOVARIA_ACCESS_TOKEN: ${{ secrets.SOVARIA_ACCESS_TOKEN }}
          ORG_ID: ${{ vars.SOVARIA_ORG_ID }}
          PROJECT_ID: ${{ vars.SOVARIA_PROJECT_ID }}
```

### Write down who may do what

A simulation finds risky rules on its own; a **rules test** checks the exact promises you make. List the cases in `sovaria.rules.test.yaml` (`sov rules init` writes a starter) and run `sov rules test`: every case is decided by your rules in one request, and the command exits 1 when any case fails. `--drafts policies/` tests local `<policy id>.spl` files in place of the live policies, so a pull request's rules are checked before they are deployed.

sovaria.rules.test.yaml:
```bash
cases:
  - name: visitors can't read notes
    as: guest              # guest | user | app:<role> | team:<role>
    do: read               # read insert update delete | doc:read doc:write | storage:read storage:list | subscribe publish
    on: notes
    expect: deny
  - name: signed-in users can add notes
    as: user
    do: insert
    on: notes
    expect: allow
```

GitHub Action:
```bash
# .github/workflows/access.yml — rules tests + simulation on every pull request
on: pull_request
jobs:
  access:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: sovaria/sovaria-cli@main
        with:
          token: ${{ secrets.SOVARIA_ACCESS_TOKEN }}
          org: ${{ vars.SOVARIA_ORG_ID }}
          project: ${{ vars.SOVARIA_PROJECT_ID }}
          drafts: policies        # optional: test this branch's policy files
          fail-on: critical
```

The action writes `sovaria-rules.xml` (JUnit) for test reports. Each rules test run and each simulation run uses one simulation from your plan's monthly allowance ([Plan limits](https://sovaria.in/docs/plan-limits)).

## Verify

- `sov sim run --resource public.notes` shows no critical findings, and visitors are denied on every action.
- In the console, **Simulation → Issues** lists nothing for `notes`.
- The CI job fails if you temporarily apply the risky policy in a staging environment and run it there (`--env <environment-id>`).

## Common errors

| You see | Why | Fix |
|---|---|---|
| `PLAN_FEATURE_REQUIRED` / `PLAN_LIMIT_REACHED` | Simulations aren't in your plan or this month's runs are used. | Upgrade or wait for the monthly reset — see [Plans, usage and limits](https://sovaria.in/docs/plans-and-limits). |
| Signed-in users denied too | RLS is enabled but not forced, or no access policy decides. | Run `alter table notes force row level security;` — see [Row-level security and access](https://sovaria.in/docs/row-level-security). |
| `401` in CI | No token, or it expired. | Refresh the `SOVARIA_ACCESS_TOKEN` secret. |

## Production notes

- Simulation never writes data, so it is safe against production — but run it on [staging](https://sovaria.in/docs/environments-and-staging) first when you are changing rules.
- Every run, including single-request checks, counts towards the monthly simulation limit.
- Keep the CI token's account to the smallest team role that can run simulations.

## Next steps

- [Simulation in depth](https://sovaria.in/docs/simulation): Every user type, the architecture map, checks and change previews.
- [Staging and deploys](https://sovaria.in/docs/environments-and-staging): Make changes in staging and deploy them with a check.
- [Undo a change](https://sovaria.in/docs/version-history): See what a change did and put it back.
