Files
discourse/cc-db-entrypoint.sh
T
notplants af76d06c2d feat(db): use pgautoupgrade image instead of pgvector + bespoke pg_upgrade
discourse core does not use the vector extension, so the pgvector image is
unnecessary. Switch db to pgautoupgrade/pgautoupgrade:17-trixie, which handles
major-version upgrades (old binaries, initdb, pg_upgrade) itself.

pgautoupgrade assumes the old cluster's install user (oid 10) equals
POSTGRES_USER and fails pg_upgrade otherwise (issue #115). A thin wrapper
entrypoint detects the old install user when an upgrade is pending and exports
POSTGRES_USER so pgautoupgrade matches it. Replaces the 64-line hand-rolled
pg_upgrade entrypoint.
2026-06-18 22:12:57 +00:00

36 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Co-op Cloud wrapper around pgautoupgrade's entrypoint.
#
# pgautoupgrade runs pg_upgrade assuming the old cluster's bootstrap superuser
# (the "install user", oid 10) equals $POSTGRES_USER. That is not always true
# (e.g. a cluster bootstrapped with the default "postgres" superuser and a
# separate "discourse" app role), and a mismatch makes pg_upgrade's consistency
# check fail (pgautoupgrade issue #115). When a major-version upgrade is pending,
# detect the old cluster's real install user and run the upgrade as that user;
# pgautoupgrade handles everything else (old binaries, initdb, pg_upgrade).
set -e
if [ -s "$PGDATA/PG_VERSION" ]; then
OLD_VERSION="$(cat "$PGDATA/PG_VERSION")"
TARGET_VERSION="${PGTARGET%%.*}"
if [ -n "$OLD_VERSION" ] && [ -n "$TARGET_VERSION" ] && [ "$OLD_VERSION" != "$TARGET_VERSION" ]; then
# query the old cluster in single-user mode (no socket/auth needed) with
# pgautoupgrade's bundled old-version binaries
OLD_POSTGRES="/usr/local-pg${OLD_VERSION}/bin/postgres"
if [ -x "$OLD_POSTGRES" ]; then
INSTALL_USER="$(echo 'select rolname from pg_authid where oid = 10;' \
| gosu postgres "$OLD_POSTGRES" --single -D "$PGDATA" template1 2>/dev/null \
| sed -n 's/.*rolname = "\([^"]*\)".*/\1/p' | head -n1)"
if [ -n "$INSTALL_USER" ]; then
echo "cc-db-entrypoint: old cluster (pg$OLD_VERSION) install user is '$INSTALL_USER'; running the pg$TARGET_VERSION upgrade as that user"
export POSTGRES_USER="$INSTALL_USER"
else
echo "cc-db-entrypoint: WARNING could not detect old install user; letting pgautoupgrade use POSTGRES_USER=$POSTGRES_USER"
fi
fi
fi
fi
exec /usr/local/bin/docker-entrypoint.sh "$@"