Skip to content

Commit 943fee7

Browse files
author
xmtp-coder-agent
committed
docs: add db agent workflow guide
1 parent 488fb43 commit 943fee7

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

CLAUDE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Both are started as goroutines from `cmd/server/main.go` and shut down gracefull
5656
- `pkg/installations/` — Installation CRUD (device registration)
5757
- `pkg/subscriptions/` — Subscription management (topic subscriptions with optional HMAC keys)
5858
- `pkg/interfaces/` — Core interfaces (`Installations`, `Subscriptions`, `Delivery`) and domain types
59-
- `pkg/db/`Bun ORM models and migrations (PostgreSQL)
59+
- `pkg/db/`SQLC queries, pgx/database/sql access, and golang-migrate migrations (see `pkg/db/AGENTS.md`)
6060
- `pkg/topics/` — Topic parsing and message type detection (V3 MLS topics only)
6161
- `pkg/options/` — CLI flag/env var configuration structs
6262
- `pkg/proto/` — Generated protobuf/Connect code (~28K LOC, do not edit)
@@ -75,11 +75,11 @@ Options are parsed from CLI flags and environment variables (via `go-flags` stru
7575

7676
### Database
7777

78-
PostgreSQL via Bun ORM. Migrations in `pkg/db/migrations/`. Test DSN: `postgres://postgres:xmtp@localhost:25432/postgres?sslmode=disable`.
78+
PostgreSQL via `database/sql` + `pgx`, with `sqlc` query generation. Migrations live in `pkg/db/migrations/`. For DB-specific agent workflow, see `pkg/db/AGENTS.md`. Test DSN: `postgres://postgres:xmtp@localhost:25432/postgres?sslmode=disable`.
7979

8080
### Testing Approach
8181

8282
- Unit tests use mockery-generated mocks for interface boundaries
8383
- API tests start a real HTTP server with mocked services
84-
- `test/helpers.go` provides `CreateTestDb()` with automatic table truncation
84+
- `test/helpers.go` provides isolated per-test PostgreSQL databases via `CreateTestDb()`
8585
- Integration tests run full stack in Docker with HTTP delivery for verification

pkg/db/AGENTS.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# pkg/db AGENTS.md
2+
3+
Guidance for coding agents working in `pkg/db/`.
4+
5+
## Overview
6+
7+
This package uses:
8+
9+
- `sqlc` for query definitions and generated Go code
10+
- `database/sql` with the `pgx` driver for runtime access
11+
- embedded `golang-migrate` SQL files for schema changes
12+
13+
Do not reintroduce Bun patterns here.
14+
15+
## File Layout
16+
17+
- `sqlc/`: hand-written SQL query inputs for `sqlc`
18+
- `queries/`: generated Go code from `sqlc` output, do not edit by hand
19+
- `migrations/*.up.sql`: forward schema migrations
20+
- `migrations/*.down.sql`: rollback schema migrations
21+
- `migrations/migrations.go`: migration runner and Bun-to-`golang-migrate` reconciliation
22+
- `migrations/create.go`: helper for creating paired migration files
23+
24+
## Adding or Changing Queries
25+
26+
1. Edit or add SQL in `pkg/db/sqlc/`.
27+
2. Prefer explicit named queries and stable column names because service code depends on the generated field names.
28+
3. Regenerate code with:
29+
30+
```bash
31+
./dev/gen-sqlc
32+
```
33+
34+
4. Review the generated files under `pkg/db/queries/`.
35+
5. Update calling code and tests together.
36+
37+
Rules:
38+
39+
- Never hand-edit files in `pkg/db/queries/`.
40+
- Keep query behavior aligned with existing API/service semantics.
41+
- If generated parameter names are poor, prefer improving the SQL with `sqlc.arg(...)` names rather than working around bad generated names in Go.
42+
43+
## Adding Migrations
44+
45+
Create a new migration pair with:
46+
47+
```bash
48+
./dev/create-migration <name>
49+
```
50+
51+
This writes paired `.up.sql` and `.down.sql` files into `pkg/db/migrations/`.
52+
53+
Rules:
54+
55+
- Treat migrations as append-only once merged.
56+
- Keep fresh-database boot and legacy Bun-database upgrade paths in mind.
57+
- Do not change the Bun reconciliation baseline casually. It is intentionally pinned to the Bun handoff version so future `golang-migrate`-only migrations still run on upgraded deployments.
58+
59+
## Legacy Upgrade Model
60+
61+
There are two migration bookkeeping systems you may encounter:
62+
63+
- `bun_migrations`: legacy Bun metadata, no longer used by the server
64+
- `schema_migrations`: `golang-migrate` metadata, current source of truth
65+
66+
For legacy Bun-initialized databases, reconciliation means:
67+
68+
- detect that the legacy application schema already exists
69+
- create `schema_migrations` if needed
70+
- record the fixed Bun handoff version
71+
- leave the application tables untouched
72+
73+
That behavior is implemented in `pkg/db/migrations/migrations.go`. If you change it, update docs and tests in the same change.
74+
75+
## Validation
76+
77+
For DB-layer changes, run:
78+
79+
```bash
80+
./dev/gen-sqlc && git diff --exit-code pkg/db/queries
81+
$(go env GOPATH)/bin/golangci-lint run --timeout=5m --config dev/.golangci.yaml
82+
go test -p 1 ./...
83+
./dev/build
84+
```
85+
86+
If you only changed DB queries or migrations, still prefer running the full serial test suite because DB-backed packages share upgrade assumptions.

0 commit comments

Comments
 (0)