Skip to content
Snippets Groups Projects
Commit 448d1187 authored by Daniel's avatar Daniel
Browse files

WIP: patch_header.sh now uses env variables + config file.

Also adapted the makefile.
parent 3a236c57
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ test-connection:
.PHONY: upgrade
upgrade:
@cd patches; ./applyPatches.sh $(MYSQL_CONNECTION) --database=$(DATABASE_NAME)
@cd patches; ./applyPatches.sh --env=../.config
.PHONY: install
install: _install _grant upgrade
......
......@@ -30,8 +30,8 @@ export UTILSPATH="./utils"
for p in $PATCHES
do
$p $MYSQL_CONNECTION --database=$DATABASE_NAME --patch=$p
$p "$@" --patch=$p
done
cd ../
./update_sql_procedures.sh $MYSQL_CONNECTION --database=$DATABASE_NAME
./update_sql_procedures.sh
......@@ -4,6 +4,7 @@
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright 2019 Daniel Hornung
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
......@@ -23,122 +24,107 @@
#header for patch scripts
CMD_MYSQL=mysql
CMD_MYSQL_DUMP=mysqldump
USAGE="$1 --database=DATABASE_NAME [ --login-path=LOGIN_PATH | --user=USER [ --password=PASSWORD ] ]\n\n"
CMD_OPTIONS="options:\n\n-h, --help\n\tShow brief help.\n-l LOGIN_PATH, --login-path=LOGIN_PATH\n\tA login-path for the mysql connection (see 'man mysql' and 'man mysql-config-editor').\n-u USER, --user=USER\n\tA mysql user.\n-p PASSWORD, --password=PASSWORD\n\tThe password for the mysql user.\n"
USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
CMD_OPTIONS=cat <<EOF
options:
-h, --help
Show brief help.
--env=ENV_FILE
A file where variables are stored in the format of shell environment
variables. Content of this file overrides environment variables already
present.
--patch=PATCH
*TODO* Please document this option.
--backupdir=BACKUPDIR
*TODO* Please document this option.
This script basically uses the same environment variables as the server
configuration make file. Notable examples are:
- LOGIN_PATH
- MYSQL_HOST
- MYSQL_PORT
- DATABASE_NAME
- DATABASE_USER
- DATABASE_USER_PW
EOF
set -e
function _print_help() {
printf -- "$USAGE"
printf -- "$CMD_OPTIONS"
echo -e "$USAGE"
echo -e "$CMD_OPTIONS"
if [ -n "$PRINT_HELP" ]; then
printf -- "$PRINT_HELP"
echo -e "$PRINT_HELP"
fi
if [ -n "$1" ]; then
printf -- "$1"
echo -e "$1"
fi
}
while test $# -gt 0; do
case "$1" in
-h|--help)
_print_help
exit 0
;;
-p)
shift
if test $# -gt 0; then
MYSQL_USER_PASSWORD=$1
else
echo "no database specified"
exit 1
fi
shift
;;
--password*)
MYSQL_USER_PASSWORD=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-u)
shift
if test $# -gt 0; then
MYSQL_USER=$1
else
echo "no database specified"
exit 1
fi
shift
;;
--user*)
MYSQL_USER=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-l)
shift
if test $# -gt 0; then
LOGIN_PATH=$1
else
echo "no database specified"
exit 1
fi
shift
;;
--login-path*)
LOGIN_PATH=`echo $1 | sed -e 's/^[^=]*=//g'`
shift
;;
-d)
shift
if test $# -gt 0; then
DATABASE_NAME=$1
else
echo "no database specified"
exit 1
fi
shift
;;
--database*)
DATABASE_NAME=`echo $1 | sed -e 's/^[^=]*=//g'`
--env*)
ENV_FILE="${1#--*=}"
shift
;;
--patch*)
PATCH=`echo $1 | sed -e 's/^[^=]*=//g'`
PATCH="${1#--*=}"
shift
;;
--backupdir*)
BACKUPDIR=`echo $1 | sed -e 's/^[^=]*=//g'`
BACKUPDIR="${1#--*=}"
shift
;;
*)
echo "Unknown option $1"
exit 1
shift
;;
esac
done
if [ -z "$DATABASE_NAME" ]
[[ -n "$ENV_FILE" ]] && source "$ENV_FILE"
if [[ -z "$DATABASE_NAME" && -z "$MYSQL_CONNECTION" ]]
then
_print_help "Please specify the database."
exit 1
fi
if [ "$LOGIN_PATH" ]
then
if [ "$LOGIN_PATH" ]; then
MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
else
MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION"
MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME"
elif [[ -z "$MYSQL_CONNECTION" ]]; then
MYSQL_CONNECTION=""
if [ "$MYSQL_USER" ]
if [ "$DATABASE_USER" ]
then
MYSQL_CONNECTION="--user=$MYSQL_USER"
MYSQL_CONNECTION="--user=$DATABASE_USER"
fi
if [ "$MYSQL_USER_PASSWORD" ]
if [ "$DATABASE_USER_PW" ]
then
MYSQL_CONNECTION="$MYSQL_CONNECTION --password=$MYSQL_USER_PASSWORD"
MYSQL_CONNECTION="$MYSQL_CONNECTION --password=$DATABASE_USER_PW"
fi
if [ "$MYSQL_HOST" != "localhost" ]; then
MYSQL_CONNECTION="$MYSQL_CONNECTION --host $MYSQL_HOST"
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"
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_CONNECTION
export MYSQL_CONNECTION_NO_DB
export DATABASE_NAME
if [ -n "$PATCH" ]; then
echo -ne "applying patch $PATCH to $DATABASE_NAME ... "
......@@ -161,7 +147,7 @@ function uptodate {
# @param $1: db version string, e.g. v2.0.0
# @return: 0 on success, 1 on failure
function check_version {
local version=$($CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -B -e "Select CaosDBVersion();")
local version=$($CMD_MYSQL $MYSQL_CONNECTION -B -e "Select CaosDBVersion();")
if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then
return 0
fi
......@@ -174,13 +160,13 @@ function update_version {
}
function dump_table {
$CMD_MYSQL_DUMP $MYSQL_CONNECTION $DATABASE_NAME $1 > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
$CMD_MYSQL_DUMP $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
function mysql_execute {
set +e
$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1"
$CMD_MYSQL $MYSQL_CONNECTION -e "$1"
ret=${PIPESTATUS[0]}
if [ "$ret" -ne 0 ]; then
failure "MYSQL ERROR"
......@@ -189,5 +175,5 @@ function mysql_execute {
}
function redo_table {
$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
$CMD_MYSQL $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment