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

MAINT: extract commonly used code from patch_header

parent f84e6d98
Branches
Tags
No related merge requests found
......@@ -27,15 +27,12 @@
BACKUPDIR=./backup #The directory which the dump is to be stored to. Do not change it here. Use the --backupdir=./my/dir/ option!
PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup)\n"
. .config
# load useful stuff - scans the parameters and so on...
. patches/utils/patch_header.sh
. load_settings.sh
. utils.sh
#create backup dir if not exists
[ -d $BACKUPDIR ] || mkdir $BACKUPDIR
function backup {
NARG_NAME=$1
NARG_FILE=$2
......@@ -50,18 +47,11 @@ function backup {
success
}
##test dump file exists
#touch dumpfile.tmp
#backup 0 1 2 "dumpfile.tmp"
#rm dumpfile.tmp
DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g')
BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql
backup $DATABASE_NAME $BACKUPFILE
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright 2019 Daniel Hornung
# Copyright 2020 Henrik tom Wörden, IndiScale GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
[[ -n "config.defaults" ]] && source "config.defaults"
[[ -n ".config" ]] && source ".config"
[[ -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
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
# TODO is this a good idea?
export MYSQL_CONNECTION
export MYSQL_CONNECTION_NO_DB
export DATABASE_NAME
......@@ -22,6 +22,10 @@
# ** end header
#
#header for patch scripts
. ../../load_settings.sh
. ../../utils.sh
USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
CMD_OPTIONS=cat <<EOF
options:
......@@ -88,61 +92,11 @@ while test $# -gt 0; do
esac
done
[[ -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
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_CONNECTION
export MYSQL_CONNECTION_NO_DB
export DATABASE_NAME
if [ -n "$PATCH" ]; then
echo -ne "applying patch $PATCH to $DATABASE_NAME ... "
fi
function success {
echo "[OK]"
exit 0
}
function failure {
echo "[FAILED] $*"
exit 1
}
function uptodate {
echo "[UPTODATE]"
exit 0
}
# @param $1: db version string, e.g. v2.0.0
# @return: 0 on success, 1 on failure
function check_version {
......@@ -157,24 +111,3 @@ function check_version {
function update_version {
mysql_execute "DROP FUNCTION IF EXISTS CaosDBVersion; CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN '$1';"
}
function dump_table {
$MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 \
> ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
function mysql_execute {
set +e
$MYSQL_CMD $MYSQL_CONNECTION -e "$1"
ret=${PIPESTATUS[0]}
if [ "$ret" -ne 0 ]; then
failure "MYSQL ERROR"
fi
set -e
}
function redo_table {
$MYSQL_CMD $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
......@@ -24,13 +24,13 @@
# Updates all SQL procedures
source patches/utils/patch_header.sh
. load_settings
echo -n "updating rules ... "
set -e
mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g rules.sql)"
echo "[OK]"
success
echo -n "updating procedures ... "
mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g procedures/*.sql procedures/query/*.sql)"
......
utils.sh 0 → 100644
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright 2019 Daniel Hornung
# Copyright 2020 Henrik tom Wörden, IndiScale GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
function dump_table {
$MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 \
> ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
function mysql_execute {
set +e
$MYSQL_CMD $MYSQL_CONNECTION -e "$1"
ret=${PIPESTATUS[0]}
if [ "$ret" -ne 0 ]; then
failure "MYSQL ERROR"
fi
set -e
}
function redo_table {
$MYSQL_CMD $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
}
function success {
echo "[OK]"
exit 0
}
function failure {
echo "[FAILED] $*"
exit 1
}
function uptodate {
echo "[UPTODATE]"
exit 0
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment