Skip to content
Snippets Groups Projects
Commit e50e2182 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

ENH: building mysql arguments is now a function

parent b17caf4b
Branches
Tags
No related merge requests found
...@@ -55,6 +55,3 @@ DATABASE_USER_PW=random1234 ...@@ -55,6 +55,3 @@ DATABASE_USER_PW=random1234
# wildcard for all hosts, so `192.168.0.%` permits logins from the local # wildcard for all hosts, so `192.168.0.%` permits logins from the local
# network. # network.
DATABASE_USER_HOST_LIST=localhost, DATABASE_USER_HOST_LIST=localhost,
# path to utility scripts
UTILSPATH=`realpath "utils"`
...@@ -24,21 +24,18 @@ ...@@ -24,21 +24,18 @@
INSTALL_SQL_FILE="db_2_0.sql" INSTALL_SQL_FILE="db_2_0.sql"
source utils/load_settings.sh if [ -z "$UTILSPATH" ]; then
source .config || true UTILSPATH="$(realpath $(dirname $0))/utils"
export UTILSPATH
fi
# To fail fast, but beware https://mywiki.wooledge.org/BashFAQ/105 source $UTILSPATH/load_settings.sh
set -e source $UTILSPATH/helpers.sh
if [ -z $LOGIN_PATH ] ; then echo "no1 $MYSQL_HOST"
MYSQL_CONNECTION="--host=${MYSQL_HOST} --port=${MYSQL_PORT} INSTALL_SQL_FILE="db_2_0.sql"
--user=${MYSQL_USER}
--password=${MYSQL_USER_PASSWORD}"
else
MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
fi
SQL="$MYSQL_CMD $MYSQL_CONNECTION" set -e
function fail() { function fail() {
echo "Some error occured, exiting." echo "Some error occured, exiting."
...@@ -66,7 +63,7 @@ function _execute_tests () { ...@@ -66,7 +63,7 @@ function _execute_tests () {
for tfile in $TESTS ; do for tfile in $TESTS ; do
echo "Running $tfile" echo "Running $tfile"
echo "----- $tfile -----" >> .TEST_RESULTS echo "----- $tfile -----" >> .TEST_RESULTS
cat $tfile | $SQL --disable-pager --batch --raw --skip-column-names --unbuffered >> .TEST_RESULTS cat $tfile | $MYSQL_CMD $(get_con_args_nodb) --disable-pager --batch --raw --skip-column-names --unbuffered >> .TEST_RESULTS
done; done;
popd popd
...@@ -87,7 +84,7 @@ function _install_unit_test_database () { ...@@ -87,7 +84,7 @@ function _install_unit_test_database () {
fi fi
fi fi
sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $SQL sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_con_args_nodb)
# crate test user # crate test user
grant grant
...@@ -111,7 +108,7 @@ function _setup_mytap() { ...@@ -111,7 +108,7 @@ function _setup_mytap() {
pushd libs > /dev/null pushd libs > /dev/null
unzip -u mytap*.zip > /dev/null unzip -u mytap*.zip > /dev/null
pushd mytap*/ > /dev/null pushd mytap*/ > /dev/null
$SQL < mytap.sql > /dev/null || exit 1 $MYSQL_CMD $(get_con_args_nodb) < mytap.sql > /dev/null || exit 1
popd > /dev/null popd > /dev/null
rm -r mytap*/ rm -r mytap*/
popd > /dev/null popd > /dev/null
...@@ -146,17 +143,18 @@ Call 'make drop-$DATABASE_NAME' to delete that database or reconfigure with ...@@ -146,17 +143,18 @@ Call 'make drop-$DATABASE_NAME' to delete that database or reconfigure with
exit 0 exit 0
fi fi
sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $SQL sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $MYSQL_CMD $(get_con_args_nodb)
} }
# Inserts the dump (arg 1) into the database # Inserts the dump (arg 1) into the database
function restore_db() { function restore_db() {
SQL_FILE="$1" SQL_FILE="$1"
$SQL -D "$DATABASE_NAME" < "$SQL_FILE" $MYSQL_CMD $(get_con_args) < "$SQL_FILE"
} }
function test-connection() { function test-connection() {
$SQL -e "select 0;" echo $SQL
$MYSQL_CMD $(get_con_args_nodb) -e "select 0;"
} }
# Creates a user and grants it sufficient rights. # Creates a user and grants it sufficient rights.
...@@ -180,7 +178,7 @@ function grant() { ...@@ -180,7 +178,7 @@ function grant() {
if [[ "$1" == "--strict" ]] ; then if [[ "$1" == "--strict" ]] ; then
for host in ${DATABASE_USER_HOST_LIST//,/ } ; do for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
CMD="SELECT COUNT(*) FROM mysql.user WHERE user='${DATABASE_USER}' AND host='${host}';" CMD="SELECT COUNT(*) FROM mysql.user WHERE user='${DATABASE_USER}' AND host='${host}';"
[[ $($SQL -s -N -e "$CMD") == 0 ]] || { [[ $($MYSQL_CMD $(get_con_args_nodb) -s -N -e "$CMD") == 0 ]] || {
echo "The user '${DATABASE_USER}@${host}' is already in the database." echo "The user '${DATABASE_USER}@${host}' is already in the database."
echo "Please use another user or delete it, e.g. with" echo "Please use another user or delete it, e.g. with"
echo "'mysql -u ${MYSQL_USER} -p -e \"DROP USER ${DATABASE_USER}@${host};\"'" echo "'mysql -u ${MYSQL_USER} -p -e \"DROP USER ${DATABASE_USER}@${host};\"'"
...@@ -191,7 +189,7 @@ function grant() { ...@@ -191,7 +189,7 @@ function grant() {
for host in ${DATABASE_USER_HOST_LIST//,/ } ; do for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
echo "Granting admin privileges to '$DATABASE_USER'@'$host'" echo "Granting admin privileges to '$DATABASE_USER'@'$host'"
$SQL <<EOF $MYSQL_CMD $(get_con_args_nodb) <<EOF
CREATE USER IF NOT EXISTS CREATE USER IF NOT EXISTS
'$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW'; '$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW';
GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host'; GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host';
...@@ -206,16 +204,16 @@ EOF ...@@ -206,16 +204,16 @@ EOF
function drop() { function drop() {
DROPDB="$1" DROPDB="$1"
for host in ${DATABASE_USER_HOST_LIST//,/ } ; do for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
$SQL -e "DROP USER '${DATABASE_USER}'@'${host}';" || true $MYSQL_CMD $(get_con_args_nodb) -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
done done
"$MYSQLADMIN_CMD" $MYSQL_CONNECTION -f drop "$DROPDB" "$MYSQLADMIN_CMD" $(get_con_args_nodb) -f drop "$DROPDB"
} }
# Returns 0 or non-zero, depending on whether the database exists already. # Returns 0 or non-zero, depending on whether the database exists already.
# Optional parameter: [DATABASE_NAME], else $DATABASE_NAME is used. # Optional parameter: [DATABASE_NAME], else $DATABASE_NAME is used.
function _db_exists() { function _db_exists() {
$SQL -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \ $MYSQL_CMD $(get_con_args_nodb) -D "${1-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
&& return 0 || return 1 && return 0 || return 1
} }
......
...@@ -22,15 +22,16 @@ ...@@ -22,15 +22,16 @@
# #
SHELL=/bin/bash SHELL=/bin/bash
include .config
INSTALL_SQL_FILE=db_2_0.sql INSTALL_SQL_FILE=db_2_0.sql
ifdef LOGIN_PATH
M_MYSQL_CONNECTION=--login-path=$(M_LOGIN_PATH)
else # TODO can this be removed?
MYSQL_CONNECTION=--host="$(MYSQL_HOST)" --port="$(MYSQL_PORT)" \ #ifdef LOGIN_PATH
--user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)" # M_MYSQL_CONNECTION=--login-path=$(M_LOGIN_PATH)
endif #else
# MYSQL_CONNECTION=--host="$(MYSQL_HOST)" --port="$(MYSQL_PORT)" \
# --user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)"
#endif
.PHONY: test-connection .PHONY: test-connection
test-connection: test-connection:
......
...@@ -35,7 +35,7 @@ fi ...@@ -35,7 +35,7 @@ fi
check_version $OLD_VERSION check_version $OLD_VERSION
function addIdx { function addIdx {
$MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE -e "ALTER TABLE $1 ADD COLUMN pidx TINYINT UNSIGNED NOT NULL DEFAULT 0;" $MYSQL_CMD $(get_con_args) -e "ALTER TABLE $1 ADD COLUMN pidx TINYINT UNSIGNED NOT NULL DEFAULT 0;"
} }
addIdx "reference_data" addIdx "reference_data"
......
...@@ -75,7 +75,7 @@ function backup() { ...@@ -75,7 +75,7 @@ function backup() {
failure "dumpfile already exists." failure "dumpfile already exists."
fi fi
echo "Dumping database $NARG_NAME to $NARG_FILE ... " echo "Dumping database $NARG_NAME to $NARG_FILE ... "
$MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $* --opt --default-character-set=utf8 --routines \ $MYSQLDUMP_CMD $(get_con_args_no_db) $* --opt --default-character-set=utf8 --routines \
"$NARG_NAME" > "$NARG_FILE" "$NARG_NAME" > "$NARG_FILE"
if [[ "$YAML" == "true" ]] ; then if [[ "$YAML" == "true" ]] ; then
......
...@@ -19,14 +19,14 @@ ...@@ -19,14 +19,14 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
function dump_table { function dump_table {
$MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 \ $MYSQLDUMP_CMD $(get_con_args_nodb) $DATABASE_NAME $1 \
> ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
} }
function mysql_execute { function mysql_execute {
set +e set +e
echo $MYSQL_CMD $MYSQL_CONNECTION -e "$1" echo $MYSQL_CMD $(get_con_args) -e "$1"
$MYSQL_CMD $MYSQL_CONNECTION -e "$1" $MYSQL_CMD $(get_con_args) -e "$1"
ret=${PIPESTATUS[0]} ret=${PIPESTATUS[0]}
if [ "$ret" -ne 0 ]; then if [ "$ret" -ne 0 ]; then
failure "MYSQL ERROR" failure "MYSQL ERROR"
...@@ -34,8 +34,32 @@ function mysql_execute { ...@@ -34,8 +34,32 @@ function mysql_execute {
set -e set -e
} }
function get_con_args {
echo "$(get_con_args_nodb) --database=$DATABASE_NAME"
}
function get_con_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"
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
fi
echo $mysql_con_arguments
}
function redo_table { function redo_table {
$MYSQL_CMD $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql $MYSQL_CMD $(get_con_args) < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
} }
function success { function success {
......
...@@ -22,42 +22,15 @@ ...@@ -22,42 +22,15 @@
[[ -n ".config" ]] && source ".config" [[ -n ".config" ]] && source ".config"
[[ -n "$ENV_FILE" ]] && source "$ENV_FILE" [[ -n "$ENV_FILE" ]] && source "$ENV_FILE"
if [[ -z "$DATABASE_NAME" && -z "$MYSQL_CONNECTION" ]]
then
echo "A database or a mysql connection need to be set via the corresponding" \
"environment variables."
exit 1
fi
if [ "$LOGIN_PATH" ]; then
MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION"
MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME"
elif [[ -z "$MYSQL_CONNECTION" ]]; then
MYSQL_CONNECTION=""
if [ "$DATABASE_USER" ]
then
MYSQL_CONNECTION="--user=$DATABASE_USER"
fi
if [ "$DATABASE_USER_PW" ]
then
MYSQL_CONNECTION="$MYSQL_CONNECTION --password=$DATABASE_USER_PW"
fi
if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then
MYSQL_CONNECTION="$MYSQL_CONNECTION --host=$MYSQL_HOST"
if [ "$MYSQL_PORT" ]; then
MYSQL_CONNECTION="$MYSQL_CONNECTION --port=$MYSQL_PORT"
fi
fi
# This option should come last, so we also have one version without the database
MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION"
MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME"
fi
export MYSQL_CMD export MYSQL_CMD
export MYSQLDUMP_CMD export MYSQLDUMP_CMD
export MYSQL_CONNECTION export MYSQLADMIN_CMD
export MYSQL_CONNECTION_NO_DB export MYSQL_CONFIG_EDITOR_CMD
export MYSQL_HOST
export MYSQL_PORT
export MYSQL_USER
export DATABASE_NAME export DATABASE_NAME
export DATABASE_USER
export DATABASE_USER_PW
export DATABASE_USER_HOST_LIST
export UTILSPATH export UTILSPATH
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
# load useful functions ####################################################### # load useful functions #######################################################
. $UTILSPATH/helpers.sh . $UTILSPATH/helpers.sh
echo $(get_con_args)
USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n" USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
CMD_OPTIONS=$(cat <<EOF CMD_OPTIONS=$(cat <<EOF
...@@ -110,7 +111,7 @@ fi ...@@ -110,7 +111,7 @@ fi
# @param $1: db version string, e.g. v2.0.0 # @param $1: db version string, e.g. v2.0.0
# @return: 0 on success, 1 on failure # @return: 0 on success, 1 on failure
function check_version { function check_version {
local version=$($MYSQL_CMD $MYSQL_CONNECTION -B -e "Select CaosDBVersion();") local version=$($MYSQL_CMD $(get_con_args) -B -e "Select CaosDBVersion();")
if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then
return 0 return 0
fi fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment