@@ -9,14 +9,24 @@ docker_setup_env
99
1010if [ -n " $DATABASE_ALREADY_EXISTS " ]; then
1111
12+ echo " -----------------------------------------"
13+ echo " Performing checks on existing database..."
14+ echo " -----------------------------------------"
15+
1216 # Make sure timescaledb library is set to preload (won't work otherwise)
17+ echo " ---------------------------------------------------------------------------------------"
1318 echo " Existing postgresql.conf found checking for shared_preload_libraries = 'timescaledb'..."
19+ echo " ---------------------------------------------------------------------------------------"
1420 RESULT=$( cat " $PGDATA /postgresql.conf" | grep " ^shared_preload_libraries = 'timescaledb'" || true)
1521
1622 if [ -n " $RESULT " ]; then
23+ echo " -------------------------------------------"
1724 echo " Timescale DB library already set to preload"
25+ echo " -------------------------------------------"
1826 else
27+ echo " ------------------------------------------------------------------"
1928 echo " Adding shared_preload_libraries = 'timescaledb' to postgresql.conf"
29+ echo " ------------------------------------------------------------------"
2030 echo " shared_preload_libraries = 'timescaledb'" >> " $PGDATA /postgresql.conf"
2131 echo " timescaledb.telemetry_level=off" >> " $PGDATA /postgresql.conf"
2232 fi
@@ -31,12 +41,18 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
3141 DB_VERSION=$( cat " ${PGDATA} /PG_VERSION" )
3242 fi
3343
44+ if [ " $DB_VERSION " != " $PG_MAJOR " ] && [ " $OR_DISABLE_AUTO_UPGRADE " == " true" ]; then
45+ echo " ---------------------------------------------------------------------------------"
46+ echo " Postgres major version has changed but OR_DISABLE_AUTO_UPGRADE=true so container will likely fail to start!"
47+ echo " ---------------------------------------------------------------------------------"
48+ fi
49+
3450 # Try and upgrade if needed
3551 if [ " $DB_VERSION " != " $PG_MAJOR " ] && [ " $OR_DISABLE_AUTO_UPGRADE " != " true" ]; then
3652
37- echo " ----------------------------------------------------------------------"
53+ echo " --------------------------------------------------------------------------------- "
3854 echo " Postgres major version is newer than the existing DB, performing auto upgrade..."
39- echo " ----------------------------------------------------------------------"
55+ echo " --------------------------------------------------------------------------------- "
4056
4157 if [ ! -d " /usr/lib/postgresql/${DB_VERSION} " ]; then
4258 echo " Postgres executable version '$DB_VERSION ' is not included in this image so cannot auto upgrade"
@@ -158,11 +174,15 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
158174 echo " Copying the old pg_hba and pg_ident configuration files is complete"
159175 echo " -------------------------------------------------------------------"
160176
177+ # Don't automatically abort on non-0 exit status
178+ set +e
179+
161180 # Copy any reindex counter files
162181 echo " --------------------------------------------------------------"
163- echo " Copying reindex files across"
182+ echo " Copying reindex and TS version files across"
164183 echo " --------------------------------------------------------------"
165184 cp -f ${OLD} /OR_REINDEX_* ${PGDATA}
185+ cp -f ${OLD} /TS_VERSION ${PGDATA}
166186 echo " -------------------------------------------------------------------"
167187 echo " Copying reindex files is complete"
168188 echo " -------------------------------------------------------------------"
@@ -171,37 +191,138 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
171191 echo " ---------------------------------"
172192 echo " Removing left over database files"
173193 echo " ---------------------------------"
174- set +e
175194 rm -rf " ${OLD} " " ${NEW} " delete_old_cluster.sh
176- set -e
177195 echo " ---------------------------------------------"
178196 echo " Removing left over database files is complete"
179197 echo " ---------------------------------------------"
180198
181199 echo " **********************************************************"
182200 echo " Automatic upgrade process finished with no errors reported"
183201 echo " **********************************************************"
202+
203+ # Return the error handling back to automatically aborting on non-0 exit status
204+ set -e
205+ fi
206+
207+ # Do timescale upgrade if needed - First look for latest extension version number in extension files
208+ echo " ----------------------------------------------------------"
209+ echo " Checking latest available TimescaleDB extension version..."
210+ echo " ----------------------------------------------------------"
211+ TS_VERSION_REGEX=" \-\-([0-9|\.]+)\."
212+ TS_SCRIPT_NAME=$( find /usr/share/postgresql/$PG_MAJOR /extension/ -type f -name " timescaledb--*.sql" | sort | tail -n 1)
213+ TS_VERSION=" "
214+ TS_VERSION_FILE=" ${PGDATA} /OR_TS_VERSION"
215+
216+ if [ " $TS_SCRIPT_NAME " == " " ] || ! [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
217+ echo " ------------------------------------------------------"
218+ echo " Cannot determine current TimescaleDB extension version"
219+ echo " ------------------------------------------------------"
220+ exit 15
221+ else
222+ TS_VERSION=${BASH_REMATCH[1]}
223+ fi
224+
225+ if [ " $TS_VERSION " == " " ]; then
226+ echo " ------------------------------------------------------"
227+ echo " Cannot determine current TimescaleDB extension version"
228+ echo " ------------------------------------------------------"
229+ exit 15
230+ fi
231+
232+ DO_TS_UPGRADE=false
233+ echo " Checking whether Timescale needs upgrading..."
234+ if [ ! -f " ${TS_VERSION_FILE} " ]; then
235+ echo " -----------------------------------------------------"
236+ echo " No OR_TS_VERSION file so assuming upgrade is required"
237+ echo " -----------------------------------------------------"
238+ DO_TS_UPGRADE=true
239+ else
240+ echo " -------------------------------------------------------"
241+ echo " Getting version number from existing OR_TS_VERSION file"
242+ echo " -------------------------------------------------------"
243+ PREVIOUS_TS_VERSION=$( cat " $TS_VERSION_FILE " )
244+ if [ " ${PREVIOUS_TS_VERSION} " != " ${TS_VERSION} " ]; then
245+ echo " ------------------------------------------------------------------------------"
246+ echo " TimescaleDB extension upgrade required ${PREVIOUS_TS_VERSION} -> ${TS_VERSION} "
247+ echo " ------------------------------------------------------------------------------"
248+ DO_TS_UPGRADE=true
249+ else
250+ echo " ----------------------------------------------------"
251+ echo " TimescaleDB extension is up to date at: ${TS_VERSION} "
252+ echo " ----------------------------------------------------"
253+ fi
254+ fi
255+
256+ if [ " $DO_TS_UPGRADE " == " true" ] && [ " $OR_DISABLE_AUTO_UPGRADE " == " true" ]; then
257+ echo " ----------------------------------------------------------------------------------"
258+ echo " TimescaleDB upgrade can be performed but OR_DISABLE_AUTO_UPGRADE=true so skipping!"
259+ echo " ----------------------------------------------------------------------------------"
184260 fi
185261
262+ if [ " ${OR_DISABLE_AUTO_UPGRADE} " == " true" ]; then
263+ DO_TS_UPGRADE=false
264+ fi
265+
266+
186267 # Do re-indexing check
268+ DO_REINDEX=false
187269 if [ " $OR_DISABLE_REINDEX " == ' true' ] || [ -z " $OR_REINDEX_COUNTER " ]; then
188- echo " REINDEX check is disabled"
270+ echo " ----------------------------"
271+ echo " OR_REINDEX check is disabled"
272+ echo " ----------------------------"
189273 else
190- echo " Checking whether REINDEX is required..."
191- REINDEX_FILE=" $PGDATA /OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER "
192- if [ -f " $REINDEX_FILE " ]; then
193- echo " REINDEX file '$REINDEX_FILE ' already exists so no re-indexing required"
194- else
195- echo " REINDEX file '$REINDEX_FILE ' doesn't exist"
196- docker_temp_server_start " $@ "
197- echo " Running timescaledb tune script..."
198- /docker-entrypoint-initdb.d/001_timescaledb_tune.sh
199- echo " Re-indexing the DB..."
200- docker_process_sql -c " REINDEX database $POSTGRES_DB ;"
201- docker_temp_server_stop
202- echo ' REINDEX completed!'
203- touch " $REINDEX_FILE "
204- fi
274+ echo " ---------------------------------------"
275+ echo " Checking whether REINDEX is required..."
276+ echo " ---------------------------------------"
277+ REINDEX_FILE=" $PGDATA /OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER "
278+ if [ -f " $REINDEX_FILE " ]; then
279+ echo " -------------------------------------------------------------------------"
280+ echo " REINDEX file '$REINDEX_FILE ' already exists so no re-indexing required"
281+ echo " -------------------------------------------------------------------------"
282+ else
283+ echo " -------------------------------------------------------------------------"
284+ echo " REINDEX file '$REINDEX_FILE ' doesn't exist so re-indexing required"
285+ echo " -------------------------------------------------------------------------"
286+ DO_REINDEX=true
287+ fi
288+ fi
289+
290+ if [ " $DO_REINDEX " == " true" ] || [ " $DO_TS_UPGRADE " == " true" ]; then
291+ echo " -------------------------"
292+ echo " Starting temporary server"
293+ echo " -------------------------"
294+ docker_temp_server_start " $@ "
295+
296+ # Cannot do this on a running DB as the extension is configured to preload
297+ if [ " $DO_TS_UPGRADE " == " true" ]; then
298+ echo " ------------------------"
299+ echo " Performing TS upgrade..."
300+ echo " ------------------------"
301+ docker_process_sql -X -c " ALTER EXTENSION timescaledb UPDATE;"
302+ docker_process_sql -c " CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit; ALTER EXTENSION timescaledb_toolkit UPDATE;"
303+
304+ echo " -------------------"
305+ echo " TS upgrade complete"
306+ echo " -------------------"
307+ echo " $TS_VERSION " > " ${PGDATA} /OR_TS_VERSION"
308+ fi
309+
310+ if [ " $DO_REINDEX " == " true" ]; then
311+ echo " ----------------------------------"
312+ echo " Running timescaledb tune script..."
313+ echo " ----------------------------------"
314+ /docker-entrypoint-initdb.d/001_timescaledb_tune.sh
315+ echo " ---------------------"
316+ echo " Re-indexing the DB..."
317+ echo " ---------------------"
318+ docker_process_sql -c " REINDEX database $POSTGRES_DB ;"
319+ echo " ------------------"
320+ echo " REINDEX completed!"
321+ echo " ------------------"
322+ touch " $REINDEX_FILE "
323+ fi
324+
325+ docker_temp_server_stop
205326 fi
206327fi
207328
0 commit comments