Skip to content

Commit

Permalink
Make sure we don't leave postgres in incoherent state if fail to update
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Oct 22, 2021
1 parent 1076b27 commit 87fb8dd
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions Postgres/13.4/migrate-docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ CURRENT_PGVERSION=""
EXPECTED_PGVERSION="$PG_MAJOR"
if [[ -f "/var/lib/postgresql/data/PG_VERSION" ]]; then
CURRENT_PGVERSION="$(cat /var/lib/postgresql/data/PG_VERSION)"
elif [[ -f "/var/lib/postgresql/data/PG_MIGRATING_FROM_VERSION" ]]; then
echo "Previous migration failed"
CURRENT_PGVERSION="$(cat /var/lib/postgresql/data/PG_MIGRATING_FROM_VERSION)"
fi

if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \
Expand Down Expand Up @@ -48,15 +51,21 @@ if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \
export PGDATAOLD="/var/lib/postgresql/data/$CURRENT_PGVERSION"
export PGDATANEW="/var/lib/postgresql/data/$EXPECTED_PGVERSION"

mkdir -p "$PGDATANEW" "$PGDATAOLD"
find "$PGDATABASE" -maxdepth 1 -mindepth 1 \
-not -wholename "$PGDATAOLD" \
-not -wholename "$PGDATANEW" \
-exec mv {} "$PGDATAOLD/" \;

chmod 700 "$PGDATAOLD" "$PGDATANEW"
chown postgres .
chown -R postgres "$PGDATAOLD" "$PGDATANEW" "$PGDATABASE"
if ! [ -f "$PGDATABASE/PG_MIGRATING_FROM_VERSION" ]; then
mkdir -p "$PGDATANEW" "$PGDATAOLD"
find "$PGDATABASE" -maxdepth 1 -mindepth 1 \
-not -wholename "$PGDATAOLD" \
-not -wholename "$PGDATANEW" \
-exec mv {} "$PGDATAOLD/" \;

chmod 700 "$PGDATAOLD" "$PGDATANEW"
chown postgres .
chown -R postgres "$PGDATAOLD" "$PGDATANEW" "$PGDATABASE"
cp "$PGDATAOLD/PG_VERSION" "$PGDATABASE/PG_MIGRATING_FROM_VERSION"
else
echo "Previous migration failed, trying one more time..."
fi

[[ "$POSTGRES_USER" ]] && export PGUSER="$POSTGRES_USER"
[[ "$POSTGRES_PASSWORD" ]] && export PGPASSWORD="$POSTGRES_PASSWORD"
if [ ! -s "$PGDATANEW/PG_VERSION" ]; then
Expand All @@ -73,6 +82,7 @@ if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \
exit 1
fi

rm "$PGDATABASE/PG_MIGRATING_FROM_VERSION"
rm $PGDATANEW/*.conf
mv $PGDATANEW/* "$PGDATABASE"
mv $PGDATAOLD/*.conf "$PGDATABASE"
Expand Down

0 comments on commit 87fb8dd

Please sign in to comment.