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,12 +146,39 @@ 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
CHANGES_DIR="$WORKSPACE/changes"
if [ -d "$CHANGES_DIR" ]; then
LAST_APPLIED_VERSION=$DB_VERSION LAST_APPLIED_VERSION=$DB_VERSION
for DIR_PATH in "$PWD/changes/"*; do for DIR_PATH in "$CHANGES_DIR/"*; do
DIR_NAME=$(basename $DIR_PATH) DIR_NAME=$(basename $DIR_PATH)
DIR_VERSION=${DIR_NAME:0:5} DIR_VERSION=${DIR_NAME:0:5}
@ -185,8 +214,9 @@ for DIR_PATH in "$PWD/changes/"*; do
LAST_APPLIED_VERSION=$DIR_VERSION LAST_APPLIED_VERSION=$DIR_VERSION
done done
fi
# Applies routines # Apply routines
applyRoutines() { applyRoutines() {
FILES_CMD=$1 FILES_CMD=$1
@ -252,9 +282,11 @@ applyRoutines() {
done done
} }
echo "[INFO] Applying changed routines."
ROUTINES_CHANGED=0 ROUTINES_CHANGED=0
ROUTINES_DIR="$WORKSPACE/routines"
if [ -d "$ROUTINES_DIR" ]; then
echo "[INFO] Applying changed routines."
PROCS_FILE=.procs-priv.sql PROCS_FILE=.procs-priv.sql
mysqldump \ mysqldump \
@ -271,8 +303,6 @@ else
applyRoutines "git diff --name-only --diff-filter=d $DB_COMMIT -- routines" applyRoutines "git diff --name-only --diff-filter=d $DB_COMMIT -- routines"
fi fi
applyRoutines "git ls-files --others --exclude-standard"
if [ "$ROUTINES_CHANGED" -gt "0" ]; then if [ "$ROUTINES_CHANGED" -gt "0" ]; then
dbExecFromFile "$PROCS_FILE" "mysql" dbExecFromFile "$PROCS_FILE" "mysql"
@ -288,10 +318,11 @@ else
echo "[INFO] -> No routines changed." echo "[INFO] -> No routines changed."
rm "$PROCS_FILE" 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",