Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .claude/marathon.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# Project kind: this repository holds production Go source, checked by `mise run check`.
[project]
kind = "code"
check = "mise run check"
# The read-only, development-time command that reports every pin trailing its latest release;
# it reads the network, so CI never runs it.
currency = "mise run currency"

# Remote platform, the commands that publish and merge a finished branch, and the CI wait.
[remote]
platform = "github"
publish = "gh pr create"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v7.0.1

- uses: jdx/mise-action@v5.2.7
- uses: jdx/mise-action@v5.2.10

- name: mise run check
run: mise run check
10 changes: 0 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
# go.work and go.work.sum are committed; this repo is multi-module by design.

# Marathon session plan files.
.claude/plans/

# marathon's task and session briefs.
.claude/briefs/

# Per-machine mise overrides.
mise.local.toml

# Build output.
bin/

# Editor and OS.
*.swp
*.swo
*~
Expand Down
13 changes: 8 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# golangci-lint v2 configuration, run per module by `mise run check`.
# golangci-lint v2 configuration, run per module by `mise run check`: the standard set plus
# testpackage and nolintlint.
# See architecture/standards/go-elemental/principles/tests-and-docs.md
# See architecture/principles/comments.md
version: "2"

linters:
# The standard set (errcheck, govet, ineffassign, staticcheck, unused),
# plus testpackage, which keeps tests black-box. Its skip regexp is
# narrowed to export_test.go, the one white-box file allowed: it exports
# a clock or probe hook to the black-box tests and holds no tests itself.
default: standard
enable:
- nolintlint
- testpackage
settings:
nolintlint:
require-explanation: true
require-specific: true
testpackage:
skip-regexp: export_test\.go
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ principles:
- Swapping the SQL provider is a port. The consumer owns its schema, its migrations, and its
domain SQL, and those are what a swap rewrites. A library that ships its own migration set,
such as `blobfs`, owns that set; the consumer declares it beneath its own.
- The HTTP half of the admin service, the route group and handler, is application code: the
reference service builds it, and the template stays engine-free.

## Packages

Expand All @@ -50,7 +52,7 @@ principles:
The repository uses a Go workspace and [mise](https://mise.jdx.dev):

```
mise run check # build, vet, format, fix, tidy, test, and lint every module; writes nothing
mise run check # build, vet, format, fix, tidy, test, and lint every module, then lint-context and lint-references; writes nothing
mise run currency # report requirements, Go, tools, and actions behind their latest
mise run upgrade # upgrade every module's go directive and requirements, and the tools, to their latest
```
Expand Down
1 change: 1 addition & 0 deletions STANDARDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ The judgement calls the standards-reviewer applies to go-database, beyond what `
- `architecture/principles/service-tiers.md`: `DB.Conn`, `Config.Options`, and the `postgres` provider.
- `architecture/principles/validation-first.md`: `Config.Finalize`, and `admin`'s verbs, which refuse an argument outside their domain before any I/O.
- `architecture/principles/context-architecture.md`: the README and each `doc.go` are the homes.
- `architecture/principles/comments.md`: every comment is one of the five kinds, in source, tests, scripts, and every configuration file (TOML, YAML, `.gitignore`); a doc comment on an unexported identifier or a test function is not package documentation.
5 changes: 4 additions & 1 deletion admin/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
// [Service.Ready] reports the schema alone, as the last operation that
// determined it found it; once Start has succeeded, a not-ready service
// re-verifies at most once per five seconds. A true Ready is not
// re-checked.
// re-checked. A re-verification's finding yields to any operation that
// determined the schema while it read, and an operation that determines
// nothing, such as one a cancelled context or a lost connection ends,
// leaves Ready as it was.
//
// # Operations
//
Expand Down
60 changes: 9 additions & 51 deletions admin/seed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/standards-lab/sqlate/sqltest"
)

// With no set configured, a seed request that names none refuses before
// any I/O and the seeder is never called.
func TestSeed_NoSetRefuses(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{}})
n, err := f.service.Seed(context.Background(), "")
Expand All @@ -26,8 +24,6 @@ func TestSeed_NoSetRefuses(t *testing.T) {
}
}

// Without a seeder, every seed operation refuses the same way, and States
// is empty.
func TestSeed_NoSeederRefuses(t *testing.T) {
s, rec := newService(t)
ctx := context.Background()
Expand All @@ -48,8 +44,6 @@ func TestSeed_NoSeederRefuses(t *testing.T) {
}
}

// A name the seeder does not declare refuses Seed and Reset before any
// I/O, and the error names it.
func TestSeed_UnknownStateRefuses(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seed: "default", Seeder: &fakeSeeder{}})
ctx := context.Background()
Expand All @@ -65,15 +59,13 @@ func TestSeed_UnknownStateRefuses(t *testing.T) {
}
}

// States is the seeder's list.
func TestStates_ListsTheSeeders(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{states: []string{"default", "empty"}}})
if got := f.service.States(); !slices.Equal(got, []string{"default", "empty"}) {
t.Errorf("States = %v", got)
}
}

// A request that names no set applies the configured one.
func TestSeed_EmptyNameAppliesTheConfiguredSet(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seed: "default", Seeder: &fakeSeeder{}})
n, err := f.service.Seed(context.Background(), "")
Expand All @@ -82,7 +74,6 @@ func TestSeed_EmptyNameAppliesTheConfiguredSet(t *testing.T) {
}
}

// A named set applies whether or not a set is configured.
func TestSeed_NamedSetAppliesWithoutConfiguration(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{states: []string{"default", "demo"}}})
n, err := f.service.Seed(context.Background(), "demo")
Expand All @@ -91,8 +82,6 @@ func TestSeed_NamedSetAppliesWithoutConfiguration(t *testing.T) {
}
}

// With a set configured, Start applies it by name once the schema is
// current and verified.
func TestStart_AppliesTheConfiguredSet(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seed: "default", Seeder: &fakeSeeder{}}, clean()...)
if err := f.service.Start(context.Background()); err != nil {
Expand All @@ -103,8 +92,6 @@ func TestStart_AppliesTheConfiguredSet(t *testing.T) {
}
}

// A configured name the seeder does not declare is a configuration
// defect: Start fails before it reads or changes the schema.
func TestStart_UnknownConfiguredSetFailsStartup(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seed: "nope", Seeder: &fakeSeeder{}})
err := f.service.Start(context.Background())
Expand All @@ -116,7 +103,6 @@ func TestStart_UnknownConfiguredSetFailsStartup(t *testing.T) {
}
}

// A startup seed that fails fails Start, and the service is not ready.
func TestStart_SeedFailureFailsStartup(t *testing.T) {
f := newFixture(t, testDialect{}, admin.Options{Seed: "default", Seeder: &fakeSeeder{seedErr: errors.New("seed failed")}}, clean()...)
err := f.service.Start(context.Background())
Expand All @@ -128,8 +114,6 @@ func TestStart_SeedFailureFailsStartup(t *testing.T) {
}
}

// Reset with no state named brings the database to the configured one,
// and refuses when none is configured.
func TestReset_EmptyNameUsesTheConfiguredSet(t *testing.T) {
responses := slices.Concat(resetting(), applying(), current())
f := newFixture(t, testDialect{}, admin.Options{Seed: "default", Seeder: &fakeSeeder{}}, responses...)
Expand All @@ -144,7 +128,6 @@ func TestReset_EmptyNameUsesTheConfiguredSet(t *testing.T) {
}
}

// States hands out a copy: changing it leaves the seeder's list alone.
func TestStates_ReturnsACopy(t *testing.T) {
states := []string{"default", "empty"}
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{states: states}})
Expand All @@ -154,10 +137,6 @@ func TestStates_ReturnsACopy(t *testing.T) {
}
}

// Reset reverts the applied set under the lock and drops its history
// table, applies it again, seeds the named state's set, and answers with
// the state, the refreshed status, and the rows. With no OnReset hook, the
// transition is these steps alone.
func TestReset_RevertsAppliesAndSeeds(t *testing.T) {
responses := slices.Concat(resetting(), applying(), current())
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{states: []string{"default", "empty"}}}, responses...)
Expand All @@ -171,8 +150,6 @@ func TestReset_RevertsAppliesAndSeeds(t *testing.T) {
if !slices.Equal(f.seeder.seeded, []string{"empty"}) || !f.service.Ready() || f.rec.Pending() != 0 {
t.Errorf("seeded %v, ready %v, pending %d", f.seeder.seeded, f.service.Ready(), f.rec.Pending())
}
// The downs run newest first, then the ups oldest first, each pass
// under its own lock.
var order []string
for _, e := range f.rec.SQL(sqltest.OpExec) {
if strings.HasPrefix(e, "SELECT lock") || strings.HasPrefix(e, "DROP") || strings.HasPrefix(e, "CREATE TABLE a") || strings.HasPrefix(e, "CREATE INDEX") {
Expand All @@ -188,13 +165,10 @@ func TestReset_RevertsAppliesAndSeeds(t *testing.T) {
}
}

// A history the transition cannot proceed from stops it at the revert,
// nothing is seeded, and the status is still read so Ready reflects the
// schema left behind.
func TestReset_DirtyHistoryStopsAtTheRevert(t *testing.T) {
dirty := history([]driver.Value{int64(1), "a", false}, []driver.Value{int64(2), "b", true})
f := newFixture(t, testDialect{}, admin.Options{Seeder: &fakeSeeder{}},
exists(true), applied(), // a clean Verify first, so Ready starts true
exists(true), applied(),
locked, created, dirty, unlocked,
exists(true), dirty,
)
Expand All @@ -210,8 +184,6 @@ func TestReset_DirtyHistoryStopsAtTheRevert(t *testing.T) {
}
}

// A transition that fails at the apply seeds nothing, and the status read
// after it finds the schema behind.
func TestReset_FailureAtTheApply(t *testing.T) {
boom := errors.New("create failed")
responses := slices.Concat(resetting(),
Expand All @@ -228,8 +200,6 @@ func TestReset_FailureAtTheApply(t *testing.T) {
}
}

// A transition that fails at the seed leaves the schema applied and
// current, and Ready reports it so.
func TestReset_FailureAtTheSeed(t *testing.T) {
boom := errors.New("seed failed")
responses := slices.Concat(resetting(), applying(), current())
Expand All @@ -243,26 +213,19 @@ func TestReset_FailureAtTheSeed(t *testing.T) {
}
}

// hookCall is what an OnReset hook saw when Reset called it.
type hookCall struct {
state string
pending int // scripted responses not yet consumed
seeded []string // the states seeded so far
pending int
seeded []string
}

// recordHook returns an OnReset hook that records each call into calls,
// with how far the fixture's script and seeder had run, and answers err.
// It reads *f at the call, so it can be wired before the fixture is built.
func recordHook(f *fixture, calls *[]hookCall, err error) func(context.Context, string) error {
return func(_ context.Context, state string) error {
*calls = append(*calls, hookCall{state: state, pending: f.rec.Pending(), seeded: slices.Clone(f.seeder.seeded)})
return err
}
}

// Reset calls the hook once, with the state it resolved, after the revert
// and the apply have run and before the seed: at the call, only the status
// read is left of the script, and nothing is seeded.
func TestReset_HookRunsBetweenTheApplyAndTheSeed(t *testing.T) {
cases := []struct {
name, configured, request, want string
Expand Down Expand Up @@ -298,9 +261,6 @@ func TestReset_HookRunsBetweenTheApplyAndTheSeed(t *testing.T) {
}
}

// A hook that fails stops Reset before the seed: the error names the step
// and wraps the hook's, nothing is seeded, and the schema is left applied
// and current, as the status read after it finds.
func TestReset_FailureAtTheHook(t *testing.T) {
boom := errors.New("hook failed")
var calls []hookCall
Expand All @@ -319,17 +279,15 @@ func TestReset_FailureAtTheHook(t *testing.T) {
}
}

// Only Reset calls the hook: Start, Seed, and the schema verbs each run
// to completion without it.
func TestOnReset_OtherVerbsNeverCallIt(t *testing.T) {
one := history([]driver.Value{int64(1), "a", false})
responses := slices.Concat(
clean(), // Start
current(), // Status
applying(), current(), // Up
[]sqltest.Response{locked, created, applied(), {}, {}, {}, unlocked, exists(true), one}, // Down 1
[]sqltest.Response{locked, created, history(), {}, {}, unlocked, exists(true), one}, // Steps +1
[]sqltest.Response{locked, {}, {}, {Affected: 1}, unlocked, exists(true), one}, // Force 1
clean(),
current(),
applying(), current(),
[]sqltest.Response{locked, created, applied(), {}, {}, {}, unlocked, exists(true), one},
[]sqltest.Response{locked, created, history(), {}, {}, unlocked, exists(true), one},
[]sqltest.Response{locked, {}, {}, {Affected: 1}, unlocked, exists(true), one},
)
var calls int
opts := admin.Options{
Expand Down
Loading