Repository navigation
Description
Proposal
Adopt sqlc for the static-query repositories after the 1.0 schema baseline lands (schema workstream #888, Phase 6 of the API v2 program #135). Raised by a senior reviewer while the v2/Huma work was starting: the schema hygiene PRs already touch the table definitions, indexes, and column types sqlc would generate against, so the question was whether to fold the data-layer migration into the same program.
Decision for now: out of the API v2 program, sequenced behind it. Rationale below so the sequencing is not relitigated later.
What sqlc would touch (today's tree)
| Count | |
|---|---|
| Non-test Go files using pgx | 235 |
| Repository / store types | 316 |
Raw SQL string literals (SELECT/INSERT/UPDATE/DELETE/WITH) |
872 |
Files building SQL dynamically (Sprintf/strings.Builder), by package |
internal/catalog 30, notifications 8, scanner 5, metadata 5, jellycompat 5, api/handlers 5, userstore/pgstore 4, userdb 3, sections 3, opslog 3 |
Driver: github.com/jackc/pgx/v5, which sqlc supports natively (sql_package: pgx/v5).
sqlc generates typed Go from static SQL. The dynamically composed queries (catalog search and filters, section fetchers, settings resolution, jellycompat browse) stay hand-written or need a builder beside sqlc; they are explicitly out of scope for the first pass.
Why after the baseline, not inside the v2 program
- Huma and sqlc do not overlap. Huma owns the HTTP transport (request/response types, validation, OpenAPI). sqlc owns the database boundary. The program's rule that v1 and v2 share domain services (
docs/architecture/api-contract.md) means a section PR converts a handler, never its repository. There is no shared code to co-edit. - The schema is still moving. The hygiene stack (chore(db): drop dead plex_sync, user_playback_sessions, and content_id_migration_map tables [schema-hygiene 1/4] #879 → chore(db): add missing user FKs, FK leading indexes, and users role/enabled NOT NULL [schema-hygiene 2/4] #889 → chore(db): convert serial ids to identity, widen notification varchars, rename tables [schema-hygiene 3/4] #893 → chore(db): widen users, media_files, and media_folders ids to bigint [schema-hygiene 4/4] #898) changes identity columns, three core ids to
bigint, three table names, and 14 column types; Phase 6 then squashes the 360-migration chain into one baseline with a bridge-tip upgrade guard. sqlc reads the schema. Generating once from the squashed baseline is strictly better than generating from the chain and regenerating after every hygiene PR. - Regression attribution. Phase 1 (feat(contracts): inventory, ledger, and tier-1 scenario catalogs for the legacy native surface [api-v2 phase 1] #902) baselines behavior per tier-1 route in executable scenario catalogs. Rewriting the data layer under a section while its wire contract changes would make any regression ambiguous in origin. Done afterwards, against frozen catalogs, sqlc gets a free regression suite.
- Scope and review surface. The program's section-sizing rule forbids combining unrelated concerns in one PR, and the 1.0 posture in
AGENTS.mdis QA and polish, not new sprawl. Rewriting 800+ queries is a second program of similar size.
Scope when it starts
- Pin sqlc (exact version) and add
sqlc.yamlreading the 1.0 baseline migration (orsqlc vetagainst a migrated scratch database); generate into per-domain packages that mirror the existing repository ownership. - Convert repositories package by package, static queries only; one package per PR; each PR runs the tier-1 scenario catalogs (
make verify-scenario-catalogsplus the DB-gated executor) as its regression gate. - CI:
sqlc diff(or generate-and-byte-compare, matching the route-inventory / OpenAPI gate pattern) so generated code cannot drift from the queries. - Leave the dynamic-query packages listed above on hand-written pgx unless a builder is adopted separately; record that boundary in
docs/architecture/.
Dependencies
- Blocked on: Schema workstream: 1.0 hygiene and migration reset (umbrella) #888 item 6 (1.0 baseline + parity gate), which is Phase 6 of API v2 program epic: /api/v2 contract reset on Huma, bounded bridge, v1 retirement at 1.0 #135.
- Not blocked on any Phase 4 section PR; can start as soon as the baseline is on
main.
Related issue: #135 (program), #888 (schema workstream).
AI disclosure: drafted by Claude Code (claude-fable-5) orchestrating the #135 program, from a maintainer instruction; figures are grep counts over the current tree and are approximate.
Activity