sqlc and goose over an ORM or DSL
Status
Section titled “Status”Accepted. An early architecture note proposed a query DSL (go-jet) and golang-migrate; neither
was ever added to the module, and the architecture note has not been updated to say so.
2026-05-27 (sqlc configuration from the first commit; the switch to goose is dated by its commit).
Context
Section titled “Context”The platform has several dozen aggregates, each with a repository that maps rows to domain entities. The data layer needs to:
- keep SQL visible, because the interesting queries (claiming rows with
FOR UPDATE SKIP LOCKED, partition maintenance, recursive reads, index-aware filters) are performance-sensitive and need to be reviewed as SQL; - catch a renamed column or wrong parameter type before runtime;
- work with the migrations that already define the schema, with no second description of the tables;
- stay readable for maintainers who know SQL and Go but do not want to learn a mapping layer.
An object-relational mapper hides the SQL and tends to generate queries that are hard to tune. A query DSL keeps compile-time checking but adds a language to learn, allocates on hot paths, and needs a code-generation step against a live database.
Decision
Section titled “Decision”Queries are SQL files, compiled by sqlc.
- SQL lives in
internal/sqlc/queries/*.sql, one file per aggregate, each statement named with a-- name: Aggregate... :one|:many|:exec|:execrowsdirective. - sqlc reads the embedded migrations as the schema and generates typed Go into
internal/sqlc/dbq. The generated code is committed.make sqlcregenerates it andmake sqlc-verify(part of CI) fails if the committed code is out of date, with a pinned sqlc version so output is stable. - A repository wraps
*dbq.Queries, calls the generated method and projects the row onto the domain entity.Queries.WithTxbinds the same generated code to a transaction, so the transactional and non-transactional paths share it. - Column overrides in
sqlc.yamlmap database types to the types the code already uses (TIMESTAMPTZtotime.Time,JSONBtojson.RawMessage, nullable columns to pointers, and the typed id types). - Dynamic search endpoints with many optional filters either use the
($1 IS NULL OR col = $1)pattern or drop to hand-built SQL in the repository. Hand-written SQL also stays in hot paths and in maintenance code (the router queue, the stream projector and partition manager). Those queries lose the compile-time schema check, which is the accepted trade.
Migrations are plain SQL, applied by goose.
- Numbered files
NNN_name.sqlininternal/migrate/sqlare embedded in the binary and applied at startup by goose, with-- +goose UpandDownsections. - A bootstrap step makes
goose_db_versionagree with the schema actually present, so databases created before goose adoption are not re-migrated (seemigration-bootstrap). - Large-table changes follow ADR 0008.
Consequences
Section titled “Consequences”Good
- SQL is reviewed as SQL, and the query that runs is the query that was written.
- Schema or column typos fail at code-generation or build time.
- The generated code is committed, so pull requests show the effect of a schema change and the build needs no database.
- No runtime reflection or ORM cache; scanning is straightforward
pgx. - Migrations are the only schema source, and the same SQL runs in embedded Postgres for development and in production.
Bad
- Dynamic queries are awkward: either every predicate is evaluated (and may lose index selectivity), or the query is hand-built and unchecked.
- Two SQL styles coexist (generated and hand-written), and contributors must know which applies.
- Generated code is Postgres-specific (
pgx/v5). Portability to another database engine is not a goal of the platform’s own data layer. - Goose keeps no checksums, so an edited, already-applied migration is not detected. Migrations are treated as append-only by convention.
Alternatives considered
Section titled “Alternatives considered”- An ORM. Rejected for hidden SQL and poor fit with partitioned tables and row-claiming queries.
go-jet(a typed query DSL). Considered in the early architecture notes. It would have covered dynamic filters well, but needs a codegen step against a live database and a DSL to learn, and would still have required raw SQL for the hot paths. It was not adopted.- Hand-written pgx everywhere. Maximum control, no schema checking.
golang-migratefor migrations. Named in the early notes; goose was adopted a day after the initial commit and supports Go-friendly embedded files and the annotations the project needs, such as no-transaction migrations for concurrent index builds.