|
| 1 | +echo "${_group}Ensuring proper PostgreSQL version ..." |
| 2 | + |
| 3 | +if [[ -n "$(docker volume ls -q --filter name=sentry-postgres)" && "$(docker run --rm -v sentry-postgres:/db busybox cat /db/PG_VERSION 2>/dev/null)" == "9.6" ]]; then |
| 4 | + docker volume rm sentry-postgres-new || true |
| 5 | + # If this is Postgres 9.6 data, start upgrading it to 14.0 in a new volume |
| 6 | + docker run --rm \ |
| 7 | + -v sentry-postgres:/var/lib/postgresql/9.6/data \ |
| 8 | + -v sentry-postgres-new:/var/lib/postgresql/14/data \ |
| 9 | + tianon/postgres-upgrade:9.6-to-14 |
| 10 | + |
| 11 | + # Get rid of the old volume as we'll rename the new one to that |
| 12 | + docker volume rm sentry-postgres |
| 13 | + docker volume create --name sentry-postgres |
| 14 | + # There's no rename volume in Docker so copy the contents from old to new name |
| 15 | + # Also append the `host all all all trust` line as `tianon/postgres-upgrade:9.6-to-14` |
| 16 | + # doesn't do that automatically. |
| 17 | + docker run --rm -v sentry-postgres-new:/from -v sentry-postgres:/to alpine ash -c \ |
| 18 | + "cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf" |
| 19 | + # Finally, remove the new old volume as we are all in sentry-postgres now. |
| 20 | + docker volume rm sentry-postgres-new |
| 21 | + echo "Re-indexing due to glibc change, this may take a while..." |
| 22 | + echo "Starting up new PostgreSQL version" |
| 23 | + $dc up -d postgres |
| 24 | + |
| 25 | + # Wait for postgres |
| 26 | + RETRIES=5 |
| 27 | + until docker exec sentry-self-hosted-postgres-1 psql -U postgres -c "select 1" >/dev/null 2>&1 || [ $RETRIES -eq 0 ]; do |
| 28 | + echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..." |
| 29 | + sleep 1 |
| 30 | + done |
| 31 | + |
| 32 | + # VOLUME_NAME is the same as container name |
| 33 | + # Reindex all databases and their system catalogs which are not templates |
| 34 | + DBS=$(docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -c "SELECT datname FROM pg_database WHERE datistemplate = false;") |
| 35 | + for db in ${DBS}; do |
| 36 | + echo "Re-indexing database: ${db}" |
| 37 | + docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -d ${db} -c "reindex system ${db}" |
| 38 | + docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -d ${db} -c "reindex database ${db};" |
| 39 | + done |
| 40 | + |
| 41 | + $dc stop postgres |
| 42 | +fi |
| 43 | + |
| 44 | +echo "${_endgroup}" |
0 commit comments