87d5d9bd744cd38501e6c8d11227d6c4a9a8b791
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_upgrade.sh
1 #!/bin/bash
2 # ============LICENSE_START=======================================================
3 # ONAP Policy Engine
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
10
11 #      http://www.apache.org/licenses/LICENSE-2.0
12
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=========================================================
19 ###
20 #
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
22
23 # Usage  : db_upgrade.sh target_db_release_level db_user_id  db_user_password
24 # Example: db_upgrade.sh 151000                  policy_user password
25 #
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
28 #
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)
31 #
32 #
33
34 TARGET_RELEASE=""
35 CURRENT_RELEASE=""
36 DB_UPGRADE_USER=""
37 DB_UPGRADE_PASSWORD=""
38 DB_UPGRADE_DIR=$POLICY_HOME/data/mysql
39 DATE=`date +"%Y%m%d%H%M%S"`
40 LOG=""
41 ERR=""
42
43 function get_current_release_level
44 {
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
51 }
52
53 function evaluate_upgrade_downgrade
54 {
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 
58     # perform db upgrade
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
61     while read -r file
62     do
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
67       fi
68     done < $UPGRADE_LIST
69     rm -f $UPGRADE_LIST
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
75     while read -r file
76     do
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}"
81       fi
82     done < $DOWNGRADE_LIST
83     rm -f $DOWNGRADE_LIST
84     set_current_release_level $TARGET_RELEASE
85   else
86     echo "CURRENT DB RELEASE LEVEL THE SAME AS TARGET RELEASE LEVEL, NO ACTION TAKEN ..." | tee -a $LOG
87   fi
88 }
89
90 function run_script
91 {
92   action="${1}"
93   script="${2}"
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
98 }
99
100 function set_current_release_level
101 {
102   RELEASE="${1}"
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}" 
106
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
111 }
112
113 function check_directory
114 {
115   if [ ! -d $DB_UPGRADE_DIR ]; then
116     echo "ERROR, DIRECTORY NOT EXIST: $DB_UPGRADE_DIR, PROCESS EXIT ..."
117     exit;
118   else
119     if [ ! -d $DB_UPGRADE_DIR/logs ]; then
120       mkdir $DB_UPGRADE_DIR/logs
121     fi
122   fi
123 }
124
125 # MAIN
126 #check_directory
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
137   #
138   if [ ${#TARGET_RELEASE} -ne 6 ]; then 
139     echo "ERROR, TARGET_RELEASE MUST BE 6 DIGITS: $TARGET_RELEASE" | tee -a $LOG | tee -a $ERR
140   else
141     typeset -r MYSQL="mysql -u${DB_UPGRADE_USER} -p${DB_UPGRADE_PASSWORD} ";
142     get_current_release_level
143     evaluate_upgrade_downgrade
144   fi
145 else
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
148 fi
149
150 echo "db_upgrade.sh completed ..." | tee -a $LOG