upgrade-postgres.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. POSTGRES_CONTAINER="sentry_postgres"
  3. USE_NEW_DEVSERVICES=${USE_NEW_DEVSERVICES:-"0"}
  4. if [ "$USE_NEW_DEVSERVICES" == "1" ]; then
  5. POSTGRES_CONTAINER="sentry-postgres-1"
  6. fi
  7. OLD_VERSION="9.6"
  8. NEW_VERSION="14"
  9. PG_IMAGE="ghcr.io/getsentry/image-mirror-library-postgres:${NEW_VERSION}-alpine"
  10. PROJECT=${PROJECT:-sentry}
  11. VOLUME_NAME="${PROJECT}_postgres"
  12. TMP_VOLUME_NAME="${VOLUME_NAME}_${NEW_VERSION}"
  13. TMP_CONTAINER="${PROJECT}_pg_migration"
  14. echo "Stop the container"
  15. docker stop "${POSTGRES_CONTAINER}"
  16. echo "Check existence of a volume"
  17. if [[ -z "$(docker volume ls -q --filter name="^${VOLUME_NAME}$")" ]]
  18. then
  19. echo "PostgreSQL volume with name ${VOLUME_NAME} does not exist. Nothing to upgrade."
  20. exit 0
  21. fi
  22. echo "Get the current PostgreSQL version"
  23. CURRENT_VERSION=$(docker run --rm -v ${VOLUME_NAME}:/db busybox cat /db/PG_VERSION 2>/dev/null)
  24. echo "Current PostgreSQL version is ${CURRENT_VERSION}"
  25. if [[ "${CURRENT_VERSION}" != "${OLD_VERSION}" ]]
  26. then
  27. echo "Expected current PostgreSQL version is ${OLD_VERSION}."
  28. exit 1
  29. fi
  30. docker volume rm "${TMP_VOLUME_NAME}" || true
  31. docker run --rm \
  32. -v ${VOLUME_NAME}:/var/lib/postgresql/${OLD_VERSION}/data \
  33. -v ${TMP_VOLUME_NAME}:/var/lib/postgresql/${NEW_VERSION}/data \
  34. tianon/postgres-upgrade:${OLD_VERSION}-to-${NEW_VERSION}
  35. # Get rid of the old volume as we'll rename the new one to that
  36. docker volume rm ${VOLUME_NAME}
  37. docker volume create --name ${VOLUME_NAME}
  38. # There's no rename volume in Docker so copy the contents from old to new name
  39. # Also append the `host all all all trust`
  40. docker run --rm -v ${TMP_VOLUME_NAME}:/from -v ${VOLUME_NAME}:/to alpine ash -c \
  41. "cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf"
  42. # Finally, remove the new old volume as we are all in sentry-postgres now
  43. docker volume rm ${TMP_VOLUME_NAME}
  44. echo "Due to glibc change re-indexing"
  45. echo "Starting up new PostgreSQL version"
  46. PG_VERSION=${NEW_VERSION} ${PROJECT} devservices up postgres
  47. # Wait for postgres
  48. RETRIES=5
  49. until docker exec ${VOLUME_NAME} psql -U postgres -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  50. echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..."
  51. sleep 1
  52. done
  53. # VOLUME_NAME is the same as container name
  54. # Reindex all databases and their system catalogs which are not templates
  55. DBS=$(docker exec ${VOLUME_NAME} psql -qAt -U postgres -c "select datname from pg_database where datistemplate = false;")
  56. for db in ${DBS}
  57. do
  58. echo "Re-indexing database: ${db}"
  59. docker exec ${VOLUME_NAME} psql -qAt -U postgres -d ${db} -c "reindex system ${db}"
  60. docker exec ${VOLUME_NAME} psql -qAt -U postgres -d ${db} -c "reindex database ${db};"
  61. done
  62. _PROFILE_LINE="export PG_VERSION=${NEW_VERSION}"
  63. echo
  64. echo "To configure your environment to use PostgreSQL with ${PROJECT}, PG_VERSION variable must be set."
  65. echo "Save the following to your shell rc file:"
  66. echo
  67. echo "${_PROFILE_LINE}"
  68. echo