Table of Contents
Month 4 · Week 2 — Exercises (databases & data access)¶
Standard-library-only Go packages with table-driven tests. No live database is
needed: each exercise models a database concept (migrations, repositories,
transactions) with an in-memory store, so go test runs anywhere.
How to run¶
go test ./exercises/month-04/week-2/...
# or one at a time, verbose:
go test -v ./exercises/month-04/week-2/migrationplan
Prompts¶
-
migrationplan/— ImplementPlan(current int, available []int) ([]int, error)that returns, in ascending order, the migration versions still to apply (every available version> current). Reject non-positive versions (ErrInvalidVersion) and duplicates (ErrDuplicateVersion), and never mutate the caller's slice. AddLatestfor the highest available version. Concepts: idempotent migrations, ordered versions, pure functions (Day 095). -
inmemrepo/— Implement the repository pattern for aUser:Create,Get,UpdateEmail,Delete,List. Return DOMAIN errors (ErrNotFound,ErrConflict) the caller matches witherrors.Is, takecontext.Contextfirst and honour cancellation, and never hand out pointers into the store. Concepts: repository pattern, sentinel errors, context-first (Day 096). -
txstore/— Implement a transactional key/value store withBegin/Set/Get/Commit/Rollbackand aWithTx(ctx, store, fn)helper that commits on success and rolls back on any error OR panic. Staged writes must be invisible untilCommit;Commitmust abort on a cancelled context; use-after-finish returnsErrTxDone. Concepts: transaction atomicity, deferred-Rollback pattern, context (Day 097).
Results¶
| Exercise | Focus | Tests | Status |
|---|---|---|---|
migrationplan |
migration version planning | go test |
✅ |
inmemrepo |
repository pattern + sentinel errors | go test |
✅ |
txstore |
transactions, WithTx, rollback-on-panic |
go test |
✅ |
Idiom checklist exercised this week:
errors.Ison wrapped sentinels,context.Contextas the first parameter, defensive copies out of a store, deferred cleanup that is a no-op on the happy path, and table-driven tests.