AlternC/install/upgrade_check.sh

94 lines
3.1 KiB
Bash
Raw Permalink Normal View History

2014-07-04 14:04:17 +00:00
#!/bin/bash
# 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() {
2012-11-09 13:43:34 +00:00
echo $1 | sed -e 's/\.[^.]*$//' -e 's/[a-z_]*$//'
}
# find the version from a filename by stripping everything but the extension
get_ext() {
echo $1 | sed 's/^.*\.\([^.]*\)$/\1/'
}
2014-03-27 15:43:36 +00:00
# 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
oldvers="`mysql --defaults-file=/etc/alternc/my.cnf --skip-column-names -e "SELECT value FROM alternc_status WHERE name='alternc_version'" 2>/dev/null||true`"
2014-03-27 15:43:36 +00:00
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`"
2014-03-28 14:06:59 +00:00
# this is a *version number*, not a *last upgrade script* we have this *border case*
if [ "$oldvers" = "3.1" ]
then
2014-07-04 14:04:17 +00:00
oldvers="3.1.0~c.sh"
2014-03-28 14:06:59 +00:00
fi
2014-03-27 15:43:36 +00:00
else
echo "##############################################"
echo "# NO VERSION FOUND TO UPGRADE FROM, ABORTING #"
echo "##############################################"
exit 1
fi
fi
2014-03-27 15:43:36 +00:00
if [ "$oldvers" = '<unknown>' ]
then
# this is not an upgrade
exit 0
fi
2014-03-27 15:43:36 +00:00
# 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';"
. /etc/alternc/local.sh
# the upgrade script we are considering
extensions="*.sql *.sh *.php"
cd /usr/share/alternc/install/upgrades
for file in $( ls $extensions | sort -n ) ; do
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
2014-03-27 15:43:36 +00:00
echo "Running upgrade script $file"
# run the proper program to interpret the upgrade script
case "$ext" in
2014-03-27 15:43:36 +00:00
sql)
2014-03-27 16:08:20 +00:00
( echo "BEGIN;"
2014-03-27 15:43:36 +00:00
cat $file
2014-03-27 16:08:20 +00:00
echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';"
echo "COMMIT;"
) | mysql --defaults-file=/etc/alternc/my.cnf -f
2014-03-27 15:43:36 +00:00
;;
php)
php -q $file
2015-04-29 08:45:40 +00:00
echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';" |
mysql --defaults-file=/etc/alternc/my.cnf -f
2014-03-27 15:43:36 +00:00
;;
sh)
2014-03-27 17:39:28 +00:00
bash $file
2015-04-29 08:45:40 +00:00
echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';" |
mysql --defaults-file=/etc/alternc/my.cnf -f
2014-03-27 15:43:36 +00:00
;;
*)
echo "skipping $file, not recognized !"
;;
esac
fi
fi
done
2014-03-27 15:43:36 +00:00