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
59 changes: 58 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,62 @@ only; the `postgres` sub-module keeps its own.

## [Unreleased]

## [v0.12.0] - 2026-11-11

The sweeper's configuration is a trailing Options struct, not variadic option functions:
`sweep.New` takes a `sweep.Options`, whose zero value keeps the defaults. The requirements move
to go-core v0.12.0 and sqlate v0.7.0, which make the same change to their own constructors and
transactions.

### Added

- `sweep.Options`, which configures a Sweeper. `Options.Unlocked` runs without claiming the
reclaimers' locks, `Options.Gate` is the gate each pass holds shared, and `Options.OnPass` is
the hook each `sweep.Pass` is reported to; a nil `Gate` or `OnPass` keeps its default, no gate
and a failed pass logged at warn.

### Changed

- **Breaking:** `sweep.New(db, regs, shutdown, logger, opts)` takes a trailing `sweep.Options` in
place of variadic `sweep.Option` values. A call with no option passes `sweep.Options{}`.
- **Breaking:** The `go-core` requirement is v0.12.0, which pulls its options break into an
importer's build: `reactor.New` takes a trailing `reactor.Options` and `event.NewRecorder` an
`event.Options` in place of variadic option values, `reactor.CancelWithin` returns a
`time.Duration` for `reactor.Options.CancelAfter`, and `reactor.Option`, `reactor.CancelAfter`,
`event.Option`, `event.Clock`, and `event.IDs` are removed.
- **Breaking:** The `sqlate` requirement is v0.7.0, which pulls its transaction-options break
into an importer's build: `DB.Begin`, `DB.Transact`, and `Transact` take a trailing
`*sql.TxOptions` in place of variadic `TxOption` values, nil for a default transaction;
`Beginner.Begin` has `DB.Begin`'s new signature; and `TxOption`, `Isolation`, and `ReadOnly`
are removed.

### Removed

- **Breaking:** `sweep.Option` and the `sweep.Unlocked`, `sweep.Gated`, and `sweep.OnPass`
option functions, with no aliases. Set `sweep.Options.Unlocked`, `sweep.Options.Gate`, and
`sweep.Options.OnPass`.

### Migrating from v0.11.0

- **A sweeper's options become one `sweep.Options` value.** Leave `Gate` or `OnPass` nil for
its default.

```go
// v0.11.0
s, err := sweep.New(db, regs, shutdown, logger)
s, err := sweep.New(db, regs, shutdown, logger, sweep.Unlocked())
s, err := sweep.New(db, regs, shutdown, logger, sweep.Gated(gate), sweep.OnPass(hook))

// v0.12.0
s, err := sweep.New(db, regs, shutdown, logger, sweep.Options{})
s, err := sweep.New(db, regs, shutdown, logger, sweep.Options{Unlocked: true})
s, err := sweep.New(db, regs, shutdown, logger, sweep.Options{Gate: gate, OnPass: hook})
```

- **go-core and sqlate calls follow their own changelogs.** go-core v0.12.0's and sqlate
v0.7.0's "Migrating" sections show the `reactor.New`, `event.NewRecorder`, `Begin`, and
`Transact` forms.

## [v0.11.0] - 2026-11-11

### Changed
Expand Down Expand Up @@ -340,7 +396,8 @@ depends on the standard library and `github.com/standards-lab/go-core v0.1.0`.
so an unknown field or trailing content in a curated seed file fails the decode. Idempotency
stays in the load function's SQL, where the conflict target is known.

[Unreleased]: https://github.com/standards-lab/go-database/compare/v0.11.0...HEAD
[Unreleased]: https://github.com/standards-lab/go-database/compare/v0.12.0...HEAD
[v0.12.0]: https://github.com/standards-lab/go-database/compare/v0.11.0...v0.12.0
[v0.11.0]: https://github.com/standards-lab/go-database/compare/v0.10.0...v0.11.0
[v0.10.0]: https://github.com/standards-lab/go-database/compare/v0.9.0...v0.10.0
[v0.9.0]: https://github.com/standards-lab/go-database/compare/v0.8.0...v0.9.0
Expand Down
2 changes: 1 addition & 1 deletion STANDARDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The judgement calls the standards-reviewer applies to go-database, beyond what `
- `architecture/standards/go-elemental/principles/lifecycle-and-context.md`: `DB`'s `Start`, `Shutdown` and `Ready`, and `admin.Service`'s `Start` and `Ready`; the constructors' panics on an unfinalized `Config` or a missing pool, and `postgres.New`'s error on a reserved option.
- `architecture/standards/go-elemental/principles/baseline-standards.md`: `Config`'s pool-size and timeout defaults, and `admin`, which takes every migration set, seed set, catalog and registry from its consumer.
- `architecture/standards/go-elemental/principles/utc-times.md`: `postgres.New`'s connections, which scan `timestamptz` in UTC.
- `architecture/standards/go-elemental/principles/construction-and-execution.md`: every new exported function and constructor in `database`, `admin`, `sweep` and `postgres`.
- `architecture/standards/go-elemental/principles/construction-and-execution.md`: `sweep.New` takes its configuration as a trailing `sweep.Options` whose zero value keeps the defaults, and every new exported function and constructor in `database`, `admin`, `sweep` and `postgres` that takes configuration does the same.
- `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.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/standards-lab/go-database
go 1.27

require (
github.com/standards-lab/go-core v0.11.0
github.com/standards-lab/sqlate v0.6.1
github.com/standards-lab/go-core v0.12.0
github.com/standards-lab/sqlate v0.7.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/standards-lab/go-core v0.11.0 h1:Ls/jdYSRaC6ch187B1xNz39qfWXmSgNpOZ5Ot+2z+U8=
github.com/standards-lab/go-core v0.11.0/go.mod h1:Yl/R+gM9YWczsePBCKXdPGh3FSdZ/5i8SapLIh3BDHU=
github.com/standards-lab/sqlate v0.6.1 h1:Kgp8freMX9Gs2KweBwG4IPzENgUmSJq8+r4tq02cDNw=
github.com/standards-lab/sqlate v0.6.1/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/standards-lab/go-core v0.12.0 h1:sqrzK6QSppEYpwvVAyiWgzNuOsjfK0Kwe86GPuZpHsU=
github.com/standards-lab/go-core v0.12.0/go.mod h1:Yl/R+gM9YWczsePBCKXdPGh3FSdZ/5i8SapLIh3BDHU=
github.com/standards-lab/sqlate v0.7.0 h1:GVb3OCDXwSS9RCxQ2BRIfV73zwBeJBhkvDLBJnTYd/U=
github.com/standards-lab/sqlate v0.7.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
18 changes: 17 additions & 1 deletion postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ changelog covers this sub-module only; the base module keeps its own.

## [Unreleased]

## [v0.7.0] - 2026-11-11

### Changed

- **Breaking:** The `go-database` requirement is v0.12.0, which requires `go-core` v0.12.0 and
`sqlate` v0.7.0 and pulls their options break into an importer's build: `sweep.New`,
`reactor.New`, and `event.NewRecorder` take a trailing Options struct, and sqlate's `Begin`
and `Transact` a trailing `*sql.TxOptions`, in place of variadic option functions, which are
removed.
- The `pgx` requirement is v5.13.0, which fixes a panic in `Rows.Values` on a server response
with fewer row values than declared fields
([GHSA-w9rp-vfrh-q8vv](https://github.com/jackc/pgx/security/advisories/GHSA-w9rp-vfrh-q8vv)),
and no longer marks a connection closed while receiving a response as safe to retry, so
`database/sql` does not repeat a statement that may already have executed.

## [v0.6.0] - 2026-11-11

### Changed
Expand Down Expand Up @@ -104,7 +119,8 @@ The first release of the PostgreSQL provider, against `github.com/standards-lab/
classifying constraint violations arrives with the write path. `postgres.Provider` types the
selection constant.

[Unreleased]: https://github.com/standards-lab/go-database/compare/postgres/v0.6.0...HEAD
[Unreleased]: https://github.com/standards-lab/go-database/compare/postgres/v0.7.0...HEAD
[v0.7.0]: https://github.com/standards-lab/go-database/compare/postgres/v0.6.0...postgres/v0.7.0
[v0.6.0]: https://github.com/standards-lab/go-database/compare/postgres/v0.5.0...postgres/v0.6.0
[v0.5.0]: https://github.com/standards-lab/go-database/compare/postgres/v0.4.0...postgres/v0.5.0
[v0.4.0]: https://github.com/standards-lab/go-database/compare/postgres/v0.3.0...postgres/v0.4.0
Expand Down
6 changes: 3 additions & 3 deletions postgres/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module github.com/standards-lab/go-database/postgres
go 1.27

require (
github.com/jackc/pgx/v5 v5.12.0
github.com/jackc/pgx/v5 v5.13.0
github.com/standards-lab/go-database v0.11.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.3 // indirect
github.com/standards-lab/go-core v0.11.0 // indirect
github.com/standards-lab/sqlate v0.6.1 // indirect
github.com/standards-lab/go-core v0.12.0 // indirect
github.com/standards-lab/sqlate v0.7.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/text v0.41.0 // indirect
)
12 changes: 6 additions & 6 deletions postgres/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.12.0 h1:hWwax8YiGT0YiN50U1oIDn5kgR7KC2tG3rmavwpPkxY=
github.com/jackc/pgx/v5 v5.12.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=
github.com/jackc/pgx/v5 v5.13.0 h1:Hl08k64maU0nrRyVUEbwCnK6/gT0EhMrtAqBHz9/gA4=
github.com/jackc/pgx/v5 v5.13.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=
github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao=
github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/standards-lab/go-core v0.11.0 h1:Ls/jdYSRaC6ch187B1xNz39qfWXmSgNpOZ5Ot+2z+U8=
github.com/standards-lab/go-core v0.11.0/go.mod h1:Yl/R+gM9YWczsePBCKXdPGh3FSdZ/5i8SapLIh3BDHU=
github.com/standards-lab/go-core v0.12.0 h1:sqrzK6QSppEYpwvVAyiWgzNuOsjfK0Kwe86GPuZpHsU=
github.com/standards-lab/go-core v0.12.0/go.mod h1:Yl/R+gM9YWczsePBCKXdPGh3FSdZ/5i8SapLIh3BDHU=
github.com/standards-lab/go-database v0.11.0 h1:VP8AK2oCGIBVJQ+M7U4p86xuH1QOaTLghsEwxmS0P60=
github.com/standards-lab/go-database v0.11.0/go.mod h1:luUB/jkgn8pTYndSrGElnh6L/IHmuHeub1W8u+Lo+dY=
github.com/standards-lab/sqlate v0.6.1 h1:Kgp8freMX9Gs2KweBwG4IPzENgUmSJq8+r4tq02cDNw=
github.com/standards-lab/sqlate v0.6.1/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/standards-lab/sqlate v0.7.0 h1:GVb3OCDXwSS9RCxQ2BRIfV73zwBeJBhkvDLBJnTYd/U=
github.com/standards-lab/sqlate v0.7.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
15 changes: 8 additions & 7 deletions sweep/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
// non-positive interval or budget, and a non-positive shutdown timeout,
// before any I/O.
//
// An [Option] configures the rest:
// Its trailing [Options] configures the rest; the zero Options keeps the
// defaults:
//
// - [Unlocked] runs without claiming the reclaimers' locks, on a dialect
// without sqlate.TryLocker or with it. Without it, such a dialect is
// [ErrNoTryLocker].
// - [Gated] sets the [Gate] each pass holds shared, such as go-core's
// - [Options.Unlocked] runs without claiming the reclaimers' locks, on a
// dialect without sqlate.TryLocker or with it. Without it, such a
// dialect is [ErrNoTryLocker].
// - [Options.Gate] is the [Gate] each pass holds shared, such as go-core's
// reactor.Gate, so an operation that holds it exclusively, such as a
// schema change, runs with the passes paused.
// - [OnPass] sets the [Hook] each [Pass] is reported to. Without it, a
// pass that returned an error is logged at warn on the sweeper's
// - [Options.OnPass] is the [Hook] each [Pass] is reported to. Without
// it, a pass that returned an error is logged at warn on the sweeper's
// logger, and no other pass is.
//
// # Cycles
Expand Down
99 changes: 41 additions & 58 deletions sweep/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Registration struct {

// ErrNoTryLocker reports a dialect without the sqlate.TryLocker capability,
// which a Sweeper needs to claim each reclaimer's lock without waiting;
// [Unlocked] opts out.
// [Options.Unlocked] opts out.
var ErrNoTryLocker = errors.New("sweep: dialect has no try-lock capability")

// Gate is a quiesce gate each pass holds shared, so an operation that holds
Expand All @@ -68,7 +68,7 @@ type Pass struct {
Name string
// More is what the pass reported: whether more may remain.
More bool
// Err is the pass's error, or, under [Gated], the gate's failure other
// Err is the pass's error, or, under [Options.Gate], the gate's failure other
// than its wait ending, in which case no pass ran.
Err error
// Duration is how long the pass took, its wait on the gate included.
Expand All @@ -79,46 +79,27 @@ type Pass struct {
// block for long: the sweeper waits on it before the next pass.
type Hook func(ctx context.Context, p Pass)

// Option configures a [Sweeper].
type Option func(*options)

type options struct {
unlocked bool
gate Gate
hook Hook
}

// Unlocked lets the sweeper run on a dialect without the sqlate.TryLocker
// capability, or with it, without claiming the locks. Its intended shape is
// a single process, or a consumer that serializes the sweep across
// processes some other way; concurrent sweepers without one each run every
// reclaimer, which their idempotent passes tolerate but no budget bounds.
func Unlocked() Option {
return func(o *options) { o.unlocked = true }
}

// Gated sets the gate each pass holds shared, from before the pass begins
// until it returns. Without it, or with a nil gate, passes hold none. A
// wait on g that the reactor's drain or the handler's context ends runs no
// pass and reports nothing. Any other failure of g is reported as the
// pass's error, with no pass run, and ends the cycle.
func Gated(g Gate) Option {
return func(o *options) {
if g != nil {
o.gate = g
}
}
}

// OnPass sets the hook each [Pass] is reported to. Without it, or with a
// nil hook, the sweeper logs each pass that returned an error at warn on
// its logger and nothing else.
func OnPass(h Hook) Option {
return func(o *options) {
if h != nil {
o.hook = h
}
}
// Options configures a [Sweeper]; its zero value claims each reclaimer's
// lock, holds no gate, and logs each failed pass at warn.
type Options struct {
// Unlocked runs the sweeper without claiming the reclaimers' locks, on a
// dialect without the sqlate.TryLocker capability or with it. Its
// intended shape is a single process, or a consumer that serializes the
// sweep across processes some other way; concurrent sweepers without
// one each run every reclaimer, which their idempotent passes tolerate
// but no budget bounds. Without it, a dialect without the capability is
// [ErrNoTryLocker].
Unlocked bool
// Gate, when non-nil, is held shared by each pass, from before the pass
// begins until it returns; when nil, passes hold none. A wait on it that
// the reactor's drain or the handler's context ends runs no pass and
// reports nothing. Any other failure of it is reported as the pass's
// error, with no pass run, and ends the cycle.
Gate Gate
// OnPass, when non-nil, receives each [Pass]. When nil, the sweeper logs
// each pass that returned an error at warn on its logger and nothing
// else.
OnPass Hook
}

// Sweeper runs a fixed list of reclaimers as one go-core reactor. Each
Expand All @@ -143,7 +124,7 @@ type Sweeper struct {
regs []Registration
names map[string]bool
tick time.Duration
opts options
opts Options
logger *slog.Logger
wake *reactor.Waker
r *reactor.Reactor[time.Time]
Expand All @@ -158,16 +139,18 @@ type Sweeper struct {

// New builds a Sweeper over db from the reclaimers in regs, logging to
// logger. shutdown is the process's drain timeout: the reactor cancels a
// pass still running at half of it ([reactor.CancelWithin]). New refuses,
// with an error and before any I/O, an empty list; a registration with an
// empty name, a name another uses, a nil reclaimer, or a non-positive
// interval or budget; a non-positive shutdown; and a dialect without
// sqlate.TryLocker, as [ErrNoTryLocker], unless [Unlocked]. A nil db or
// logger is a wiring defect and panics.
// pass still running at half of it ([reactor.Options.CancelAfter], set
// by [reactor.CancelWithin]). New refuses, with an error and before any
// I/O, an empty list; a registration with an empty name, a name another
// uses, a nil reclaimer, or a non-positive interval or budget; a
// non-positive shutdown; and a dialect without sqlate.TryLocker, as
// [ErrNoTryLocker], unless opts sets [Options.Unlocked]. The zero opts
// keeps the defaults; see [Options]. A nil db or logger is a wiring defect
// and panics.
//
// Every reclaimer is due at start: New nudges the source, which delivers
// as soon as the reactor starts.
func New(db *sqlate.DB, regs []Registration, shutdown time.Duration, logger *slog.Logger, opts ...Option) (*Sweeper, error) {
func New(db *sqlate.DB, regs []Registration, shutdown time.Duration, logger *slog.Logger, opts Options) (*Sweeper, error) {
if db == nil {
panic("sweep: nil db")
}
Expand All @@ -184,14 +167,14 @@ func New(db *sqlate.DB, regs []Registration, shutdown time.Duration, logger *slo
db: db,
regs: append([]Registration(nil), regs...),
names: make(map[string]bool, len(regs)),
opts: opts,
logger: logger,
now: time.Now,
last: make(map[string]time.Time, len(regs)),
nudged: make(map[string]bool, len(regs)),
}
s.opts.hook = logPasses(logger)
for _, opt := range opts {
opt(&s.opts)
if s.opts.OnPass == nil {
s.opts.OnPass = logPasses(logger)
}
for _, reg := range s.regs {
switch {
Expand All @@ -211,7 +194,7 @@ func New(db *sqlate.DB, regs []Registration, shutdown time.Duration, logger *slo
s.tick = reg.Policy.Interval
}
}
if !s.opts.unlocked {
if !s.opts.Unlocked {
locker, ok := db.Dialect().(sqlate.TryLocker)
if !ok {
return nil, ErrNoTryLocker
Expand All @@ -220,7 +203,7 @@ func New(db *sqlate.DB, regs []Registration, shutdown time.Duration, logger *slo
}
s.wake = reactor.Wake(s.tick)
s.wake.Nudge()
s.r = reactor.New(s.wake, s.handle, reactor.CancelWithin(shutdown))
s.r = reactor.New(s.wake, s.handle, reactor.Options{CancelAfter: reactor.CancelWithin(shutdown)})
return s, nil
}

Expand Down Expand Up @@ -337,7 +320,7 @@ func (s *Sweeper) cycle(ctx context.Context, drain <-chan struct{}, reg Registra
if !ran {
return nil
}
s.opts.hook(ctx, p)
s.opts.OnPass(ctx, p)
// more alone decides: a failed pass that reports more is followed
// by another, since a reclaimer's passes are idempotent. A gate
// failure ran no pass, so it reports no more and ends the cycle.
Expand Down Expand Up @@ -402,7 +385,7 @@ func (s *Sweeper) claimFailed(ctx context.Context, name string, err error) error
func (s *Sweeper) pass(ctx context.Context, drain <-chan struct{}, reg Registration) (p Pass, ran bool) {
p.Name = reg.Name
start := time.Now()
if s.opts.gate != nil {
if s.opts.Gate != nil {
release, ended, err := s.share(ctx, drain)
if ended {
return p, false
Expand Down Expand Up @@ -432,7 +415,7 @@ func (s *Sweeper) share(ctx context.Context, drain <-chan struct{}) (release fun
}
}()
}
release, err = s.opts.gate.Shared(wait)
release, err = s.opts.Gate.Shared(wait)
if err != nil {
if wait.Err() != nil && errors.Is(err, wait.Err()) {
return nil, true, nil
Expand Down
Loading
Loading