aboutsummaryrefslogtreecommitdiffstats
path: root/policy-db-migrator
diff options
context:
space:
mode:
authoradheli.tavares <adheli.tavares@est.tech>2022-03-28 12:29:01 +0100
committeradheli.tavares <adheli.tavares@est.tech>2022-03-28 12:37:48 +0100
commit7a58ae144338d6b8ecf9904c05a62ee00ee0538b (patch)
tree4a5b288be7da402a5a4a9baa1ddbba2e1bd78f54 /policy-db-migrator
parent6d3f79492ea9ad35347606c2c4748b50133a5ba8 (diff)
DB migrator fixes after smoke test
Issue-ID: POLICY-3999 Change-Id: I65d95cbfab4b49245bb68f2ca5067fda9437b531 Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'policy-db-migrator')
-rwxr-xr-xpolicy-db-migrator/smoke-test/mariadb-tests.sh332
-rwxr-xr-xpolicy-db-migrator/smoke-test/postgres-tests.sh195
-rw-r--r--policy-db-migrator/smoke-test/readme.md9
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/0900/downgrade/0140-pk_pdpstatistics.sql2
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0150-toscaproperty.sql2
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0160-jpapolicyaudit_pk.sql3
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0170-pdpstatistics_pk.sql3
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql2
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql2
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql2
-rw-r--r--policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql2
-rwxr-xr-xpolicy-db-migrator/src/main/docker/db-migrator308
-rwxr-xr-xpolicy-db-migrator/src/main/docker/db-migrator-pg314
13 files changed, 847 insertions, 329 deletions
diff --git a/policy-db-migrator/smoke-test/mariadb-tests.sh b/policy-db-migrator/smoke-test/mariadb-tests.sh
new file mode 100755
index 00000000..a5f1e31f
--- /dev/null
+++ b/policy-db-migrator/smoke-test/mariadb-tests.sh
@@ -0,0 +1,332 @@
+#!/bin/sh
+# ============LICENSE_START====================================================
+# Copyright (C) 2022 Nordix Foundation.
+# =============================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END======================================================
+# shellcheck disable=SC2039
+# shellcheck disable=SC2086
+# shellcheck disable=SC2012
+echo "Start of test $(date +%F-%T)"
+
+export POLICY_HOME=/opt/app/policy
+export SQL_USER=policy_user
+export SQL_PASSWORD=policy_user
+export SQL_HOST=mariadb
+export MYSQL_ROOT_PASSWORD=secret
+export SCHEMA=policyadmin
+export SCRIPT_DIRECTORY=sql
+
+# Test variables
+TOTAL_COUNT=0
+HONOLULU_UPGRADE_COUNT=$(ls /home/policy/sql/0800/upgrade/*.sql | wc -l)
+HONOLULU_DOWNGRADE_COUNT=$(ls /home/policy/sql/0800/downgrade/*.sql | wc -l)
+ISTANBUL_UPGRADE_COUNT=$(ls /home/policy/sql/0900/upgrade/*.sql | wc -l)
+ISTANBUL_DOWNGRADE_COUNT=$(ls /home/policy/sql/0900/downgrade/*.sql | wc -l)
+JAKARTA_UPGRADE_COUNT=$(ls /home/policy/sql/1000/upgrade/*.sql | wc -l)
+JAKARTA_DOWNGRADE_COUNT=$(ls /home/policy/sql/1000/downgrade/*.sql | wc -l)
+
+NEW_SQL_EXECUTIONS=0
+START_VERSION=""
+PREVIOUS_SQL_EXECUTIONS=0
+END_VERSION=""
+RECENT_SQL_EXECUTIONS=0
+END_STATUS=0
+TEST_STATUS="FAIL"
+TEST_MSG=""
+TESTS=0
+PASSED=0
+FAILED=0
+
+# SQL statements
+PDPSTATISTICS="CREATE TABLE IF NOT EXISTS pdpstatistics ("
+PDPSTATISTICS=${PDPSTATISTICS}"PDPGROUPNAME VARCHAR(120) NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"PDPSUBGROUPNAME VARCHAR(120) NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYFAILCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDFAILCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS}"POLICYUNDEPLOYCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS2}"POLICYUNDEPLOYFAILCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS2}"POLICYUNDEPLOYSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"timeStamp datetime NOT NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS2}"timeStamp datetime DEFAULT NULL NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS2}"ID BIGINT NOT NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"name VARCHAR(120) NOT NULL, "
+PDPSTATISTICS2=${PDPSTATISTICS2}"name VARCHAR(120) NOT NULL, "
+PDPSTATISTICS=${PDPSTATISTICS}"version VARCHAR(20) NOT NULL,"
+PDPSTATISTICS2=${PDPSTATISTICS2}"version VARCHAR(20) NOT NULL,"
+PDPSTATISTICS=${PDPSTATISTICS}"CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (timeStamp, name, version));"
+PDPSTATISTICS2=${PDPSTATISTICS2}"CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (ID, name, version));"
+
+IDX_TSIDX1="CREATE INDEX IDX_TSIDX1 ON pdpstatistics(timeStamp, name, version);"
+INSERT="INSERT INTO pdpstatistics(PDPGROUPNAME,PDPSUBGROUPNAME,POLICYDEPLOYCOUNT,POLICYDEPLOYFAILCOUNT,POLICYDEPLOYSUCCESSCOUNT,"
+INSERT1="${INSERT}""POLICYEXECUTEDCOUNT,POLICYEXECUTEDFAILCOUNT,POLICYEXECUTEDSUCCESSCOUNT,timeStamp,name,version)"
+INSERT2="${INSERT}""POLICYEXECUTEDCOUNT,POLICYEXECUTEDFAILCOUNT,POLICYEXECUTEDSUCCESSCOUNT,POLICYUNDEPLOYCOUNT,"
+INSERT2="${INSERT2}""POLICYUNDEPLOYFAILCOUNT,POLICYUNDEPLOYSUCCESSCOUNT,timeStamp,ID,name,version)"
+SQL1="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test1', '1.0')"
+SQL2="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test1', '1.0')"
+SQL3="${INSERT2}"" values('groupname', 'subgroup',1,1,1,1,1,1,1,1,1,now(),3,'test1', '1.0')"
+SQL4="${INSERT2}"" values('groupname', 'subgroup',1,1,1,1,1,1,1,1,1,now(),4,'test1', '1.0')"
+SQL5="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test2', '1.0')"
+SQL6="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test2', '1.0')"
+
+run_sql() {
+ local user="${1}" password="${2}" schema="${3}" sql="${4}"
+ MYSQL="mysql -u${user} -p${password} -h ${SQL_HOST} ${schema}"
+ ${MYSQL} --execute "${sql}"
+ return $?
+}
+
+start_test() {
+ echo ""
+ echo "############################################################################################################"
+ echo ""
+ let TESTS=$TESTS+1
+ reportStatus=$(/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report | tail -2)
+ echo "Starting test $TESTS"
+ echo "$reportStatus"
+ START_VERSION=$(echo $reportStatus | cut -f13 -d' ')
+ PREVIOUS_SQL_EXECUTIONS=$(echo $reportStatus | cut -f1 -d' ')
+
+ if [ "${START_VERSION}" == "" ]; then
+ START_VERSION="0"
+ PREVIOUS_SQL_EXECUTIONS=0
+ fi
+}
+
+end_test() {
+ reportStatus=$(/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report | tail -2)
+ echo "Ending test $TESTS"
+ echo "$reportStatus"
+ END_VERSION=$(echo $reportStatus | cut -f13 -d' ')
+ RECENT_SQL_EXECUTIONS=$(echo $reportStatus | cut -f1 -d' ')
+ END_STATUS=$(echo $reportStatus | cut -f7 -d' ')
+}
+
+check_results() {
+ local status="${1}" operation="${2}" startVer="${3}" endVer="${4}" newRecords="${5}" filesRan="${6}"
+
+ echo ""
+ echo "Test summary: status:$status, operation:$operation, from:$startVer, to:$endVer, new executions:$newRecords, sqlFiles:$filesRan"
+ # Convert to number
+ startVer=$(echo ${startVer} | awk '{$0=int($0)}1')
+ endVer=$(echo ${endVer} | awk '{$0=int($0)}1')
+
+ if [ ${startVer} -eq ${endVer} ] && [ $newRecords -eq $filesRan ] && [ $newRecords -eq 0 ]; then
+ TEST_MSG="No ${operation} required"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ elif [ $status -eq 1 ] && [ "${operation}" == "upgrade" ] && [ ${startVer} -le ${endVer} ] && [ $newRecords -eq $filesRan ]; then
+ TEST_MSG="Successful ${operation}"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ elif [ $status -eq 1 ] && [ "${operation}" == "downgrade" ] && [ ${startVer} -ge ${endVer} ] && [ $newRecords -eq $filesRan ]; then
+ TEST_MSG="Successful ${operation}"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ else
+ TEST_MSG="Errors occurred during ${operation}"
+ TEST_STATUS="FAIL"
+ let FAILED=$FAILED+1
+ fi
+ echo ""
+ echo "*** Test $TESTS: $TEST_STATUS , $TEST_MSG, current version: $END_VERSION ***"
+ echo ""
+}
+
+# Test 1 - Upgrade to Istanbul
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+start_test
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
+end_test
+let TOTAL_COUNT=$HONOLULU_UPGRADE_COUNT+$ISTANBUL_UPGRADE_COUNT
+check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $RECENT_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 2 - downgrade to 0800
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $ISTANBUL_DOWNGRADE_COUNT
+
+sleep 5
+
+# Test 3 - upgrade to 0900
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $ISTANBUL_UPGRADE_COUNT
+
+sleep 5
+
+# Test4 - run upgrade on db where tables already exist and migration schema is empty
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+run_sql "root" "${MYSQL_ROOT_PASSWORD}" "${SCHEMA}" "DROP DATABASE IF EXISTS migration;"
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS
+check_results $END_STATUS 'upgrade' "0800" "${END_VERSION}" $NEW_SQL_EXECUTIONS $ISTANBUL_UPGRADE_COUNT
+
+sleep 5
+
+# Test5 - upgrade after failed downgrade
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS2}"
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${IDX_TSIDX1}"
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+# (files run before error * 2) + 1 to run the file again
+let TOTAL_COUNT=11
+check_results $END_STATUS 'upgrade' "0800" "0900" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test6 - Downgrade after failed downgrade
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS2}"
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${IDX_TSIDX1}"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+# +1 to run the file again
+let TOTAL_COUNT=$ISTANBUL_DOWNGRADE_COUNT+1
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test7 - downgrade after failed upgrade
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS}"
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+# (files run before error * 2) + 1 to run the file again
+let TOTAL_COUNT=3
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 8 - Upgrade after failed upgrade
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS}"
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+# +1 to run the file again
+let TOTAL_COUNT=$ISTANBUL_UPGRADE_COUNT+1
+check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 9 - Upgrade when pdpstatistics contains data
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+start_test
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL1}"
+sleep 1
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL2}"
+sleep 1
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL3}"
+sleep 1
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL4}"
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+# +1 to run the file again
+let TOTAL_COUNT=$ISTANBUL_UPGRADE_COUNT
+check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 10 - downgrade to 0800 with records in pdpstatistics
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL5}"
+sleep 1
+run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL6}"
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $ISTANBUL_DOWNGRADE_COUNT
+
+sleep 5
+
+# Test 11 - downgrade from 0800
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $HONOLULU_DOWNGRADE_COUNT
+
+sleep 5
+
+# Test 12 - Full upgrade
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+let TOTAL_COUNT=$HONOLULU_UPGRADE_COUNT+$ISTANBUL_UPGRADE_COUNT+$JAKARTA_UPGRADE_COUNT
+check_results $END_STATUS 'upgrade' "0" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 13 - Full downgrade
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 1000 -t 0
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+let TOTAL_COUNT=$HONOLULU_DOWNGRADE_COUNT+$ISTANBUL_DOWNGRADE_COUNT+$JAKARTA_DOWNGRADE_COUNT
+check_results $END_STATUS 'downgrade' "${START_VERSION}" "0" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+echo
+echo "-----------------------------------------------------------------------"
+echo "Number of Tests: $TESTS, Tests Passed: $PASSED, Tests Failed: $FAILED"
+echo "-----------------------------------------------------------------------"
+
+echo "End of test $(date +%F-%T)"
+
+nc -lk -p 6824
+
+exit 0
diff --git a/policy-db-migrator/smoke-test/postgres-tests.sh b/policy-db-migrator/smoke-test/postgres-tests.sh
new file mode 100755
index 00000000..be38a5d8
--- /dev/null
+++ b/policy-db-migrator/smoke-test/postgres-tests.sh
@@ -0,0 +1,195 @@
+#!/bin/sh
+# ============LICENSE_START====================================================
+# Copyright (C) 2022 Nordix Foundation.
+# =============================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END======================================================
+# shellcheck disable=SC2039
+# shellcheck disable=SC2086
+# shellcheck disable=SC2012
+
+echo "Start of test $(date +%F-%T)"
+
+export POLICY_HOME=/opt/app/policy
+export SQL_USER=policy_user
+export SQL_HOST=postgres
+export SQL_PASSWORD=policy_user
+export SCHEMA=policyadmin
+export POSTGRES_PASSWORD=secret
+export SCRIPT_DIRECTORY=postgres
+
+# Test variables
+TOTAL_COUNT=0
+HONOLULU_COUNT=96
+ISTANBUL_COUNT=13
+JAKARTA_COUNT=9
+
+NEW_SQL_EXECUTIONS=0
+PREVIOUS_SQL_EXECUTIONS=0
+RECENT_SQL_EXECUTIONS=0
+TEST_STATUS="FAIL"
+TEST_MSG=""
+TESTS=0
+PASSED=0
+FAILED=0
+
+run_sql() {
+ local user="${1}" schema="${2}" sql="${3}"
+ PGSQL="psql -U ${user} -d ${schema} -h ${SQL_HOST}"
+ ${PGSQL} --command "${sql}"
+ return $?
+}
+
+start_test() {
+ echo ""
+ echo "############################################################################################################"
+ echo ""
+ let TESTS=$TESTS+1
+ echo "Starting test $TESTS"
+ PREVIOUS_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS
+}
+
+end_test() {
+ echo "Ending test $TESTS"
+ reportStatus=$(/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o report | tail -3)
+ echo "Status: $reportStatus"
+ RECENT_SQL_EXECUTIONS=$(echo $reportStatus | cut -f1 -d' ' | sed 's/(//')
+}
+
+check_results() {
+ local operation="${1}" startVer="${2}" endVer="${3}" newRecords="${4}" filesRan="${5}"
+
+ echo ""
+ echo "Test summary: operation:$operation, from:$startVer, to:$endVer, new executions:$newRecords, sqlFiles:$filesRan"
+ # Convert to number
+ startVer=$(echo ${startVer} | awk '{$0=int($0)}1')
+ endVer=$(echo ${endVer} | awk '{$0=int($0)}1')
+
+ if [ ${startVer} -eq ${endVer} ] && [ $newRecords -eq $filesRan ] && [ $newRecords -eq 0 ]; then
+ TEST_MSG="No ${operation} required"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ elif [ "${operation}" == "upgrade" ] && [ ${startVer} -le ${endVer} ] && [ $newRecords -eq $filesRan ]; then
+ TEST_MSG="Successful ${operation}"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ elif [ "${operation}" == "downgrade" ] && [ ${startVer} -ge ${endVer} ] && [ $newRecords -eq $filesRan ]; then
+ TEST_MSG="Successful ${operation}"
+ TEST_STATUS="PASS"
+ let PASSED=$PASSED+1
+ else
+ TEST_MSG="Errors occurred during ${operation}"
+ TEST_STATUS="FAIL"
+ let FAILED=$FAILED+1
+ fi
+ echo ""
+ echo "*** Test $TESTS: $TEST_STATUS , $TEST_MSG, current version: $END_VERSION ***"
+ echo ""
+}
+
+# Test 1 - Upgrade to Istanbul
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o upgrade -t 0900
+end_test
+let TOTAL_COUNT=$HONOLULU_COUNT+$ISTANBUL_COUNT
+check_results 'upgrade' "0" "0900" $RECENT_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 2 - downgrade to 0800
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results 'downgrade' "0900" "0800" $NEW_SQL_EXECUTIONS $ISTANBUL_COUNT
+
+sleep 5
+
+# Test 3 - upgrade to 1000
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o upgrade -f 0800 -t 1000
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+let TOTAL_COUNT=$ISTANBUL_COUNT+$JAKARTA_COUNT
+check_results 'upgrade' "0800" "1000" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test4 - run upgrade on db where tables already exist and migration schema is empty
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o downgrade -t 0800
+run_sql "postgres" "postgres" "DROP DATABASE IF EXISTS migration;"
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o upgrade -t 0900
+end_test
+check_results 'upgrade' "0800" "0900" $RECENT_SQL_EXECUTIONS $ISTANBUL_COUNT
+
+sleep 5
+
+# Test5 - downgrade
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o downgrade -f 0900 -t 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results 'downgrade' "0900" "0800" $NEW_SQL_EXECUTIONS $ISTANBUL_COUNT
+
+sleep 5
+
+
+# Test 6 - downgrade from 0800
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o downgrade -f 0800
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+check_results 'downgrade' "0800" "0" $NEW_SQL_EXECUTIONS $HONOLULU_COUNT
+
+sleep 5
+
+# Test 7 - Full upgrade
+start_test
+/opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o upgrade
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+let TOTAL_COUNT=$HONOLULU_COUNT+$ISTANBUL_COUNT+$JAKARTA_COUNT
+check_results 'upgrade' "0" "1000" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+sleep 5
+
+# Test 13 - Full downgrade
+start_test
+/opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
+/opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o downgrade -f 1000 -t 0
+end_test
+let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
+let TOTAL_COUNT=$HONOLULU_COUNT+$ISTANBUL_COUNT+$JAKARTA_COUNT
+check_results 'downgrade' "1000" "0" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
+
+echo
+echo "-----------------------------------------------------------------------"
+echo "Number of Tests: $TESTS, Tests Passed: $PASSED, Tests Failed: $FAILED"
+echo "-----------------------------------------------------------------------"
+
+echo "End of test $(date +%F-%T)"
+
+nc -lk -p 6824
+
+exit 0
diff --git a/policy-db-migrator/smoke-test/readme.md b/policy-db-migrator/smoke-test/readme.md
new file mode 100644
index 00000000..5a00de12
--- /dev/null
+++ b/policy-db-migrator/smoke-test/readme.md
@@ -0,0 +1,9 @@
+# DB Migrator Smoke test
+CSIT docker compose for db-migrator can be used for this or any other docker compose.
+
+- Check if `mariadb-tests.sh` and `postgres-test.sh` have the correct variables.
+- Add any extras tests necessary for the test.
+- Change the `db_migrator_policy_init.sh` on db-migrator docker to the `*-test.sh` file.
+- Run docker compose
+- Collect logs with `docker-compose logs > results.txt`
+- Tear down compose. Repeat process if necessary. \ No newline at end of file
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/0900/downgrade/0140-pk_pdpstatistics.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/0900/downgrade/0140-pk_pdpstatistics.sql
index 6b87eef3..078270c8 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/0900/downgrade/0140-pk_pdpstatistics.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/0900/downgrade/0140-pk_pdpstatistics.sql
@@ -17,6 +17,6 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE pdpstatistics DROP CONSTRAINT PRIMARY KEY;
+ALTER TABLE pdpstatistics DROP CONSTRAINT PK_PDPSTATISTICS;
UPDATE pdpstatistics set ID = 0;
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0150-toscaproperty.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0150-toscaproperty.sql
index e1abf28f..1db3a625 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0150-toscaproperty.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0150-toscaproperty.sql
@@ -19,4 +19,4 @@
CREATE TABLE IF NOT EXISTS jpatoscaproperty_constraints (parentLocalName VARCHAR(120) NULL, localName VARCHAR(120) NULL, parentKeyVersion VARCHAR(15) NULL, parentKeyName VARCHAR(120) NULL, CONSTRAINTS VARCHAR(255) NULL);
CREATE TABLE IF NOT EXISTS jpatoscaproperty_metadata (parentLocalName VARCHAR(120) NULL, localName VARCHAR(120) NULL, parentKeyVersion VARCHAR(15) NULL, parentKeyName VARCHAR(120) NULL, METADATA VARCHAR(255) NULL, METADATA_KEY VARCHAR(255) NULL);
-CREATE TABLE IF NOT EXISTS toscaproperty (DEFAULTVALUE VARCHAR(255) NULL, DESCRIPTION VARCHAR(255) NULL, ENTRYSCHEMA BYTEA DEFAULT NULL NULL, REQUIRED BIT DEFAULT 0 NULL, STATUS INT DEFAULT NULL NULL, parentLocalName VARCHAR(120) NOT NULL, localName VARCHAR(120) NOT NULL, parentKeyVersion VARCHAR(15) NOT NULL, parentKeyName VARCHAR(120) NOT NULL, name VARCHAR(120) NULL, version VARCHAR(20) NULL, CONSTRAINT PK_TOSCAPROPERTY PRIMARY KEY (parentLocalName, localName, parentKeyVersion, parentKeyName));
+CREATE TABLE IF NOT EXISTS toscaproperty (DEFAULTVALUE VARCHAR(255) NULL, DESCRIPTION VARCHAR(255) NULL, ENTRYSCHEMA BYTEA DEFAULT NULL NULL, REQUIRED BIT DEFAULT 0::bit NULL, STATUS INT DEFAULT NULL NULL, parentLocalName VARCHAR(120) NOT NULL, localName VARCHAR(120) NOT NULL, parentKeyVersion VARCHAR(15) NOT NULL, parentKeyName VARCHAR(120) NOT NULL, name VARCHAR(120) NULL, version VARCHAR(20) NULL, CONSTRAINT PK_TOSCAPROPERTY PRIMARY KEY (parentLocalName, localName, parentKeyVersion, parentKeyName));
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0160-jpapolicyaudit_pk.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0160-jpapolicyaudit_pk.sql
index 3a3d3ea9..65b0dc48 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0160-jpapolicyaudit_pk.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0160-jpapolicyaudit_pk.sql
@@ -1,6 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2022 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,5 +18,5 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE jpapolicyaudit DROP CONSTRAINT PRIMARY KEY;
+ALTER TABLE jpapolicyaudit DROP CONSTRAINT PK_JPAPOLICYAUDIT;
ALTER TABLE jpapolicyaudit ADD CONSTRAINT PK_JPAPOLICYAUDIT PRIMARY KEY (ID, name, version);
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0170-pdpstatistics_pk.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0170-pdpstatistics_pk.sql
index 08065e42..29c24f03 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0170-pdpstatistics_pk.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0170-pdpstatistics_pk.sql
@@ -1,6 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2022 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,5 +18,5 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE pdpstatistics DROP CONSTRAINT PRIMARY KEY;
+ALTER TABLE pdpstatistics DROP CONSTRAINT PK_PDPSTATISTICS;
ALTER TABLE pdpstatistics ADD CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (ID, name, version);
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
index 3ea7f9ba..e8e7cad2 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
@@ -17,4 +17,4 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE jpatoscanodetemplate_metadata modify METADATA VARCHAR; \ No newline at end of file
+ALTER TABLE jpatoscanodetemplate_metadata ALTER COLUMN METADATA TYPE VARCHAR(255); \ No newline at end of file
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
index 9c4a118a..1d91fcb2 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/postgres/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
@@ -17,4 +17,4 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE jpatoscanodetemplate_metadata modify METADATA LONGTEXT; \ No newline at end of file
+ALTER TABLE jpatoscanodetemplate_metadata ALTER COLUMN METADATA TYPE TEXT; \ No newline at end of file
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql b/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
index 3ea7f9ba..0aafd387 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/downgrade/0180-jpatoscanodetemplate_metadata.sql
@@ -17,4 +17,4 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE jpatoscanodetemplate_metadata modify METADATA VARCHAR; \ No newline at end of file
+ALTER TABLE jpatoscanodetemplate_metadata MODIFY COLUMN METADATA VARCHAR(255); \ No newline at end of file
diff --git a/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql b/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
index 9c4a118a..426613de 100644
--- a/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
+++ b/policy-db-migrator/src/main/docker/config/policyadmin/sql/1000/upgrade/0180-jpatoscanodetemplate_metadata.sql
@@ -17,4 +17,4 @@
* ============LICENSE_END=========================================================
*/
-ALTER TABLE jpatoscanodetemplate_metadata modify METADATA LONGTEXT; \ No newline at end of file
+ALTER TABLE jpatoscanodetemplate_metadata MODIFY COLUMN METADATA LONGTEXT; \ No newline at end of file
diff --git a/policy-db-migrator/src/main/docker/db-migrator b/policy-db-migrator/src/main/docker/db-migrator
index 785dd834..565ff1c4 100755
--- a/policy-db-migrator/src/main/docker/db-migrator
+++ b/policy-db-migrator/src/main/docker/db-migrator
@@ -4,7 +4,7 @@
# ONAP
# ================================================================================
# Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
-# Modifications Copyright (C) 2021 Nordix Foundation.
+# Modifications Copyright (C) 2021-2022 Nordix Foundation.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -102,8 +102,7 @@ function usage() {
# current_schema
#####################################################
-function current_schema
-{
+function current_schema() {
if [ "${DEBUG}" = "y" ]; then
echo "-- current_schema --"
set -x
@@ -125,8 +124,7 @@ function current_schema
# ensure global metadata
#####################################################
-function ensure_metadata
-{
+function ensure_metadata() {
if [ "${DEBUG}" = "y" ]; then
echo "-- ensure_metadata --"
set -x
@@ -148,13 +146,11 @@ function ensure_metadata
return $?
}
-
#####################################################
# ensure metadata on a per schema basis
#####################################################
-function ensure_metadata_schema
-{
+function ensure_metadata_schema() {
if [ "${DEBUG}" = "y" ]; then
echo "-- ensure_metadata_schema --"
set -x
@@ -178,13 +174,11 @@ function ensure_metadata_schema
return $?
}
-
#####################################################
# target_release
#####################################################
-function target_release
-{
+function target_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- target_release --"
set -x
@@ -195,7 +189,7 @@ function target_release
TARGET_UPGRADE_RELEASE=${ZERO_VERSION}
TARGET_DOWNGRADE_RELEASE=${ZERO_VERSION}
- upgradeSqls=$(ls -v -r "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v -r "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
for sql in ${upgradeSqls}; do
TARGET_UPGRADE_RELEASE="$(basename $(dirname $(dirname $sql)))"
break
@@ -209,8 +203,7 @@ function target_release
# is_upgrade
#####################################################
-function is_upgrade
-{
+function is_upgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- is_upgrade --"
set -x
@@ -218,7 +211,7 @@ function is_upgrade
local upgradeSqls
- upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
if [ -z "${upgradeSqls}" ]; then
return 1
else
@@ -226,13 +219,11 @@ function is_upgrade
fi
}
-
#####################################################
# is_downgrade
#####################################################
-function is_downgrade
-{
+function is_downgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- is_downgrade --"
set -x
@@ -240,7 +231,7 @@ function is_downgrade
local downgradeSqls
- downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2> /dev/null)
+ downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2>/dev/null)
if [ -z "${downgradeSqls}" ]; then
return 1
else
@@ -248,35 +239,32 @@ function is_downgrade
fi
}
-
#####################################################
# set_current_release
#####################################################
-function set_current_release
-{
- if [ "${DEBUG}" = "y" ]; then
- echo "-- set_current_release --"
- set -x
- fi
+function set_current_release() {
+ if [ "${DEBUG}" = "y" ]; then
+ echo "-- set_current_release --"
+ set -x
+ fi
- CURRENT_RELEASE="${1}"
+ CURRENT_RELEASE="${1}"
- local sql
- sql="INSERT INTO ${METADATA_TABLE} (name, version) "
- sql=${sql}"VALUES('${SCHEMA}', '${CURRENT_RELEASE}') "
- sql=${sql}"ON DUPLICATE KEY UPDATE version='${CURRENT_RELEASE}';"
+ local sql
+ sql="INSERT INTO ${METADATA_TABLE} (name, version) "
+ sql=${sql}"VALUES('${SCHEMA}', '${CURRENT_RELEASE}') "
+ sql=${sql}"ON DUPLICATE KEY UPDATE version='${CURRENT_RELEASE}';"
- ${MYSQL} --execute "${sql}"
- return $?
+ ${MYSQL} --execute "${sql}"
+ return $?
}
#####################################################
# current_release
#####################################################
-function current_release
-{
+function current_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- current_release --"
set -x
@@ -298,8 +286,7 @@ function current_release
# previous_release
#####################################################
-function previous_release
-{
+function previous_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- previous_release --"
set -x
@@ -308,28 +295,26 @@ function previous_release
local current_release_int_val previous_release_int_val previous_release
if [ $current_release == $ZERO_VERSION ] || [ $current_release == $BASE_VERSION ]; then
- PREVIOUS_RELEASE=$ZERO_VERSION
+ PREVIOUS_RELEASE=$ZERO_VERSION
else
- current_release_int_val=$(echo $current_release | awk '{$0=int($0)}1')
- let previous_release_int_val=$current_release_int_val-100
- if [ $previous_release_int_val -lt 1000 ]; then
- previous_release="0"$previous_release_int_val
- else
- previous_release=$previous_release_int_val
- fi
- PREVIOUS_RELEASE=$previous_release
+ current_release_int_val=$(echo $current_release | awk '{$0=int($0)}1')
+ let previous_release_int_val=$current_release_int_val-100
+ if [ $previous_release_int_val -lt 1000 ]; then
+ previous_release="0"$previous_release_int_val
+ else
+ previous_release=$previous_release_int_val
+ fi
+ PREVIOUS_RELEASE=$previous_release
fi
return 0
}
-
#####################################################
# last_operation_status
#####################################################
-function last_operation_status
-{
+function last_operation_status() {
if [ "${DEBUG}" = "y" ]; then
echo "-- last_operation_status --"
set -x
@@ -355,15 +340,14 @@ function last_operation_status
rc=1
fi
- return $rc;
+ return $rc
}
#####################################################
# execute sql script history
#####################################################
-function track_script
-{
+function track_script() {
if [ "${DEBUG}" = "y" ]; then
echo "-- track_script $* --"
set -x
@@ -371,7 +355,7 @@ function track_script
local script="${1}" operation="${2}" success="${3}" from_version="${4}" to_version="${5}" tag="${6}"
if [ $operation == "downgrade" ]; then
- to_version=${TARGET_DOWNGRADE_RELEASE}
+ to_version=${TARGET_DOWNGRADE_RELEASE}
fi
local sql="INSERT INTO ${METADATA_HISTORY}(script,operation,from_version,to_version,tag,success,atTime) "
@@ -381,13 +365,11 @@ function track_script
return $?
}
-
#####################################################
# execute sql script
#####################################################
-function run_script
-{
+function run_script() {
if [ "${DEBUG}" == "y" ]; then
echo "-- run_script $* --"
set -x
@@ -398,7 +380,7 @@ function run_script
echo
echo "> ${operation} ${script}"
- ${MYSQL} ${SCHEMA} --verbose < "${scriptPath}"
+ ${MYSQL} ${SCHEMA} --verbose <"${scriptPath}"
local rc=$?
if [ ${rc} -ne 0 ]; then
success="0"
@@ -415,8 +397,7 @@ function run_script
# upgrade
#####################################################
-function upgrade
-{
+function upgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- upgrade --"
set -x
@@ -428,20 +409,20 @@ function upgrade
echo "upgrade: ${CURRENT_RELEASE} -> ${TARGET_UPGRADE_RELEASE}"
if [ ${CURRENT_RELEASE} \< ${TARGET_UPGRADE_RELEASE} ]; then
- upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
for sqlFile in ${upgradeSqls}; do
sqlName=$(basename "${sqlFile}")
schemaVersion="$(basename $(dirname $(dirname $sqlFile)))"
previous_release $schemaVersion
- if [ "${schemaVersion}" -gt "${CURRENT_RELEASE}" ] && \
+ if [ "${schemaVersion}" -gt "${CURRENT_RELEASE}" ] &&
[ "${schemaVersion}" -le "${TARGET_UPGRADE_RELEASE}" ]; then
tag=${tagDate}"${schemaVersion}u"
- if [ $RETRY -eq 1 ] && \
+ if [ $RETRY -eq 1 ] &&
# Compare the numeric portion of the filename because shell doesn't support string comparison
- [ $(echo ${sqlName}| awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT}| awk -F- '{print $1}') ]; then
- rc=1
+ [ $(echo ${sqlName} | awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT} | awk -F- '{print $1}') ]; then
+ rc=1
else
- rc=0
+ rc=0
fi
if [ ${rc} -eq 0 ]; then
run_script "upgrade" "${sqlName}" "${sqlFile}" "${PREVIOUS_RELEASE}" "${schemaVersion}" "${tag}"
@@ -465,8 +446,7 @@ function upgrade
# downgrade
#####################################################
-function downgrade
-{
+function downgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- downgrade --"
set -x
@@ -478,19 +458,19 @@ function downgrade
echo "downgrade: ${CURRENT_RELEASE} -> ${TARGET_DOWNGRADE_RELEASE}"
if [ ${CURRENT_RELEASE} \> ${TARGET_DOWNGRADE_RELEASE} ]; then
- downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2> /dev/null)
+ downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2>/dev/null)
for sqlFile in ${downgradeSqls}; do
sqlName=$(basename "${sqlFile}")
schemaVersion="$(basename $(dirname $(dirname $sqlFile)))"
- if [ "${schemaVersion}" -le "${CURRENT_RELEASE}" ] && \
+ if [ "${schemaVersion}" -le "${CURRENT_RELEASE}" ] &&
[ "${schemaVersion}" -gt "${TARGET_DOWNGRADE_RELEASE}" ]; then
tag=${tagDate}"${schemaVersion}d"
- if [ $RETRY -eq 1 ] && \
+ if [ $RETRY -eq 1 ] &&
# Compare the numeric portion of the filename because shell doesn't support string comparison
- [ $(echo ${sqlName}| awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT}| awk -F- '{print $1}') ]; then
- rc=1
+ [ $(echo ${sqlName} | awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT} | awk -F- '{print $1}') ]; then
+ rc=1
else
- rc=0
+ rc=0
fi
if [ ${rc} -eq 0 ]; then
run_script "downgrade" "${sqlName}" "${sqlFile}" "${schemaVersion}" "${PREVIOUS_RELEASE}" ${tag}
@@ -514,8 +494,7 @@ function downgrade
# erase
#####################################################
-function erase
-{
+function erase() {
if [ "${DEBUG}" = "y" ]; then
echo "-- erase --"
set -x
@@ -527,7 +506,7 @@ function erase
local deleteHistory="DELETE FROM ${METADATA_HISTORY};"
${MYSQL} --execute "${deleteHistory}"
- local dropDB="DROP DATABASE IF EXISTS ${SCHEMA_DB}";
+ local dropDB="DROP DATABASE IF EXISTS ${SCHEMA_DB}"
${MYSQL} --execute "${dropDB}"
}
@@ -535,8 +514,7 @@ function erase
# report
#####################################################
-function report
-{
+function report() {
if [ "${DEBUG}" = "y" ]; then
echo "-- report --"
set -x
@@ -551,8 +529,7 @@ function report
okay
}
-function okay
-{
+function okay() {
if [ "${DEBUG}" = "y" ]; then
echo "-- okay --"
set -x
@@ -588,40 +565,47 @@ if [ "${DEBUG}" = "y" ]; then
fi
until [ -z "$1" ]; do
case $1 in
- -s|--schema|--database) shift
- SCHEMA=$1
- ;;
- -b|--basedir) shift
- MIGRATION_DIR=$1
- ;;
- -t|--target) shift
- INPUT_TARGET_RELEASE=$1
- ;;
- -f|--from) shift
- INPUT_FROM_RELEASE=$1
- ;;
- -o|--operation) shift
- OPERATION=$1
- ;;
- *) usage
- exit 1
- ;;
+ -s | --schema | --database)
+ shift
+ SCHEMA=$1
+ ;;
+ -b | --basedir)
+ shift
+ MIGRATION_DIR=$1
+ ;;
+ -t | --target)
+ shift
+ INPUT_TARGET_RELEASE=$1
+ ;;
+ -f | --from)
+ shift
+ INPUT_FROM_RELEASE=$1
+ ;;
+ -o | --operation)
+ shift
+ OPERATION=$1
+ ;;
+ *)
+ usage
+ exit 1
+ ;;
esac
shift
done
case ${OPERATION} in
- upgrade) ;;
- downgrade) ;;
- auto) ;;
- version) ;;
- erase) ;;
- report) ;;
- ok) ;;
- *) echo "error: invalid operation provided"
- usage
- exit 1
- ;;
+upgrade) ;;
+downgrade) ;;
+auto) ;;
+version) ;;
+erase) ;;
+report) ;;
+ok) ;;
+*)
+ echo "error: invalid operation provided"
+ usage
+ exit 1
+ ;;
esac
if [ -z "${SCHEMA}" ]; then
@@ -637,8 +621,8 @@ if [ -z "${SQL_HOST}" ] || [ -z "${SQL_USER}" ] || [ -z "${SQL_PASSWORD}" ]; the
exit 4
fi
-MYSQL="mysql -u${SQL_USER} -p${SQL_PASSWORD} -h ${SQL_HOST}";
-if ! ${MYSQL} -h"${SQL_HOST}" --execute "show databases;" > /dev/null 2>&1; then
+MYSQL="mysql -u${SQL_USER} -p${SQL_PASSWORD} -h ${SQL_HOST}"
+if ! ${MYSQL} -h"${SQL_HOST}" --execute "show databases;" >/dev/null 2>&1; then
echo "error: No DB connectivity to ${SQL_HOST} for ${SQL_USER}"
exit 5
fi
@@ -647,7 +631,7 @@ if [ "${SCHEMA}" = "ALL" ]; then
SCHEMA="*"
fi
-SCHEMA_S=$(ls -d "${MIGRATION_DIR}"/${SCHEMA}/ 2> /dev/null)
+SCHEMA_S=$(ls -d "${MIGRATION_DIR}"/${SCHEMA}/ 2>/dev/null)
if [ -z "${SCHEMA_S}" ]; then
echo "error: no databases available"
exit 0
@@ -668,7 +652,7 @@ for dbPath in ${SCHEMA_S}; do
TARGET_RELEASE=${INPUT_TARGET_RELEASE}
if is_upgrade && is_downgrade; then
- echo "${SCHEMA}: failure: invalid configuration: ${UPGRADE_SQL_SUFFIX} and "\
+ echo "${SCHEMA}: failure: invalid configuration: ${UPGRADE_SQL_SUFFIX} and " \
"${DOWNGRADE_SQL_SUFFIX} exist under ${DOWNGRADE_DIR}"
rc=1
continue
@@ -714,29 +698,29 @@ for dbPath in ${SCHEMA_S}; do
last_operation_status
last_status=$?
if [ $last_status -eq 0 ]; then
- echo "Partial $LAST_OPERATION detected"
- RETRY=1
- if [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'downgrade' ]; then
- CURRENT_RELEASE="${LAST_FROM_VERSION}"
- # greater than
- COMPARE="gt"
- echo "Downgrade will attempt to resume at $LAST_SCRIPT"
- elif [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
- CURRENT_RELEASE="${LAST_TO_VERSION}"
- # greater than or equal to
- COMPARE="ge"
- echo "Downgrade will attempt to resume before $LAST_SCRIPT"
- elif [ "${OPERATION}" == 'upgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
- CURRENT_RELEASE="${LAST_FROM_VERSION}"
- # less than
- COMPARE="lt"
- echo "Upgrade will attempt to resume at $LAST_SCRIPT"
- else
- CURRENT_RELEASE="${LAST_TO_VERSION}"
- # less than or equal
- COMPARE="le"
- echo "Upgrade will attempt to resume after $LAST_SCRIPT"
- fi
+ echo "Partial $LAST_OPERATION detected"
+ RETRY=1
+ if [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'downgrade' ]; then
+ CURRENT_RELEASE="${LAST_FROM_VERSION}"
+ # greater than
+ COMPARE="gt"
+ echo "Downgrade will attempt to resume at $LAST_SCRIPT"
+ elif [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
+ CURRENT_RELEASE="${LAST_TO_VERSION}"
+ # greater than or equal to
+ COMPARE="ge"
+ echo "Downgrade will attempt to resume before $LAST_SCRIPT"
+ elif [ "${OPERATION}" == 'upgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
+ CURRENT_RELEASE="${LAST_FROM_VERSION}"
+ # less than
+ COMPARE="lt"
+ echo "Upgrade will attempt to resume at $LAST_SCRIPT"
+ else
+ CURRENT_RELEASE="${LAST_TO_VERSION}"
+ # less than or equal
+ COMPARE="le"
+ echo "Upgrade will attempt to resume after $LAST_SCRIPT"
+ fi
fi
if [ -n "${INPUT_FROM_RELEASE}" ]; then
@@ -749,29 +733,35 @@ for dbPath in ${SCHEMA_S}; do
fi
case ${OPERATION} in
- upgrade) if upgrade; then
- echo "${SCHEMA}: OK: upgrade (${CURRENT_RELEASE})"
- else
- rc=1
- echo "${SCHEMA}: failure: upgrade to release ${TARGET_UPGRADE_RELEASE} (${CURRENT_RELEASE})"
- fi
- ;;
- downgrade) if downgrade; then
- echo "${SCHEMA}: OK: downgrade (${CURRENT_RELEASE})"
- else
- rc=1
- echo "${SCHEMA}: failure: downgrade to release ${TARGET_DOWNGRADE_RELEASE} (${CURRENT_RELEASE})"
- fi
- ;;
- version) echo "${SCHEMA}: ${CURRENT_RELEASE}"
- ;;
- erase) erase
- ;;
- report) report
- ;;
- ok) okay
- ;;
- esac
+ upgrade)
+ if upgrade; then
+ echo "${SCHEMA}: OK: upgrade (${CURRENT_RELEASE})"
+ else
+ rc=1
+ echo "${SCHEMA}: failure: upgrade to release ${TARGET_UPGRADE_RELEASE} (${CURRENT_RELEASE})"
+ fi
+ ;;
+ downgrade)
+ if downgrade; then
+ echo "${SCHEMA}: OK: downgrade (${CURRENT_RELEASE})"
+ else
+ rc=1
+ echo "${SCHEMA}: failure: downgrade to release ${TARGET_DOWNGRADE_RELEASE} (${CURRENT_RELEASE})"
+ fi
+ ;;
+ version)
+ echo "${SCHEMA}: ${CURRENT_RELEASE}"
+ ;;
+ erase)
+ erase
+ ;;
+ report)
+ report
+ ;;
+ ok)
+ okay
+ ;;
+ esac
done
exit $rc
diff --git a/policy-db-migrator/src/main/docker/db-migrator-pg b/policy-db-migrator/src/main/docker/db-migrator-pg
index a0ca2265..607a86fa 100755
--- a/policy-db-migrator/src/main/docker/db-migrator-pg
+++ b/policy-db-migrator/src/main/docker/db-migrator-pg
@@ -101,8 +101,7 @@ function usage() {
# current_schema
#####################################################
-function current_schema
-{
+function current_schema() {
if [ "${DEBUG}" = "y" ]; then
echo "-- current_schema --"
set -x
@@ -125,8 +124,7 @@ function current_schema
# ensure global metadata
#####################################################
-function ensure_metadata
-{
+function ensure_metadata() {
if [ "${DEBUG}" = "y" ]; then
echo "-- ensure_metadata --"
set -x
@@ -149,13 +147,11 @@ function ensure_metadata
return $?
}
-
#####################################################
# ensure metadata on a per schema basis
#####################################################
-function ensure_metadata_schema
-{
+function ensure_metadata_schema() {
if [ "${DEBUG}" = "y" ]; then
echo "-- ensure_metadata_schema --"
set -x
@@ -179,13 +175,11 @@ function ensure_metadata_schema
return $?
}
-
#####################################################
# target_release
#####################################################
-function target_release
-{
+function target_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- target_release --"
set -x
@@ -196,7 +190,7 @@ function target_release
TARGET_UPGRADE_RELEASE=${ZERO_VERSION}
TARGET_DOWNGRADE_RELEASE=${ZERO_VERSION}
- upgradeSqls=$(ls -v -r "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v -r "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
for sql in ${upgradeSqls}; do
TARGET_UPGRADE_RELEASE="$(basename $(dirname $(dirname $sql)))"
break
@@ -210,8 +204,7 @@ function target_release
# is_upgrade
#####################################################
-function is_upgrade
-{
+function is_upgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- is_upgrade --"
set -x
@@ -219,7 +212,7 @@ function is_upgrade
local upgradeSqls
- upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
if [ -z "${upgradeSqls}" ]; then
return 1
else
@@ -227,13 +220,11 @@ function is_upgrade
fi
}
-
#####################################################
# is_downgrade
#####################################################
-function is_downgrade
-{
+function is_downgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- is_downgrade --"
set -x
@@ -241,7 +232,7 @@ function is_downgrade
local downgradeSqls
- downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2> /dev/null)
+ downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2>/dev/null)
if [ -z "${downgradeSqls}" ]; then
return 1
else
@@ -249,36 +240,33 @@ function is_downgrade
fi
}
-
#####################################################
# set_current_release
#####################################################
-function set_current_release
-{
- if [ "${DEBUG}" = "y" ]; then
- echo "-- set_current_release --"
- set -x
- fi
+function set_current_release() {
+ if [ "${DEBUG}" = "y" ]; then
+ echo "-- set_current_release --"
+ set -x
+ fi
- CURRENT_RELEASE="${1}"
+ CURRENT_RELEASE="${1}"
- local sql
- sql="INSERT INTO ${METADATA_TABLE} (name, version) "
- sql=${sql}"VALUES('${SCHEMA}', '${CURRENT_RELEASE}') "
- sql=${sql}"ON CONFLICT (name) DO UPDATE SET version='${CURRENT_RELEASE}';"
+ local sql
+ sql="INSERT INTO ${METADATA_TABLE} (name, version) "
+ sql=${sql}"VALUES('${SCHEMA}', '${CURRENT_RELEASE}') "
+ sql=${sql}"ON CONFLICT (name) DO UPDATE SET version='${CURRENT_RELEASE}';"
- PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${METADATA_DB}";
- ${PSQL} --command "${sql}"
- return $?
+ PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${METADATA_DB}"
+ ${PSQL} --command "${sql}"
+ return $?
}
#####################################################
# current_release
#####################################################
-function current_release
-{
+function current_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- current_release --"
set -x
@@ -302,8 +290,7 @@ function current_release
# previous_release
#####################################################
-function previous_release
-{
+function previous_release() {
if [ "${DEBUG}" = "y" ]; then
echo "-- previous_release --"
set -x
@@ -312,28 +299,26 @@ function previous_release
local current_release_int_val previous_release_int_val previous_release
if [ $current_release == $ZERO_VERSION ] || [ $current_release == $BASE_VERSION ]; then
- PREVIOUS_RELEASE=$ZERO_VERSION
+ PREVIOUS_RELEASE=$ZERO_VERSION
else
- current_release_int_val=$(echo $current_release | awk '{$0=int($0)}1')
- let previous_release_int_val=$current_release_int_val-100
- if [ $previous_release_int_val -lt 1000 ]; then
- previous_release="0"$previous_release_int_val
- else
- previous_release=$previous_release_int_val
- fi
- PREVIOUS_RELEASE=$previous_release
+ current_release_int_val=$(echo $current_release | awk '{$0=int($0)}1')
+ let previous_release_int_val=$current_release_int_val-100
+ if [ $previous_release_int_val -lt 1000 ]; then
+ previous_release="0"$previous_release_int_val
+ else
+ previous_release=$previous_release_int_val
+ fi
+ PREVIOUS_RELEASE=$previous_release
fi
return 0
}
-
#####################################################
# last_operation_status
#####################################################
-function last_operation_status
-{
+function last_operation_status() {
if [ "${DEBUG}" = "y" ]; then
echo "-- last_operation_status --"
set -x
@@ -360,15 +345,14 @@ function last_operation_status
rc=1
fi
- return $rc;
+ return $rc
}
#####################################################
# execute sql script history
#####################################################
-function track_script
-{
+function track_script() {
if [ "${DEBUG}" = "y" ]; then
echo "-- track_script $* --"
set -x
@@ -376,24 +360,22 @@ function track_script
local script="${1}" operation="${2}" success="${3}" from_version="${4}" to_version="${5}" tag="${6}"
if [ $operation == "downgrade" ]; then
- to_version=${TARGET_DOWNGRADE_RELEASE}
+ to_version=${TARGET_DOWNGRADE_RELEASE}
fi
local sql="INSERT INTO ${METADATA_HISTORY}(script,operation,from_version,to_version,tag,success,atTime) "
sql=${sql}"VALUES ('${script}','${operation}','${from_version}','${to_version}','${tag}','${success}',now()) "
- PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${METADATA_DB}";
+ PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${METADATA_DB}"
${PSQL} --command "${sql}"
return $?
}
-
#####################################################
# execute sql script
#####################################################
-function run_script
-{
+function run_script() {
if [ "${DEBUG}" == "y" ]; then
echo "-- run_script $* --"
set -x
@@ -405,7 +387,7 @@ function run_script
echo "> ${operation} ${script}"
PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${SQL_DB}"
- ${PSQL} ${SCHEMA} < "${scriptPath}"
+ ${PSQL} <"${scriptPath}"
local rc=$?
if [ ${rc} -ne 0 ]; then
success="0"
@@ -422,8 +404,7 @@ function run_script
# upgrade
#####################################################
-function upgrade
-{
+function upgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- upgrade --"
set -x
@@ -435,22 +416,22 @@ function upgrade
echo "upgrade: ${CURRENT_RELEASE} -> ${TARGET_UPGRADE_RELEASE}"
if [ ${CURRENT_RELEASE} \< ${TARGET_UPGRADE_RELEASE} ]; then
- upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2> /dev/null)
+ upgradeSqls=$(ls -v "${UPGRADE_DIR}"/*/upgrade/*.sql 2>/dev/null)
for sqlFile in ${upgradeSqls}; do
sqlName=$(basename "${sqlFile}")
schemaVersion="$(basename $(dirname $(dirname $sqlFile)))"
previous_release $schemaVersion
- if [ "${schemaVersion}" -gt "${CURRENT_RELEASE}" ] && \
+ if [ "${schemaVersion}" -gt "${CURRENT_RELEASE}" ] &&
[ "${schemaVersion}" -le "${TARGET_UPGRADE_RELEASE}" ]; then
tag=${tagDate}"${schemaVersion}u"
- if [ $RETRY -eq 1 ] && \
+ if [ $RETRY -eq 1 ] &&
# Compare the numeric portion of the filename because shell doesn't support string comparison
- [ $(echo ${sqlName}| awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT}| awk -F- '{print $1}') ]; then
- rc=1
- echo "rc=1"
+ [ $(echo ${sqlName} | awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT} | awk -F- '{print $1}') ]; then
+ rc=1
+ echo "rc=1"
else
- rc=0
- echo "rc=0"
+ rc=0
+ echo "rc=0"
fi
if [ ${rc} -eq 0 ]; then
run_script "upgrade" "${sqlName}" "${sqlFile}" "${PREVIOUS_RELEASE}" "${schemaVersion}" "${tag}"
@@ -474,8 +455,7 @@ function upgrade
# downgrade
#####################################################
-function downgrade
-{
+function downgrade() {
if [ "${DEBUG}" = "y" ]; then
echo "-- downgrade --"
set -x
@@ -487,19 +467,19 @@ function downgrade
echo "downgrade: ${CURRENT_RELEASE} -> ${TARGET_DOWNGRADE_RELEASE}"
if [ ${CURRENT_RELEASE} \> ${TARGET_DOWNGRADE_RELEASE} ]; then
- downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2> /dev/null)
+ downgradeSqls=$(ls -v -r "${DOWNGRADE_DIR}"/*/downgrade/*.sql 2>/dev/null)
for sqlFile in ${downgradeSqls}; do
sqlName=$(basename "${sqlFile}")
schemaVersion="$(basename $(dirname $(dirname $sqlFile)))"
- if [ "${schemaVersion}" -le "${CURRENT_RELEASE}" ] && \
+ if [ "${schemaVersion}" -le "${CURRENT_RELEASE}" ] &&
[ "${schemaVersion}" -gt "${TARGET_DOWNGRADE_RELEASE}" ]; then
tag=${tagDate}"${schemaVersion}d"
- if [ $RETRY -eq 1 ] && \
+ if [ $RETRY -eq 1 ] &&
# Compare the numeric portion of the filename because shell doesn't support string comparison
- [ $(echo ${sqlName}| awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT}| awk -F- '{print $1}') ]; then
- rc=1
+ [ $(echo ${sqlName} | awk -F- '{print $1}') -${COMPARE} $(echo ${LAST_SCRIPT} | awk -F- '{print $1}') ]; then
+ rc=1
else
- rc=0
+ rc=0
fi
if [ ${rc} -eq 0 ]; then
run_script "downgrade" "${sqlName}" "${sqlFile}" "${schemaVersion}" "${PREVIOUS_RELEASE}" ${tag}
@@ -523,8 +503,7 @@ function downgrade
# erase
#####################################################
-function erase
-{
+function erase() {
if [ "${DEBUG}" = "y" ]; then
echo "-- erase --"
set -x
@@ -537,7 +516,7 @@ function erase
local deleteHistory="DELETE FROM ${METADATA_HISTORY};"
${PSQL} --command "${deleteHistory}"
- local dropDB="DROP DATABASE IF EXISTS ${SCHEMA_DB}";
+ local dropDB="DROP DATABASE IF EXISTS ${SCHEMA_DB}"
${PSQL} --command "${dropDB}"
}
@@ -545,8 +524,7 @@ function erase
# report
#####################################################
-function report
-{
+function report() {
if [ "${DEBUG}" = "y" ]; then
echo "-- report --"
set -x
@@ -562,8 +540,7 @@ function report
okay
}
-function okay
-{
+function okay() {
if [ "${DEBUG}" = "y" ]; then
echo "-- okay --"
set -x
@@ -599,40 +576,47 @@ if [ "${DEBUG}" = "y" ]; then
fi
until [ -z "$1" ]; do
case $1 in
- -s|--schema|--database) shift
- SCHEMA=$1
- ;;
- -b|--basedir) shift
- MIGRATION_DIR=$1
- ;;
- -t|--target) shift
- INPUT_TARGET_RELEASE=$1
- ;;
- -f|--from) shift
- INPUT_FROM_RELEASE=$1
- ;;
- -o|--operation) shift
- OPERATION=$1
- ;;
- *) usage
- exit 1
- ;;
+ -s | --schema | --database)
+ shift
+ SCHEMA=$1
+ ;;
+ -b | --basedir)
+ shift
+ MIGRATION_DIR=$1
+ ;;
+ -t | --target)
+ shift
+ INPUT_TARGET_RELEASE=$1
+ ;;
+ -f | --from)
+ shift
+ INPUT_FROM_RELEASE=$1
+ ;;
+ -o | --operation)
+ shift
+ OPERATION=$1
+ ;;
+ *)
+ usage
+ exit 1
+ ;;
esac
shift
done
case ${OPERATION} in
- upgrade) ;;
- downgrade) ;;
- auto) ;;
- version) ;;
- erase) ;;
- report) ;;
- ok) ;;
- *) echo "error: invalid operation provided"
- usage
- exit 1
- ;;
+ upgrade) ;;
+ downgrade) ;;
+ auto) ;;
+ version) ;;
+ erase) ;;
+ report) ;;
+ ok) ;;
+ *)
+ echo "error: invalid operation provided"
+ usage
+ exit 1
+ ;;
esac
if [ -z "${SCHEMA}" ]; then
@@ -648,7 +632,7 @@ if [ -z "${SQL_HOST}" ] || [ -z "${SQL_USER}" ] || [ -z "${SQL_PASSWORD}" ]; the
exit 4
fi
-PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${SQL_DB}";
+PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${SQL_DB}"
if ! ${PSQL} --command '\l'; then
echo "error: No DB connectivity to ${SQL_HOST} for ${SQL_USER}"
exit 5
@@ -658,7 +642,7 @@ if [ "${SCHEMA}" = "ALL" ]; then
SCHEMA="*"
fi
-SCHEMA_S=$(ls -d "${MIGRATION_DIR}"/${SCHEMA}/ 2> /dev/null)
+SCHEMA_S=$(ls -d "${MIGRATION_DIR}"/${SCHEMA}/ 2>/dev/null)
if [ -z "${SCHEMA_S}" ]; then
echo "error: no databases available"
exit 0
@@ -679,7 +663,7 @@ for dbPath in ${SCHEMA_S}; do
TARGET_RELEASE=${INPUT_TARGET_RELEASE}
if is_upgrade && is_downgrade; then
- echo "${SCHEMA}: failure: invalid configuration: ${UPGRADE_SQL_SUFFIX} and "\
+ echo "${SCHEMA}: failure: invalid configuration: ${UPGRADE_SQL_SUFFIX} and " \
"${DOWNGRADE_SQL_SUFFIX} exist under ${DOWNGRADE_DIR}"
rc=1
continue
@@ -718,7 +702,7 @@ for dbPath in ${SCHEMA_S}; do
fi
fi
- PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${SQL_DB}";
+ PSQL="psql -h ${SQL_HOST} -U ${SQL_USER} -d ${SQL_DB};"
# Check if the schema has already been installed
current_schema
RETRY=0
@@ -726,29 +710,29 @@ for dbPath in ${SCHEMA_S}; do
last_operation_status
last_status=$?
if [ $last_status -eq 0 ]; then
- echo "Partial $LAST_OPERATION detected"
- RETRY=1
- if [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'downgrade' ]; then
- CURRENT_RELEASE="${LAST_FROM_VERSION}"
- # greater than
- COMPARE="gt"
- echo "Downgrade will attempt to resume at $LAST_SCRIPT"
- elif [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
- CURRENT_RELEASE="${LAST_TO_VERSION}"
- # greater than or equal to
- COMPARE="ge"
- echo "Downgrade will attempt to resume before $LAST_SCRIPT"
- elif [ "${OPERATION}" == 'upgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
- CURRENT_RELEASE="${LAST_FROM_VERSION}"
- # less than
- COMPARE="lt"
- echo "Upgrade will attempt to resume at $LAST_SCRIPT"
- else
- CURRENT_RELEASE="${LAST_TO_VERSION}"
- # less than or equal
- COMPARE="le"
- echo "Upgrade will attempt to resume after $LAST_SCRIPT"
- fi
+ echo "Partial $LAST_OPERATION detected"
+ RETRY=1
+ if [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'downgrade' ]; then
+ CURRENT_RELEASE="${LAST_FROM_VERSION}"
+ # greater than
+ COMPARE="gt"
+ echo "Downgrade will attempt to resume at $LAST_SCRIPT"
+ elif [ "${OPERATION}" == 'downgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
+ CURRENT_RELEASE="${LAST_TO_VERSION}"
+ # greater than or equal to
+ COMPARE="ge"
+ echo "Downgrade will attempt to resume before $LAST_SCRIPT"
+ elif [ "${OPERATION}" == 'upgrade' ] && [ "${LAST_OPERATION}" == 'upgrade' ]; then
+ CURRENT_RELEASE="${LAST_FROM_VERSION}"
+ # less than
+ COMPARE="lt"
+ echo "Upgrade will attempt to resume at $LAST_SCRIPT"
+ else
+ CURRENT_RELEASE="${LAST_TO_VERSION}"
+ # less than or equal
+ COMPARE="le"
+ echo "Upgrade will attempt to resume after $LAST_SCRIPT"
+ fi
fi
if [ -n "${INPUT_FROM_RELEASE}" ]; then
@@ -761,29 +745,35 @@ for dbPath in ${SCHEMA_S}; do
fi
case ${OPERATION} in
- upgrade) if upgrade; then
- echo "${SCHEMA}: OK: upgrade (${CURRENT_RELEASE})"
- else
- rc=1
- echo "${SCHEMA}: failure: upgrade to release ${TARGET_UPGRADE_RELEASE} (${CURRENT_RELEASE})"
- fi
- ;;
- downgrade) if downgrade; then
- echo "${SCHEMA}: OK: downgrade (${CURRENT_RELEASE})"
- else
- rc=1
- echo "${SCHEMA}: failure: downgrade to release ${TARGET_DOWNGRADE_RELEASE} (${CURRENT_RELEASE})"
- fi
- ;;
- version) echo "${SCHEMA}: ${CURRENT_RELEASE}"
- ;;
- erase) erase
- ;;
- report) report
- ;;
- ok) okay
- ;;
- esac
+ upgrade)
+ if upgrade; then
+ echo "${SCHEMA}: OK: upgrade (${CURRENT_RELEASE})"
+ else
+ rc=1
+ echo "${SCHEMA}: failure: upgrade to release ${TARGET_UPGRADE_RELEASE} (${CURRENT_RELEASE})"
+ fi
+ ;;
+ downgrade)
+ if downgrade; then
+ echo "${SCHEMA}: OK: downgrade (${CURRENT_RELEASE})"
+ else
+ rc=1
+ echo "${SCHEMA}: failure: downgrade to release ${TARGET_DOWNGRADE_RELEASE} (${CURRENT_RELEASE})"
+ fi
+ ;;
+ version)
+ echo "${SCHEMA}: ${CURRENT_RELEASE}"
+ ;;
+ erase)
+ erase
+ ;;
+ report)
+ report
+ ;;
+ ok)
+ okay
+ ;;
+ esac
done
exit $rc