salix/db/import-changes.sh

104 lines
2.4 KiB
Bash
Raw Normal View History

2019-03-20 11:21:32 +00:00
#!/bin/bash
FORCE=FALSE
usage() {
2019-04-03 08:29:05 +00:00
echo "[ERROR] Usage: $0 [-f] [environment]"
2019-03-20 11:21:32 +00:00
exit 1
}
while getopts ":f" option
do
case $option in
f)
FORCE=TRUE
;;
\?|:)
usage
;;
esac
done
shift $(($OPTIND - 1))
ENV=$1
2019-04-03 09:02:19 +00:00
DIR="$(dirname "${BASH_SOURCE[0]}")"
# Production protection
2019-03-20 11:21:32 +00:00
if [ "$ENV" == "production" ]; then
echo ""
echo " ( ( ) ( ( ) ) "
2019-04-03 08:29:05 +00:00
echo " )\ ))\ ) ( /( )\ ) ( ))\ ) ( /( ( /( "
2019-03-20 11:21:32 +00:00
echo "(()/(()/( )\()|()/( ( )\ ) /(()/( )\()) )\())"
echo " /(_))(_)|(_)\ /(_)) )\ (((_) ( )(_))(_)|(_)\ ((_)\ "
echo "(_))(_)) ((_|_))_ _ ((_))\___(_(_()|_)) ((_) _((_)"
echo "| _ \ _ \ / _ \| \| | | ((/ __|_ _|_ _| / _ \| \| |"
echo "| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |"
echo "|_| |_|_\ \___/|___/ \___/ \___| |_| |___| \___/|_|\_|"
echo ""
if [ "$FORCE" != "TRUE" ]; then
2019-04-03 08:29:05 +00:00
read -p "[INTERACTIVE] Are you sure? (Default: no) [yes|no]: " ANSWER
2019-03-20 11:21:32 +00:00
if [ "$ANSWER" != "yes" ]; then
2019-04-03 08:29:05 +00:00
echo "[INFO] Aborting changes."
2019-03-20 11:21:32 +00:00
exit;
fi
fi
fi
2019-04-03 08:29:05 +00:00
# Configuration file
if [ -z "$ENV" ]; then
INI_FILE="$DIR/config.ini"
else
INI_FILE="$DIR/config.$ENV.ini"
fi
2019-03-20 11:21:32 +00:00
if [ ! -f "$INI_FILE" ]; then
2019-04-03 08:29:05 +00:00
echo "[ERROR] Config file doesn't exists: $INI_FILE"
2019-03-20 11:21:32 +00:00
exit 2
fi
2019-04-03 08:29:05 +00:00
echo "[INFO] Using config file: $INI_FILE"
# Fetching database version
GET_VERSION="SELECT dbVersion FROM util.config LIMIT 1"
DB_VERSION=`echo "$GET_VERSION" | mysql --defaults-file="$INI_FILE" --silent --raw`
echo "[INFO] Current database version: $DB_VERSION"
if [[ ! "$DB_VERSION" =~ ^[0-9]+$ ]]; then
echo "[ERROR] Cannot fetch the database version"
exit 3
fi
# Applying changes
N_CHANGES=0
echo "[INFO] Applying changes."
for VERSION_DIR in "$DIR/changes/"*; do
DIR_VERSION=$(basename $VERSION_DIR)
2019-03-20 11:21:32 +00:00
2019-04-03 08:29:05 +00:00
if [ "$DB_VERSION" -lt "$DIR_VERSION" ]; then
echo "[INFO] Applying version: $DIR_VERSION"
2019-03-20 11:22:34 +00:00
2019-04-03 08:29:05 +00:00
for FILE in "$VERSION_DIR/"*; do
echo "[INFO] -> $(basename "$FILE")"
mysql --defaults-file="$INI_FILE" --comments < $FILE
N_CHANGES=$((N_CHANGES + 1))
done
UPDATE_VERSION="UPDATE util.config SET dbVersion='$DIR_VERSION'"
echo $UPDATE_VERSION | mysql --defaults-file="$INI_FILE"
fi
2019-03-20 11:21:32 +00:00
done
2019-04-03 08:29:05 +00:00
if [ "$N_CHANGES" -eq "0" ]; then
echo "[INFO] No changes to apply."
fi