Go Database
Rule
Make persistence explicit, transactional, context-aware, and isolated from real mutable
systems. Keep SQL close enough to review and data access behind small boundaries.
Hard Stops
Stop before:
- Touching production or developer databases without explicit approval and backup/safety
plan.
- Running destructive migrations, changing schema, or changing data retention semantics
without rollback and compatibility discussion.
- Adding an ORM, query generator, migration tool, or container dependency without approval.
- Using real credentials or committing generated secrets.
Defaults
- Use
database/sql for generic SQL access and jackc/pgx/v5 for PostgreSQL driver/pool
needs.
- Prefer explicit SQL and small repository/store types over full ORMs in greenfield Go.
- Use
golang-migrate/migrate/v4 for versioned SQL migrations when migration tooling is
needed.
- Pass contexts into queries and transactions.
- Make transaction boundaries explicit; do not hide large workflows in auto-transactions.
- Map database errors to domain errors at the data boundary.
- Configure pool limits and timeouts intentionally.
Testing
Use isolated test databases, transaction rollbacks, temporary schemas, or approved
testcontainers-go when real database behavior matters. Do not use production-like shared
state by default. Test migrations up/down when the tool supports it, constraints, not-found
behavior, uniqueness conflicts, and transaction rollback.
Workflow
- Define schema, migrations, queries, transaction boundaries, indexes, and failure behavior.
- Choose stdlib
database/sql, pgx, or project-approved tool based on needs.
- Implement data access behind a small interface or concrete store consumed by services.
- Add isolated tests and fixtures.
- Run migrations/tests,
go test ./..., race tests for concurrent DB use, lint, and
just check.
Antipatterns
- Building SQL with string concatenation and untrusted input.
- Global DB handles hidden in packages.
- Ignoring
rows.Err() or leaking Rows/transactions.
- N+1 queries without measurement or accepted tradeoff.
- Tests that depend on a developer's local database.
Completion
Report schema/query changes, transaction model, tool choices, test database approach,
commands, and safety boundaries.
Source: nyquistwilder/personal-pi — distributed by TomeVault.
1---2name: go-database-23description: Greenfield Go database workflow for database/sql, pgx, migrations, transactions, pooling, query correctness, test databases, and safe data boundaries. Use when this capability is needed.4---56# Go Database78## Rule910Make persistence explicit, transactional, context-aware, and isolated from real mutable11systems. Keep SQL close enough to review and data access behind small boundaries.1213## Hard Stops1415Stop before:1617- Touching production or developer databases without explicit approval and backup/safety18 plan.19- Running destructive migrations, changing schema, or changing data retention semantics20 without rollback and compatibility discussion.21- Adding an ORM, query generator, migration tool, or container dependency without approval.22- Using real credentials or committing generated secrets.2324## Defaults2526- Use `database/sql` for generic SQL access and `jackc/pgx/v5` for PostgreSQL driver/pool27 needs.28- Prefer explicit SQL and small repository/store types over full ORMs in greenfield Go.29- Use `golang-migrate/migrate/v4` for versioned SQL migrations when migration tooling is30 needed.31- Pass contexts into queries and transactions.32- Make transaction boundaries explicit; do not hide large workflows in auto-transactions.33- Map database errors to domain errors at the data boundary.34- Configure pool limits and timeouts intentionally.3536## Testing3738Use isolated test databases, transaction rollbacks, temporary schemas, or approved39`testcontainers-go` when real database behavior matters. Do not use production-like shared40state by default. Test migrations up/down when the tool supports it, constraints, not-found41behavior, uniqueness conflicts, and transaction rollback.4243## Workflow44451. Define schema, migrations, queries, transaction boundaries, indexes, and failure behavior.462. Choose stdlib `database/sql`, pgx, or project-approved tool based on needs.473. Implement data access behind a small interface or concrete store consumed by services.484. Add isolated tests and fixtures.495. Run migrations/tests, `go test ./...`, race tests for concurrent DB use, lint, and50 `just check`.5152## Antipatterns5354- Building SQL with string concatenation and untrusted input.55- Global DB handles hidden in packages.56- Ignoring `rows.Err()` or leaking `Rows`/transactions.57- N+1 queries without measurement or accepted tradeoff.58- Tests that depend on a developer's local database.5960## Completion6162Report schema/query changes, transaction model, tool choices, test database approach,63commands, and safety boundaries.6465---66> Source: [nyquistwilder/personal-pi](https://github.com/nyquistwilder/personal-pi) — distributed by [TomeVault](https://tomevault.io).67<!-- tomevault:4.0:skill_md:2026-06-15 -->