db-update.sh 2.8 KB
#!/bin/bash

# Pour Mac OS recent (>=10.10, Yosemite), la syntaxe du SED est differente
# Il faut donc exécuter ce script de la manière suivante :
# ./macos-db-update.sh <ce_script.sh> db-update-AAAA-MM-JJ.sql

# Mode DEBUG (=1 pour activer)
DEBUG=0
#DEBUG=1

# Mac ou Linux ?
# Mac
#MACOS=1
# Linux
MACOS=0

if [ ! -f ../../config/app.php ] ; then
	echo "Vous devez executer ce script depuis le dossier database/update/"
	exit 1
fi

# Ce script attend un argument $1 qui est le nom du script sql à exécuter
# Exit if no argument
[[ $1 == '' ]] && exit 1
sqlscript=$1
#echo $sqlscript


function abort() {
	echo "******************************************************"
	echo "!!! Script $0 aborté à cause d'une erreur d'exécution !!!"	
	echo "******************************************************"
	exit 1
}


# Exit si ce script n'existe pas
# Juste le nom du script sql, sans le path qui pourrait être devant
sqlscript=$(basename $sqlscript)
if [[ ! -f ./script_sql/$sqlscript ]] ; then
	echo "Le script SQL ./script_sql/$sqlscript n'existe pas" 
	abort
fi


# Nom du script sql, sans l'extension .sql
sqlscript_name=${sqlscript%%.*}
#echo $sqlscript_name


# Get login, pass, dbname, and hostname
username=$(grep "/\*d\*/'username'" ../../config/app.php | cut -d"'" -f4) || abort
password=$(grep "/\*d\*/'password'" ../../config/app.php | cut -d"'" -f4) || abort
database=$(grep "/\*d\*/'database'" ../../config/app.php | cut -d"'" -f4) || abort
host=$(grep "/\*d\*/'host'" ../../config/app.php | cut -d"'" -f4) || abort

if [[ $DEBUG == 1 ]] ; then
	echo "db: $database"
	echo "user: $username"
	echo "pass: $password"
	echo "host: $host"
	exit
fi

#cp -p ./script_sql/db-update-2016-07-01-irap.sql ./script_sql/db-update-2016-07-01-irap-build.sql
#cp -p ./script_sql/$sqlscript.sql ./script_sql/$sqlscript-build.sql || abort
cp -p ./script_sql/$sqlscript_name.sql ./script_sql/$sqlscript_name-build.sql || abort
#ls -l ./script_sql/

# Execute sql update script
if [[ $MACOS == 1 ]] ; then
	# MacOS only :
	sed -e "s/database/$database/" -i '' ./script_sql/$sqlscript_name-build.sql || abort
else
	# Linux :
	sed -e "s/database/$database/" -i ./script_sql/$sqlscript_name-build.sql || abort
fi
mysql --user=$username --password=$password -h $host < ./script_sql/$sqlscript_name-build.sql || abort

# Delete temporary file and cakephp cache (-f avoids warning if no file)
rm -f ./script_sql/$sqlscript_name-build.sql
sudo rm -f ../../tmp/cache/models/*
# Vraiment utile ?
sudo rm -f ../../tmp/cache/persistent/*
#ls -l ./script_sql/

# Faire ca aussi si ca suffit pas...
#sudo chmod -R 777 ../../tmp
#sudo chmod -R 777 ../../vendor
#sudo chmod -R 777 ../../webroot


# PLUGIN update
# Installation plugin cakephp-dompdf
# (on va a la racine du projet)
#cd ../../
#php composer.phar require daoandco/cakephp-dompdf
#bin/cake plugin assets symlink
#cd -