diff --git a/.github/dependabot.yml b/.github/dependabot.yml index a85ccad..c19100a 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -40,6 +40,11 @@ updates: - package-ecosystem: github-actions directory: / + # PostgreSQL majors move by hand with a data upgrade (docker-compose.yml); a bump would + # also turn the one-shot 17 dump image into 18. + ignore: + - dependency-name: postgres + update-types: ["version-update:semver-major"] schedule: interval: weekly cooldown: @@ -52,6 +57,11 @@ updates: - package-ecosystem: docker directory: / + # PostgreSQL majors move by hand with a data upgrade (docker-compose.yml); a bump would + # also turn the one-shot 17 dump image into 18. + ignore: + - dependency-name: postgres + update-types: ["version-update:semver-major"] schedule: interval: weekly cooldown: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dae643c..de9cbba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -172,7 +172,7 @@ jobs: profile: [normal, fips] services: postgres: - image: postgres:17-alpine + image: postgres:18.7-alpine3.24@sha256:0115267ab5a509022df9319c77be365cef335adb71556c75f60f5360ee247e71 env: POSTGRES_USER: ci POSTGRES_PASSWORD: ci @@ -330,7 +330,7 @@ jobs: name: Docker image build & run if: github.event.pull_request.draft != true runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 15 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Build image @@ -339,6 +339,10 @@ jobs: - name: Strong-key profile preserves Compose configuration run: python3 docs/check-strong-crypto-compose.py + # Seeds 17 data with the previous docker-compose.yml, then proves this one moves it to 18. + - name: PostgreSQL 17 to 18 upgrade on docker-compose.yml + run: scripts/test-postgres-17-to-18.sh + - name: Container serves the app run: | # The hardening docker-compose.yml applies, so the image is proven to run under it. diff --git a/AGENTS.md b/AGENTS.md index 8620098..7a1ab07 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -20,7 +20,7 @@ Design authority: `docs/superpowers/specs/2026-09-26-kypulse-design.md`. Plans l - `internal/egress`, `internal/poller`, `internal/alerts`, `internal/notify`, `internal/monitor`, `internal/kyyard`: the monitoring backend; each has its own AGENTS.md. - `web/`: the React PWA, embedded into the binary from `web/dist`. -- `scripts/`: smoke test. +- `scripts/`: smoke test; `postgres-17-to-18.sh` is the shared Ky kit, vendored byte for byte (never edit it here), and `test-postgres-17-to-18.sh` proves it on `docker-compose.yml`. - `docs/RESTORE.md`: restore runbook. - `docs/STRONG-CRYPTO.md`, `docker-compose.strong-crypto.yml` and `docs/check-strong-crypto-compose.py`: supported key/TLS configuration, diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e7bda..b0e6f9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.6.1 — pending +- The bundled PostgreSQL (`--profile postgres`) moves from 17 to 18 (`18.7`, pinned by digest), in a new `postgres` volume. The next `up` dumps the 17 `pgdata` volume once and restores it into 18; `pgdata` is kept for rollback, and removing it is the operator's choice. It needs about twice the 17 data in free disk. Read [PostgreSQL 17 to 18](docs/CONFIGURATION.md#postgresql-17-to-18) before upgrading. + - Keep SQLite `v1.62.0` with its upstream-required libc `v1.80.1` and memory `v1.12.1`; defer independent updates of those runtime dependencies. - Prepare application version `0.6.1` (from `0.6.0`) for the compatible dependency refresh. Rebuild or install the updated application; this version bump requires no additional data, account, key or configuration migration. diff --git a/Makefile b/Makefile index d92bdec..685e012 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ test-race: # Runs the same suite against PostgreSQL; needs a reachable server. The default DSN matches # CI's throwaway one, e.g. docker run --rm -d -p 127.0.0.1:5432:5432 -e POSTGRES_USER=ci -# -e POSTGRES_PASSWORD=ci -e POSTGRES_DB=ci_test postgres:17-alpine +# -e POSTGRES_PASSWORD=ci -e POSTGRES_DB=ci_test postgres:18.7-alpine3.24@sha256:0115267ab5a509022df9319c77be365cef335adb71556c75f60f5360ee247e71 test-postgres: @echo "==> Running test suite against PostgreSQL..." @KYPULSE_TEST_POSTGRES_DSN="$${KYPULSE_TEST_POSTGRES_DSN:-postgres://ci:ci@127.0.0.1:5432/ci_test?sslmode=disable}" go test -count=1 ./... diff --git a/cmd/server/deploy_test.go b/cmd/server/deploy_test.go index f9523c3..f451535 100644 --- a/cmd/server/deploy_test.go +++ b/cmd/server/deploy_test.go @@ -1,6 +1,8 @@ package main import ( + "crypto/sha256" + "fmt" "os" "path/filepath" "regexp" @@ -65,3 +67,64 @@ func TestComposeForwardsSettingsAndBindsLoopback(t *testing.T) { t.Errorf("app port is not loopback by default: %v", ports) } } + +// The bundled PostgreSQL is 18, pinned by digest, on the volume layout 18 accepts, and an +// install that ran 17 is moved by the vendored, unmodified upgrade script: never a bare image +// bump, which leaves 18 refusing the old mount. scripts/test-postgres-17-to-18.sh runs it. +func TestComposePostgres18UpgradesFrom17(t *testing.T) { + raw, err := os.ReadFile(filepath.Join("..", "..", "docker-compose.yml")) + if err != nil { + t.Fatal(err) + } + body := string(raw) + service := func(name string) string { + m := regexp.MustCompile(`(?ms)^ ` + name + `:\n(.*?)(?:\n\n|\z)`).FindStringSubmatch(body) + if m == nil { + t.Fatalf("docker-compose.yml has no %s service", name) + } + return m[1] + } + for name, rules := range map[string][]string{ + "postgres": { + `(?m)^ image: postgres:18\.7-alpine3\.24@sha256:0115267ab5a509022df9319c77be365cef335adb71556c75f60f5360ee247e71$`, + `(?m)^ entrypoint: \[bash, /ky/postgres-17-to-18\.sh, restore\]$`, + `(?m)^ command: \[postgres\]$`, + `(?m)^ stop_grace_period: 2m$`, + `(?m)^ postgres-17-dump:\n condition: service_completed_successfully$`, + `(?m)^ - postgres:/var/lib/postgresql$`, + `(?m)^ - \./scripts/postgres-17-to-18\.sh:/ky/postgres-17-to-18\.sh:ro$`, + }, + "postgres-17-dump": { + `(?m)^ image: postgres:17\.12-alpine3\.24@sha256:f9cf3e1f0066f0eb0c48707d864f0ac5941e9ff454a072077d97a4d278d07d73$`, + `(?m)^ network_mode: none$`, + `(?m)^ stop_grace_period: 2m$`, + `(?m)^ restart: "no"$`, + `(?m)^ profiles: \["postgres"\]$`, + `(?m)^ entrypoint: \[bash, /ky/postgres-17-to-18\.sh, dump\]$`, + `(?m)^ POSTGRES_USER: postgres$`, + `(?m)^ OLD: /var/lib/postgresql/data$`, + `(?m)^ NEW: /pg18$`, + `(?m)^ - pgdata:/var/lib/postgresql/data$`, + `(?m)^ - postgres:/pg18$`, + `(?m)^ - \./scripts/postgres-17-to-18\.sh:/ky/postgres-17-to-18\.sh:ro$`, + }, + } { + s := service(name) + for _, rule := range rules { + if !regexp.MustCompile(rule).MatchString(s) { + t.Errorf("%s service lacks %s", name, rule) + } + } + } + if strings.Contains(service("postgres"), "/var/lib/postgresql/data") || strings.Contains(service("postgres"), "PGDATA") { + t.Error("the 18 service mounts the 17 path or sets PGDATA; 18 refuses the first and the second skips the move") + } + script, err := os.ReadFile(filepath.Join("..", "..", "scripts", "postgres-17-to-18.sh")) + if err != nil { + t.Fatal(err) + } + // The kit's sha256; every Ky repo vendors the same bytes. + if sum := fmt.Sprintf("%x", sha256.Sum256(script)); sum != "2992644109716cd34297fbcb76eba8907f4a5e3031a12c05aade010e5ea2b571" { + t.Errorf("scripts/postgres-17-to-18.sh differs from the shared kit (sha256 %s)", sum) + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 14cbe9a..c1bcb67 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -109,17 +109,49 @@ services: # KYPULSE_DB_DSN=postgres://postgres:@postgres:5432/kypulse?sslmode=disable # (An unset password cannot be a `:?` error here: Compose interpolates every profile, so it # would break the default SQLite install too.) + # PostgreSQL 18 keeps its data in the `postgres` volume. An install that ran 17 (volume + # `pgdata`) is moved on the first `up`: postgres-17-dump dumps it once, then the 18 service + # restores that dump before it starts. `pgdata` is only read and never removed + # (docs/CONFIGURATION.md, PostgreSQL 17 to 18). postgres: - image: postgres:17-alpine + image: postgres:18.7-alpine3.24@sha256:0115267ab5a509022df9319c77be365cef335adb71556c75f60f5360ee247e71 container_name: kypulse_postgres restart: unless-stopped + # Room for a clean shutdown, so the data never needs crash recovery on the next start. + stop_grace_period: 2m profiles: ["postgres"] + entrypoint: [bash, /ky/postgres-17-to-18.sh, restore] + command: [postgres] + depends_on: + postgres-17-dump: + condition: service_completed_successfully environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-} POSTGRES_DB: kypulse + volumes: + - postgres:/var/lib/postgresql + - ./scripts/postgres-17-to-18.sh:/ky/postgres-17-to-18.sh:ro + + # One-shot, no network: dumps PostgreSQL 17 data from `pgdata` into `postgres` once, and + # exits at once when there is nothing to move or the move is done. + postgres-17-dump: + image: postgres:17.12-alpine3.24@sha256:f9cf3e1f0066f0eb0c48707d864f0ac5941e9ff454a072077d97a4d278d07d73 + restart: "no" + stop_grace_period: 2m + profiles: ["postgres"] + network_mode: none + entrypoint: [bash, /ky/postgres-17-to-18.sh, dump] + environment: + POSTGRES_USER: postgres + OLD: /var/lib/postgresql/data + NEW: /pg18 volumes: - pgdata:/var/lib/postgresql/data + - postgres:/pg18 + - ./scripts/postgres-17-to-18.sh:/ky/postgres-17-to-18.sh:ro volumes: + # PostgreSQL 17 data, kept for rollback until the operator removes it. pgdata: + postgres: diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 83c6ef7..366831e 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -87,5 +87,31 @@ compose network. Point KyPulse at it with `KYPULSE_DB_DRIVER=postgres` and `KYPULSE_DB_DSN=postgres://postgres:@postgres:5432/kypulse?sslmode=disable`. Backups snapshot SQLite only, so a PostgreSQL deployment backs up its database separately. +### PostgreSQL 17 to 18 + +The bundled database is PostgreSQL 18, with its data in the `postgres` volume. Earlier +releases ran 17 with its data in the `pgdata` volume, which 18 cannot open. The next +`docker compose --profile postgres up -d` moves it. Nothing needs to be done by hand: + +- The one-shot `postgres-17-dump` service (the 17 image, no network) dumps `pgdata` once + into `upgrade-from-17/` in the `postgres` volume. 18 then restores that dump before it + starts, so roles, passwords and data come across. A restore that is interrupted runs + again on the next start. A dump that does not restore never leaves an empty database: + postgres keeps failing, with the error in `docker compose logs postgres`. Later starts + skip both steps. A fresh install has nothing to move and initialises 18. +- **Space:** about twice the size of the 17 data, free on the `postgres` volume's disk. The + dump checks first and refuses with the amount it needs, writing nothing. +- **Time:** about as long as a `pg_dump` and a restore of the same data. A small KyPulse + database takes seconds. Until postgres accepts connections, the app fails its start and + Compose restarts it, as on any slow postgres start. +- **Rollback:** `pgdata` is only read and never removed, so 17 can still run on it. Stop + the stack, check out the previous release's `docker-compose.yml` and start it. Rows + written while on 18 are not carried back. +- **Clean-up**, once you are satisfied with 18, and only if you want it. The volume names + carry your Compose project name, `kypulse-server` by default, so check them with + `docker volume ls`. Run `docker volume rm _pgdata` to drop the 17 data and + `docker compose --profile postgres run --rm --no-deps --entrypoint rm postgres -r + /var/lib/postgresql/upgrade-from-17` to drop the dump. + The published image is `ghcr.io/busnes-app/kypulse-server`. `docker-compose.yml` explains how to pin a commit by digest (`KYPULSE_IMAGE`) and verify its build attestation first. diff --git a/scripts/postgres-17-to-18.sh b/scripts/postgres-17-to-18.sh new file mode 100755 index 0000000..85b7729 --- /dev/null +++ b/scripts/postgres-17-to-18.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# Moves a PostgreSQL 17 data directory to 18 by dump and restore. The 17 data is only read +# (starting 17 may replay its WAL); it is never deleted or reinitialised. +# dump in the postgres:17 image. OLD = the 17 data directory, NEW = the directory that the +# 18 container mounts at /var/lib/postgresql. Dumps OLD once into NEW/upgrade-from-17. +# restore in the postgres:18 image, as its entrypoint: restores that dump once into +# $PGDATA.part, renames it to $PGDATA, then runs the image's entrypoint with the args. +# Runs as root (compose) or as the postgres user (Kubernetes runAsUser 70). +set -Eeuo pipefail +mode=${1:-}; shift || true +new=${NEW:-/var/lib/postgresql} +dump_dir=$new/upgrade-from-17 +as_pg() { if [ "$(id -u)" = 0 ]; then gosu postgres "$@"; else "$@"; fi; } +sock=$(mktemp -d) +[ "$(id -u)" != 0 ] || chown postgres "$sock" + +case $mode in +dump) + old=${OLD:?set OLD to the PostgreSQL 17 data directory} + if [ -s "$new/18/docker/PG_VERSION" ] || [ -s "$dump_dir/dump.sql" ]; then + echo "postgres-17-to-18: already done"; exit 0 + fi + if [ ! -s "$old/PG_VERSION" ]; then + echo "postgres-17-to-18: no PostgreSQL 17 data in $old, nothing to move"; exit 0 + fi + if [ "$(cat "$old/PG_VERSION")" != 17 ]; then + echo "postgres-17-to-18: $old holds PostgreSQL $(cat "$old/PG_VERSION"), expected 17" >&2; exit 1 + fi + # PostgreSQL's own lock check cannot see a server in another container (its PID 1 looks + # like our grandparent), so a 17 still running on OLD would get a second postmaster. + if [ -e "$old/postmaster.pid" ]; then + echo "postgres-17-to-18: $old/postmaster.pid exists: PostgreSQL 17 is running on it, or did not stop cleanly; stop it (start it once with the previous setup and stop it, if it crashed), then start again" >&2 + exit 1 + fi + # The dump and the restored cluster each take at most about the size of the 17 data. + need=$(( $(du -sk "$old" | cut -f1) * 2 )) + free=$(df -Pk "$new" | awk 'NR == 2 { print $4 }') + if [ "$free" -lt "$need" ]; then + echo "postgres-17-to-18: $new has $((free / 1024)) MiB free, the move needs $((need / 1024)) MiB; free space or grow the volume, then start again" >&2 + exit 1 + fi + mkdir -p "$dump_dir" + [ "$(id -u)" != 0 ] || chown postgres "$new" "$dump_dir" + # No network, and a private socket that only trusts local connections. + printf 'local all all trust\n' > "$sock/hba" + [ "$(id -u)" != 0 ] || chown postgres "$sock/hba" + as_pg pg_ctl -D "$old" -w -t 3600 -l "$sock/log" \ + -o "-c listen_addresses= -c unix_socket_directories=$sock -c hba_file=$sock/hba" start || + { cat "$sock/log" >&2; exit 1; } + trap 'as_pg pg_ctl -D "$old" -m fast -w stop' EXIT + su=$(as_pg psql -h "$sock" -U "${POSTGRES_USER:-postgres}" -d template1 -XAtc "select rolname from pg_roles where oid = 10") + printf '%s\n' "$su" > "$dump_dir/superuser" + as_pg pg_dumpall -h "$sock" -U "$su" -f "$dump_dir/dump.sql.part" + as_pg sync "$dump_dir/dump.sql.part" + mv "$dump_dir/dump.sql.part" "$dump_dir/dump.sql" + sync "$dump_dir" + echo "postgres-17-to-18: dumped $old into $dump_dir/dump.sql; the 17 data is kept" + ;; +restore) + final=${PGDATA:?} + if [ ! -s "$final/PG_VERSION" ] && [ -s "$dump_dir/dump.sql" ]; then + # shellcheck source=/dev/null # the image's entrypoint, only inside the postgres image + source /usr/local/bin/docker-entrypoint.sh + # initdb's superuser is the 17 cluster's bootstrap role, so the dump's roles map one to one. + POSTGRES_USER=$(cat "$dump_dir/superuser") + export POSTGRES_USER PGDATA=$final.part + rm -rf "$PGDATA" # a restore an earlier start did not finish; never the 17 data + docker_setup_env + docker_create_db_directories + as_pg bash -c 'source /usr/local/bin/docker-entrypoint.sh; docker_setup_env; docker_init_database_dir; pg_setup_hba_conf' + as_pg pg_ctl -D "$PGDATA" -w -o "-c listen_addresses= -c unix_socket_directories=$sock" start + # pg_dumpall writes CREATE ROLE for the role it ran as, which initdb already made: drop that + # one line, only in the roles section above the first \connect (row data comes later). + q=$(as_pg psql -h "$sock" -U "$POSTGRES_USER" -d postgres -XAtc "select quote_ident(current_user)") + awk -v skip="CREATE ROLE $q;" 'seen || $0 != skip { print } /^\\connect / { seen = 1 }' "$dump_dir/dump.sql" | + as_pg psql -h "$sock" -U "$POSTGRES_USER" -d postgres -X -q -v ON_ERROR_STOP=1 + as_pg vacuumdb -h "$sock" -U "$POSTGRES_USER" --all --analyze-only -q + as_pg pg_ctl -D "$PGDATA" -m fast -w stop + sync "$PGDATA" + mv "$PGDATA" "$final" + sync "$(dirname "$final")" + export PGDATA=$final + echo "postgres-17-to-18: restored $dump_dir/dump.sql into $final" + fi + rm -rf "$sock" + exec docker-entrypoint.sh "$@" + ;; +*) echo "usage: postgres-17-to-18.sh dump | restore postgres [args...]" >&2; exit 2 ;; +esac diff --git a/scripts/test-postgres-17-to-18.sh b/scripts/test-postgres-17-to-18.sh new file mode 100755 index 0000000..b9b085c --- /dev/null +++ b/scripts/test-postgres-17-to-18.sh @@ -0,0 +1,96 @@ +#!/bin/bash +# Proves the PostgreSQL 17 -> 18 move on this repository's docker-compose.yml: data seeded by the +# previous compose file (17, volume pgdata) comes up on 18 with its rows, roles and passwords; +# 17 still starts on the kept volume; a rerun is a no-op; a dump that fails to restore never +# yields an empty cluster; a fresh install initialises. The move runs by `up` over a running 17 +# stack, and the dump refuses beside a live 17. +# usage: scripts/test-postgres-17-to-18.sh (NEW_COMPOSE= tests another candidate file) +set -Eeuo pipefail +root=$(cd "$(dirname "$0")/.." && pwd) +prev=dd87863676e311153543d3e7177f976fe39b9ef6 # the last docker-compose.yml on PostgreSQL 17 +new=${NEW_COMPOSE:-$root/docker-compose.yml} +P18=postgres:18.7-alpine3.24@sha256:0115267ab5a509022df9319c77be365cef335adb71556c75f60f5360ee247e71 +p=kypulse-pg1718-$$ +tmp=$(mktemp -d) +old=$tmp/old.yml +unset COMPOSE_FILE COMPOSE_PROFILES COMPOSE_PROJECT_NAME +git -C "$root" cat-file -e "$prev^{commit}" 2>/dev/null || git -C "$root" fetch -q --depth=1 origin "$prev" +git -C "$root" show "$prev:docker-compose.yml" > "$old" +printf 'POSTGRES_PASSWORD=pgpass\n' > "$tmp/env" +# A per-run container name, so this never meets an installed kypulse_postgres. +printf 'services:\n postgres:\n container_name: !reset null\n' > "$tmp/override.yml" + +dc() { local f=$1; shift; docker compose -p "$p" --project-directory "$root" --env-file "$tmp/env" -f "$f" -f "$tmp/override.yml" --profile postgres "$@"; } +cleanup() { dc "$new" down -v >/dev/null 2>&1 || true; dc "$old" down -v >/dev/null 2>&1 || true; rm -rf "$tmp"; } +trap cleanup EXIT +fail() { echo "FAIL: $*" >&2; dc "$new" logs postgres-17-dump postgres 2>&1 | tail -30 >&2 || true; exit 1; } +cid() { dc "$1" ps -aq postgres; } +in_vol() { docker run --rm -v "${p}_$1:/v" "$P18" sh -c "$2"; } +# Ready over TCP, so the entrypoint's socket-only init server does not count. +ready() { local c; for _ in $(seq 1 180); do c=$(cid "$1"); [ -n "$c" ] && docker exec "$c" pg_isready -q -h 127.0.0.1 -U postgres -d kypulse 2>/dev/null && return 0; sleep 1; done; return 1; } +# q : from another container on the compose +# network, as the app connects (inside the container, 127.0.0.1 is trusted without a password). +q() { docker run --rm --network "${p}_default" -e PGPASSWORD="$3" "$P18" psql -h postgres -U "$2" -d "$4" -XAtc "$5"; } + +# 1. Real 17 data from the previous compose file. +dc "$old" up -d postgres >/dev/null 2>&1 || fail "17 did not start" +ready "$old" || fail "17 not ready" +docker exec -i "$(cid "$old")" psql -U postgres -d kypulse -X -q -v ON_ERROR_STOP=1 <<'SQL' >/dev/null +create role reader login password 'readerpass'; +create database cdb owner reader locale 'C' encoding 'UTF8' template template0; +create table t(id bigserial primary key, s text unique, j jsonb); +insert into t(s, j) select 'row' || g, jsonb_build_object('g', g) from generate_series(1, 20000) g; +select lo_from_bytea(0, 'blob'); +\connect cdb reader +create table c(x text collate "C" primary key); +insert into c values ('a'), ('B'), ('é'); +SQL + +# 1b. The dump one-shot run by hand beside the live 17 refuses and writes nothing. +out=$(dc "$new" run --rm postgres-17-dump 2>&1) && fail "dump ran beside a live 17: $out" +grep -q 'postmaster.pid' <<<"$out" || fail "no live-17 message: $out" +in_vol postgres 'test ! -e /v/upgrade-from-17/dump.sql' || fail "dump written beside a live 17" + +# 2. `up` with the new file over the running 17 stack (no `down` first) moves it to 18. +dc "$new" up -d postgres >/dev/null 2>&1 || fail "up with the new compose file failed" +ready "$new" || fail "18 did not start after the move" +[ "$(q "$new" postgres pgpass kypulse 'show server_version_num' | cut -c1-2)" = 18 ] || fail "not 18" +[ "$(q "$new" postgres pgpass kypulse "select count(*) || ',' || sum((j->>'g')::int) from t")" = "20000,200010000" ] || fail "rows" +[ "$(q "$new" postgres pgpass kypulse 'select count(*) from pg_largeobject_metadata')" = 1 ] || fail "large object" +q "$new" postgres wrong kypulse 'select 1' >/dev/null 2>&1 && fail "a wrong password logged in" +[ "$(q "$new" reader readerpass cdb "select string_agg(x, ',' order by x) from c")" = "B,a,é" ] || fail "C collation or reader password" +[ "$(q "$new" postgres pgpass kypulse 'show data_directory')" = /var/lib/postgresql/18/docker ] || fail "data directory" +dc "$new" logs postgres-17-dump | grep -q 'dumped' || fail "dump did not run" +[ "$(in_vol pgdata 'cat /v/PG_VERSION')" = 17 ] || fail "17 volume changed" + +# 3. Rollback: the previous file still starts 17 on the kept volume, with every row. +dc "$new" down >/dev/null 2>&1 +dc "$old" up -d postgres >/dev/null 2>&1 || fail "17 did not start for the rollback" +ready "$old" || fail "17 no longer starts on its data" +[ "$(q "$old" postgres pgpass kypulse 'select count(*) from t')" = 20000 ] || fail "17 data changed" +dc "$old" down >/dev/null 2>&1 + +# 4. Rerun is a no-op. +dc "$new" up -d postgres >/dev/null 2>&1 || fail "rerun up failed" +ready "$new" || fail "18 did not restart" +dc "$new" logs postgres-17-dump | grep -q 'already done' || fail "rerun dumped again" +dc "$new" logs postgres | grep -q 'restored' && fail "rerun restored again" +[ "$(q "$new" postgres pgpass kypulse 'select count(*) from t')" = 20000 ] || fail "rows after rerun" + +# 5. A dump that fails to restore never yields an empty cluster, however often it restarts. +dc "$new" down >/dev/null 2>&1 +in_vol postgres 'mv /v/18/docker /v/18/docker.saved && echo "THIS IS NOT SQL;" >> /v/upgrade-from-17/dump.sql' +dc "$new" up -d postgres >/dev/null 2>&1 || fail "up with a bad dump failed early" +for _ in $(seq 1 180); do [ "$(docker inspect -f '{{.RestartCount}}' "$(cid "$new")")" -ge 2 ] && break; sleep 1; done +[ "$(docker inspect -f '{{.RestartCount}}' "$(cid "$new")")" -ge 2 ] || fail "bad dump: postgres did not fail and restart" +dc "$new" stop postgres >/dev/null 2>&1 +in_vol postgres 'test ! -e /v/18/docker' || fail "bad dump produced a cluster" + +# 6. A fresh install: nothing to move, a normal initdb. +dc "$new" down -v >/dev/null 2>&1 +dc "$new" up -d postgres >/dev/null 2>&1 || fail "fresh up failed" +ready "$new" || fail "fresh 18 did not start" +dc "$new" logs postgres-17-dump | grep -q 'nothing to move' || fail "fresh install did not skip the dump" +dc "$new" logs postgres | grep -q 'restored' && fail "fresh install restored something" +[ "$(q "$new" postgres pgpass kypulse 'show data_directory')" = /var/lib/postgresql/18/docker ] || fail "fresh data directory" +echo "PASS: docker-compose.yml moves 17 to 18; rollback, rerun, bad dump and fresh install hold"