Basic check for missing DB upgrades
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 4 Jun 2010 20:53:22 +0000 (20:53 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 4 Jun 2010 20:53:22 +0000 (20:53 +0000)
Run this after a update_db.sh failure to see what failed.
Or run in before update_db.sh to make sure you are ready to apply
new updates.

git-svn-id: svn://svn.open-ils.org/ILS/trunk@16596 dcc99617-32d9-48b4-a31d-7c20da2025e4

build/tools/db_versions_check.sh [new file with mode: 0755]

diff --git a/build/tools/db_versions_check.sh b/build/tools/db_versions_check.sh
new file mode 100755 (executable)
index 0000000..f03ad74
--- /dev/null
@@ -0,0 +1,68 @@
+#!/bin/bash
+#
+# Author: Joe Atzberger
+# 
+
+DB_HOST=$1
+DB_USER=$2
+DB_NAME=$3
+
+function usage() {
+    cat <<END_OF_USAGE
+usage: $0  db_host  db_user  db_name
+
+Look for missing or failed DB updates in the update log.
+
+ALL parameters are required to access the postgres database.
+
+PARAMETERS:
+  db_host - database host system (e.g. "localhost" or "10.121.99.6")
+  db_user - database username
+  db_name - database name
+    
+You will be prompted for the postgres password if necessary.
+
+END_OF_USAGE
+}
+
+function die() {
+    echo "ERROR: $1" >&2;
+    exit 1;
+}
+
+function usage_die() {
+    exec >&2;
+    echo;
+    echo "ERROR: $1";
+    echo;
+    usage;
+    exit 1;
+}
+
+[ -z "$DB_HOST" -o -z "$DB_USER" -o -z "$DB_NAME" ] && usage_die "Need all DB parameters";
+
+PSQL_ACCESS="-h $DB_HOST -U $DB_USER $DB_NAME";
+
+declare -a FILES;
+declare -a MISSING;
+STEP=0;
+
+psql -c "SELECT version FROM config.upgrade_log ORDER BY version" -t $PSQL_ACCESS | \
+while read VERSION; do 
+    [ -z $VERSION ] && break;
+    [    $VERSION ] || break;
+    STEP=$((${STEP}+1));
+    # echo -n "Version: $VERSION  ";
+    FILES[${#FILES[@]}]=$VERSION;      # "push" onto FILES array
+    VERSION=$(echo $VERSION | sed -e 's/^ *0*//');    # This is a separate step so we can check $? above.
+    # echo $VERSION " (" ${#FILES[@]} " / " ${#MISSING[@]} ")";
+    while [[ $STEP -lt $VERSION ]] ; do
+        echo "MISSING:" $(printf "%0.4d" $STEP) "*******";
+        MISSING[${#MISSING[@]}]=$(printf "%0.4d" $STEP);      # "push" onto FILES array
+        STEP=$((${STEP}+1));
+    done;
+    # [ $VERBOSE ] && echo RAW VERSION: $VERSION      # TODO: for verbose mod
+done;
+[  $? -gt 0  ] && die "Database access failed or was interrupted.";
+exit;
+