From b1c1f12c4b2c015f210c26844026fa6c7d362993 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Tue, 12 Dec 2023 14:32:05 +0100
Subject: [PATCH] DOC: update CHANGELOG, MAINT: refactoring make_db and
 helpers.sh

---
 CHANGELOG.md           |  7 +++++++
 README_SETUP.md        | 13 +++++++++++++
 config.defaults        |  6 ++++++
 utils/backup.sh        |  2 +-
 utils/helpers.sh       | 38 ++++++++------------------------------
 utils/load_settings.sh |  1 +
 utils/make_db          | 26 +++++++++++++-------------
 7 files changed, 49 insertions(+), 44 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d49bdee..0b45fe1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed ###
 
+* make install: Could not connect to MariaDB in local docker
+  container
+  [linkahead-mariadbbackend](https://gitlab.com/linkahead/linkahead-mariadbbackend/-/issues/33)
+* Wrong ids in data_type table due to a bug in the `linkahead-server < v0.11.0`
+  which has been fixed but left broken data. This resulted in server errors
+  [linkahead-mariadbbackend#34](https://gitlab.com/linkahead/linkahead-mariadbbackend/-/issues/34)
+
 ### Security ###
 
 ## [7.0.1] - 2023-11-01
diff --git a/README_SETUP.md b/README_SETUP.md
index 95f975d..46d9aeb 100644
--- a/README_SETUP.md
+++ b/README_SETUP.md
@@ -87,6 +87,19 @@ with the then current version of the stored entities.
     * Alternatively, to run the tests in a containerized MariaDB instance, run `make test-docker`,
       followed by `make test-docker-stop`.
 
+
+### Running in a Docker Container
+
+You can use `.docker/docker-compose.yml` to start a docker container
+(`docker-compose -f .docker/docker-compose.yml up -d`) that runs
+mariadb. You need appropriate settings in `.config`:
+`MYSQL_OPTS="--protocol=TCP"` and
+`DATABASE_USER_HOST_LIST=%,`
+After the first start, you need to install the database: `make install`.
+Then, you can connect to it with 
+`mariadb --protocol=TCP -u caosdb -prandom1234 caosdb`.
+
+
 ### Troubleshooting
 
 #### MySQL has failing tests
diff --git a/config.defaults b/config.defaults
index f12278d..1860377 100644
--- a/config.defaults
+++ b/config.defaults
@@ -40,6 +40,12 @@ MYSQL_PORT=3306
 # which will then be used by the CaosDB Server.
 MYSQL_USER=root
 MYSQL_USER_PASSWORD=caosdb1234
+# Additional options for the connection
+# e.g. if you want to connect a dockerized MariaDB at localhost you need to put
+# "--protocol=TCP" here because when host=localhost the connection defaults to
+# SOCKET.
+# MYSQL_OPTS='--protocol=TCP'
+MYSQL_OPTS=
 
 # # DATABASE
 # The name of the SQL database.
diff --git a/utils/backup.sh b/utils/backup.sh
index bdc345c..4d53da3 100755
--- a/utils/backup.sh
+++ b/utils/backup.sh
@@ -56,7 +56,7 @@ function backup() {
     fi
 
     echo "Dumping database $database to $backupfile ..."
-    $MYSQLDUMP_CMD $(get_mysql_args_nodb) --opt --default-character-set=utf8 \
+    $MYSQLDUMP_CMD $(get_db_args_nodb) --opt --default-character-set=utf8 \
           --routines "$database" > "$backupfile"
 
     success
diff --git a/utils/helpers.sh b/utils/helpers.sh
index 6adb318..e313233 100644
--- a/utils/helpers.sh
+++ b/utils/helpers.sh
@@ -52,47 +52,25 @@ function mysql_execute_file {
 	fi
 }
 
-
-function get_mysql_args {
-	echo "$(get_mysql_args_nodb) --database=$DATABASE_NAME"
-}
-
 function get_db_args {
 	echo "$(get_db_args_nodb) --database=$DATABASE_NAME"
 }
 
 function get_db_args_nodb {
-	if [ "$DATABASE_USER" ]; then
+	if [ -n "$DATABASE_USER" ]; then
 		mysql_con_arguments="--user=$DATABASE_USER"
 	fi
-	if [ "$DATABASE_USER_PW" ]; then
+	if [ -n "$DATABASE_USER_PW" ]; then
 		mysql_con_arguments="$mysql_con_arguments --password=$DATABASE_USER_PW"
 	fi
-	if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then
+	if [ -n "$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"
-	else
-		if [ "$MYSQL_USER" ]; then
-			mysql_con_arguments="--user=$MYSQL_USER"
-		fi
-		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"
-			if [ "$MYSQL_PORT" ]; then
-				mysql_con_arguments="$mysql_con_arguments --port=$MYSQL_PORT"
-			fi
-		fi
+    if [ -n "$MYSQL_PORT" ] && [ "$MYSQL_PORT" != "3306" ]; then
+		mysql_con_arguments="$mysql_con_arguments --port=$MYSQL_PORT"
+	fi
+	if [ -n "$MYSQL_OPTS" ] ; then
+		mysql_con_arguments="$mysql_con_arguments $MYSQL_OPTS"
 	fi
 	echo $mysql_con_arguments
 }
diff --git a/utils/load_settings.sh b/utils/load_settings.sh
index c24a0f4..d25dce5 100644
--- a/utils/load_settings.sh
+++ b/utils/load_settings.sh
@@ -65,6 +65,7 @@ export MYSQL_CONFIG_EDITOR_CMD
 export MYSQL_HOST
 export MYSQL_PORT
 export MYSQL_USER
+export MYSQL_OPTS
 export DATABASE_NAME
 export DATABASE_USER
 export DATABASE_USER_PW
diff --git a/utils/make_db b/utils/make_db
index 4a90931..35ed2d9 100755
--- a/utils/make_db
+++ b/utils/make_db
@@ -87,7 +87,7 @@ function _install_unit_test_database () {
     if _db_exists "$DATABASE_NAME"; then
         echo "using $DATABASE_NAME"
     else
-        sed "s/db_5_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
+        sed "s/db_5_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_db_args_nodb)
 
         # create test user
         grant
@@ -113,7 +113,7 @@ function _setup_mytap() {
     pushd libs > /dev/null
     unzip -u mytap*.zip > /dev/null
     pushd mytap*/ > /dev/null
-    $MYSQL_CMD $(get_mysql_args_nodb) < mytap.sql > /dev/null || exit 1
+    $MYSQL_CMD $(get_db_args_nodb) < mytap.sql > /dev/null || exit 1
     popd > /dev/null
     rm -r mytap*/
     popd > /dev/null
@@ -129,14 +129,14 @@ name in your .config file "
         exit 0
     fi
 
-    sed "s/db_5_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_mysql_args_nodb)
+    sed "s/db_5_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_db_args_nodb)
 }
 
 function sanity_check() {
     trap "$(shopt -po errexit)" RETURN
     set +e
     echo -n "running sanity checks ..."
-    msg="$($MYSQL_CMD $(get_mysql_args) < "utils/sanity_check.sql" 2>/dev/null)"
+    msg="$($MYSQL_CMD $(get_db_args) < "utils/sanity_check.sql" 2>/dev/null)"
     code="$?"
     if [ "$code" -eq "0" ] ; then
         echo " [OK]"
@@ -150,7 +150,7 @@ function sanity_check() {
 # Inserts the dump (arg 1) into the database
 function restore_db() {
     SQL_FILE="$1"
-    $MYSQL_CMD $(get_mysql_args) < "$SQL_FILE"
+    $MYSQL_CMD $(get_db_args) < "$SQL_FILE"
     cat <<EOF
 If this is not the same SQL server where the SQL dump was originally created
 from, make sure that a user with sufficient permissions exists.  Note that you
@@ -159,7 +159,7 @@ EOF
 }
 
 function test-connection() {
-    $MYSQL_CMD $(get_mysql_args_nodb) -e "select 0;"
+    $MYSQL_CMD $(get_db_args_nodb) -e "select 0;"
 }
 
 # Creates a user and grants it sufficient rights.
@@ -183,7 +183,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_mysql_args_nodb) -s -N -e "$CMD") = 0 ]] || {
+            [[ $($MYSQL_CMD $(get_db_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};\"'"
@@ -197,7 +197,7 @@ function grant() {
         # FIXME Are all these permissions necessary? See
         # https://gitlab.indiscale.com/caosdb/src/caosdb-mysqlbackend/-/issues/28 "Default
         # installation target does not work for existing databases"
-        $MYSQL_CMD $(get_mysql_args_nodb) <<EOF
+        $MYSQL_CMD $(get_db_args_nodb) <<EOF
 CREATE USER IF NOT EXISTS
     '$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW';
 GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host';
@@ -212,16 +212,16 @@ EOF
 function drop() {
     DROPDB="$1"
     for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
-        $MYSQL_CMD $(get_mysql_args_nodb) -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
+        $MYSQL_CMD $(get_db_args_nodb) -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
     done
-    "$MYSQLADMIN_CMD" $(get_mysql_args_nodb) -f drop "$DROPDB"
+    "$MYSQLADMIN_CMD" $(get_db_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_mysql_args_nodb) -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
+    $MYSQL_CMD $(get_db_args_nodb) -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
         && return 0 || return 1
 }
 
@@ -250,7 +250,7 @@ function grant-permission() {
     fi
 
     cmd="SELECT COUNT(1) from roles where name='${role}';"
-    count=$($MYSQL_CMD $(get_mysql_args) -AN -e "$cmd")
+    count=$($MYSQL_CMD $(get_db_args) -AN -e "$cmd")
     if [[ $count == "0" ]]; then
         echo "Role not found!"
         exit 1
@@ -259,7 +259,7 @@ function grant-permission() {
     cmd="INSERT INTO permissions (role, permissions) VALUE ('${role}', '${permissions}')"
     cmd+="ON DUPLICATE KEY UPDATE role='${role}', permissions='${permissions}'"
     cmd+=";"
-    $MYSQL_CMD $(get_mysql_args) -e "$cmd"
+    $MYSQL_CMD $(get_db_args) -e "$cmd"
 }
 
 
-- 
GitLab