Fixes and refactor

This commit is contained in:
Juan Ferrer 2020-11-17 18:15:09 +01:00
parent 2ed8f78d69
commit 24f72db15c
4 changed files with 131 additions and 96 deletions

View File

@ -95,7 +95,7 @@ module.exports = class Docker {
* Does the minium effort to start the database container, if it doesn't * Does the minium effort to start the database container, if it doesn't
* exists calls the 'docker' task, if it is started does nothing. Keep in * exists calls the 'docker' task, if it is started does nothing. Keep in
* mind that when you do not rebuild the docker you may be using an outdated * mind that when you do not rebuild the docker you may be using an outdated
* version of it. See the 'docker' task for more info. * version of it.
*/ */
async start() { async start() {
let state; let state;

View File

@ -35,8 +35,10 @@ if (!command) {
const commandArgs = { const commandArgs = {
push: { push: {
alias: { alias: {
env: 'e',
force: 'f', force: 'f',
user: 'u' user: 'u',
applyUncommited: 'a'
}, },
default: { default: {
force: false, force: false,
@ -127,6 +129,7 @@ class MyVC {
let args = []; let args = [];
if (opts.force) args.push('-f'); if (opts.force) args.push('-f');
if (opts.user) args.push('-u'); if (opts.user) args.push('-u');
if (opts.applyUncommited) args.push('-a');
if (opts.env) args = args.concat(['-e', opts.env]); if (opts.env) args = args.concat(['-e', opts.env]);
await dockerRun('myvc-push.sh', await dockerRun('myvc-push.sh',

View File

@ -2,13 +2,15 @@
FORCE=FALSE FORCE=FALSE
IS_USER=FALSE IS_USER=FALSE
APPLY_UNCOMMITED=FALSE
WORKSPACE="$PWD"
usage() { usage() {
echo "[ERROR] Usage: $0 [-f] [-u] [-e environment]" echo "[ERROR] Usage: $0 [-f] [-u] [-a] [-e environment]"
exit 1 exit 1
} }
while getopts ":fue:" option while getopts ":fuae:" option
do do
case $option in case $option in
f) f)
@ -20,6 +22,9 @@ do
e) e)
ENV="$OPTARG" ENV="$OPTARG"
;; ;;
a)
APPLY_UNCOMMITED=TRUE
;;
\?|:) \?|:)
usage usage
;; ;;
@ -28,6 +33,8 @@ done
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
# Load configuration
CONFIG_FILE="myvc.config.json" CONFIG_FILE="myvc.config.json"
if [ ! -f "$CONFIG_FILE" ]; then if [ ! -f "$CONFIG_FILE" ]; then
@ -38,36 +45,12 @@ fi
DIR="$(dirname "${BASH_SOURCE[0]}")" DIR="$(dirname "${BASH_SOURCE[0]}")"
CODE=$(jq -r ".code" "$CONFIG_FILE") CODE=$(jq -r ".code" "$CONFIG_FILE")
# Production protection # Load database configuration
if [ "$ENV" == "production" ]; then
echo ""
echo " ( ( ) ( ( ) ) "
echo " )\ ))\ ) ( /( )\ ) ( ))\ ) ( /( ( /( "
echo "(()/(()/( )\()|()/( ( )\ ) /(()/( )\()) )\())"
echo " /(_))(_)|(_)\ /(_)) )\ (((_) ( )(_))(_)|(_)\ ((_)\ "
echo "(_))(_)) ((_|_))_ _ ((_))\___(_(_()|__)) ((_) _((_)"
echo "| _ \ _ \ / _ \| \| | | ((/ __|_ _|_ _| / _ \| \| |"
echo "| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |"
echo "|_| |_|_\ \___/|___/ \___/ \___| |_| |___| \___/|_|\_|"
echo ""
if [ "$FORCE" != "TRUE" ]; then
read -p "[INTERACTIVE] Are you sure? (Default: no) [yes|no]: " ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "[INFO] Aborting changes."
exit
fi
fi
fi
# Configuration file
if [ -z "$ENV" ]; then if [ -z "$ENV" ]; then
INI_FILE="$PWD/db.ini" INI_FILE="$WORKSPACE/db.ini"
else else
INI_FILE="$PWD/db.$ENV.ini" INI_FILE="$WORKSPACE/db.$ENV.ini"
fi fi
if [ ! -f "$INI_FILE" ]; then if [ ! -f "$INI_FILE" ]; then
@ -83,6 +66,28 @@ if [ "$?" -ne "0" ]; then
exit 3 exit 3
fi fi
# Fetch git information
git diff-index --quiet --cached HEAD --
STAGED=$?
git diff-files --quiet
CHANGED=$?
UNTRACKED=`git ls-files --others --exclude-standard`
if [ $STAGED ] || [ $CHANGED ] || [ -n "$UNTRACKED" ]; then
if [ "$APPLY_UNCOMMITED" == "TRUE" ]; then
echo "[WARN] You are applying uncommited changes."
else
echo "[ERROR] You have uncommited changes, commit them before pushing or use -a option."
exit 2
fi
fi
COMMIT_SHA=$(git rev-parse HEAD)
echo "[INFO] HEAD: $COMMIT_SHA"
# Query functions # Query functions
dbQuery() { dbQuery() {
@ -99,10 +104,7 @@ dbExecFromFile() {
mysql --defaults-file="$INI_FILE" --default-character-set=utf8 --comments "$SCHEMA" < $FILE_PATH mysql --defaults-file="$INI_FILE" --default-character-set=utf8 --comments "$SCHEMA" < $FILE_PATH
} }
# Fetches database version # Fetch database version
COMMIT_SHA=$(git rev-parse HEAD)
echo "[INFO] Commit: $COMMIT_SHA"
dbQuery "SELECT number, gitCommit FROM util.version WHERE code = '$CODE'" dbQuery "SELECT number, gitCommit FROM util.version WHERE code = '$CODE'"
RETVAL=($RETVAL) RETVAL=($RETVAL)
@ -144,49 +146,77 @@ if [ "$IS_USER" == "TRUE" ]; then
fi fi
fi fi
# Applies changes # Production protection
if [ "$ENV" == "production" ]; then
echo ""
echo " ( ( ) ( ( ) ) "
echo " )\ ))\ ) ( /( )\ ) ( ))\ ) ( /( ( /( "
echo "(()/(()/( )\()|()/( ( )\ ) /(()/( )\()) )\())"
echo " /(_))(_)|(_)\ /(_)) )\ (((_) ( )(_))(_)|(_)\ ((_)\ "
echo "(_))(_)) ((_|_))_ _ ((_))\___(_(_()|__)) ((_) _((_)"
echo "| _ \ _ \ / _ \| \| | | ((/ __|_ _|_ _| / _ \| \| |"
echo "| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |"
echo "|_| |_|_\ \___/|___/ \___/ \___| |_| |___| \___/|_|\_|"
echo ""
if [ "$FORCE" != "TRUE" ]; then
read -p "[INTERACTIVE] Are you sure? (Default: no) [yes|no]: " ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "[INFO] Aborting changes."
exit
fi
fi
fi
# Apply changes
N_CHANGES=0 N_CHANGES=0
LAST_APPLIED_VERSION=$DB_VERSION CHANGES_DIR="$WORKSPACE/changes"
for DIR_PATH in "$PWD/changes/"*; do if [ -d "$CHANGES_DIR" ]; then
DIR_NAME=$(basename $DIR_PATH) LAST_APPLIED_VERSION=$DB_VERSION
DIR_VERSION=${DIR_NAME:0:5}
if [ "$DIR_NAME" == "README.md" ]; then for DIR_PATH in "$CHANGES_DIR/"*; do
continue DIR_NAME=$(basename $DIR_PATH)
fi DIR_VERSION=${DIR_NAME:0:5}
if [[ ! "$DIR_NAME" =~ ^[0-9]{5}(-[a-zA-Z0-9]+)?$ ]]; then
echo "[WARN] Ignoring wrong directory name: $DIR_NAME"
continue
fi
if [ "$DB_VERSION" -ge "$DIR_VERSION" ]; then
echo "[INFO] Ignoring already applied version: $DIR_NAME"
continue
fi
echo "[INFO] Applying version: $DIR_NAME" if [ "$DIR_NAME" == "README.md" ]; then
for FILE in "$DIR_PATH/"*; do
FILE_NAME=$(basename "$FILE")
if [ "$FILE_NAME" == "*" ]; then
continue continue
fi fi
if [[ ! "$FILE_NAME" =~ ^[0-9]{2}-[a-zA-Z0-9_]+\.sql$ ]]; then if [[ ! "$DIR_NAME" =~ ^[0-9]{5}(-[a-zA-Z0-9]+)?$ ]]; then
echo "[WARN] Ignoring wrong file name: $FILE_NAME" echo "[WARN] Ignoring wrong directory name: $DIR_NAME"
continue
fi
if [ "$DB_VERSION" -ge "$DIR_VERSION" ]; then
echo "[INFO] Ignoring already applied version: $DIR_NAME"
continue continue
fi fi
echo "[INFO] -> $FILE_NAME" echo "[INFO] Applying version: $DIR_NAME"
dbExecFromFile "$FILE"
N_CHANGES=$((N_CHANGES + 1)) for FILE in "$DIR_PATH/"*; do
FILE_NAME=$(basename "$FILE")
if [ "$FILE_NAME" == "*" ]; then
continue
fi
if [[ ! "$FILE_NAME" =~ ^[0-9]{2}-[a-zA-Z0-9_]+\.sql$ ]]; then
echo "[WARN] Ignoring wrong file name: $FILE_NAME"
continue
fi
echo "[INFO] -> $FILE_NAME"
dbExecFromFile "$FILE"
N_CHANGES=$((N_CHANGES + 1))
done
LAST_APPLIED_VERSION=$DIR_VERSION
done done
fi
LAST_APPLIED_VERSION=$DIR_VERSION # Apply routines
done
# Applies routines
applyRoutines() { applyRoutines() {
FILES_CMD=$1 FILES_CMD=$1
@ -252,46 +282,47 @@ applyRoutines() {
done done
} }
echo "[INFO] Applying changed routines."
ROUTINES_CHANGED=0 ROUTINES_CHANGED=0
ROUTINES_DIR="$WORKSPACE/routines"
PROCS_FILE=.procs-priv.sql if [ -d "$ROUTINES_DIR" ]; then
mysqldump \ echo "[INFO] Applying changed routines."
--defaults-file="$INI_FILE" \
--no-create-info \
--skip-triggers \
--insert-ignore \
mysql procs_priv > "$PROCS_FILE"
if [[ -z "$DB_COMMIT" ]]; then PROCS_FILE=.procs-priv.sql
applyRoutines "find routines -type f" mysqldump \
else --defaults-file="$INI_FILE" \
applyRoutines "git diff --name-only --diff-filter=D $DB_COMMIT -- routines" --no-create-info \
applyRoutines "git diff --name-only --diff-filter=d $DB_COMMIT -- routines" --skip-triggers \
fi --insert-ignore \
mysql procs_priv > "$PROCS_FILE"
applyRoutines "git ls-files --others --exclude-standard" if [[ -z "$DB_COMMIT" ]]; then
applyRoutines "find routines -type f"
if [ "$ROUTINES_CHANGED" -gt "0" ]; then
dbExecFromFile "$PROCS_FILE" "mysql"
if [ "$?" -eq "0" ]; then
dbExec "FLUSH PRIVILEGES"
rm "$PROCS_FILE"
else else
echo "[WARN] An error ocurred when restoring routine privileges, backup saved at $PROCS_FILE" applyRoutines "git diff --name-only --diff-filter=D $DB_COMMIT -- routines"
applyRoutines "git diff --name-only --diff-filter=d $DB_COMMIT -- routines"
fi fi
echo "[INFO] -> $ROUTINES_CHANGED routines have changed." if [ "$ROUTINES_CHANGED" -gt "0" ]; then
else dbExecFromFile "$PROCS_FILE" "mysql"
echo "[INFO] -> No routines changed."
rm "$PROCS_FILE" if [ "$?" -eq "0" ]; then
dbExec "FLUSH PRIVILEGES"
rm "$PROCS_FILE"
else
echo "[WARN] An error ocurred when restoring routine privileges, backup saved at $PROCS_FILE"
fi
echo "[INFO] -> $ROUTINES_CHANGED routines have changed."
else
echo "[INFO] -> No routines changed."
rm "$PROCS_FILE"
fi
fi fi
N_CHANGES=$((N_CHANGES + ROUTINES_CHANGED)) N_CHANGES=$((N_CHANGES + ROUTINES_CHANGED))
# Displaying summary # Display summary
if [ "$N_CHANGES" -gt "0" ]; then if [ "$N_CHANGES" -gt "0" ]; then
if [ "$IS_USER" == "TRUE" ]; then if [ "$IS_USER" == "TRUE" ]; then

View File

@ -1,6 +1,6 @@
{ {
"name": "myvc", "name": "myvc",
"version": "1.0.13", "version": "1.0.14",
"author": "Verdnatura Levante SL", "author": "Verdnatura Levante SL",
"description": "MySQL Version Control", "description": "MySQL Version Control",
"license": "GPL-3.0", "license": "GPL-3.0",
@ -31,6 +31,7 @@
"keywords": [ "keywords": [
"mysql", "mysql",
"mariadb", "mariadb",
"migrations",
"git", "git",
"vcs", "vcs",
"database", "database",