]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/tools/update_db.sh
LP#1635737 Due date DST noncat thinko fix
[working/Evergreen.git] / build / tools / update_db.sh
1 #!/bin/bash
2 #
3 # Based on a script by Bill Erickson.
4 #
5 # TODO:
6 #   ADD OPTIONS:
7 #     ~ single-step mode that calls psql -f once per file
8 #          (but also prompts for password once per file).
9 #     ~ help/usage option
10
11 DB_HOST=$1
12 DB_USER=$2
13 DB_NAME=$3
14
15 function usage() {
16     cat <<END_OF_USAGE
17 usage: $0  db_host  db_user  db_name
18
19 Automtically update the DB with all numbered updates beyond the last installed one.
20
21 ALL parameters are required to access the postgres database.
22
23 PARAMETERS:
24   db_host - database host system (e.g. "localhost" or "10.121.99.6")
25   db_user - database username
26   db_name - database name
27     
28 Run from your source repository root or Open-ILS/src/sql/Pg directory.
29
30 You will be prompted for the postgres password if necessary.
31
32 END_OF_USAGE
33 }
34
35 function die() {
36     echo "ERROR: $1" >&2;
37     exit 1;
38 }
39
40 function usage_die() {
41     exec >&2;
42     echo;
43     echo "ERROR: $1";
44     echo;
45     usage;
46     exit 1;
47 }
48
49 function feedback() {
50     #TODO: add a test and verbose mode that use this.
51     echo "Updating database $DB_NAME on $DB_HOST as user $DB_USER";
52 }
53
54 [ -z "$DB_HOST" -o -z "$DB_USER" -o -z "$DB_NAME" ] && usage_die "Need all DB parameters";
55
56 PSQL_ACCESS="-h $DB_HOST -U $DB_USER $DB_NAME";
57
58 # Find the current version of Evergreen, which is the installed version to which the upgrade script is being applied
59 EGVERSION=$(eg_config --version|cut -f2 -d' ');
60 [  $? -gt 0  ] && die "Could not find eg_config, please make sure it is in your path.";
61 [  -z "$EGVERSION"  ] && die "Could not determine Evergreen version from eg_config.";
62
63 # Need to avoid versions like '1.6.0.4' from throwing off the upgrade
64 VERSION=$(psql -c "SELECT MAX(version) FROM config.upgrade_log WHERE version ~ E'^\\\\d+$'" -t $PSQL_ACCESS);
65 [  $? -gt 0  ] && die "Database access failed.";
66 # [ $VERBOSE ] && echo RAW VERSION: $VERSION     # TODO: for verbose mode
67 VERSION=$(echo $VERSION | sed -e 's/^ *0*//');    # This is a separate step so we can check $? above.
68 [ -z "$VERSION" ] && usage_die "config.upgrade_log missing ANY installed version data!";
69 echo "* Last installed version  ->  $VERSION";
70
71 if [ -d ./Open-ILS/src/sql/Pg ] ; then
72     cd ./Open-ILS/src/sql/Pg ;
73 fi
74 [ -d ./upgrade ] || usage_die "No ./upgrade directory found.  Please run from Open-ILS/src/sql/Pg";
75
76 MAX=$(ls upgrade/[0-9][0-9][0-9][0-9]* 2>/dev/null | tail -1 | cut -c9-12 );   # could take an optional arg to set this, if we wanted.
77 echo "* Last upgrade file found -> $MAX";
78 MAX=$(echo $MAX | sed -e 's/^ *0*//');      # remove leading zeroes
79
80 declare -a FILES;
81 declare -a SKIPPED;
82 while true; do
83     VERSION=$(($VERSION + 1));
84     [ $VERSION -gt $MAX ] && break;
85     PREFIX=$(printf "%0.4d" $VERSION);
86     FILE=$(ls upgrade/$PREFIX* 2>/dev/null);
87     if [ -f "$FILE" ] ; then
88         # Note: we only report skipped files once we find the next one.  
89         # Otherwise, we'd report everything from $VERSION+1 to $MAX
90         for skip in ${SKIPPED[@]} ; do
91             echo "* WARNING: Upgrade $skip NOT FOUND.  Skipping it."; 
92         done
93         SKIPPED=();                     # After we reported them, reset array.
94         FILES[${#FILES[@]}]=$FILE;      # "push" onto FILES array
95         # echo "* Pending $FILE";
96     else
97         SKIPPED[${#SKIPPED[@]}]=$PREFIX; # "push" onto SKIPPED array
98     fi
99 done;
100
101 COUNT=${#FILES[@]};
102
103 if [ $COUNT -gt 0 ] ; then
104     echo "* $COUNT update scripts to apply."
105     exec 3>&1;  # our copy of STDOUT
106     for (( i=0; i<$COUNT; i++ )) ; do
107         echo "* Applying ${FILES[$i]}" >&3;   # to the main script STDOUT
108         cat ${FILES[$i]};                     # to the psql pipe
109     done | psql --set=eg_version="'$EGVERSION'" $PSQL_ACCESS ;
110 else
111     echo "* Nothing to update";
112 fi