# SQL and migrations

## The SQL box

The project's **SQL** page runs SQL inside that one project.

- It signs in to the database as the project's own confined role, on a connection used once. That role reaches the project's own section of the database and nothing else: not another project, not the platform's own tables, and it cannot change role.
- It sees all of the project's rows regardless of policies. It is for the project's own members, not for the application. To see what a user is given, use **View as** on the Access rules page.
- A table you create here belongs to the project and starts **closed**: row-level security on, no policy, and nothing allowed to the application. Open it on the Access rules page, or in the same statement batch with `create policy` and a `grant` to the application's role.
- Statements are recorded on the project's audit record.

Read-only members of a workspace cannot run statements that change anything.

## Writing SQL for your project

Your tables live in the project's own schema, named `p_` followed by the project ID for a project made in the console. Inside the SQL workspace, the command-line tool and the connector you can refer to them by their plain names (`select * from notes`).

Three functions tell a policy, a default or a trigger who is calling. Their names and meanings will not change.

| Function | Gives | Meaning |
|---|---|---|
| `core.app_user()` | uuid | The signed-in user making the request. Null when nobody is signed in. |
| `core.app_org()` | uuid | The organization the request acts for. |
| `core.current_app()` | text | The application the database connection belongs to. |

A table created with SQL, opened to the application in the same batch:

```sql
create table tasks (
  id uuid primary key default gen_random_uuid(),
  organization_id uuid not null default core.app_org(),
  owner_id uuid default core.app_user(),
  title text not null,
  done boolean not null default false,
  created_at timestamptz not null default now()
);

create policy own_organization on tasks
  for all
  using (organization_id = core.app_org())
  with check (organization_id = core.app_org());
```

Then allow the application to use it, on the Access rules page under "The application may", or with a `grant` to the application's role. `rezin projects --json` shows that role's name.

## Migrations

A migration is a named SQL file that is applied to a project **once**. Keep them in your repository, named so that they sort in order:

```
migrations/
  0001_create_notes.sql
  0002_add_pinned.sql
```

Apply the ones not yet applied with the [command-line tool](https://rezincore.com/docs/tools/cli.md):

```
rezin migrations list
rezin migrations push ./migrations
```

- Each migration runs in one transaction. If any statement fails, none of it is applied.
- Its name is recorded, so it is never applied twice.
- Files are applied in name order.
- A migration is named like `0001_create_notes.sql` and can be up to 1,000,000 characters.
- Applying one needs a read-write token.

An assistant connected through the [connector](https://rezincore.com/docs/tools/connector.md) applies migrations the same way, with the `apply_migration` tool.

The project's **Data** section lists the migrations applied to it.

## Limits

| What | Limit |
|---|---|
| One SQL request through the tools | 200,000 characters |
| Rows returned by one SQL request through the tools | 1,000 |
| One migration | 1,000,000 characters |

## Known limit

A project's SQL role can see the *names* of other projects' tables and functions through Postgres's own catalog. It cannot read their contents. Separate databases per project would close this and are not built.
