salix/db/import-changes.sh

120 lines
2.8 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-04-03 10:09:45 +00:00
exit
2019-03-20 11:21:32 +00:00
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 10:09:45 +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
2019-04-03 09:21:54 +00:00
echo "[ERROR] Cannot fetch the database version."
2019-04-03 08:29:05 +00:00
exit 3
fi
# Applying changes
N_CHANGES=0
2019-04-03 11:39:35 +00:00
for DIR_PATH in "$DIR/changes/"*; do
DIR_NAME=$(basename $DIR_PATH)
DIR_VERSION=${DIR_NAME:0:5}
2019-03-20 11:21:32 +00:00
2019-04-03 11:39:35 +00:00
if [[ ! "$DIR_NAME" =~ ^[0-9]{5}(-[a-zA-Z0-9]+)?$ ]]; then
echo "[WARN] Ignoring wrong directory name: $DIR_NAME"
2019-04-03 10:09:45 +00:00
continue
fi
if [ "$DB_VERSION" -ge "$DIR_VERSION" ]; then
2019-04-03 11:39:35 +00:00
echo "[INFO] Ignoring already applied version: $DIR_NAME"
2019-04-03 10:09:45 +00:00
continue
fi
2019-03-20 11:22:34 +00:00
2019-04-03 11:39:35 +00:00
echo "[INFO] Applying version: $DIR_NAME"
2019-04-03 08:29:05 +00:00
2019-04-03 11:39:35 +00:00
for FILE in "$DIR_PATH/"*; do
2019-04-08 07:38:38 +00:00
FILE_NAME=$(basename "$FILE")
2019-06-07 07:18:54 +00:00
if [[ ! "$FILE_NAME" =~ ^[0-9]{2}-[a-zA-Z0-9_]+\.sql$ ]]; then
2019-04-08 07:38:38 +00:00
echo "[WARN] Ignoring wrong file name: $FILE_NAME"
continue
fi
echo "[INFO] -> $FILE_NAME"
2019-04-03 10:09:45 +00:00
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"
2019-03-20 11:21:32 +00:00
done
2019-04-03 08:29:05 +00:00
if [ "$N_CHANGES" -eq "0" ]; then
2019-04-08 07:38:38 +00:00
echo "[INFO] No changes applied."
2019-04-03 11:39:35 +00:00
else
echo "[INFO] Changes applied succesfully."
2019-04-03 08:29:05 +00:00
fi