2 # ============LICENSE_START=======================================================
4 # ================================================================================
5 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
21 # db_upgrade.sh: Run this script to upgrade database to a given release level, it is recommanded switch to policy user to run this script
23 # Usage : db_upgrade.sh target_db_release_level db_user_id db_user_password
24 # Example: db_upgrade.sh 151000 policy_user password
26 # Assumption: 1. DB upgrade sql script in $POLICY_HOME/data/mysql folder with read permission
27 # 2. DB user has privilege to create/drop/alter database table
29 # Note: The default location for db release script is $POLICY_HOME/data/mysql
30 # The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001)
37 DB_UPGRADE_PASSWORD=""
38 DB_UPGRADE_DIR=$POLICY_HOME/data/mysql
39 DATE=`date +"%Y%m%d%H%M%S"`
43 function get_current_release_level
45 echo "Get current release level started ...@`date`" | tee -a $LOG
46 # display output vertical
47 query="select version from support.db_version where the_key = 'VERSION' \G"
48 CURRENT_RELEASE=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"`
49 echo "CURRENT_RELEASE: [$CURRENT_RELEASE]" | tee -a $LOG
50 echo "Get current release level completed ...@`date`" | tee -a $LOG
53 function evaluate_upgrade_downgrade
55 echo "CURRENT_RELEASE --> [$CURRENT_RELEASE]" | tee -a $LOG
56 echo "TARGET_RELEASE --> [$TARGET_RELEASE] " | tee -a $LOG
57 if [[ "${CURRENT_RELEASE}" < "${TARGET_RELEASE}" ]]; then
59 UPGRADE_LIST=/tmp/db_upgrade_list.$$
60 find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort > $UPGRADE_LIST
63 RELEASE=`basename $file | cut -d'_' -f1`
64 #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG
65 if [ "${RELEASE}" -gt "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -le "${TARGET_RELEASE}" ]; then
66 run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG
70 set_current_release_level $TARGET_RELEASE
71 elif [[ "${CURRENT_RELEASE}" > "${TARGET_RELEASE}" ]]; then
72 # perform db downgrade
73 DOWNGRADE_LIST=/tmp/db_downgrade_list.$$
74 find ${DB_UPGRADE_DIR} -name "*_downgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort -r > $DOWNGRADE_LIST
77 RELEASE=`basename $file | cut -d'_' -f1`
78 #echo "[$RELEASE] [$TARGET_RELEASE]" | tee -a $LOG
79 if [ "${RELEASE}" -le "${CURRENT_RELEASE}" ] && [ "${RELEASE}" -gt "${TARGET_RELEASE}" ]; then
80 run_script "DOWNGRADE" "${file}"
82 done < $DOWNGRADE_LIST
84 set_current_release_level $TARGET_RELEASE
86 echo "CURRENT DB RELEASE LEVEL THE SAME AS TARGET RELEASE LEVEL, NO ACTION TAKEN ..." | tee -a $LOG
94 echo "Perform DB $action use $script ..." | tee -a $LOG
95 echo "--" | tee -a $LOG
96 ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG
97 echo "--" | tee -a $LOG
100 function set_current_release_level
103 echo "Set current release level to [$RELEASE] started ...@`date`" | tee -a $LOG
104 update_statement="insert into support.db_version (the_key, version) values ('VERSION', '${RELEASE}') on duplicate key update version='${RELEASE}';"
105 ${MYSQL} --execute "${update_statement}"
107 echo "" | tee -a $LOG
108 echo "CURRENT_RELEASE set to: [$RELEASE]" | tee -a $LOG
109 echo "" | tee -a $LOG
110 echo "Set current release level completed ...@`date`" | tee -a $LOG
113 function check_directory
115 if [ ! -d $DB_UPGRADE_DIR ]; then
116 echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..."
119 if [ ! -d $DB_UPGRADE_DIR/logs ]; then
120 mkdir $DB_UPGRADE_DIR/logs
127 LOG=$POLICY_HOME/logs/db_upgrade_$DATE.log
128 ERR=$POLICY_HOME/logs/db_upgrade_$DATE.err
129 echo "db_upgrade.sh started ..." | tee -a $LOG
130 if [ $# -eq 3 ]; then
131 TARGET_RELEASE="${1}"
132 DB_UPGRADE_USER="${2}"
133 DB_UPGRADE_PASSWORD="${3}"
134 echo "TARGET_RELEASE : $TARGET_RELEASE" | tee -a $LOG
135 echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG
136 echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG
138 if [ ${#TARGET_RELEASE} -ne 6 ]; then
139 echo "ERROR, TARGET_RELEASE MUST BE 6 DIGITS: $TARGET_RELEASE" | tee -a $LOG | tee -a $ERR
141 typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} ";
142 get_current_release_level
143 evaluate_upgrade_downgrade
146 echo "Usage : db_upgrade.sh target_release_level db_user_id db_user_password" | tee -a $LOG
147 echo "Example: db_upgrade.sh 151000 policy_user password" | tee -a $LOG
150 echo "db_upgrade.sh completed ..." | tee -a $LOG