]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/tools/db_versions_check.sh
Stage 2: Staff Client
[working/Evergreen.git] / build / tools / db_versions_check.sh
1 #!/bin/bash
2 #
3 # Author: Joe Atzberger
4
5
6 DB_HOST=$1
7 DB_USER=$2
8 DB_NAME=$3
9
10 function usage() {
11     cat <<END_OF_USAGE
12 usage: $0  db_host  db_user  db_name
13
14 Look for missing or failed DB updates in the update log.
15
16 ALL parameters are required to access the postgres database.
17
18 PARAMETERS:
19   db_host - database host system (e.g. "localhost" or "10.121.99.6")
20   db_user - database username
21   db_name - database name
22     
23 You will be prompted for the postgres password if necessary.
24
25 END_OF_USAGE
26 }
27
28 function die() {
29     echo "ERROR: $1" >&2;
30     exit 1;
31 }
32
33 function usage_die() {
34     exec >&2;
35     echo;
36     echo "ERROR: $1";
37     echo;
38     usage;
39     exit 1;
40 }
41
42 [ -z "$DB_HOST" -o -z "$DB_USER" -o -z "$DB_NAME" ] && usage_die "Need all DB parameters";
43
44 PSQL_ACCESS="-h $DB_HOST -U $DB_USER $DB_NAME";
45
46 declare -a FILES;
47 declare -a MISSING;
48 STEP=0;
49
50 psql -c "SELECT version FROM config.upgrade_log ORDER BY version" -t $PSQL_ACCESS | \
51 while read VERSION; do 
52     [ -z $VERSION ] && break;
53     [    $VERSION ] || break;
54     STEP=$((${STEP}+1));
55     # echo -n "Version: $VERSION  ";
56     FILES[${#FILES[@]}]=$VERSION;      # "push" onto FILES array
57     VERSION=$(echo $VERSION | sed -e 's/^ *0*//');    # This is a separate step so we can check $? above.
58     # echo $VERSION " (" ${#FILES[@]} " / " ${#MISSING[@]} ")";
59     while [[ $STEP -lt $VERSION ]] ; do
60         echo "MISSING:" $(printf "%0.4d" $STEP) "*******";
61         MISSING[${#MISSING[@]}]=$(printf "%0.4d" $STEP);      # "push" onto FILES array
62         STEP=$((${STEP}+1));
63     done;
64     # [ $VERBOSE ] && echo RAW VERSION: $VERSION      # TODO: for verbose mod
65 done;
66 [  $? -gt 0  ] && die "Database access failed or was interrupted.";
67 exit;
68