3 # ============LICENSE_START=======================================================
5 # ================================================================================
6 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
23 # db_upgrade_remote.sh: This script is to perform database schema upgrade on remote db,
24 # no shecma downgrade will be performed in case db_version is higher then target_version
26 # Logic: 1. Get target schema version from db scripts in $POLICY_HOME/data/mysql
27 # 2. Get current db schema version from support.db_version table (of target system)
28 # 3. Apply db upgrade script in order if target_version is HIGHER than db_version
29 # 4. Print out warning message if target_version is LOWER than db_version
30 # 4. Print out message if target_version is EQUAL to db_version
33 # Usage : db_upgrade_remote.sh db_user_id db_user_password hostname
34 # Example: db_upgrade_remote.sh policy_user password localhost.com
36 # Assumption: 1. DB schema upgrade script in $POLICY_HOME/data/mysql folder with read permission
37 # 2. DB user has privilege to create/drop/alter database table
39 # Note: The default location for db schema upgrade script is $POLICY_HOME/data/mysql
40 # The release level is represented as Two-digit-Year+Two-digit-Month+two-digit-Sub-release (151000, 151001)
44 TARGET_SCHEMA_VERSION=""
45 CURRENT_SCHEMA_VERSION=""
47 DB_UPGRADE_PASSWORD=""
49 DB_UPGRADE_DIR=$POLICY_HOME/data/mysql
50 DATE=`date +"%Y%m%d%H%M%S"`
54 function get_current_schema_version
56 echo "Get current schema version from [${DB_HOSTNAME}] started ...@`date`" | tee -a $LOG
57 # display output vertical
58 query="select version from support.db_version where the_key = 'VERSION' \G"
59 CURRENT_SCHEMA_VERSION=`${MYSQL} --skip-column-names --execute "${query}" 2>$ERR | grep -v "*"`
60 error_msg=`cat $ERR | grep "doesn't exist"`
61 if [ "${error_msg}" != "" ]; then
62 echo "Create support.db_version table ..." | tee -a $LOG
63 sql="create database if not exists support;"
64 ${MYSQL} --execute "${sql}"
65 sql="create table support.db_version(the_key varchar(20) not null, version varchar(20), primary key(the_key));"
66 ${MYSQL} --execute "${sql}"
67 CURRENT_SCHEMA_VERSION="00"
69 echo "CURRENT_SCHEMA_VERSION: [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG
70 echo "Get current schema version from [${DB_HOSTNAME}] completed ...@`date`" | tee -a $LOG
73 function get_target_schema_version
75 UPGRADE_LIST=/tmp/db_upgrade_list.$$
76 find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort -r | head -1 > $UPGRADE_LIST
79 TARGET_SCHEMA_VERSION=`basename $file | cut -d'_' -f1`
80 echo "TARGET_SCHEMA_VERSION: [$TARGET_SCHEMA_VERSION]" | tee -a $LOG
86 function evaluate_upgrade_downgrade
88 echo "CURRENT_SCHEMA_VERSION --> [$CURRENT_SCHEMA_VERSION]" | tee -a $LOG
89 echo "TARGET_SCHEMA_VERSION --> [$TARGET_SCHEMA_VERSION] " | tee -a $LOG
90 if [[ "${CURRENT_SCHEMA_VERSION}" < "${TARGET_SCHEMA_VERSION}" ]]; then
92 UPGRADE_LIST=/tmp/db_upgrade_list.$$
93 find ${DB_UPGRADE_DIR} -name "*_upgrade_script.sql" 2>/dev/null | grep -v "droolspdp" | sort > $UPGRADE_LIST
96 DB_VERSION=`basename $file | cut -d'_' -f1`
97 #echo "[$DB_VERSION] [$TARGET_SCHEMA_VERSION]" | tee -a $LOG
98 if [ "${DB_VERSION}" -gt "${CURRENT_SCHEMA_VERSION}" ] && [ "${DB_VERSION}" -le "${TARGET_SCHEMA_VERSION}" ]; then
99 run_script "UPGRADE" "${file}" 2>&1 | tee -a $LOG
103 set_current_release_level $TARGET_SCHEMA_VERSION
104 elif [[ "${CURRENT_SCHEMA_VERSION}" > "${TARGET_SCHEMA_VERSION}" ]]; then
106 echo "WARNING: Target db schema version is LOWER than current db scema version, please run downgrade script manually." | tee -a $LOG | tee -a $ERR
108 echo "CURRENT SCHEMA VERSION THE SAME AS TARGET SCHEMA VERSION, NO ACTION TAKEN ..." | tee -a $LOG
116 echo "Perform DB $action on [${DB_HOSTNAME}] use $script ..." | tee -a $LOG
117 echo "--" | tee -a $LOG
118 ${MYSQL} --verbose < "${script}" 2>$ERR | tee -a $LOG
119 echo "--" | tee -a $LOG
122 function set_current_release_level
125 echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] started ...@`date`" | tee -a $LOG
126 update_statement="insert into support.db_version (the_key, version) values ('VERSION', '${DB_VERSION}') on duplicate key update version='${DB_VERSION}';"
127 ${MYSQL} --execute "${update_statement}"
129 echo "" | tee -a $LOG
130 echo "CURRENT_SCHEMA_VERSION set to: [$DB_VERSION]" | tee -a $LOG
131 echo "" | tee -a $LOG
132 echo "Set current release level on [${DB_HOSTNAME}] to [$DB_VERSION] completed ...@`date`" | tee -a $LOG
135 function check_directory
137 if [ ! -d $DB_UPGRADE_DIR ]; then
138 echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..."
141 if [ ! -d $DB_UPGRADE_DIR/logs ]; then
142 mkdir $DB_UPGRADE_DIR/logs
149 if [ -z ${POLICY_LOGS} ]; then
150 POLICY_LOGS=/var/log/onap
152 mkdir -p $POLICY_LOGS/policy/db
153 LOG=$POLICY_LOGS/policy/db/db_upgrade_remote_$DATE.log
154 ERR=$POLICY_LOGS/policy/db/db_upgrade_remote_$DATE.err
155 echo "db_upgrade_remote.sh started ..." | tee -a $LOG
156 if [ $# -eq 3 ]; then
157 DB_UPGRADE_USER="${1}"
158 DB_UPGRADE_PASSWORD="${2}"
160 echo "DB_UPGRADE_USER: $DB_UPGRADE_USER" | tee -a $LOG
161 echo "DB_UPGRADE_DIR : $DB_UPGRADE_DIR" | tee -a $LOG
162 echo "DB_HOSTNAME : $DB_HOSTNAME" | tee -a $LOG
164 typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} -h ${DB_HOSTNAME}";
165 get_target_schema_version
166 if [ ${#TARGET_SCHEMA_VERSION} -ne 6 ]; then
167 echo "ERROR, TARGET_SCHEMA_VERSION MUST BE 6 DIGITS: $TARGET_SCHEMA_VERSION" | tee -a $LOG | tee -a $ERR
169 get_current_schema_version
170 evaluate_upgrade_downgrade
173 echo "Usage : db_upgrade_remote.sh db_user_id db_user_password db_hostname" | tee -a $LOG
174 echo "Example: db_upgrade_remote.sh policy_user password localhost.com" | tee -a $LOG
177 echo "db_upgrade_remote.sh completed ..." | tee -a $LOG