Skip to content

Instantly share code, notes, and snippets.

@deric
Last active January 24, 2022 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deric/0e45b86650f94f144084c0007ceb15c7 to your computer and use it in GitHub Desktop.
Save deric/0e45b86650f94f144084c0007ceb15c7 to your computer and use it in GitHub Desktop.
PostgreSQL 12 -> 13 upgrade check script
#!/bin/bash
function -h {
cat <<USAGE
USAGE: Check postgresql database for pg_pltemplate modifcations. Sanity checks before upgrading 12 -> 13
-p / --port PostgreSQL port
-v / --verbose debugging output
./${0##*/}
USAGE
}; function --help { -h ;}
function msg { out "$*" >&1 ;}
function out { printf '%s\n' "$*" ;}
function err { local x=$? ; msg "$*" ; return $(( $x == 0 ? 1 : $x )) ;}
function main {
local verbose=false
local port=5432
while [[ $# -gt 0 ]]
do
case "$1" in # Munging globals, beware
-p|--port) port="$2"; shift 2 ;;
-v|--verbose) verbose=true; shift 1 ;;
*) err 'Argument error. Please see help: -h' ;;
esac
done
local error_found=false
if [[ $verbose == true ]]; then
set -ex
fi
for db in $(psql -tc "SELECT datname FROM pg_database;")
do
if [[ "${db}" != "template0" ]]; then
msg "Checking database ${db}"
local dump=$(pg_dump --port ${port} --schema-only --quote-all-identifiers ${db} | grep pg_pltemplate)
if [ ! -z "$dump" ]; then
msg "${dump}"
err "ERROR: ${db} contains pg_pltemplate modifications. pg_upgrade will fail"
msg "HINT: Revert REVOKE ... FROM ... statements using GRANT ... TO ... (and viceversa)"
error_found=true
fi
fi
done
if [ ${error_found} = "true" ]; then
msg "CRITICAL: upgrade to postgresql 13 will fail"
exit 1
else
msg "OK: no pg_pltemplate modifications were found"
exit 0
fi
}
if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${1:-}"
then
case "$1" in
-h|--help) : ;;
*) ;;
esac
"$@"
else
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment