Table of contents
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Database and sqlc
The app runs on PostgreSQL 16 or newer (the live deployment is
18.6; tests self-provision an embedded Postgres 16, so CI needs
no database service). Every query rides a pgxpool (sqlc
generates pgx code from the hand-written SQL); the pool's
ceiling is DB_MAX_CONNS (default 20), and the same pool backs
the scs pgxstore session store (sessions live in the
sessions table the baseline creates). A database/sql handle
over the pgx stdlib driver remains for schema migration and
raw SQL in tests.
The schema is a series of numbered steps in
internal/store/schema_pg/: 001_baseline.sql is the whole schema
as of the move to Postgres (BIGINT/DOUBLE PRECISION keep the
generated Go models' int64/float64 types), and each later file is
one change. At startup store.Open applies whichever steps a database
is missing, in order. Each step runs in a single transaction
together with its row in the schema_migrations table, so a step
lands completely or not at all, and that table is the record of
what has been applied. A database from before the table existed is
adopted on first start: its existing steps are recorded, not run
again.
Times are timestamptz columns and time.Time (or sql.NullTime
where "never" is a possible answer) in the code; a connection
always hands them back in UTC, whatever zone the machine is in.
The baseline kept them as RFC 3339 text, and steps 009–011 convert
those columns, carrying every stored value over as the same
instant. Those three steps rewrite the tables they touch, so the
first start after upgrading past them takes as long as copying
those tables once; the snapshot table is by far the largest.
To change the schema, add the next numbered file, then embed it and
add one line to schemaSteps, both in internal/store/schema.go.
Regenerate query code after editing internal/db/query/ with:
make gen # sqlc generate