diff --git a/AGENTS.md b/AGENTS.md index 12b327b..946fa55 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -189,6 +189,8 @@ When the user requests a durable behavior change, record it here or in the relev (17 image, no network, read-only root) dumps `pgdata/` once, and postgres's entrypoint restores it before the stock one; both run `scripts/postgres-17-to-18.sh`, Ky's shared kit, byte for byte (the check holds its checksum; fix it upstream in the kit, never only here). + After a rollback that ran 17, the dump refuses until the operator moves 17 again or keeps 18 + (the kit's `18/moved-from-17.checkpoint`). `scripts/test-postgres-17-to-18.sh` (CI job `postgres-upgrade`) proves the move on 17 data seeded with the last 17 Compose files; operator steps in `docs/CHAT-SETUP.md` "PostgreSQL 18". `restore-matrix` (profile `restore`, `docker compose run --rm restore-matrix`) runs the app image as diff --git a/CHANGELOG.md b/CHANGELOG.md index 28ac745..b5a8e91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,10 @@ rollback until you remove it. It needs about twice the database's size free ([Chat setup](docs/CHAT-SETUP.md#postgresql-18)). The app image's `pg_dump`/`pg_restore` are now PostgreSQL 18, and capsules taken on 17 still restore. +- After a rollback to PostgreSQL 17 that ran 17 again, going forward to 18 no longer starts the + older 18 cluster silently: Postgres stays stopped and `postgres-17-dump` says how to move 17 + again or keep 18. Removing `upgrade-from-17/` alone no longer matters to later starts + ([Chat setup](docs/CHAT-SETUP.md#postgresql-18)). - A SCIM account with the admin role signs in to the console through KyIdentity only when `KY_SCIM_ALLOW_ADMIN=true`, and SCIM turning an inactive admin back on needs it too. An empty `roles` from SCIM now removes the admin role. diff --git a/docs/CHAT-SETUP.md b/docs/CHAT-SETUP.md index 58bf386..f128bd8 100644 --- a/docs/CHAT-SETUP.md +++ b/docs/CHAT-SETUP.md @@ -210,10 +210,23 @@ Matrix's database runs PostgreSQL 18. A stack set up on PostgreSQL 17 moves on t stops with the error instead. - **Roll back** by checking out the previous release (Compose files and app image together) and `docker compose up -d`: 17 starts on its untouched data. Anything written while on 18 - is not in it. Going forward again reuses the 18 cluster. -- **Clean up**, once you are satisfied and no longer want to roll back, if you choose to: - `docker compose exec postgres rm -r /var/lib/postgresql/pgdata /var/lib/postgresql/upgrade-from-17`. - That frees the space and makes a rollback to 17 impossible. + is not in it. +- **Going forward again** after a rollback: if 17 ran at all, the 18 cluster lacks what 17 + wrote since, so `postgres-17-dump` refuses, Postgres stays stopped, and + `docker compose logs postgres-17-dump` says so. Choose one, then `docker compose up -d`: + - **Move 17 again** (18 gets 17's data as it is now; what was written on 18 stays only in + the renamed copy): `docker compose down`, then rename the 18 cluster and the dump, deleting + nothing: + `docker compose run --rm --no-deps --entrypoint sh postgres -c 'mv /var/lib/postgresql/18 /var/lib/postgresql/18.replaced; [ ! -e /var/lib/postgresql/upgrade-from-17 ] || mv /var/lib/postgresql/upgrade-from-17 /var/lib/postgresql/upgrade-from-17.replaced'`. + Passwords you rotated while on 18 come back as they were on 17: apply `role-passwords.sql` + again as `matrix-init` prints. + - **Keep 18** as it is (what 17 wrote during the rollback stays only in `pgdata/`): + `docker compose run --rm --no-deps --entrypoint rm postgres /var/lib/postgresql/18/moved-from-17.checkpoint`. +- **Clean up**, once you are satisfied, if you choose to; each step is separate: + - `docker compose exec postgres rm -r /var/lib/postgresql/upgrade-from-17` frees the dump. + It is safe: a rollback is still possible and still caught on the way back. + - `docker compose exec postgres rm -r /var/lib/postgresql/pgdata` frees the 17 data and makes + a rollback to 17 impossible. Element's Home view (no room open) shows "Welcome to KyMessages" and links to the other suite apps KyIdentity lists, served by the console at `/element-home.html` (Element's diff --git a/scripts/check-compose-matrix.sh b/scripts/check-compose-matrix.sh index 98620c4..6556b83 100755 --- a/scripts/check-compose-matrix.sh +++ b/scripts/check-compose-matrix.sh @@ -33,7 +33,7 @@ declare -A image=( declare -A named=([livekit]="" [postgres]=matrix-postgres [postgres-17-dump]=matrix-postgres [synapse]=matrix-media [mas]="" [element]="" [synapse-media-owner]=matrix-media) declare -A own=([livekit]=livekit [postgres]=postgres [postgres-17-dump]=none [synapse]=synapse [mas]=mas [element]=element [synapse-media-owner]=none) # The vendored PostgreSQL 17 -> 18 kit, byte for byte (Ky's postgres-17-to-18.sh). -pgkit=$root/scripts/postgres-17-to-18.sh pgkit_sha256=2992644109716cd34297fbcb76eba8907f4a5e3031a12c05aade010e5ea2b571 +pgkit=$root/scripts/postgres-17-to-18.sh pgkit_sha256=195ae8684e34dd515158a88b1f76b918cf6e3f3c854ce83fe4fb190a5178cf06 [ "$(jq -c '[.services | keys[] | select(. != "app")] | sort' <<<"$out")" = '["element","livekit","mas","postgres","postgres-17-dump","synapse","synapse-media-owner"]' ] \ || bad "unexpected Matrix services: $(jq -c '.services | keys' <<<"$out")" diff --git a/scripts/postgres-17-to-18.sh b/scripts/postgres-17-to-18.sh index 85b7729..263b42f 100755 --- a/scripts/postgres-17-to-18.sh +++ b/scripts/postgres-17-to-18.sh @@ -10,6 +10,9 @@ set -Eeuo pipefail mode=${1:-}; shift || true new=${NEW:-/var/lib/postgresql} dump_dir=$new/upgrade-from-17 +# 17's checkpoint as the dump left it. Beside the 18 cluster, so removing the dump keeps it, and +# renaming 18/ to move again takes it along. +moved=$new/18/moved-from-17.checkpoint as_pg() { if [ "$(id -u)" = 0 ]; then gosu postgres "$@"; else "$@"; fi; } sock=$(mktemp -d) [ "$(id -u)" != 0 ] || chown postgres "$sock" @@ -17,8 +20,23 @@ sock=$(mktemp -d) case $mode in dump) old=${OLD:?set OLD to the PostgreSQL 17 data directory} + checkpoint() { LC_ALL=C as_pg pg_controldata "$old" | awk -F': *' '/^Latest checkpoint location/ { print $2 }'; } + # 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. + refuse_running() { + 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 + } if [ -s "$new/18/docker/PG_VERSION" ] || [ -s "$dump_dir/dump.sql" ]; then - echo "postgres-17-to-18: already done"; exit 0 + if [ ! -s "$old/PG_VERSION" ]; then echo "postgres-17-to-18: already done"; exit 0; fi + [ ! -e "$old/postmaster.pid" ] || refuse_running + # Moved by kit v2, which kept no checkpoint: take the current one as the moved state. + [ -s "$moved" ] || { mkdir -p "$(dirname "$moved")"; checkpoint > "$moved"; } + # Every start of 17 writes a new checkpoint. A changed one means 17 ran after the move (a + # rollback), so 18 lacks what 17 wrote since: never start 18 on it silently. + if [ "$(checkpoint)" = "$(cat "$moved")" ]; then echo "postgres-17-to-18: already done"; exit 0; fi + echo "postgres-17-to-18: PostgreSQL 17 has run on $old since it was moved to 18, so 18 lacks what 17 wrote since then. To move 17 again: stop everything, rename $new/18 (and $dump_dir, if it is there) with a suffix such as .replaced (nothing is deleted), then start again. To keep 18 as it is: delete $moved, then start again." >&2 + exit 1 fi if [ ! -s "$old/PG_VERSION" ]; then echo "postgres-17-to-18: no PostgreSQL 17 data in $old, nothing to move"; exit 0 @@ -26,12 +44,7 @@ dump) 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 + [ ! -e "$old/postmaster.pid" ] || refuse_running # 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 }') @@ -52,6 +65,11 @@ dump) 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" + trap - EXIT + as_pg pg_ctl -D "$old" -m fast -w stop + mkdir -p "$new/18" + [ "$(id -u)" != 0 ] || chown postgres "$new/18" + checkpoint > "$moved" 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" diff --git a/scripts/test-postgres-17-to-18.sh b/scripts/test-postgres-17-to-18.sh index 3112167..74d19ef 100755 --- a/scripts/test-postgres-17-to-18.sh +++ b/scripts/test-postgres-17-to-18.sh @@ -4,8 +4,10 @@ # 17-era backup, then switches to this checkout's Compose files with 17 still running and # proves: rows, roles and passwords survive; the 17 data is untouched and 17 still starts on it # (rollback); a rerun is a no-op; matrix-init's kybackup-role.sql and rotation work after the -# move; a dump that fails to restore never yields a cluster; a fresh install initialises; and -# that 17 backup restores on 18 with the app's pg_restore flags. +# move; after a rollback that wrote on 17, 18 never starts until the operator moves 17 again or +# keeps 18, also after the dump was cleaned up; a dump that fails to restore never yields a +# cluster; a fresh install initialises; and that 17 backup restores on 18 with the app's +# pg_restore flags. # usage: scripts/test-postgres-17-to-18.sh [kymessages binary] (needs docker, jq, git; go # when no binary is given). Postgres runs as the caller (KY_MATRIX_UID); contacts nothing. set -Eeuo pipefail @@ -107,15 +109,38 @@ logs postgres-17-dump | grep -q 'already done' || fail "rerun dumped again" logs postgres | grep -q 'restored' && fail "rerun restored again" [ "$(q synapse synapse 'select count(*) from t')" = 50001 ] || fail "rows after rerun" -# 5. Rollback: the previous Compose files start 17 on the untouched 17 data; then forward again. -dc down --remove-orphans >/dev/null 2>&1; rm -r "$s/scripts"; old -up || fail "17 no longer starts on its data" -[ "$(q postgres postgres 'show server_version_num' | cut -c1-2)" = 17 ] || fail "rollback is not 17" -[ "$(q kybackup synapse 'select count(*) from t')" = 50000 ] || fail "17 data changed" -dc down --remove-orphans >/dev/null 2>&1; new -up || fail "18 did not start after the rollback" -[ "$(q synapse synapse 'select count(*) from t')" = 50001 ] || fail "18 data lost after the rollback" -logs postgres | grep -q 'restored' && fail "restored again after the rollback" +# 5. Rollback: the previous Compose files start 17 on the untouched 17 data, and it takes a write. +rollback() { + dc down --remove-orphans >/dev/null 2>&1; rm -r "$s/scripts"; old + up || fail "17 no longer starts on its data" + [ "$(q postgres postgres 'show server_version_num' | cut -c1-2)" = 17 ] || fail "rollback is not 17" + [ "$(q kybackup synapse "select count(*) from t where s not like 'on17%'")" = 50000 ] || fail "17 data changed" + q postgres synapse "insert into t(s, j) values ('$1', '{\"g\": 0}')" >/dev/null || fail "write on 17" + dc down --remove-orphans >/dev/null 2>&1; new +} +# has ROW: on 18 (as postgres, since a password rotated on 18 is not on 17). +has() { q postgres synapse "select count(*) from t where s = '$1'"; } +# forward_refused: 18 never starts on a 17 that ran after the move, and the dump says why. +forward_refused() { + up && fail "18 started after 17 ran since the move ($1)" + logs postgres-17-dump | grep -q 'has run on' || fail "no rollback refusal ($1): $(logs postgres-17-dump)" + [ -z "$(dc ps -q postgres)" ] || fail "postgres was started after the refusal ($1)" +} +rollback on17 +forward_refused "first rollback" +# 5b. The refusal's "move 17 again": rename 18/ and upgrade-from-17/, start again. 18 then holds +# what 17 wrote, and the replaced 18 cluster is kept. +dc down --remove-orphans >/dev/null 2>&1 +# docs/CHAT-SETUP.md's commands, verbatim. +dc run --rm --no-deps --entrypoint sh postgres -c 'mv /var/lib/postgresql/18 /var/lib/postgresql/18.replaced; [ ! -e /var/lib/postgresql/upgrade-from-17 ] || mv /var/lib/postgresql/upgrade-from-17 /var/lib/postgresql/upgrade-from-17.replaced' >/dev/null 2>&1 || fail "move again" +up || fail "18 did not start after moving 17 again" +logs postgres | grep -q 'restored' || fail "no restore when moving again" +[ "$(has on17)" = 1 ] || fail "moving again lost the row written on 17" +[ "$(has on18)" = 0 ] || fail "moving again kept a row only 18 had" +in_vol "$proj" 'test -s /v/18.replaced/docker/PG_VERSION && test -s /v/upgrade-from-17.replaced/dump.sql' || fail "the replaced 18 is gone" +# The rotation of step 3 was made on 18 only: apply it again, as matrix-init printed. +dc exec -T postgres psql -U postgres -v ON_ERROR_STOP=1 -f /docker-entrypoint-initdb.d/role-passwords.sql >/dev/null || fail "role-passwords.sql after moving again" +[ "$(q synapse synapse 'select count(*) from t')" = 50001 ] || fail "rows after moving again" # 6. A dump that fails to restore never yields a cluster, however often postgres restarts. dc stop postgres >/dev/null 2>&1 @@ -128,8 +153,22 @@ in_vol "$proj" 'rm -rf /v/18/docker.part && mv /v/18/docker.saved /v/18/docker & up || fail "18 did not come back" [ "$(q synapse synapse 'select count(*) from t')" = 50001 ] || fail "rows after the bad dump" -# 6b. The documented clean-up removes the 17 data and the dump; 18 keeps running on later starts. -dc exec -T postgres rm -r /var/lib/postgresql/pgdata /var/lib/postgresql/upgrade-from-17 || fail "clean-up" +# 6b. The documented clean-up of the dump alone (17 kept): later starts go on, and a rollback +# after it is still refused. +dc exec -T postgres rm -r /var/lib/postgresql/upgrade-from-17 || fail "dump clean-up" +dc up -d --wait --wait-timeout 300 --force-recreate postgres-17-dump postgres >/dev/null 2>&1 || fail "18 after removing the dump" +logs postgres-17-dump | grep -q 'already done' || fail "dump after removing the dump" +rollback on17b +forward_refused "rollback after removing the dump" +# 6c. The refusal's "keep 18": delete the checkpoint, start again. 18 runs as it was, and stays. +dc run --rm --no-deps --entrypoint rm postgres /var/lib/postgresql/18/moved-from-17.checkpoint >/dev/null 2>&1 || fail "keep 18" +up || fail "keep 18 was not accepted" +[ "$(has on17b)" = 0 ] || fail "keep 18 moved 17 again" +dc up -d --wait --wait-timeout 300 --force-recreate postgres-17-dump postgres >/dev/null 2>&1 || fail "keep 18 did not stick" +logs postgres-17-dump | grep -q 'already done' || fail "dump after keep 18" + +# 6d. The documented clean-up of the 17 data; 18 keeps running on later starts. +dc exec -T postgres rm -r /var/lib/postgresql/pgdata || fail "clean-up" dc up -d --wait --wait-timeout 300 --force-recreate postgres-17-dump postgres >/dev/null 2>&1 || fail "18 after the clean-up" logs postgres-17-dump | grep -q 'already done' || fail "dump after the clean-up" [ "$(q synapse synapse 'select count(*) from t')" = 50001 ] || fail "rows after the clean-up" @@ -154,4 +193,4 @@ for db in synapse mas; do done [ "$(fq synapse synapse "select count(*) || ',' || sum((j->>'g')::int) from t")" = $seeded ] || fail "restored synapse rows" [ "$(fq mas mas "select string_agg(name, ',') from u where name % 'alic'")" = alice ] || fail "restored mas pg_trgm" -echo "PASS: 17 -> 18 on the shipped Compose files: move with 17 running, rows and passwords, matrix-init re-run and rotation, rerun, rollback to 17, bad dump, fresh install, 17 backup restored on 18" +echo "PASS: 17 -> 18 on the shipped Compose files: move with 17 running, rows and passwords, matrix-init re-run and rotation, rerun, rollback to 17 then refused, moved again or kept, after clean-up too, bad dump, fresh install, 17 backup restored on 18"