upgrade_check.sh 2.64 KB
Newer Older
Alan Garcia's avatar
Alan Garcia committed
1
#!/bin/bash -e
2 3 4 5 6 7 8 9 10 11

# this script will look for upgrade scripts in
# /usr/share/alternc/install/upgrades and execute them based on the
# extension
#
# an upgrade file is considered only if its basename is a version
# number greater than the $oldvers argument

# remove version from filename by stripping the extension
strip_ext() {
12
	echo $1 | sed -e 's/\.[^.]*$//' -e 's/[a-z_]*$//'
13 14 15 16 17 18 19 20
}

# find the version from a filename by stripping everything but the extension
get_ext() {
	echo $1 | sed 's/^.*\.\([^.]*\)$/\1/'
}


21 22 23
# Reading the current version in the DB.
# If the DB exist but the alternc_status table doesn't, we will initialize it below
# In that case we search where we upgrade from in /var/lib/alternc/backups/lastversion from debian.postinstall script
Benjamin Sonntag's avatar
Benjamin Sonntag committed
24
oldvers="`mysql --defaults-file=/etc/alternc/my.cnf --skip-column-names -e "SELECT value FROM alternc_status WHERE name='alternc_version'"||true`"
25 26 27 28 29 30 31 32 33 34 35 36
if [ -z "$oldvers" ]
then
    # no version number, we check from /var/lib/alternc
    if [ -f "/var/lib/alternc/backups/lastversion" ]
    then
	oldvers="`cat /var/lib/alternc/backups/lastversion`"
    else
	echo "##############################################"
	echo "# NO VERSION FOUND TO UPGRADE FROM, ABORTING #"
	echo "##############################################"
	exit 1
    fi
37 38
fi

39 40 41 42
if [ "$oldvers" = '<unknown>' ]
then
	# this is not an upgrade
	exit 0
43 44
fi

45 46 47
# Thanks to that, we handle alternc older than 3.1.0~b.php
mysql --defaults-file=/etc/alternc/my.cnf -e "CREATE TABLE IF NOT EXISTS alternc_status (name VARCHAR(48) NOT NULL DEFAULT '',value LONGTEXT NOT NULL,PRIMARY KEY (name),KEY name (name) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;"
mysql --defaults-file=/etc/alternc/my.cnf -e "INSERT IGNORE INTO alternc_status SET name='alternc_version',value='$oldvers';"
48

49 50 51 52 53
. /etc/alternc/local.sh

# the upgrade script we are considering
extensions="*.sql *.sh *.php"
cd /usr/share/alternc/install/upgrades
54
for file in $( ls $extensions | sort -n ) ; do
55 56 57 58 59 60
	if [ -r $file ]; then
                # the version in the filename
		upvers=`strip_ext $file`
                # the extension
		ext=`get_ext $file`
		if dpkg --compare-versions $upvers gt $oldvers; then
61
		  echo "Running upgrade script $file"
62 63
                  # run the proper program to interpret the upgrade script
		  case "$ext" in
64
		      sql)
Benjamin Sonntag's avatar
Benjamin Sonntag committed
65
			  ( echo "BEGIN;"
66
			      cat $file
Benjamin Sonntag's avatar
Benjamin Sonntag committed
67 68
			      echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';"
			      echo "COMMIT;"
69 70 71 72 73 74
			  ) | mysql --defaults-file=/etc/alternc/my.cnf
			  ;;
		      php)
		  	  php -q $file
			  ;;
		      sh)
Alan Garcia's avatar
Alan Garcia committed
75
		  	  bash $file
76 77 78 79
			  ;;
                      *)
			  echo "skipping $file, not recognized !"
			  ;;
80 81 82 83
		  esac
		fi
	fi
done
84