diff --git a/configure b/configure index b8510b381343d05f8c4427553274ee815a01443e..9c6e7544472870152afc3991b4a307dfc3b03420 100755 --- a/configure +++ b/configure @@ -84,5 +84,5 @@ for opt in $OPTIONS; do esc_dollar=${esc_hash//\$/'$$'} esc_dq=${esc_dollar//\"/\\\"} esc_sq=${esc_dq//\'/\\\'} - echo "$opt=$esc_sq" >> .config + echo "$opt=\"$esc_sq\"" >> .config done diff --git a/makefile b/makefile index 437160876e7226a5e4e199d1641d2b90a93ef6f0..a505f77b5bfe597a040d5a210fcf0b4a02369177 100644 --- a/makefile +++ b/makefile @@ -23,21 +23,30 @@ SHELL=/bin/bash 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 ifdef LOGIN_PATH - MYSQL_CONNECTION=--login-path=$(LOGIN_PATH) + M_MYSQL_CONNECTION=--login-path=$(M_LOGIN_PATH) else - MYSQL_CONNECTION=--user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)" + M_MYSQL_CONNECTION=--user="$(M_MYSQL_USER)" --password="$(M_MYSQL_USER_PASSWORD)" endif .PHONY: test-connection test-connection: - @$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "select 0;" + $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "select 0;" .PHONY: upgrade upgrade: - @cd patches; ./applyPatches.sh $(MYSQL_CONNECTION) --database=$(DATABASE_NAME) + @cd patches; ./applyPatches.sh $(M_MYSQL_CONNECTION) --database=$(M_DATABASE_NAME) .PHONY: install install: _install _grant upgrade @@ -45,35 +54,35 @@ install: _install _grant upgrade .PHONY: _install _install: @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; \ - 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:=, .PHONY: _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 \ - printf "\n Please enter the password for $(DATABASE_USER): "; read -s password; \ - printf "\nPlease repeat the password for $(DATABASE_USER): "; read -s password2; \ + printf "\n Please enter the password for $(M_DATABASE_USER): "; read -s password; \ + 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; \ done ; \ echo "" ; \ - for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "CREATE USER '$(DATABASE_USER)'@'$$host' identified by '$$password';"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \ + for host in $(subst $(comma), ,$(M_DATABASE_USER_HOST_LIST)); do \ + $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "CREATE USER IF NOT EXISTS '$(M_DATABASE_USER)'@'$$host' identified by '$$password';" ; \ + $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \ + $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(M_DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \ + $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \ done .PHONY: drop-% drop-%: - @for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \ - $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "DROP USER '$(DATABASE_USER)'@'$$host';" || true ; \ - done - @$(MYSQLADMIN_CMD) $(MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@) + @$(M_MYSQLADMIN_CMD) $(M_MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@) .PHONY: _exists _exists: - @if $(MYSQL_CMD) $(MYSQL_CONNECTION) -D "$(DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \ - echo "$(DATABASE_NAME) does exist." ; exit 0 ; \ - else echo "$(DATABASE_NAME) does not exist." ; exit 1; fi + @if $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -D "$(M_DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \ + echo "$(M_DATABASE_NAME) does exist." ; exit 0 ; \ + else echo "$(M_DATABASE_NAME) does not exist." ; exit 1; fi diff --git a/patches/patch20150715-0/patch.sh b/patches/patch20150715-0/patch.sh index 1df77d68be6789051b2cb1de7fabbbce049ae2dd..4941c817bb0b36d6c4c51fe40f0534666b9ee346 100755 --- a/patches/patch20150715-0/patch.sh +++ b/patches/patch20150715-0/patch.sh @@ -37,7 +37,7 @@ fi mysql_execute "CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN 'v2.0.0';" # 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.', user_id INT UNSIGNED NOT NULL COMMENT 'User.', date INT NOT NULL COMMENT 'Date of transaction.', @@ -48,19 +48,19 @@ mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log ( ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;" # 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 -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 failure "The rows do not match. Something went wrong." fi # 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 -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 diff --git a/patches/utils/patch_header.sh b/patches/utils/patch_header.sh index 8403fd7d7e9a60ee5dda5f1ed996f77ccd6cf6ad..3af2273e2f423912dcca90bc7a7db96f379cabec 100644 --- a/patches/utils/patch_header.sh +++ b/patches/utils/patch_header.sh @@ -21,9 +21,12 @@ # ** end header # #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" +if test -e ../.config ; then + . ../.config +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" set -e @@ -155,7 +158,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=$($MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME -B -e "Select CaosDBVersion();") if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then return 0 fi @@ -168,13 +171,13 @@ function update_version { } 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 { set +e - $CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1" + $MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1" ret=${PIPESTATUS[0]} if [ "$ret" -ne 0 ]; then failure "MYSQL ERROR" @@ -183,5 +186,6 @@ function mysql_execute { } 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 } +