Skip to content

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

  1. migrationplan/ — Implement Plan(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. Add Latest for the highest available version. Concepts: idempotent migrations, ordered versions, pure functions (Day 095).

  2. inmemrepo/ — Implement the repository pattern for a User: Create, Get, UpdateEmail, Delete, List. Return DOMAIN errors (ErrNotFound, ErrConflict) the caller matches with errors.Is, take context.Context first and honour cancellation, and never hand out pointers into the store. Concepts: repository pattern, sentinel errors, context-first (Day 096).

  3. txstore/ — Implement a transactional key/value store with Begin/Set/Get/Commit/Rollback and a WithTx(ctx, store, fn) helper that commits on success and rolls back on any error OR panic. Staged writes must be invisible until Commit; Commit must abort on a cancelled context; use-after-finish returns ErrTxDone. 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.Is on wrapped sentinels, context.Context as the first parameter, defensive copies out of a store, deferred cleanup that is a no-op on the happy path, and table-driven tests.