Skip to content

feat(store): migrate messages + topics + delivery to sqlc (T4) - #795

Open
rigel-mintaka wants to merge 2 commits into
mainfrom
compass-repo/rig-3034-sqlc-t4-messages
Open

feat(store): migrate messages + topics + delivery to sqlc (T4)#795
rigel-mintaka wants to merge 2 commits into
mainfrom
compass-repo/rig-3034-sqlc-t4-messages

Conversation

@rigel-mintaka

@rigel-mintaka rigel-mintaka commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Migrate the message, topic, and delivery-consumer store domains from inline
pgx SQL to sqlc-generated queries (RIG-3034 T4, design record §T4). The
hand-written Store methods keep their exact exported signatures and wrap the
generated calls; error wrapping, domain-type mapping, tx orchestration, the
D9 not-found/forbidden merge, and the D2 seed self-guard all stay
hand-written.

Files migrated (queries/*.sql compiled into internal/store/db/):

  • messages.go (16 sites): post-policy read, insert (:one, ON CONFLICT dedup
    signalled via errMessageInsertConflict), topic get-or-create, list/search
    (:many), ask-containment find (FOR UPDATE), authored update (:one RETURNING),
    block update (:execrows), dedup read.
  • topics.go (9 sites): list, resolve-for-update, rename/merge resolution loop,
    archive, get.
  • delivery_cursors.go (15 sites): seed (per-agent + per-channel), owed-mention
    record/read/clear (:execrows)/count, mention-routed mark + recovery scan,
    AckDelivery resolve/load(FOR UPDATE)/self-authored/advance, undelivered
    sweep, in-sweep-set.
  • delivery_reads.go (7 sites): subscribed agents, channel agent members,
    is-agent, message-by-id, message-channel, topic/channel names, sweep
    channels.
  • presence_reads.go (2 sites): open-ask JSONB path probe, shared-channel
    EXISTS. Const-hoisted, never in the gate allowlist — no entry to remove.

Each migrated statement is semantically identical to the inline SQL it
replaces; RowsAffected-branching sites use :execrows. scanMessages/scanTopics/
scanMessagesByChannel and the now-dead execer interface are removed in favor of
generated-row mappers (messageFromParts, topicFromRow); the fail-loud decode
error on a malformed block set is preserved.

inline-sql-gate allowlist: 21 -> 17 (drop messages.go, topics.go,
delivery_cursors.go, delivery_reads.go).

Verified: go build ./... clean; go test ./internal/store/ ok; full pgtest lane
(podman postgres:16) ok; golangci-lint 0 issues; sqlc generate drift-clean;
GATE_ROOT=. bun tools/inline-sql-gate/index.ts exits 0 (allowlist 17).

Co-authored-by: Matt Wilkinson matt@rigel.build

Migrate the channels, channel_pins, and coordination store domains from
inline pgx SQL to sqlc-generated typed queries (RIG-3034, design record
sqlc-adoption T3), following the T2 recipe exactly. Pool-scoped calls go
through s.q; tx-scoped through s.q.WithTx(tx) / db.New(tx). Every migrated
statement is semantically identical to the inline SQL it replaced (same
columns, WHERE, JOINs, ORDER BY, null-handling); the CTE-based channel and
group visibility reads move into queries/channels.sql with their predicates
inlined per query (sqlc has no fragment composition), textually identical so
the stream-edge single-id checks cannot drift from the list reads.

The DELETE-and-check-rows-affected member removal uses the :execrows kind so
removeMember/SetCoordinationMembersTx keep their RowsAffected semantics. The
coordination per-owner advisory lock pg_advisory_xact_lock(hashtext('coordination:' || $1))
is now a tx-bound LockOwnerCoordination call inside its transaction; the
suffix-search channel-provision loop, the WithTx seam, error wrapping,
domain-type mapping, and the FOR UPDATE lock / cap-check control flow all
stay hand-written. Regenerated internal/store/db (drift-clean) and dropped
the three migrated inline-sql-gate allowlist entries (24 -> 21).

Co-authored-by: Matt Wilkinson <matt@rigel.build>
@linear-code

linear-code Bot commented Aug 31, 2026

Copy link
Copy Markdown

RIG-3034

@github-actions

github-actions Bot commented Aug 31, 2026

Copy link
Copy Markdown

Compass engineering docs preview: https://compass-repo-rig-3034-sqlc-t-qu7x.compass-eng-docs.pages.dev

Deployed from compass-repo/rig-3034-sqlc-t4-messages at 8c39f8b.

Migrate the message, topic, and delivery-consumer store domains from inline
pgx SQL to sqlc-generated queries (RIG-3034 T4, design record §T4). The
hand-written Store methods keep their exact exported signatures and wrap the
generated calls; error wrapping, domain-type mapping, tx orchestration, the
D9 not-found/forbidden merge, and the D2 seed self-guard all stay
hand-written.

Files migrated (queries/*.sql compiled into internal/store/db/):
- messages.go (16 sites): post-policy read, insert (:one, ON CONFLICT dedup
  signalled via errMessageInsertConflict), topic get-or-create, list/search
  (:many), ask-containment find (FOR UPDATE), authored update (:one RETURNING),
  block update (:execrows), dedup read.
- topics.go (9 sites): list, resolve-for-update, rename/merge resolution loop,
  archive, get.
- delivery_cursors.go (15 sites): seed (per-agent + per-channel), owed-mention
  record/read/clear (:execrows)/count, mention-routed mark + recovery scan,
  AckDelivery resolve/load(FOR UPDATE)/self-authored/advance, undelivered
  sweep, in-sweep-set.
- delivery_reads.go (7 sites): subscribed agents, channel agent members,
  is-agent, message-by-id, message-channel, topic/channel names, sweep
  channels.
- presence_reads.go (2 sites): open-ask JSONB path probe, shared-channel
  EXISTS. Const-hoisted, never in the gate allowlist — no entry to remove.

Each migrated statement is semantically identical to the inline SQL it
replaces; RowsAffected-branching sites use :execrows. scanMessages/scanTopics/
scanMessagesByChannel and the now-dead execer interface are removed in favor of
generated-row mappers (messageFromParts, topicFromRow); the fail-loud decode
error on a malformed block set is preserved.

inline-sql-gate allowlist: 21 -> 17 (drop messages.go, topics.go,
delivery_cursors.go, delivery_reads.go).

Verified: go build ./... clean; go test ./internal/store/ ok; full pgtest lane
(podman postgres:16) ok; golangci-lint 0 issues; sqlc generate drift-clean;
GATE_ROOT=. bun tools/inline-sql-gate/index.ts exits 0 (allowlist 17).

Co-authored-by: Matt Wilkinson <matt@rigel.build>
@rigel-mintaka
rigel-mintaka force-pushed the compass-repo/rig-3034-sqlc-t4-messages branch from e49ae3f to 8c39f8b Compare August 31, 2026 16:52
Base automatically changed from compass-repo/rig-3034-sqlc-t3-channels to main August 31, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant