Skip to content

Commit ca48317

Browse files
committed
Improve DB migration
1 parent 74cd43d commit ca48317

5 files changed

Lines changed: 94 additions & 63 deletions

File tree

Dockerfile

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# -----------------------------------------------------------------------------------------------
22
# POSTGIS and TimescaleDB (inc. toolkit for hyperfunctions) image built for aarch64 support
3-
# using alpine base image.
3+
# using timescaledev/timescaledb-ha base image with:
4+
#
5+
# - OR specific ENV variables and a healthcheck added
6+
# - PGDATA path set to match old Alpine image (for ease of DB migration)
7+
# - POSTGRES user UID and GID changed to match old Alpine image (for ease of DB migration)
8+
# - OR_DISABLE_REINDEX env variable with associated scripts to determine if a REINDEX of the entire DB should be carried
9+
# out at first startup with existing DB (checks whether or not $PGDATA/OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER exists).
10+
# This is used when a collation change has occurred (glibc version change, muslc <-> glibc) which can break the indexes;
11+
# migration can either be manually handled or auto handled depending on OR_DISABLE_REINDEX env variable value.
12+
# NOTE THAT A REINDEX CAN TAKE A LONG TIME DEPENDING ON THE SIZE OF THE DB! And startup will be delayed until completed
13+
# This functionality is intended to simplify migration for basic users; advanced users with large DBs should take care of this
14+
# themselves.
15+
#
16+
#
17+
#
418
#
519
# timescale/timescaledb-ha image is ubuntu based and only currently supports amd64; they are
620
# working on ARM64 support in timescaledev/timescaledb-ha see:
@@ -25,18 +39,33 @@ RUN usermod -u 70 postgres \
2539
&& (find / -group 1000 -exec chgrp -h postgres {} \; || true) \
2640
&& (find / -user 1000 -exec chown -h postgres {} \; || true)
2741

28-
2942
# Set PGDATA to the same location as our old alpine image
3043
RUN mkdir -p /var/lib/postgresql && mv /home/postgres/pgdata/* /var/lib/postgresql/ && chown -R postgres:postgres /var/lib/postgresql
3144

32-
# Below is copied from https://github.com/timescale/timescaledb-docker-ha/blob/master/Dockerfile
33-
# to minimise the size of this image
45+
# Add custom entry point (see file header for details)
46+
COPY or-entrypoint.sh /
47+
RUN chmod +x /or-entrypoint.sh
48+
49+
# Add custom initdb script(s)
50+
COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
51+
RUN chmod +x /docker-entrypoint-initdb.d/*
52+
53+
54+
# Below is mostly copied from https://github.com/timescale/timescaledb-docker-ha/blob/master/Dockerfile (with OR specific entrypoint,
55+
# workdir and OR env defaults)
56+
57+
3458
## Create a smaller Docker image from the builder image
3559
FROM scratch
3660
COPY --from=trimmed / /
3761

3862
ARG PG_MAJOR=14
39-
ENTRYPOINT ["/docker-entrypoint.sh"]
63+
64+
# Increment this to indicate that a re-index should be carried out on first startup with existing data; REINDEX can still be overidden
65+
# with OR_DISABLE_REINDEX=true
66+
ARG OR_REINDEX_COUNTER=1
67+
68+
ENTRYPOINT ["/or-entrypoint.sh"]
4069
CMD ["postgres"]
4170

4271
ENV PGROOT=/var/lib/postgresql \
@@ -50,17 +79,18 @@ ENV PGROOT=/var/lib/postgresql \
5079
LC_ALL=C.UTF-8 \
5180
LANG=C.UTF-8 \
5281
# When having an interactive psql session, it is useful if the PAGER is disable
53-
PAGER=""
82+
PAGER="" \
83+
# OR ENV DEFAULTS
84+
TZ=${TZ:-Europe/Amsterdam} \
85+
PGTZ=${PGTZ:-Europe/Amsterdam} \
86+
POSTGRES_DB=${POSTGRES_DB:-openremote} \
87+
POSTGRES_USER=${POSTGRES_USER:-postgres} \
88+
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres} \
89+
OR_REINDEX_COUNTER=${OR_REINDEX_COUNTER} \
90+
OR_DISABLE_REINDEX=${OR_DISABLE_REINDEX:-false}
5491

5592
WORKDIR /var/lib/postgresql
5693
EXPOSE 5432 8008 8081
5794
USER postgres
5895

59-
## ADD OR convenience default ENV values and healthcheck
60-
ENV TZ ${TZ:-Europe/Amsterdam}
61-
ENV PGTZ ${PGTZ:-Europe/Amsterdam}
62-
ENV POSTGRES_DB ${POSTGRES_DB:-openremote}
63-
ENV POSTGRES_USER ${POSTGRES_USER:-postgres}
64-
ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD:-postgres}
65-
6696
HEALTHCHECK --interval=3s --timeout=3s --start-period=2s --retries=30 CMD pg_isready
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# THIS SCRIPT JUST CREATES THE REINDEX_FILE WHICH WILL PREVENT RE-INDEXING FOR HAPPENING ON A NEWLY INITIALISED DB
4+
5+
6+
if [ -n "$OR_REINDEX_COUNTER" ]; then
7+
REINDEX_FILE="$PGDATA/OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER"
8+
echo "Creating REINDEX_FILE '$REINDEX_FILE'"...
9+
touch "$REINDEX_FILE"
10+
fi

initdb-postgis.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

or-entrypoint.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# THIS FILE IS FOR MIGRATION OF EXISTING DB TO TIMESCALEDB IMAGE AS TIMESCALE INIT SCRIPTS AREN'T RUN WHEN DB
4+
# ALREADY EXISTS; IT ALSO DOES AN AUTOMATIC REINDEX OF THE DB WHEN OR_REINDEX_COUNTER changes TO SIMPLIFY MIGRATIONS
5+
6+
source /docker-entrypoint.sh
7+
docker_setup_env
8+
9+
if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
10+
11+
# Make sure timescaledb library is set to preload (won't work otherwise)
12+
echo "Existing postgresql.conf found checking for shared_preload_libraries = 'timescaledb'..."
13+
RESULT=$(cat "$PGDATA/postgresql.conf" | grep "^shared_preload_libraries = 'timescaledb'" || true)
14+
15+
if [ -n "$RESULT" ]; then
16+
echo "Timescale DB library already set to preload"
17+
else
18+
echo "Adding shared_preload_libraries = 'timescaledb' to postgresql.conf"
19+
echo "shared_preload_libraries = 'timescaledb'" >> "$PGDATA/postgresql.conf"
20+
fi
21+
22+
# Do re-indexing check
23+
if [ "$OR_DISABLE_REINDEX" == 'true' ] || [ -z "$OR_REINDEX_COUNTER" ]; then
24+
echo "REINDEX check is disabled"
25+
else
26+
echo "Checking whether REINDEX is required..."
27+
REINDEX_FILE="$PGDATA/OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER"
28+
if [ -f "$REINDEX_FILE" ]; then
29+
echo "REINDEX file '$REINDEX_FILE' already exists so no re-indexing required"
30+
else
31+
echo "REINDEX file '$REINDEX_FILE' doesn't exist so re-indexing the DB..."
32+
docker_temp_server_start "$@"
33+
docker_process_sql -c "REINDEX database $POSTGRES_DB;"
34+
docker_temp_server_stop
35+
echo 'REINDEX completed!'
36+
touch "$REINDEX_FILE"
37+
fi
38+
fi
39+
fi
40+
41+
exec /docker-entrypoint.sh $@

update-postgis.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)