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