Files

43 lines
2.1 KiB
Bash

#!/bin/bash
# ─────────────────────────────────────────────────────────────────────────
# postgresql/cutover/post_init_wrapper.sh
#
# Wraps Spilo's own /scripts/post_init.sh to work around a hardcoded
# assumption in that (unmodified, upstream) script: it contains the line
# ALTER VIEW public.failed_authentication_${i} OWNER TO postgres;
# with "postgres" as a literal, non-parameterized role name. This fails
# with "role postgres does not exist" whenever PGUSER_SUPERUSER is set to
# anything other than "postgres" — which we do (PGUSER_SUPERUSER=PGadmin,
# to match this environment's actual production superuser name).
#
# This wrapper is invoked in place of /scripts/post_init.sh via the
# SPILO_CONFIGURATION override (bootstrap.post_init), which Spilo's
# configure_spilo.py deep-merges on top of its own generated Patroni
# config, with user-supplied values taking precedence. Spilo's real
# post_init.sh is NOT modified or forked — this script only ensures a
# "postgres" role exists first, then hands off to the original script
# unchanged, with its original arguments intact.
#
# Arguments (passed through from Patroni's bootstrap.post_init call,
# unchanged): $1 = HUMAN_ROLE (e.g. "zalandos"), $2 = database name to
# connect to for the initial connection (see Spilo's own post_init.sh
# usage for exact semantics — not altered here).
# ─────────────────────────────────────────────────────────────────────────
set -euo pipefail
echo "post_init_wrapper: ensuring role 'postgres' exists before running Spilo's post_init.sh"
psql -d "$2" -v ON_ERROR_STOP=1 <<'SQL'
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'postgres') THEN
CREATE ROLE postgres;
END IF;
END;
$$;
SQL
echo "post_init_wrapper: role check complete, handing off to Spilo's post_init.sh"
exec /scripts/post_init.sh "$1" "$2"