# SQL and PostgreSQL

> How trimtest selects pgTAP and plain psql test scripts for SQL and PostgreSQL projects.

trimtest runs SQL tests in two forms and selects them from the static graph only (no recorded tier). The
[safety rules](/concepts/safety-rules/) apply: what it cannot trace runs more tests, never fewer.

| Form | How it is recognised | Command |
|---|---|---|
| pgTAP | the script calls `plan()`, `no_plan()`, `runtests()` or `finish()` | `pg_prove <files>` |
| Plain script | any other test script | `psql -X -q -v ON_ERROR_STOP=1 -f <file>`, one process per file |

A script is a **test** when its name ends in `_test.sql`, `_tests.sql` or `.test.sql`, starts with `test_`, sits in
a `test/`, `tests/` or `t/` directory (not under `fixtures/`, `data/`, `helpers/`, `support/`, `lib/` and similar, and
not called `setup*`, `teardown*` or `schema*`), or is a pgTAP script. Add [`[tests]`](/reference/config/) globs for
other conventions.

## Detection

The directory above the `test/`, `tests/` or `t/` folder is the project root, and `pg_prove` and `psql` run there.
A project is detected when a script in such a folder is a pgTAP test or has a test-looking name. A plain script in
`tests/` with neither only counts when no other project (Python, Ruby, ...) shares its root, because those
repositories keep SQL fixtures there. Force a project with:

```toml
[[project]]
kind = "sql"
dir = "db"
```

## The database

trimtest never creates, migrates or drops a database. Tests run against whatever the usual `PG*` variables
(`PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER`, `PGPASSWORD`, `PGSERVICE`) or a `~/.pgpass` entry describe, exactly as if
you had typed the command. A failed connection makes `psql` or `pg_prove` exit non-zero, so those tests count as
failed. To run inside a container, prefix the commands:

```toml
[runner]
sql = "docker compose exec -T db"
```

## What selects what

A file **provides** the objects it creates, alters, drops or writes to: `create table users`, `alter function f`,
`insert into items`, `create index .. on t`, `comment on` and `grant .. on`. It **depends on** every identifier it
mentions, including names inside strings and function bodies (`has_table('users')`). Tests that name `users` are
selected when a file providing `users` changes, and so are tests of files that reference it in turn (a foreign key or
a view over the table).

- `\i path` and `\ir path` are file edges. `\i` is relative to the project root (where psql runs), `\ir` to the
  including file.
- `COPY .. FROM 'file'` and `\copy .. from 'file'` with a literal path make the script depend on that file.
- Comments and whitespace do not count as a change. Strings, quoted identifiers, dollar-quoted bodies (`$$ .. $$`),
  psql meta-command lines and `/*+ hint */` comments do.
- A script that runs as `begin; .. rollback;` (the pgTAP idiom) leaves nothing behind, so other files do not depend on
  its objects. Temp objects are never providers, and a test that does not roll back still provides what it creates.
- A new test always runs.

## What runs everything

- `migrations/`, `migrate/`, `db/migrate/`, `deploy/`, `revert/` and `verify/` (sqitch) below the project root.
- `sqitch.plan`, `sqitch.conf`, `.proverc`, `pg_prove.conf`, `.psqlrc`, `schema.sql` and `structure.sql`.

## What makes a file depend on everything

Anything the static graph cannot see makes the file depend on every non-test file of the project, so a change
anywhere selects what uses it: `EXECUTE` of built SQL (`EXECUTE format(..)`, but not `GRANT EXECUTE` or
`EXECUTE FUNCTION` in a trigger), `\gexec`, and a variable used as an include path (`\i :file`).

## Limits

- Static only. A name used only through `search_path` tricks, a role-dependent default, or an extension's own
  objects is not traced; the same name in two files links the test to both.
- A SQL file read by another project's tests (a seed loaded by a Python test) still selects those tests: they are
  matched by file name as for any data file.
- Results come from the exit status. A failing `pg_prove` run marks every file in that command as failed.
