File: upgrade-db.sh

package info (click to toggle)
gnumed-server 21.11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 45,408 kB
  • ctags: 3,891
  • sloc: sql: 1,210,640; python: 13,526; sh: 1,476; makefile: 19
file content (230 lines) | stat: -rwxr-xr-x 6,926 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash

# ========================================================
# Upgrade GNUmed database from version to version.
#
# usage:
#  upgrade-db.sh vX vX+1 <secrets>
#
# limitations:
#  Only works from version to version sequentially.
#
# prerequisites:
#  update_db-vX_vX+1.conf must exist
#
# "secret" command line options:
#  no-backup
#  - don't create a database backup (you got faith)
#  no-compression
#  - create a backup but don't compress it (you got plenty of disk but run low on cycles)
#  quiet
#  - display failures only, don't chatter
#
# ========================================================

set -o pipefail

PREV_VER="$1"
NEXT_VER="$2"
SKIP_BACKUP="$3"
BZIP_BACKUP="$3"
QUIET="$3"
LOG_BASE="."
LOG="${LOG_BASE}/update_db-v${PREV_VER}_v${NEXT_VER}.log"
CONF="update_db-v${PREV_VER}_v${NEXT_VER}.conf"
BAK_FILE="backup-upgrade-v${PREV_VER}-to-v${NEXT_VER}-"`hostname`".sql"


if test ! -f $CONF ; then
	echo ""
	echo "Trying to upgrade from version <${PREV_VER}> to version <${NEXT_VER}> ..."
	echo ""
	echo "========================================="
	echo "ERROR: The configuration file:"
	echo "ERROR:"
	echo "ERROR:  $CONF"
	echo "ERROR"
	echo "ERROR: does not exist. Aborting."
	echo "========================================="
	echo ""
	echo "USAGE: $0 x x+1"
	echo "   x   - database version to upgrade from"
	echo "   x+1 - database version to upgrade to (sequentially only)"
	exit 1
fi ;


# if you need to adjust the database name to something
# other than what the config file has you can use the
# following environment variable:
#export GM_CORE_DB="gnumed_v${NEXT_VER}"


# if you need to adjust the port you want to use to
# connect to PostgreSQL you can use the environment
# variable below (this may be necessary if your PostgreSQL
# server is running on a port different from the default 5432)
#export GM_DB_PORT="5433"


# tell libpq-based tools about the non-default port, if any
if test -n "${GM_DB_PORT}" ; then
	PORT_DEF="-p ${GM_DB_PORT}"
	export PGPORT="${GM_DB_PORT}"
else
	PORT_DEF=""
fi ;


if test "$QUIET" != "quiet"; then
	QUIET="$4"
fi ;
function echo_msg () {
	if test "$QUIET" == "quiet"; then
		return
	fi ;
	echo $1 ;
}



# Darwin/MacOSX ?
# (MacOSX cannot "su -c" ...)
SYSTEM=`uname -s`
if test "${SYSTEM}" != "Darwin" ; then
	# Does source database exist ?
	TEMPLATE_DB="gnumed_v${PREV_VER}"
	VER_EXISTS=`su -c "psql -l ${PORT_DEF}" -l postgres | grep ${TEMPLATE_DB}`
	if test "${VER_EXISTS}" == "" ; then
		echo ""
		echo "Trying to upgrade from version <${PREV_VER}> to version <${NEXT_VER}> ..."
		echo ""
		echo "========================================="
		echo "ERROR: The template database"
		echo "ERROR:"
		echo "ERROR:  ${TEMPLATE_DB}"
		echo "ERROR:"
		echo "ERROR: does not exist. Aborting."
		echo "========================================="
		read
		exit 1
	fi ;
fi ;



# show what we do
echo_msg ""
echo_msg "==========================================================="
echo_msg "Upgrading GNUmed database: v${PREV_VER} -> v${NEXT_VER}"
echo_msg ""
echo_msg "This will create a new GNUmed v${NEXT_VER} database from an"
echo_msg "existing v${PREV_VER} database. Patient data is transferred and"
echo_msg "transformed as necessary. The old v${PREV_VER} database will"
echo_msg "remain unscathed. For the upgrade to proceed there must"
echo_msg "not be any connections to it by other users, however."
echo_msg ""
echo_msg "The name of the new database will be \"gnumed_v${NEXT_VER}\". Note"
echo_msg "that any pre-existing v${NEXT_VER} database WILL BE DELETED"
echo_msg "during the upgrade !"
echo_msg ""
echo_msg "(this script usually needs to be run as <root>)"
echo_msg "==========================================================="



# better safe than sorry
if test "${SYSTEM}" != "Darwin" ; then
	# Does TARGET database exist ?
	VER_EXISTS=`su -c "psql -l ${PORT_DEF}" -l postgres | grep gnumed_v${NEXT_VER}`
	if test "${VER_EXISTS}" != "" ; then
		echo ""
		echo "WARNING: The target database"
		echo "WARNING:"
		echo "WARNING:  gnumed_v${NEXT_VER}"
		echo "WARNING:"
		echo "WARNING: already exists."
		echo "WARNING:"
		echo "WARNING: Note that during the upgrade this"
		echo "WARNING: database will be OVERWRITTEN !"
		echo ""
		echo "Continue upgrading (overwrites gnumed_v${NEXT_VER}) ? "
		echo ""
		read -e -p "[yes / NO]: "
		if test "${REPLY}" != "yes" ; then
			echo "Upgrading aborted by user."
			exit 1
		fi
	fi
fi



# eventually attempt the upgrade
echo_msg ""
echo_msg "1) creating backup of the database that is to be upgraded ..."
if test "$SKIP_BACKUP" != "no-backup" ; then
	#echo_msg "   Note that this may take a substantial amount of time and disk space!"
	#echo_msg "   You may need to type in the password for gm-dbo."
	echo_msg "   This step may take a substantial amount of time and disk space."
	echo_msg "   (you will be prompted if you need to type in the password for gm-dbo)"
	if test "$BZIP_BACKUP" != "no-compression" ; then
		pg_dump -C -U gm-dbo ${PORT_DEF} gnumed_v${PREV_VER} | bzip2 -z9 > ${BAK_FILE}.bz2
		ARCHIVED="$?"
	else
		pg_dump -C -U gm-dbo ${PORT_DEF} -f ${BAK_FILE} gnumed_v${PREV_VER}
		ARCHIVED="$?"
	fi ;
	if test "${ARCHIVED}" != "0" ; then
		echo ""
		echo "========================================="
		echo "ERROR: Backing up database"
		echo "ERROR:"
		echo "ERROR:  gnumed_v${PREV_VER}"
		echo "ERROR:"
		echo "ERROR: failed. Aborting."
		echo "========================================="
		read
		exit 1
	fi
else
	echo_msg ""
	echo "   !!! SKIPPED backup !!!"
	echo_msg ""
	echo_msg "   This may prevent you from being able to restore your"
	echo_msg "   database from an up-to-date backup should you need to."
	echo_msg ""
	echo_msg "   Be sure you really know what you are doing !"
fi ;


echo_msg ""
echo_msg "2) upgrading to new database ..."
# fixup for schema hash function
# - cannot be done inside bootstrapper
# - only needed for converting anything below v6 with a v6 bootstrapper
#echo_msg "==> fixup for database hashing (will probably ask for gm-dbo password) ..."
#psql -U gm-dbo -d gnumed_v${PREV_VER} ${PORT_DEF} -f ../sql/gmConcatTableStructureFutureStub.sql
./bootstrap_gm_db_system.py --log-file=${LOG} --conf-file=${CONF} --${QUIET}
if test "$?" != "0" ; then
	echo "Upgrading \"gnumed_v${PREV_VER}\" to \"gnumed_v${NEXT_VER}\" did not finish successfully."
	read
	exit 1
fi


#echo_msg ""
#echo_msg "3) preparing new database for efficient use ..."
#echo_msg "   If the database is large this may take quite a while!"
#echo_msg "   You may need to type in the password for gm-dbo."
#vacuumdb --full --analyze ${PORT_DEF} -U gm-dbo -d gnumed_v${NEXT_VER}


echo_msg ""
echo_msg "=========================================================="
echo_msg " Make sure to upgrade your database backup procedures to"
echo_msg " appropriately refer to the new database \"gnumed_v${NEXT_VER}\" !"
echo_msg "=========================================================="


exit 0