#!/bin/sh
#
# 

# should be run as user postgres

if [ ! `id -un` = postgres ]
then
  echo please only run as user postgres
  exit 0
fi
 
CURRENT=`psql -t astium -c "select value from astglobals where name = 'dbversion'"`
echo $CURRENT

if [ -z "$CURRENT" ]
then
  logger "ERROR - database not upgraded"
  exit 0
fi

cd /usr/local/astium/setup/db/sql-changes
for i in `ls [0-9]*`
do
  if [ $i -gt $CURRENT ]
  then
    echo "Applying upgrade $i.."
    psql -a astium < $i
    psql astium -c "update astglobals set value = '$i' where name = 'dbversion'"  
  fi
done 2>&1 | tee /tmp/apply-changes.out

