Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# The documentation site on GitHub Pages, rebuilt on every push to main:
# The documentation site on GitHub Pages: built on every pull request, which
# fails if agents/ is not what the pages write, and deployed from main.
# odoc's reference from the .mli files, then Starlight's pages around it.
name: docs

on:
push:
branches: [main]
pull_request:

# A build reads the repository and nothing more; the deploy job alone is
# given the rights to publish.
permissions:
contents: read
pages: write
id-token: write

# A newer push to a pull request replaces its build; main's deploys queue
# and always finish.
concurrency:
group: pages
cancel-in-progress: true
group: pages-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
deploy:
build:
runs-on: ubuntu-24.04
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/checkout@v5
- uses: ocaml/setup-ocaml@v3
with:
Expand All @@ -38,8 +38,23 @@ jobs:
# the token, past its limit for a runner's shared address.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# The build writes agents/ from the pages: a page changed without it
# differs here.
- run: git add --intent-to-add agents/ && git diff --exit-code agents/
- uses: actions/upload-pages-artifact@v5
with:
path: _build/docs
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/deploy-pages@v5
id: deployment
16 changes: 14 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ make test # starts the test Postgres in Podman, runs every test;
# RUNTIME=docker for Docker
make lint # formatting, odoc, and the release build
make fmt # format in place
make docs # the documentation site into _build/docs, as Pages publishes it
make docs # the documentation site into _build/docs, as Pages publishes it, and
# agents/ from its pages
make docs-serve # the built site, served locally, search included
make release VERSION=1.2.3 # once CHANGES.md names it: tag and release with open-opam
```
Expand All @@ -25,7 +26,8 @@ Bun. It runs every suite under `rowtype-test run`, one Postgres for the
whole run, removed at its end; each suite on Postgres makes a template and
a copy per case in it, and `test_sqlite` needs none. CI runs `make lint`
and `make test` on OCaml 5.5, and `make test` again in Docker, and
`docs.yml` publishes the site to GitHub Pages on every push to `main`.
`docs.yml` builds the site on every pull request and publishes it to GitHub
Pages from `main`.

## Layout

Expand Down Expand Up @@ -61,6 +63,10 @@ docs/ the documentation site, a Starlight site in Hypha's look,
<Code>, odoc's reference copied into public/reference/
by make docs, and the site built into _build/docs,
which dune clean removes
agents/ the documentation for a user's agent, generated by
starlight-hypha-agents from the pages, `make docs`:
llms.txt, the catalogue; pages/, each page as
Markdown; installed by the rowtype package
examples/ the programs the site shows, each built, the SQLite ones
run with the tests, and beside them each piece a page
shows that is no program of its own, a plain module
Expand Down Expand Up @@ -191,6 +197,12 @@ Each rule comes with why it exists and the test that catches a break.
starts leaves the transaction, and its lock, on a connection its caller
believes is free. Test: `transactions` in `test_rowtype` and in
`test_sqlite`, `cancelled while waiting` among them.
- **`agents/` is generated, never edited.** starlight-hypha-agents writes
it from the documentation's pages and its options in
`docs/astro.config.mjs`: a page changed is `make docs` run in the same
commit. Why: a user's agent reads what the release installs, offline,
and a page it reads must be the release's. Test: the docs workflow builds
the pages on every pull request and fails if `agents/` then differs.
- **The one module `open` is `open Shape` in `src/rowtype.ml`, and a local
open is a span's alone**, `Mtime.Span.(10 * s)`. Why: the walk exists to
pattern-match that GADT, and every constructor qualified would bury the
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## Unreleased

- The rowtype package installs its documentation for agents: each page as
Markdown and `llms.txt`, at `$(opam var rowtype:doc)`. The README's *With an
agent* gives the line for a project's `AGENTS.md` that sends an agent there.

## 0.10.2 (2026-11-10)

- rowtype is published to open-opam, `opam repo add open-opam
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ let () =

The same statements run on SQLite unchanged: [SQLite](https://hyphatech.github.io/rowtype/guide/sqlite/).

## With an agent

Each release installs its documentation for an agent with it, at
`$(opam var rowtype:doc)`: `llms.txt`, and every page in Markdown. An agent
reads it when your project's `AGENTS.md` tells it to:

```md
Before writing code with rowtype, read `$(opam var rowtype:doc)/llms.txt` and
the pages it lists for the parts you need, and use those parts rather than
writing your own.
```

## Contributing

See [AGENTS.md](AGENTS.md).
Expand Down
28 changes: 28 additions & 0 deletions agents/llms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# rowtype

> SQL queries and migrations for modern OCaml.

## Start here

- [Getting Started](pages/index.md): SQL queries and migrations for modern OCaml.
- [Quick start](pages/quick-start.md)
- [Why not Caqti?](pages/why-not-caqti.md)

## Guides

- [Statements](pages/guide/statements.md)
- [Columns and types](pages/guide/columns.md)
- [Bulk writes](pages/guide/bulk-writes.md)
- [Errors](pages/guide/errors.md)
- [Transactions](pages/guide/transactions.md)
- [Pools](pages/guide/pools.md)
- [Checking statements](pages/guide/checking-statements.md)
- [SQLite](pages/guide/sqlite.md)
- [Migrations](pages/guide/migrations.md)
- [Migrating from a test](pages/guide/migrating-from-a-test.md)
- [A database for a test run](pages/guide/test-databases.md)
- [A library's migrations](pages/guide/library-migrations.md)

## Reference

- [Reference](pages/reference.md)
75 changes: 75 additions & 0 deletions agents/pages/guide/bulk-writes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Bulk writes

Three ways write many rows, each in one round trip: a list bound as an
array, one statement run for each value, and a load through `COPY`.

## A list bound as an array

A list bound as an array writes many rows in one statement and one round
trip:

```ocaml title="add_all.ml"
module S = Rowtype
module Pg = Rowtype_postgres

let add_users =
S.exec
~params:S.(t2 (array text) (array text))
"insert into users (name, email) select * from unnest($1::text[], \
$2::text[])"

let add_all db users = Pg.run db add_users (List.split users)
```

For a few rows, this is the simplest: one statement, which
[`verify`](checking-statements.md) checks as any other.

## One statement for each value

`Pg.run_many db statement values` runs an `exec` statement once for each
value in one round trip, all or nothing: if any run fails, none applies.

```ocaml title="add_each.ml"
module S = Rowtype
module Pg = Rowtype_postgres

let add_user =
S.exec
~params:S.(t2 text text)
"insert into users (name, email) values ($1, $2)"

let add_each db users = Pg.run_many db add_user users
```

A statement that answers a row is refused before anything is sent, since a
batch reads no rows.

## A load through COPY

`Pg.copy_in` loads rows of any number with one `COPY`, each bound by its
shape as a statement's parameters are, and read from a `Seq.t` as they are
sent, so a load is held a row at a time:

```ocaml title="load.ml"
module S = Rowtype
module Pg = Rowtype_postgres

let load db users =
Pg.copy_in db ~table:"users" ~columns:[ "name"; "email" ]
S.(t2 text text)
(List.to_seq users)
```

It answers how many rows it wrote. `~columns` are in the row's order, every
column in table order where empty, and quoted as given, so `"Jobs"` is not
`jobs`; a number of them that is not the row's is refused before anything
is sent. `?schema` names the table's schema.

A row that cannot be bound, a constraint, and a raise from the sequence each
fail the whole `COPY`, with nothing written and the connection usable; the
raise passes. A cancelled load closes the connection and cancels the `COPY`,
as for any statement.

On SQLite, `Sq.copy_in` loads rows of any number from a `Seq.t` with one
prepared `insert`, all or none, as `Pg.copy_in` does with `COPY`. Inside a
transaction, the load is the transaction's, and goes with it.
65 changes: 65 additions & 0 deletions agents/pages/guide/checking-statements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Checking statements

`Pg.verify db statements` has Postgres describe every statement without
running it, and compares the parameters and columns with what you declared.
Run it in a test against a migrated database, and a renamed column fails the
test instead of a request.

```ocaml title="check.ml"
module S = Rowtype
module Pg = Rowtype_postgres

let statements =
[
S.Any Orders.ship;
S.Any Orders.count;
S.Any Orders.find;
S.Any Orders.of_customer;
S.Any Orders.claim;
]

let check db = Pg.verify db statements
```

`S.Any` wraps a statement whatever it takes and answers, so every statement
of a program goes in one list. Here they are the
[statements page's](statements.md) `Orders`.

## What it compares

For each statement, how many parameters and, for a statement that reads
rows, how many columns, and each one's type: an `int` an integer or an
`oid`, a `float` a float, a `text` text, a name, a uuid or JSON, and the
rest their own, a domain as the type it is made from. A parameter the
database could not type is left to it. A statement it accepts is one
`Pg.run` reads.

An enum is read and written by an `S.enum` each of whose labels is one of
the enum's. A label only the database has is no disagreement, since a
migration adds one ahead of the code that reads it, and a row holding it is
refused where it is read. An enum is also written from a `text`, and read as
one through a cast, `::text`.

Whether a column may be NULL is not checked: the database says so only of a
column read as it is stored, and a query may promise more, so a check would
refuse what is right. A NULL a shape does not allow fails where it is read,
naming its column.

## What it answers

`` `Disagreements `` names every problem, in words for a person, each naming
its statement's first line and where in it. A check that could not ask -- a
lost connection -- is that failure, as a statement's is, and no statement is
said to disagree. `Pg.error_to_string` tells either.

## On SQLite

`Sq.verify db statements` prepares every statement without running it and
holds its parameters and columns to what you declared, a column of a table
to its declared type: an `integer` column as one an `int`, an `int64` or a
`bool` reads, a `real` one as a `float`'s, a `text` one as text's, an
instant's or any other kept as text, and a `blob` one as `bytes` or a
`uuid`. A column made by an expression, or declared with no type, holds any
kind of value, and is checked where it is read. Its placeholders are held to
the rule a statement runs by, so a statement it passes is not refused for
them when it runs.
Loading