Skip to content

sqlc and goose over an ORM or DSL

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).

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.

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|:execrows directive.
  • sqlc reads the embedded migrations as the schema and generates typed Go into internal/sqlc/dbq. The generated code is committed. make sqlc regenerates it and make 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.WithTx binds the same generated code to a transaction, so the transactional and non-transactional paths share it.
  • Column overrides in sqlc.yaml map database types to the types the code already uses (TIMESTAMPTZ to time.Time, JSONB to json.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.sql in internal/migrate/sql are embedded in the binary and applied at startup by goose, with -- +goose Up and Down sections.
  • A bootstrap step makes goose_db_version agree with the schema actually present, so databases created before goose adoption are not re-migrated (see migration-bootstrap).
  • Large-table changes follow ADR 0008.

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.
  • 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-migrate for 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.