Skip to content
Snippets Groups Projects
Commit aefdfe11 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

Merge branch 'f-server-side-scripting' of...

Merge branch 'f-server-side-scripting' of gitlab.gwdg.de:bmp-caosdb/caosdb-mysqlbackend into f-server-side-scripting
parents bed4b333 f021d12a
Branches
Tags
No related merge requests found
...@@ -84,5 +84,5 @@ for opt in $OPTIONS; do ...@@ -84,5 +84,5 @@ for opt in $OPTIONS; do
esc_dollar=${esc_hash//\$/'$$'} esc_dollar=${esc_hash//\$/'$$'}
esc_dq=${esc_dollar//\"/\\\"} esc_dq=${esc_dollar//\"/\\\"}
esc_sq=${esc_dq//\'/\\\'} esc_sq=${esc_dq//\'/\\\'}
echo "$opt=$esc_sq" >> .config echo "$opt=\"$esc_sq\"" >> .config
done done
...@@ -23,21 +23,30 @@ ...@@ -23,21 +23,30 @@
SHELL=/bin/bash SHELL=/bin/bash
include .config include .config
M_MYSQL_USER=$(shell echo $(MYSQL_USER))
M_MYSQL_USER_PASSWORD=$(shell echo $(MYSQL_USER_PASSWORD))
M_LOGIN_PATH=$(shell echo $(LOGIN_PATH))
M_DATABASE_NAME=$(shell echo $(DATABASE_NAME))
M_DATABASE_USER_HOST_LIST=$(shell echo $(DATABASE_USER_HOST_LIST))
M_DATABASE_USER=$(shell echo $(DATABASE_USER))
M_MYSQL_CMD=$(shell echo $(MYSQL_CMD))
M_MYSQLDUMP_CMD=$(shell echo $(MYSQLDUMP_CMD))
M_MYSQLADMIN_CMD=$(shell echo $(MYSQLADMIN_CMD))
INSTALL_SQL_FILE=db_2_0.sql INSTALL_SQL_FILE=db_2_0.sql
ifdef LOGIN_PATH ifdef LOGIN_PATH
MYSQL_CONNECTION=--login-path=$(LOGIN_PATH) M_MYSQL_CONNECTION=--login-path=$(M_LOGIN_PATH)
else else
MYSQL_CONNECTION=--user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)" M_MYSQL_CONNECTION=--user="$(M_MYSQL_USER)" --password="$(M_MYSQL_USER_PASSWORD)"
endif endif
.PHONY: test-connection .PHONY: test-connection
test-connection: test-connection:
@$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "select 0;" $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "select 0;"
.PHONY: upgrade .PHONY: upgrade
upgrade: upgrade:
@cd patches; ./applyPatches.sh $(MYSQL_CONNECTION) --database=$(DATABASE_NAME) @cd patches; ./applyPatches.sh $(M_MYSQL_CONNECTION) --database=$(M_DATABASE_NAME)
.PHONY: install .PHONY: install
install: _install _grant upgrade install: _install _grant upgrade
...@@ -45,35 +54,35 @@ install: _install _grant upgrade ...@@ -45,35 +54,35 @@ install: _install _grant upgrade
.PHONY: _install .PHONY: _install
_install: _install:
@if $(MAKE) _exists > /dev/null 2>&1; then \ @if $(MAKE) _exists > /dev/null 2>&1; then \
printf "\n\nA database with with the name \"$(DATABASE_NAME)\" does already exist.\nCall 'make drop-$(DATABASE_NAME)' to delete that database or reconfigure with './configure'.\n"; \ printf "\n\nA database with with the name \"$(M_DATABASE_NAME)\" does already exist.\nCall 'make drop-$(M_DATABASE_NAME)' to delete that database or reconfigure with './configure'.\n"; \
exit 1; \ exit 1; \
else sed 's/db_2_0/$(DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(MYSQL_CMD) $(MYSQL_CONNECTION); fi else sed 's/db_2_0/$(M_DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION); fi
comma:=, comma:=,
.PHONY: _grant .PHONY: _grant
_grant: _grant:
@for host in $(subst $(comma), ,$(M_DATABASE_USER_HOST_LIST)); do \
( $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "SELECT COUNT(*) FROM mysql.user WHERE user='$(M_DATABASE_USER)' AND host=\"$$host\";" | grep 0 ) || ( echo "The user '$(M_DATABASE_USER)@$$host' is already in the database. Please use another user or delete her. E.g. with 'mysql -u $(M_MYSQL_USER) -p -e \"DROP USER $(M_DATABASE_USER)@$$host;\"'" ; exit 1 ) ; \
done
@while true; do \ @while true; do \
printf "\n Please enter the password for $(DATABASE_USER): "; read -s password; \ printf "\n Please enter the password for $(M_DATABASE_USER): "; read -s password; \
printf "\nPlease repeat the password for $(DATABASE_USER): "; read -s password2; \ printf "\nPlease repeat the password for $(M_DATABASE_USER): "; read -s password2; \
if [ "$$password" != "$$password2" ]; then printf "\n\nThe passwords didn't match. Try again." ; else break ; fi; \ if [ "$$password" != "$$password2" ]; then printf "\n\nThe passwords didn't match. Try again." ; else break ; fi; \
done ; \ done ; \
echo "" ; \ echo "" ; \
for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ for host in $(subst $(comma), ,$(M_DATABASE_USER_HOST_LIST)); do \
$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "CREATE USER '$(DATABASE_USER)'@'$$host' identified by '$$password';"; \ $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "CREATE USER IF NOT EXISTS '$(M_DATABASE_USER)'@'$$host' identified by '$$password';" ; \
$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \
$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \ $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(M_DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \
$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \
done done
.PHONY: drop-% .PHONY: drop-%
drop-%: drop-%:
@for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ @$(M_MYSQLADMIN_CMD) $(M_MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@)
$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "DROP USER '$(DATABASE_USER)'@'$$host';" || true ; \
done
@$(MYSQLADMIN_CMD) $(MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@)
.PHONY: _exists .PHONY: _exists
_exists: _exists:
@if $(MYSQL_CMD) $(MYSQL_CONNECTION) -D "$(DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \ @if $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -D "$(M_DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \
echo "$(DATABASE_NAME) does exist." ; exit 0 ; \ echo "$(M_DATABASE_NAME) does exist." ; exit 0 ; \
else echo "$(DATABASE_NAME) does not exist." ; exit 1; fi else echo "$(M_DATABASE_NAME) does not exist." ; exit 1; fi
...@@ -37,7 +37,7 @@ fi ...@@ -37,7 +37,7 @@ fi
mysql_execute "CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN 'v2.0.0';" mysql_execute "CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN 'v2.0.0';"
# create transaction_log table with new schema # create transaction_log table with new schema
mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log ( mysql_execute "CREATE TABLE new_transaction_log (
transaction VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'TRANSACTION.', transaction VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'TRANSACTION.',
user_id INT UNSIGNED NOT NULL COMMENT 'User.', user_id INT UNSIGNED NOT NULL COMMENT 'User.',
date INT NOT NULL COMMENT 'Date of transaction.', date INT NOT NULL COMMENT 'Date of transaction.',
...@@ -48,19 +48,19 @@ mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log ( ...@@ -48,19 +48,19 @@ mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log (
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;" ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;"
# copy from old transaction_log # copy from old transaction_log
mysql $MYSQL_CONNECTION -D $DATABASE -e "INSERT INTO new_transaction_log SELECT transaction, user_id, (Year(timestamp) * 10000 + Month(timestamp) * 100 + Day(timestamp)) as date, ((Hour(timestamp) + 1) * 10000 + (Minute(timestamp) + 1)*100 + Second(timestamp) + 1 ) as time, NULL as ns, entity_id from transaction_log"; mysql_execute "INSERT INTO new_transaction_log SELECT transaction, user_id, (Year(timestamp) * 10000 + Month(timestamp) * 100 + Day(timestamp)) as date, ((Hour(timestamp) + 1) * 10000 + (Minute(timestamp) + 1)*100 + Second(timestamp) + 1 ) as time, NULL as ns, entity_id from transaction_log";
# check if both have same number of rows # check if both have same number of rows
ROWSTEST=$(mysql $MYSQL_CONNECTION -D $DATABASE -e "SELECT count(*) from new_transaction_log into @newRows; SELECT count(*) from transaction_log into @oldRows; Select (@newRows = @oldRows) as test" | sed -e ':a;N;$!ba;s/[^01]//g') ROWSTEST=$(mysql_execute "SELECT count(*) from new_transaction_log into @newRows; SELECT count(*) from transaction_log into @oldRows; Select (@newRows = @oldRows) as test" | sed -e ':a;N;$!ba;s/[^01]//g')
if [ 0 -eq "$ROWSTEST" ]; then if [ 0 -eq "$ROWSTEST" ]; then
failure "The rows do not match. Something went wrong." failure "The rows do not match. Something went wrong."
fi fi
# delete old transaction_log # delete old transaction_log
mysql $MYSQL_CONNECTION -D $DATABASE -e "DROP TABLE transaction_log;" mysql_execute "DROP TABLE transaction_log;"
# rename new_transaction_log to transaction_log # rename new_transaction_log to transaction_log
mysql $MYSQL_CONNECTION -D $DATABASE -e "ALTER TABLE new_transaction_log RENAME TO transaction_log" mysql_execute "ALTER TABLE new_transaction_log RENAME TO transaction_log"
success success
...@@ -21,9 +21,12 @@ ...@@ -21,9 +21,12 @@
# ** end header # ** end header
# #
#header for patch scripts #header for patch scripts
CMD_MYSQL=mysql if test -e ../.config ; then
CMD_MYSQL_DUMP=mysqldump . ../.config
USAGE="$1 --database=DATABASE_NAME [ --login-path=LOGIN_PATH | --user=USER [ --password=PASSWORD ] ]\n\n" fi
if test -e .config ; then
. .config
fi
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" 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"
set -e set -e
...@@ -155,7 +158,7 @@ function uptodate { ...@@ -155,7 +158,7 @@ function uptodate {
# @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=$($CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -B -e "Select CaosDBVersion();") local version=$($MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME -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
...@@ -168,13 +171,13 @@ function update_version { ...@@ -168,13 +171,13 @@ function update_version {
} }
function dump_table { function dump_table {
$CMD_MYSQL_DUMP $MYSQL_CONNECTION $DATABASE_NAME $1 > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql $MYSQLDUMP_CMD $MYSQL_CONNECTION $DATABASE_NAME $1 > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
} }
function mysql_execute { function mysql_execute {
set +e set +e
$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1" $MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1"
ret=${PIPESTATUS[0]} ret=${PIPESTATUS[0]}
if [ "$ret" -ne 0 ]; then if [ "$ret" -ne 0 ]; then
failure "MYSQL ERROR" failure "MYSQL ERROR"
...@@ -183,5 +186,6 @@ function mysql_execute { ...@@ -183,5 +186,6 @@ function mysql_execute {
} }
function redo_table { function redo_table {
$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql $MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME < ${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