Wire the sqlc-generated dbgen package into db.DB so it stops being dead
code (audit A-2026-07-05) and becomes the real, CI-verified query layer.
db.DB now holds a *dbgen.Queries (initialized in Open via dbgen.New).
Query method bodies delegate to it; sqlc owns the SQL text and parameter
binding (make sqlc-verify), while db keeps its stable public API and
domain model types so no caller in api/admin/ws/service changes. The
migration is incremental — a method either delegates to d.q.* or still
runs raw SQL — so both layers are correct during the transition.
Converted domains (now load-bearing through sqlc):
- blocks: BlockUser, UnblockUser, IsBlocked, IsEitherBlocked,
ListBlockedUsers (added the query to blocks.sql + regenerated).
Empty ListBlockedUsers now returns []int64{} instead of nil, matching
the MemStore backend — a latent inconsistency fixed, not a regression.
- lockouts: UpsertLockout, LoadActiveLockouts, CleanupExpiredLockouts,
DeleteLockout (RFC3339 time formatting/parsing kept in the wrappers).
- roles: GetRoleByID, ListRoles, GetRoleForUser via a shared roleFromGen
mapper (int64 position/is_default -> int/bool). GetUserWithRole stays
raw for now.
Remaining domains stay on raw SQL and are tracked in
docs/plans/sqlc-adoption.md; store/ event+plugin SQL is intentionally
excluded (that layer is removed in D3). Decisions doc + audit closure
updated (A-2026-07-05 -> in progress).
Verified: go build ./...; go test -race ./db ./service ./auth ./ws (api
green non-race, race run matches CI's -timeout 20m); make sqlc-verify and
protocol-verify pass with the regenerated output committed.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
3.0 KiB
sqlc Adoption (D2) — Progress & Plan
Decision: D2 in audit-2026-07-19-decisions.md — adopt
sqlc as the real query layer; Closes: audit finding A-2026-07-05 (dead db/dbgen).
Approach
db.DB now holds a *dbgen.Queries (db/db.go, initialized in Open via
dbgen.New(sqlDB)). Query method bodies delegate to it; sqlc owns the SQL text
and parameter binding (verified in CI by make sqlc-verify), while the db
package keeps its stable public API and domain model types so no caller in
api/admin/ws/service changes. Migration is incremental — a method
either delegates to d.q.* or still runs raw SQL against sqlDB; both are
correct during the transition.
Two mechanical frictions drive the per-domain effort:
- Model mismatch. Generated row structs use
int64/*int64and store booleans asint64; the domain models useint/*int/bool. SELECT conversions need a smallfromGenmapper per domain (seeroleFromGenindb/role_queries.go). - Missing queries. A few hand-written methods had no generated
counterpart (e.g.
ListBlockedUsers); add the query todb/queries/sqlite/<domain>.sqlandmake sqlc-generatebefore delegating.
Status
Phase 1 — done (2026-07-19)
sqlc is now load-bearing in production (previously dead code):
| Domain | Methods delegated | Notes |
|---|---|---|
blocks (block_queries.go) |
BlockUser, UnblockUser, IsBlocked, IsEitherBlocked, ListBlockedUsers | Added ListBlockedUsers query. Empty result now []int64{} (matches MemStore; was nil). |
lockouts (lockout_queries.go) |
UpsertLockout, LoadActiveLockouts, CleanupExpiredLockouts, DeleteLockout | Time formatting/parsing kept in the wrapper. |
roles (role_queries.go) |
GetRoleByID, ListRoles, GetRoleForUser | Shared roleFromGen mapper. GetUserWithRole (joined User+Role) stays raw. |
Phase 2 — remaining domains (raw SQL still, each survives D3)
Convert with the same pattern; add missing queries + a fromGen mapper where
needed. Rough order by mapping simplicity:
- Simple/exec-heavy: invites (
invite_queries.go), profile (profile_queries.go), attachments (attachment_queries.go). - Model-mapped reads: sessions (
auth_queries.go), users (auth_queries.go—GetUserByID/GetUserByUsername/ListAllUsers), channels (channel_queries.go), voice (voice_queries.go), dm (dm_queries.go), admin/settings (admin_queries.go). - Complex/joined: messages (
message_queries.go— search, cursor pagination, reactions),GetUserWithRole,GetServerStats.
Out of scope for D2
store/event + plugin SQL (store/sqlite_events.go, plugin store) — these live in the store layer being removed in D3; converting them is throwaway. D3 moves the survivingdbmethods (sqlc-backed) to direct service use.
Verification (per phase)
go build ./...; go test -race ./db/ ./service/ ./auth/ ./api/ ./ws/;
make sqlc-verify (generated output committed & in sync).