diff --git a/README.md b/README.md index 34a2e5a..1fbf425 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,8 @@ The repository is a Go workspace of four modules. Tasks run through ```sh mise run check # build, vet, format, fix, tidy, unit tier, lint, sqlint, lint-context, lint-references; writes nothing -mise run currency # report requirements, Go, tools, actions, images, and Dockerfile source ARGs behind their latest -mise run upgrade # upgrade every module's go directive and requirements, and the tools, to their latest +mise run currency # report tools, Go, requirements, actions, images, and Dockerfile source ARGs behind their latest, except a held one; writes nothing +mise run upgrade # move every pin currency reports to its latest, except a held one: tools, then Go and requirements, actions, images, and Dockerfile source ARGs mise run db:up # build and start the compose PostgreSQL and wait until it is healthy mise run db:down # stop PostgreSQL, keeping its data mise run db:reset # stop PostgreSQL and drop its data @@ -144,8 +144,10 @@ image pin and which carries its configuration and health check. The Dockerfile b from source at `ARG PGVECTOR_VERSION`, the extension's one version pin ([the image](docs/features.md#an-image-with-pgvector)). `mise run currency` reports either pin when it trails its latest: it compares the `FROM` tag with the image's tags, and the `ARG` with -the highest semver tag of the GitHub repository that the `ADD` line fetches. `mise.toml` sets -`SQLATE_DSN`, whose port follows `POSTGRES_PORT`. +the highest semver tag of the GitHub repository that the `ADD` line fetches; `mise run upgrade` +moves it. A pin named in `mise.toml`'s `HOLDS` list, one ` : ` line each, is +reported as held and never moved. `mise.toml` sets `SQLATE_DSN`, whose port follows +`POSTGRES_PORT`. The integration tier covers two modules, `postgres` and `postgres/pgvector`, each behind the `integration` build tag. `mise run integration` runs both against the running PostgreSQL, and diff --git a/STANDARDS.md b/STANDARDS.md index 49e61f8..5a5ba6b 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -8,6 +8,7 @@ The judgement calls the standards-reviewer applies to sqlate, beyond what `mise - `architecture/standards/go-elemental/principles/topology-and-naming.md`: the base module and the `postgres`, `postgres/pgvector`, and `sqlint` sub-modules, and their tags. - `architecture/standards/go-elemental/principles/release-and-ci.md`: the check, currency, and a changelog per module. - `architecture/standards/go-elemental/principles/dsl-driven-services.md`: `query`, `header`, and every authored `.sql` file, the library's patterns included. +- `architecture/standards/go-elemental/principles/database-extensions.md`: `migrate`'s extension declarations, `postgres`'s extension statements, the `postgres/pgvector` sub-module, and the compose Dockerfile. - `architecture/standards/go-elemental/principles/baseline-standards.md`: `query`'s guards and projections, which take every name and bound from their caller. - `architecture/standards/go-elemental/principles/utc-times.md`: `query.Scanner` and `query.Scalar`, which return every time in UTC, a cursor's keyed time, and the `postgres` dialect's `timestamp with time zone` history. - `architecture/standards/go-elemental/principles/construction-and-execution.md`: `DB.Begin`, `DB.Transact`, and `Transact` take their transaction's configuration as a trailing `*sql.TxOptions` whose nil keeps a default transaction, and a new runner that takes configuration does the same. diff --git a/context/goals/v1.data.extensions.md b/context/goals/v1.data.extensions.md index af05f56..93a8673 100644 --- a/context/goals/v1.data.extensions.md +++ b/context/goals/v1.data.extensions.md @@ -10,136 +10,126 @@ 2. [x] sqlint-extensions 3. [x] pgvector-sql 4. [x] pgvector-go -5. [ ] principles +5. [x] principles -## Task brief · pgvector-go +## Task brief · principles **Problem** -sqlate's migrator now manages extensions, and sqlate's `postgres/pgvector` names pgvector's -metrics. go-database has neither yet: +The strategy for Postgres extensions sits in a coordinator note, and the note trails the merged +code in four places. sqlate's and go-database's merged code already expresses the strategy. Until +it is an architecture page, review can't cite it, and the two repositories' standards files can't +point to it. -- admin's `Status` reports only migration sets. It says ready while `Verify` fails on an - extension. -- No go-database module resolves a configured metric or shows how an application uses pgvector-go. -- go-database has no live tests. - -capstone-athena needs all three before it stores embeddings. +Promoting it extends rolling currency to the pin of a source a Dockerfile builds. Neither +repository's upgrade meets rolling currency today: +- it moves only the mise pins and the Go modules, and runs the Go modules first; +- it moves no workflow action pin, no compose `FROM` line, and no source ARG; +- it reads no hold list; +- sqlate's guide says a maintainer raises the pgvector ARG by hand. **Behaviors** -1. go-database's base module requires sqlate v0.8.0, claude-plugins 0.24.0 and mise-action - v5.3.0, and its currency reports none of them trailing. -2. `admin.Status` gains `Extensions` (`extensions` in JSON). It has one entry per declaration, in - declared order, each with name, removed, installed, default and pending. The list is empty, - never nil, when no extension is declared. -3. `Status.Ready` is false when any declaration doesn't match the database: one pending, one the - image doesn't offer, or one with no update path to the default. The service's readiness follows - `Status` as it does for sets. -4. `ErrExtensionUnavailable` and `ErrNoUpdatePath`, from `Start`, `Verify` and the schema verbs, - wrap in `admin.ErrConflict` with the migrate sentinel still in the chain, and mark the service - not ready. -5. Before `Start` applies, it logs each pending extension as `extension pending; applying`, with - its name and its installed and default versions, beside the pending sets. -6. A new module, `github.com/standards-lab/go-database/postgres/pgvector`, with its own changelog - and `doc.go`: - - Its package exports `ParseMetric(name string) (Metric, error)` and a sentinel - `ErrUnknownMetric`. - - `Metric` is sqlate's `pgvector.Metric`; the package declares no type of its own. - - `ParseMetric` matches a name exactly against sqlate's `pgvector.Metrics()`. An unknown name - returns an error that unwraps to `ErrUnknownMetric` and names it, with no I/O. - - The package imports only the standard library and sqlate's `postgres/pgvector`. pgvector-go - v0.4.1, go-database v0.13.0 and sqlate's `postgres` v0.9.0 are required for the tests only. -7. The package doc shows pgvector-go used directly, in the packages the application's design - declares: `pgvector.NewVector` for a parameter, and `sql.Null[pgvector.Vector]` with `Slice()` - for a nullable column. Domain types hold `[]float32`. The doc also shows a metric resolved from - configuration with `ParseMetric`. -8. go-database has a compose stack: - - It copies sqlate's canonical PostgreSQL image: `postgres:19.0-alpine`, with pgvector built - from source and pinned by `ARG PGVECTOR_VERSION`. - - It publishes `127.0.0.1:${POSTGRES_PORT:-5438}` and keeps its data on tmpfs. - - `mise run db:up` builds and starts it and waits until it is healthy, and `db:down` stops it. -9. go-database's currency is sqlate's script. It checks the requirements, Go, tools, actions, - image lines, the Dockerfile's `FROM` line and its source ARGs. It keeps go-database's comment on - failing scans, and mise installs crane for the image checks. -10. The module's integration tier sits behind the `integration` tag and uses `GO_DATABASE_DSN` and - `pgtest`: - - `admin.Start` with `vector` declared activates it. `Status` then reports it installed at the - image's default, not pending, and `Ready`. - - A `vector` column round-trips `pgvector.NewVector` values, and a NULL, through - `sql.Null[pgvector.Vector]`, over pgx's `database/sql` adapter. - - Each metric's operator orders a nearest-neighbor query. -11. `mise run integration` runs the tier against a running stack. `mise run acceptance` brings the - stack up, runs the tier, brings the stack down whatever the result, and prints the stack's log - on failure. -12. The check, build, tidy, test and lint tasks include the new module, and go.work uses it. The - check vets, fixes and lints the `integration`-tagged suite with its tag. -13. The README and the standards file name the module and its dependency line. The README says the - integration tier runs locally only, through `mise run acceptance`. -14. The base changelog records `v0.13.0`: the sqlate requirement, `Status.Extensions`, the - readiness rule and the error mapping. The module's changelog records `v0.1.0`. +1. go-database's `postgres` module requires go-database v0.13.0, with an Unreleased changelog + entry, and go-database's currency reports nothing trailing. +2. sqlate's currency script takes one mode, `report` or `upgrade`. `mise run currency` runs + `report` and `mise run upgrade` runs `upgrade`. The two modes use the same scans, so upgrade + moves exactly what report lists. +3. `upgrade` moves every pin `report` lists as trailing, in rolling currency's order: + 1. the mise pins; + 2. each module's `go` directive (to the current Go minor, with no toolchain line), its direct + requirements, and a tidy; + 3. each workflow's action pins; + 4. each compose Dockerfile's `FROM` tag; + 5. each `ARG _VERSION` whose `ADD` fetches a GitHub repository at `v${_VERSION}`. + + It prints one `upgrade:` line for each pin it moves, and on a current tree it changes nothing. +4. A `HOLDS` list in mise.toml's `[env]` holds pins, one ` : ` line each, with + the item named as `report` names it, as in docproc. It ships empty. + - `report` shows a held pin as `held:` with its reason, and doesn't count it as trailing. + - `report` exits 0 when every pin it lists is held. + - `upgrade` skips a held pin. +5. sqlate's guide describes both modes, what `upgrade` moves, and `HOLDS`, and no longer says the + ARG is raised by hand. The README's task lines match. +6. go-database's currency script is byte-identical to sqlate's. Its tasks, its empty `HOLDS`, and + its README task lines are wired the same way, so behaviors 2–4 hold in go-database. +7. sqlate's standards file points to the Go Elemental "Database extensions" principle. Its scope + line names `migrate`'s extension declarations, `postgres`'s extension statements, the + `postgres/pgvector` sub-module, and the compose Dockerfile. +8. go-database's standards file points to the same page. Its scope line names `admin`'s extension + status and readiness, the `postgres/pgvector` sub-module, and the compose Dockerfile. +9. A new Go Elemental principle, "Database extensions" (level `go-elemental`), is listed in both + Go Elemental indexes and states four rules: + - (a) An extension's features are native tier, and only the packages that may import a provider + use them. + - (b) The image is the version. Each extension is built from source onto the engine's one + baseline image and pinned once in the Dockerfile, and no code declares its version. + - (c) The application declares extensions by name only. The migrator reconciles them against + the engine's catalog ahead of every migration set, with no history table: + - it fails before any set when an extension is unavailable or has no update path; + - it drops an extension only when the extension is declared removed; + - readiness is false on any mismatch. + - (d) An extension's SQL layer is a sub-module of the SQL library, and its Go layer a sub-module + of the infrastructure library. The engine module never requires its extensions, and each + extension module proves itself in its own integration tier. + + The page asserts no repository's version or state, and may link sqlate's examples pinned at a + tag. It leaves out build flags, catalog names, metric tables, sqlint config syntax, typed + errors, status fields, index recipes, client-library usage, and the shipper rule. +10. "DSL-driven services" extends its native-forms bullet: the linter also refuses a declared + extension's native forms in a standard-tier file, and a native file's header names the + extension it uses. +11. "Rolling currency" lists, among the versions it covers, each source a Dockerfile builds, + pinned by one build argument. "Releases and CI" adds it to what currency reports and upgrade + moves. +12. The architecture check passes, and so does `mise run check` in sqlate and in go-database. **Test seams** -- `admin.Service` over sqlate's `sqltest`, with a dialect that implements - `migrate.ExtensionCatalog` (behaviors 2–5). -- `ParseMetric`'s unit tests (behavior 6). -- `mise run currency`, and `mise run db:up` offering `vector` (behaviors 1, 8 and 9). -- `pgtest.MigratedWith` and `admin.New` in the module's integration tier, through - `mise run acceptance` (behavior 10). +- Each repository's `mise run currency` and `mise run upgrade`, run against pins lowered by hand + and against a `HOLDS` entry (behaviors 1–4 and 6). +- The architecture check (links, front matter, `lint-context`, `lint-references`), plus a read for + coherence and for rule-only claims (behaviors 9–11). **Slices** -1. Upgrade go-database: sqlate v0.8.0, claude-plugins 0.24.0, mise-action v5.3.0. Done when - currency exits 0 with the current script and the check passes. -2. admin's extension status: `Status.Extensions`, the readiness rule, the error mapping and the - pending log, with unit tests; admin's doc.go and the base changelog's Unreleased entry. Done - when the check passes. -3. The module and `ParseMetric`: the package, its unit tests, its doc with the pgvector-go usage, - its changelog; go.work, `GO_MODULES`, the README and the standards file. Done when the check - passes with the module listed. -4. The compose stack and currency: the copied Dockerfile, the compose files, `db:up`/`db:down`; - sqlate's currency script with the comment carried over, and crane. Done when `mise run db:up` - offers `vector` 0.8.7, currency exits 0, and lowering the ARG default by hand makes currency - report pgvector trailing. -5. The live proof: the integration tier and the module's test requirements, with go-database - bridged by a `replace` until `v0.13.0` is tagged; the `integration` and `acceptance` tasks, the - check's tagged pass, and the README's local-only note. Done when `mise run acceptance` passes - and the check passes. +1. go-database upgrade: `postgres` requires go-database v0.13.0, with the Unreleased entry. Done + when go-database's currency exits 0 and its check passes. +2. sqlate's upgrade mode and pointer: the two-mode script with `HOLDS`, the rewired tasks, the + guide and README, and the standards pointer. Done when: + - `upgrade` on the current tree leaves no diff; + - with an action pin, the compose `FROM` tag, and `PGVECTOR_VERSION` lowered by hand, `report` + lists all three and exits 1, then `upgrade` restores them and `report` exits 0; + - with the ARG lowered and a `HOLDS` line naming it, `upgrade` leaves the ARG, and `report` + shows it held and exits 0; + - the check passes. +3. go-database's copy and pointer: the copied script, its wiring and `HOLDS`, the README, and the + standards pointer. Done when slice 2's demonstrations pass in go-database, the script matches + sqlate's byte for byte, and the check passes. +4. The architecture pages: "Database extensions" with its two index entries, the "DSL-driven + services" clause, and source-ARG coverage in "Rolling currency" and "Releases and CI". Done + when the architecture check passes and the pages read as rules only. **Out of scope** - -- Wrapping pgvector-go's types, a `migrate.Extension` helper, SQL generation, a space registry, and - distance or score conversion. -- pgvector-go's pgx module and an AfterConnect hook. -- `halfvec` and `sparsevec` helpers. -- An integration job in CI, and changes to the CI workflow. -- Upgrading the ARG in `mise run upgrade`. -- A `postgres` release, and any change to sqlate. +- Pointers in any other repository. +- From docproc's script, everything sqlate and go-database have no use for: image pins in Go + source, tool modules, and per-tool upgrades. +- Changes to CI, including running currency there. +- Any Go code change, release, or tag. +- The shipper rule on a page. **Door** -One-way for both tags. Once `v0.13.0` is pushed, `Status.Extensions`, its JSON shape, the -readiness rule and the `ErrConflict` mapping are public. Once `postgres/pgvector/v0.1.0` is pushed, -`ParseMetric`, `ErrUnknownMetric` and the module path are public. The compose stack, the currency -script and the tests stay two-way. - -**Release** - -`v0.13.0`, then `postgres/pgvector/v0.1.0`, whose `go.mod` pins go-database `v0.13.0` in place of -the `replace`. +Two-way. Docs, scripts, and task wiring in three repositories, with no tag. ## Decisions -- plan: The image is an extension's sole version source: `postgres:19.0-alpine` with each extension - built from source, pgvector pinned by `ARG PGVECTOR_VERSION`. Rejected: pgvector/pgvector's - Debian images, PGDG on Debian, and a version declared in Go. -- plan: Shipped migration sets declare no extensions; only the application does. - sqlint-extensions: rejected sqlfluff and squawk, which lint against a fixed dialect and can't read a module's exported deny list. -- pgvector-sql: an extension module's live proof runs in its own module's integration tier, with - that module requiring `postgres` for `pgtest`. The engine module never requires its extensions. -- pgvector-go: go-database wraps no pgvector-go type; applications use pgvector-go directly - (go-database's `postgres/pgvector/doc.go`). Rejected: `Param`/`Dest` wrappers over `[]float32`, - which the import rule already makes unnecessary, and a `migrate.Extension` helper. +- pgvector-go: rejected `Param`/`Dest` wrappers over `[]float32`, which the import rule already + makes unnecessary, and a `migrate.Extension` helper in go-database's `postgres/pgvector`. +- principles: the extension rules move from Go Elemental to the architecture level once a second + standard's code expresses them. +- principles: go-database's `scripts/currency.sh` is a byte-for-byte copy of sqlate's. Rejected: + keeping the inline upgrade task in `mise.toml`. ## Pending edits @@ -147,29 +137,27 @@ the `replace`. `postgres/pgvector` sub-modules to their repositories' entries, and pgvector and pgvector-go under upstream sources. - coordinator · `roadmap/v1.data.extensions.toml`: sqlint-extensions' summary adds moving postgres - onto sqlate v0.8.0 and releasing `postgres/v0.9.0`. -- coordinator · `context/postgres-extensions.md`, for `principles` to promote: - - the check fails on any installed version with no update path to the image's default (read - through `pg_extension_update_paths`), not only a newer one; a migrate dialect capability - supplies the standard pair. - - "sqlint": an extension module with no forms declares an empty `[export.native_forms]` table - beside `[export] reason`, not `forms = []`; an `extensions` entry is a module path or a - directory of the tree. - - the image build: `make install` also needs `with_llvm=no`, since the official image's - PostgreSQL is built `--with-llvm` (llvm21); the extension's catalog name is `vector`, not - `pgvector`; pgvector publishes git tags but no GitHub releases, so currency reads tags - (`git ls-remote --tags`). - - "The modules": go-database's module holds no helpers over pgvector-go. Applications use - pgvector-go directly, and the module holds `ParseMetric` and the integration tier. - - once `principles` promotes the note, delete it, and remove its `context/README.md` entry and - the pointers in `data-intelligence.md` and `migration-sets.md`. + onto sqlate v0.8.0 and releasing `postgres/v0.9.0`; principles' `repos` become `["sqlate", + "go-database", "architecture"]`, and its summary adds sqlate's two-mode currency script, whose + `upgrade` also moves action pins, `FROM` tags, and source ARGs and skips a pin `HOLDS` names, + go-database's byte-for-byte copy of it, and the two standards pointers; `context` drops + `standards-lab/context/postgres-extensions.md`. +- coordinator · `context/postgres-extensions.md`: delete it, and remove its `context/README.md` + entry. +- coordinator · `context/data-intelligence.md`: the v1.data.extensions bullet and evidence 10 + point to architecture's Go Elemental "Database extensions" principle + (`standards/go-elemental/principles/database-extensions.md`) in place of the note; add a capstone + check: go-database's `database/sql` text format is fast enough for the capstone's batch loads. +- coordinator · `context/migration-sets.md`: drop the `postgres-extensions.md` pointer from the + shipper rule, and add that no shipped set needs an extension yet; the first that does reopens + the rule. - coordinator · `roadmap/sqlate-test-tier.toml`: create the goal ("test-tier follow-ups for sqlate and go-database", repos `["sqlate", "go-database"]`), whose summary lists: sqlate's check doesn't vet, fix or lint the `integration`-tagged suites of `postgres` and `postgres/pgvector` with their - tag, as tests-and-docs.md and release-and-ci.md require; neither sqlate nor go-database has a CI - integration job, and building pgvector adds about 10 seconds to one; the live tests' `text` - helper in `postgres/migrate_live_test.go` and its copy in `postgres/pgvector` lose a Scan error; - pgvector's forms tests copy sqlint's stripping, and can go through `sqlint.Lint` once + tag, as tests-and-docs.md and release-and-ci.md require; neither repository has a CI integration + job, and building pgvector adds about 10 seconds to one; the live tests' `text` helper in + `postgres/migrate_live_test.go` and its copy in `postgres/pgvector` lose a Scan error; + pgvector's forms tests copy sqlint's stripping, and can go through `sqlint.Lint`, since `sqlint/v0.5.0` is tagged. `roadmap/index.toml`: add it to `backlog`. - coordinator · `roadmap/sqlate-extension-match.toml`: create the goal ("admin follows sqlate's extension match rule", repos `["sqlate", "go-database"]`): admin's readiness (`matched` in diff --git a/docs/features.md b/docs/features.md index fedf429..e282711 100644 --- a/docs/features.md +++ b/docs/features.md @@ -906,7 +906,27 @@ compares an `ARG _VERSION=` whose `ADD` line fetches tag; it reads tags, not GitHub releases, because pgvector publishes no releases. A trailing `ARG` is reported as `compose/postgres/Dockerfile: pgvector/pgvector 0.8.6 -> 0.8.7` and fails currency, and a failed lookup fails it too. Currency ignores every other `ARG`. `mise run upgrade` -does not move the `ARG`, so a maintainer raises it by hand. +moves both pins to their latest: it rewrites the `FROM` tag and the `ARG`'s value. + +`mise run currency` and `mise run upgrade` run `scripts/currency.sh`'s two modes, `report` and +`upgrade`, which run the same scans, so `upgrade` moves exactly what `report` lists. The scans +cover the mise tools; each module's `go` directive, against the current Go minor, and its direct +requirements; the workflows' action pins; the `image:` lines of the compose files and workflows; +and each compose Dockerfile's `FROM` tag and source `ARG`. `report` prints one line for each pin +that trails, `: -> `, reads the network, writes nothing, and exits 1 +when any pin it lists is not held. `upgrade` moves every trailing pin in that order: the mise +pins first, so the modules then move under the Go just pinned; then each module's `go` directive, +to the current Go minor with no toolchain line, its direct requirements, and a tidy; then the +action pins, the `image:` lines, the `FROM` tags, and the `ARG`s. It prints one `upgrade:` line +for each pin it moves, and on a current tree it changes nothing. Review the diff and run the check +after it. + +`HOLDS`, in `mise.toml`'s `[env]`, holds a pin for a stated reason, one ` : ` +line each, the item named as `report` names it: a requirement by its module path, the `go` +directive as `go`, a mise tool by its name, an action by its path, an image by its image, and a +source `ARG` by its repository, as in `pgvector/pgvector 0.8.6: `. `report` shows a held +pin with `held:` and its reason and does not count it as trailing, and `upgrade` skips it. The +hold names the pin it holds, so moving the pin ends the hold. The list ships empty. ### The integration tier diff --git a/mise.toml b/mise.toml index a02f646..34e036b 100644 --- a/mise.toml +++ b/mise.toml @@ -12,6 +12,8 @@ jq = "1.8.2" [env] GO_MODULES = ". postgres postgres/pgvector sqlint" SQLATE_DSN = "postgres://app:app@127.0.0.1:{{ get_env(name='POSTGRES_PORT', default='5433') }}/app?sslmode=disable" +HOLDS = ''' +''' [tasks.check] description = "Build, vet, format, fix, tidy, test, and lint every module, then sqlint the repository under the committed go.work, through which it resolves the modules sqlint.toml names, then run lint-context and lint-references; writes nothing" @@ -46,25 +48,12 @@ exit "$fail" ''' [tasks.currency] -description = "Report every requirement, Go version, tool, action pin, image tag, and Dockerfile source ARG behind its latest; writes nothing" -run = "scripts/currency.sh" +description = "Report every mise tool, Go version, requirement, action pin, image tag, and Dockerfile source ARG behind its latest, a held one with its reason; writes nothing" +run = "scripts/currency.sh report" [tasks.upgrade] -description = "Upgrade every module's go directive (to the current Go minor, with no toolchain line) and direct requirements and the local mise tools to their latest, rewriting go.mod and mise.toml" -run = ''' -set -eu -export GOWORK=off -go_minor=$(mise latest go | cut -d. -f1,2) -for mod in $GO_MODULES; do - cd "$MISE_PROJECT_ROOT/$mod" - go mod edit -go="$go_minor" -toolchain=none - direct=$(go mod edit -json | jq -r '.Require[]? | select(.Indirect | not) | .Path') - [ -z "$direct" ] || go get $(printf '%s@latest ' $direct) - go mod tidy -done -cd "$MISE_PROJECT_ROOT" -mise upgrade --bump --local -''' +description = "Bump the mise tools, then each module's go directive (to the current Go minor, with no toolchain line) and direct requirements, with a tidy, then the action pins, image tags, Dockerfile FROM tags, and Dockerfile source ARGs, to their latest, but a held one; rewrites mise.toml, go.mod, go.sum, the workflows, and the compose files" +run = "scripts/currency.sh upgrade" [tasks.tidy] description = "Run go mod tidy across all modules" diff --git a/scripts/currency.sh b/scripts/currency.sh index e6bc92f..8425804 100755 --- a/scripts/currency.sh +++ b/scripts/currency.sh @@ -1,141 +1,346 @@ #!/usr/bin/env bash -# currency reports every dependency, toolchain, and pin that trails its latest -# release, one line each, and exits non-zero when it reports any. It reads the -# network and writes nothing. Run it through `mise run currency`. The actions it -# checks are those of the project root's workflows and, when the repository nests -# the project in a subdirectory, of the git top-level's. A compose Dockerfile's -# `ARG _VERSION` whose `ADD` fetches a GitHub repository at `v${_VERSION}` -# is compared with that repository's highest semver tag. A scan command that fails -# fails currency. +# currency reports, or upgrades, every pin that trails its latest release: the mise tools, each Go +# module's go directive and direct requirements, the workflows' action pins, the image lines of +# the compose files and workflows, each compose Dockerfile's FROM tag, and each compose +# Dockerfile's `ARG _VERSION` whose `ADD` fetches a GitHub repository at +# `v${_VERSION}`, compared with that repository's highest semver tag. Both modes run the same +# scans. report prints one line for each trailing pin, a pin the HOLDS list names as held with its +# reason, and exits 1 when any it reports is not held; it reads the network and writes nothing. +# upgrade moves every trailing pin but a held one, printing one line for each. The actions it +# scans are those of the project root's workflows and, when the repository nests the project in a +# subdirectory, of the git top-level's. A scan command that fails fails currency. +# Run it through `mise run currency` or `mise run upgrade`. +# +# usage: scripts/currency.sh +# See architecture/principles/rolling-currency.md set -euo pipefail cd "$MISE_PROJECT_ROOT" export GOWORK=off -stale=0 -report() { - echo "$1" - stale=1 +if [ $# -ne 1 ] || { [ "$1" != report ] && [ "$1" != upgrade ]; }; then + echo "usage: scripts/currency.sh " >&2 + exit 2 +fi +action=$1 +holds=${HOLDS:-} + +held() { + grep -m1 -E "^$(escape_regex "$1 $2: ")" <<<"$holds" | cut -d: -f2- | sed 's/^ //' || true } -matches() { - local pattern=$1 file - local files=() - shift +go() { + mise exec -- go "$@" +} + +escape_regex() { + # shellcheck disable=SC2001 # a bracket expression, which a ${//} pattern has no escape for + sed 's/[][\\.*^$|+?(){}]/\\&/g' <<<"$1" +} + +existing() { + local file for file in "$@"; do - if [ -e "$file" ]; then files+=("$file"); fi + if [ -e "$file" ]; then echo "$file"; fi done - [ ${#files[@]} -gt 0 ] || return 0 - grep -ho "$pattern" "${files[@]}" || [ $? -eq 1 ] } -report_outdated_requirements() { - local mod=$1 updates line - updates=$(cd "$mod" && go list -m -u -f \ - '{{if and (not .Main) (not .Indirect) .Update}}{{.Path}} {{.Version}} -> {{.Update.Version}}{{end}}' all) - while read -r line; do - if [ -n "$line" ]; then - report "$mod/go.mod: $line" - fi - done <<<"$updates" +matches() { + local pattern=$1 + shift + if [ $# -eq 0 ]; then + return 0 + fi + grep -ho "$pattern" "$@" || [ $? -eq 1 ] } -report_outdated_go_directive() { - local mod=$1 go_minor=$2 directive - directive=$(cd "$mod" && go mod edit -json | jq -r .Go | cut -d. -f1,2) - [ "$directive" = "$go_minor" ] || report "$mod/go.mod: go $directive -> go $go_minor" +outdated_mise_tools() { + mise outdated --bump --no-global --json | + jq -r 'to_entries[] | select(.value.bump != null) | "\(.key) \(.value.requested) \(.value.bump)"' } -report_outdated_mise_tools() { - local outdated line - outdated=$(mise outdated --bump --local --json | - jq -r 'to_entries[] | "\(.key) \(.value.requested) -> \(.value.bump)"') - while read -r line; do - if [ -n "$line" ]; then - report "mise.toml: $line" - fi - done <<<"$outdated" +outdated_requirements() { + go list -m -u -f \ + '{{if and (not .Main) (not .Indirect) .Update}}{{.Path}} {{.Version}} {{.Update.Version}}{{end}}' all } -report_outdated_actions() { - local actions uses action pin repo latest - actions=$(matches 'uses: *[^ ]*@[^ ]*' "${workflows[@]}" | sed 's/uses: *//' | sort -u) +trailing_go_directive() { + local minor + minor=$(go mod edit -json | jq -r .Go | cut -d. -f1,2) || return + if [ "$minor" != "$go_minor" ]; then + echo "go $minor $go_minor" + fi +} + +outdated_actions() { + local actions uses name pinned repo latest + actions=$(matches 'uses: *[^ ]*@[^ ]*' "${workflows[@]}" | sed 's/uses: *//' | sort -u) || return for uses in $actions; do - action=${uses%@*} - pin=${uses#*@} - repo=$(echo "$action" | cut -d/ -f1,2) - latest=$(gh api "repos/$repo/releases/latest" --jq .tag_name) - [ "$pin" = "$latest" ] || report ".github/workflows: $action $pin -> $latest" + name=${uses%@*} + pinned=${uses#*@} + repo=$(echo "$name" | cut -d/ -f1,2) || return + latest=$(gh api "repos/$repo/releases/latest" --jq .tag_name) || return + if [ "$pinned" != "$latest" ]; then + echo "$name $pinned $latest" + fi done } latest_dotted_release_tag() { - local image=$1 pin=$2 suffix dotted_release_pattern latest + local image=$1 pin=$2 suffix dotted_release_pattern tags latest suffix=${pin#"${pin%%[!0-9.]*}"} dotted_release_pattern="^[0-9]+(\.[0-9]+)+${suffix//./\\.}\$" - latest=$(crane ls "$image" | grep -E "$dotted_release_pattern" | sed "s/${suffix}\$//" | sort -V | tail -n1) || return + tags=$(crane ls "$image") || return + latest=$(grep -E "$dotted_release_pattern" <<<"$tags" | sed "s/${suffix}\$//" | sort -V | tail -n1) || return echo "$latest$suffix" } -report_outdated_image_lines() { - local images ref image pin latest - images=$(matches '^ *image: *[^ ]*' compose.yml compose/*.yml "${workflows[@]}" | - sed 's/^ *image: *//' | tr -d "\"'" | sort -u) +outdated_image_lines() { + local images ref image pinned latest + images=$(matches '^ *image: *[^ ]*' "${image_files[@]}" | sed 's/^ *image: *//' | tr -d "\"'" | sort -u) || return for ref in $images; do image=${ref%:*} - pin=${ref##*:} - latest=$(latest_dotted_release_tag "$image" "$pin") - [ "$pin" = "$latest" ] || report "images: $image $pin -> $latest" + pinned=${ref##*:} + latest=$(latest_dotted_release_tag "$image" "$pinned") || return + if [ "$pinned" != "$latest" ]; then + echo "$image $pinned $latest" + fi done } -report_outdated_dockerfile_bases() { - local dockerfile ref image pin latest +outdated_dockerfile_bases() { + local dockerfile ref image pinned latest for dockerfile in compose/*/Dockerfile; do - [ -e "$dockerfile" ] || continue - ref=$(sed -n '/^FROM /{s/^FROM *\([^ ]*\).*/\1/p;q;}' "$dockerfile") + if [ ! -e "$dockerfile" ]; then + continue + fi + ref=$(sed -n '/^FROM /{s/^FROM *\([^ ]*\).*/\1/p;q;}' "$dockerfile") || return image=${ref%:*} - pin=${ref##*:} - latest=$(latest_dotted_release_tag "$image" "$pin") - [ "$pin" = "$latest" ] || report "$dockerfile: $image $pin -> $latest" + pinned=${ref##*:} + latest=$(latest_dotted_release_tag "$image" "$pinned") || return + if [ "$pinned" != "$latest" ]; then + echo "$image $pinned $latest $dockerfile" + fi done } latest_semver_git_tag() { local repo=$1 tags latest tags=$(GIT_TERMINAL_PROMPT=0 git ls-remote --tags --refs "https://github.com/$repo.git") || return - latest=$(echo "$tags" | sed -n 's|.*refs/tags/v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)$|\1|p' | sort -V | tail -n1) - [ -n "$latest" ] || return 1 + latest=$(sed -n 's|.*refs/tags/v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)$|\1|p' <<<"$tags" | sort -V | tail -n1) || return + if [ -z "$latest" ]; then + return 1 + fi echo "$latest" } -report_outdated_dockerfile_args() { - local dockerfile args name pin repo latest +outdated_dockerfile_args() { + local dockerfile args arg pinned repo latest for dockerfile in compose/*/Dockerfile; do - [ -e "$dockerfile" ] || continue - args=$(sed -n 's/^ARG *\([A-Za-z0-9_]*_VERSION\)=\([^ ]*\) *$/\1 \2/p' "$dockerfile") - while read -r name pin; do - [ -n "$name" ] || continue - repo=$(sed -n "s|^ADD *https://github\.com/\([^/ ]*/[^/ ]*\)\.git#v\\\${$name} .*|\1|p" "$dockerfile") - [ -n "$repo" ] || continue - latest=$(latest_semver_git_tag "$repo") - [ "$pin" = "$latest" ] || report "$dockerfile: $repo $pin -> $latest" + if [ ! -e "$dockerfile" ]; then + continue + fi + args=$(sed -n 's/^ARG *\([A-Za-z0-9_]*_VERSION\)=\([^ ]*\) *$/\1 \2/p' "$dockerfile") || return + while read -r arg pinned; do + if [ -z "$arg" ]; then + continue + fi + repo=$(sed -n "s|^ADD *https://github\.com/\([^/ ]*/[^/ ]*\)\.git#v\\\${$arg} .*|\1|p" "$dockerfile") || return + if [ -z "$repo" ]; then + continue + fi + latest=$(latest_semver_git_tag "$repo") || return + if [ "$pinned" != "$latest" ]; then + echo "$repo $pinned $latest $dockerfile $arg" + fi done <<<"$args" done } +stale=0 +trailing() { + local reason + reason=$(held "$2" "$3") + if [ -n "$reason" ]; then + echo "$1: $2 $3 -> $4 held: $reason" + else + echo "$1: $2 $3 -> $4" + stale=1 + fi +} + +report_mise_tools() { + local scanned name pinned latest + scanned=$(outdated_mise_tools) || return + while read -r name pinned latest; do + if [ -n "$name" ]; then trailing mise.toml "$name" "$pinned" "$latest"; fi + done <<<"$scanned" +} + +report_go_modules() { + local mod scanned name pinned latest + for mod in $GO_MODULES; do + scanned=$(cd "$mod" && outdated_requirements && trailing_go_directive) || return + while read -r name pinned latest; do + if [ -n "$name" ]; then trailing "$mod/go.mod" "$name" "$pinned" "$latest"; fi + done <<<"$scanned" + done +} + +report_actions() { + local scanned name pinned latest + scanned=$(outdated_actions) || return + while read -r name pinned latest; do + if [ -n "$name" ]; then trailing .github/workflows "$name" "$pinned" "$latest"; fi + done <<<"$scanned" +} + +report_image_lines() { + local scanned name pinned latest + scanned=$(outdated_image_lines) || return + while read -r name pinned latest; do + if [ -n "$name" ]; then trailing images "$name" "$pinned" "$latest"; fi + done <<<"$scanned" +} + +report_dockerfile_bases() { + local scanned name pinned latest dockerfile + scanned=$(outdated_dockerfile_bases) || return + while read -r name pinned latest dockerfile; do + if [ -n "$name" ]; then trailing "$dockerfile" "$name" "$pinned" "$latest"; fi + done <<<"$scanned" +} + +report_dockerfile_args() { + local scanned name pinned latest dockerfile + scanned=$(outdated_dockerfile_args) || return + while read -r name pinned latest dockerfile _; do + if [ -n "$name" ]; then trailing "$dockerfile" "$name" "$pinned" "$latest"; fi + done <<<"$scanned" +} + +upgrade_mise_tools() { + local scanned name pinned latest + local tools=() + scanned=$(outdated_mise_tools) || return + while read -r name pinned latest; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: mise.toml: $name $pinned -> $latest" + tools+=("$name") + done <<<"$scanned" + if [ ${#tools[@]} -gt 0 ]; then + mise upgrade --bump --no-global "${tools[@]}" + fi +} + +upgrade_go_module() { + local mod=$1 scanned name pinned latest + local requirements=() + scanned=$(trailing_go_directive) || return + if [ -n "$scanned" ]; then + read -r name pinned latest <<<"$scanned" + if [ -z "$(held go "$pinned")" ]; then + echo "upgrade: $mod/go.mod: go $pinned -> $latest" + go mod edit -go="$go_minor" -toolchain=none + fi + fi + scanned=$(outdated_requirements) || return + while read -r name pinned latest; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: $mod/go.mod: $name $pinned -> $latest" + requirements+=("$name@latest") + done <<<"$scanned" + if [ ${#requirements[@]} -gt 0 ]; then + go get "${requirements[@]}" + fi + go mod tidy +} + +upgrade_go_modules() { + local mod + for mod in $GO_MODULES; do + ( + cd "$mod" + upgrade_go_module "$mod" + ) + done +} + +upgrade_actions() { + local scanned name pinned latest key + scanned=$(outdated_actions) || return + while read -r name pinned latest; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: .github/workflows: $name $pinned -> $latest" + key=$(escape_regex "$name@$pinned") || return + sed -i -E "s#(uses: *)$key( |\$)#\\1$name@$latest\\2#" "${workflows[@]}" + done <<<"$scanned" +} + +upgrade_image_lines() { + local scanned name pinned latest key + scanned=$(outdated_image_lines) || return + while read -r name pinned latest; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: images: $name $pinned -> $latest" + key=$(escape_regex "$name:$pinned") || return + sed -i -E "s#^( *image: *[\"']?)$key([\"']?( |\$))#\\1$name:$latest\\2#" "${image_files[@]}" + done <<<"$scanned" +} + +upgrade_dockerfile_bases() { + local scanned name pinned latest dockerfile key + scanned=$(outdated_dockerfile_bases) || return + while read -r name pinned latest dockerfile; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: $dockerfile: $name $pinned -> $latest" + key=$(escape_regex "$name:$pinned") || return + sed -i -E "s#^(FROM +)$key( |\$)#\\1$name:$latest\\2#" "$dockerfile" + done <<<"$scanned" +} + +upgrade_dockerfile_args() { + local scanned name pinned latest dockerfile arg key + scanned=$(outdated_dockerfile_args) || return + while read -r name pinned latest dockerfile arg; do + if [ -z "$name" ] || [ -n "$(held "$name" "$pinned")" ]; then + continue + fi + echo "upgrade: $dockerfile: $name $pinned -> $latest" + key=$(escape_regex "$pinned") || return + sed -i -E "s#^(ARG +$arg=)$key( *)\$#\\1$latest\\2#" "$dockerfile" + done <<<"$scanned" +} + root=$(pwd -P) top=$(git rev-parse --show-toplevel 2>/dev/null || echo "$root") -workflows=("$root"/.github/workflows/*.yml) -[ "$top" = "$root" ] || workflows+=("$top"/.github/workflows/*.yml) +candidates=("$root"/.github/workflows/*.yml) +if [ "$top" != "$root" ]; then candidates+=("$top"/.github/workflows/*.yml); fi +mapfile -t workflows < <(existing "${candidates[@]}") +mapfile -t image_files < <(existing compose.yml compose/*.yml "${workflows[@]}") go_minor=$(mise latest go | cut -d. -f1,2) -for mod in $GO_MODULES; do - report_outdated_requirements "$mod" - report_outdated_go_directive "$mod" "$go_minor" -done -report_outdated_mise_tools -report_outdated_actions -report_outdated_image_lines -report_outdated_dockerfile_bases -report_outdated_dockerfile_args -exit "$stale" +if [ "$action" = report ]; then + report_mise_tools + report_go_modules + report_actions + report_image_lines + report_dockerfile_bases + report_dockerfile_args + exit "$stale" +fi +upgrade_mise_tools +upgrade_go_modules +upgrade_actions +upgrade_image_lines +upgrade_dockerfile_bases +upgrade_dockerfile_args