From caef918c77644ebb76ca3f8f23ea60b41682ad8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org>
Date: Wed, 25 Mar 2020 18:09:57 +0100
Subject: [PATCH] MAINT: trying to get it work

---
 make_db               | 30 ++++++++++++-------------
 utils/helpers.sh      | 52 +++++++++++++++++++++++++++++++++----------
 utils/patch_header.sh |  3 +--
 3 files changed, 55 insertions(+), 30 deletions(-)

diff --git a/make_db b/make_db
index 0ff4537..daa82c7 100755
--- a/make_db
+++ b/make_db
@@ -32,7 +32,6 @@ fi
 source $UTILSPATH/load_settings.sh
 source $UTILSPATH/helpers.sh
 
-echo "no1 $MYSQL_HOST"
 INSTALL_SQL_FILE="db_2_0.sql"
 
 set -e
@@ -63,7 +62,7 @@ function _execute_tests () {
     for tfile in $TESTS ; do
         echo "Running $tfile"
         echo "----- $tfile -----" >> .TEST_RESULTS
-		cat $tfile | $MYSQL_CMD $(get_con_args_nodb) --disable-pager --batch --raw --skip-column-names --unbuffered >> .TEST_RESULTS
+        cat $tfile | $MYSQL_CMD $(get_db_args_nodb) --disable-pager --batch --raw --skip-column-names --unbuffered >> .TEST_RESULTS
     done;
 
     popd
@@ -84,9 +83,9 @@ function _install_unit_test_database () {
         fi
     fi
 
-    sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_con_args_nodb)
+    sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
 
-    # crate test user
+    # create test user
     grant
 
     # update to latest
@@ -108,7 +107,7 @@ function _setup_mytap() {
     pushd libs > /dev/null
     unzip -u mytap*.zip > /dev/null
     pushd mytap*/ > /dev/null
-    $MYSQL_CMD $(get_con_args_nodb) < mytap.sql > /dev/null || exit 1
+    $MYSQL_CMD $(get_mysql_args_nodb) < mytap.sql > /dev/null || exit 1
     popd > /dev/null
     rm -r mytap*/
     popd > /dev/null
@@ -143,18 +142,17 @@ Call 'make drop-$DATABASE_NAME' to delete that database or reconfigure with
         exit 0
     fi
 
-	  sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_con_args_nodb)
+	  sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
 }
 
 # Inserts the dump (arg 1) into the database
 function restore_db() {
     SQL_FILE="$1"
-    $MYSQL_CMD $(get_con_args) < "$SQL_FILE"
+    $MYSQL_CMD $(get_mysql_args) < "$SQL_FILE"
 }
 
 function test-connection() {
-	echo $SQL
-	$MYSQL_CMD $(get_con_args_nodb) -e "select 0;"
+    $MYSQL_CMD $(get_mysql_args_nodb) -e "select 0;"
 }
 
 # Creates a user and grants it sufficient rights.
@@ -178,7 +176,7 @@ function grant() {
     if [[ "$1" == "--strict" ]] ; then
         for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
             CMD="SELECT COUNT(*) FROM mysql.user WHERE user='${DATABASE_USER}' AND host='${host}';"
-		        [[ $($MYSQL_CMD $(get_con_args_nodb) -s -N -e "$CMD") == 0 ]] || {
+		        [[ $($MYSQL_CMD $(get_mysql_args_nodb) -s -N -e "$CMD") == 0 ]] || {
                 echo "The user '${DATABASE_USER}@${host}' is already in the database."
                 echo "Please use another user or delete it, e.g. with"
                 echo "'mysql -u ${MYSQL_USER} -p -e \"DROP USER ${DATABASE_USER}@${host};\"'"
@@ -189,7 +187,7 @@ function grant() {
 
     for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
         echo "Granting admin privileges to '$DATABASE_USER'@'$host'"
-        $MYSQL_CMD $(get_con_args_nodb) <<EOF
+        $MYSQL_CMD $(get_mysql_args_nodb) <<EOF
 CREATE USER IF NOT EXISTS
     '$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW';
 GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host';
@@ -203,17 +201,17 @@ EOF
 # The first argument to this function is the database that shal be dropped.
 function drop() {
     DROPDB="$1"
-    for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
-        $MYSQL_CMD $(get_con_args_nodb) -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
-    done
-    "$MYSQLADMIN_CMD" $(get_con_args_nodb) -f drop "$DROPDB"
+    #for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
+        #$MYSQL_CMD $(get_mysql_args_nodb) -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
+    #done
+    "$MYSQLADMIN_CMD" $(get_mysql_args_nodb) -f drop "$DROPDB"
 }
 
 
 # Returns 0 or non-zero, depending on whether the database exists already.
 # Optional parameter: [DATABASE_NAME], else $DATABASE_NAME is used.
 function _db_exists() {
-    $MYSQL_CMD $(get_con_args_nodb) -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
+    $MYSQL_CMD $(get_mysql_args_nodb) -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
         && return 0 || return 1
 }
 
diff --git a/utils/helpers.sh b/utils/helpers.sh
index cbe6d23..af0ff03 100644
--- a/utils/helpers.sh
+++ b/utils/helpers.sh
@@ -19,14 +19,14 @@
 # along with this program. If not, see <https://www.gnu.org/licenses/>.
 
 function dump_table {
-	$MYSQLDUMP_CMD $(get_con_args_nodb) $DATABASE_NAME $1 \
+	$MYSQLDUMP_CMD $(get_db_args_nodb) $DATABASE_NAME $1 \
                     > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
 }
 
 function mysql_execute {
+	#TODO is it meaningful that here always the database user is used???
 	set +e
-	echo $MYSQL_CMD $(get_con_args) -e "$1"
-	$MYSQL_CMD $(get_con_args) -e "$1"
+	$MYSQL_CMD $(get_db_args) -e "$1"
 	ret=${PIPESTATUS[0]}
 	if [ "$ret" -ne 0 ]; then
 		failure "MYSQL ERROR"
@@ -34,20 +34,47 @@ function mysql_execute {
 	set -e
 }
 
-function get_con_args {
-	echo "$(get_con_args_nodb) --database=$DATABASE_NAME"
+function get_mysql_args {
+#	if [ "$DATABASE_NAME" = "caosdb" ];then 
+#		echo "WRONGDB"
+#		exit 1
+#	fi
+	echo "$(get_mysql_args_nodb) --database=$DATABASE_NAME"
 }
 
-function get_con_args_nodb {
+function get_db_args {
+#	if [ "$DATABASE_NAME" = "caosdb" ];then 
+#		echo "WRONGDB"
+#		exit 1
+#	fi
+	echo "$(get_db_args_nodb) --database=$DATABASE_NAME"
+}
+
+function get_db_args_nodb {
+	if [ "$DATABASE_USER" ]; then
+		mysql_con_arguments="--user=$DATABASE_USER"
+	fi
+	if [ "$DATABASE_USER_PW" ]; then
+		mysql_con_arguments="$mysql_con_arguments --password=$DATABASE_USER_PW"
+	fi
+	if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then
+		mysql_con_arguments="$mysql_con_arguments --host=$MYSQL_HOST"
+		if [ "$MYSQL_PORT" ]; then
+			mysql_con_arguments="$mysql_con_arguments --port=$MYSQL_PORT"
+		fi
+	fi
+	echo $mysql_con_arguments
+}
+
+function get_mysql_args_nodb {
 	if [ "$LOGIN_PATH" ]; then
 		mysql_con_arguments="--login-path=$LOGIN_PATH"
-		mysql_con_arguments="$mysql_con_arguments"
 	else
-		if [ "$DATABASE_USER" ]; then
-			mysql_con_arguments="--user=$DATABASE_USER"
+		if [ "$MYSQL_USER" ]; then
+			mysql_con_arguments="--user=$MYSQL_USER"
 		fi
-		if [ "$DATABASE_USER_PW" ]; then
-			mysql_con_arguments="$mysql_con_arguments --password=$DATABASE_USER_PW"
+		if [ "$MYSQL_USER_PASSWORD" ]; then
+			mysql_con_arguments="$mysql_con_arguments --password=$MYSQL_USER_PASSWORD"
 		fi
 		if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then
 			mysql_con_arguments="$mysql_con_arguments --host=$MYSQL_HOST"
@@ -58,8 +85,9 @@ function get_con_args_nodb {
 	fi
 	echo $mysql_con_arguments
 }
+
 function redo_table {
-	$MYSQL_CMD $(get_con_args) < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
+	$MYSQL_CMD $(get_db_args) < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
 }
 
 function success {
diff --git a/utils/patch_header.sh b/utils/patch_header.sh
index c0ce98f..36a591f 100644
--- a/utils/patch_header.sh
+++ b/utils/patch_header.sh
@@ -25,7 +25,6 @@
 
 # load useful functions #######################################################
 . $UTILSPATH/helpers.sh
-echo $(get_con_args)
 
 USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
 CMD_OPTIONS=$(cat <<EOF
@@ -111,7 +110,7 @@ fi
 # @param $1: db version string, e.g. v2.0.0
 # @return: 0 on success, 1 on failure
 function check_version {
-    local version=$($MYSQL_CMD $(get_con_args) -B -e "Select CaosDBVersion();")
+    local version=$($MYSQL_CMD $(get_db_args) -B -e "Select CaosDBVersion();")
 	  if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then
         return 0
 	  fi
-- 
GitLab