3 # Based on a script by Bill Erickson.
7 # ~ single-step mode that calls psql -f once per file
8 # (but also prompts for password once per file).
17 usage: $0 db_host db_user db_name
19 Automtically update the DB with all numbered updates beyond the last installed one.
21 ALL parameters are required to access the postgres database.
24 db_host - database host system (e.g. "localhost" or "10.121.99.6")
25 db_user - database username
26 db_name - database name
28 Run from your source repository root or Open-ILS/src/sql/Pg directory.
30 You will be prompted for the postgres password if necessary.
40 function usage_die() {
50 #TODO: add a test and verbose mode that use this.
51 echo "Updating database $DB_NAME on $DB_HOST as user $DB_USER";
54 [ -z "$DB_HOST" -o -z "$DB_USER" -o -z "$DB_NAME" ] && usage_die "Need all DB parameters";
56 PSQL_ACCESS="-h $DB_HOST -U $DB_USER $DB_NAME";
58 VERSION=$(psql -c "select max(version) from config.upgrade_log" -t $PSQL_ACCESS);
59 [ $? -gt 0 ] && die "Database access failed.";
60 # [ $VERBOSE ] && echo RAW VERSION: $VERSION # TODO: for verbose mode
61 VERSION=$(echo $VERSION | sed -e 's/^ *0*//'); # This is a separate step so we can check $? above.
62 [ -z "$VERSION" ] && usage_die "config.upgrade_log missing ANY installed version data!";
63 echo "* Last installed version -> $VERSION";
65 if [ -d ./Open-ILS/src/sql/Pg ] ; then
66 cd ./Open-ILS/src/sql/Pg ;
68 [ -d ./upgrade ] || usage_die "No ./upgrade directory found. Please run from Open-ILS/src/sql/Pg";
70 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.
71 echo "* Last upgrade file found -> $MAX";
72 MAX=$(echo $MAX | sed -e 's/^ *0*//'); # remove leading zeroes
77 VERSION=$(($VERSION + 1));
78 [ $VERSION -gt $MAX ] && break;
79 PREFIX=$(printf "%0.4d" $VERSION);
80 FILE=$(ls upgrade/$PREFIX* 2>/dev/null);
81 if [ -f "$FILE" ] ; then
82 # Note: we only report skipped files once we find the next one.
83 # Otherwise, we'd report everything from $VERSION+1 to $MAX
84 for skip in ${SKIPPED[@]} ; do
85 echo "* WARNING: Upgrade $skip NOT FOUND. Skipping it.";
87 SKIPPED=(); # After we reported them, reset array.
88 FILES[${#FILES[@]}]=$FILE; # "push" onto FILES array
89 # echo "* Pending $FILE";
91 SKIPPED[${#SKIPPED[@]}]=$PREFIX; # "push" onto SKIPPED array
97 if [ $COUNT -gt 0 ] ; then
98 echo "* $COUNT update scripts to apply."
99 exec 3>&1; # our copy of STDOUT
100 for (( i=0; i<$COUNT; i++ )) ; do
101 echo "* Applying ${FILES[$i]}" >&3; # to the main script STDOUT
102 cat ${FILES[$i]}; # to the psql pipe
103 done | psql $PSQL_ACCESS ;
105 echo "* Nothing to update";