-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement automated PGSQL DB migration. #63
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||
#!/bin/bash | ||||||||
set -exuo pipefail | ||||||||
|
||||||||
# This script is intended to be run in a container. Its job is to migrate the PGSQL database to a newer version. | ||||||||
|
||||||||
# The following variables are required: | ||||||||
DB_FOLDER=/home/${SYSTEM_USER}/.postgresql | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting for future reference that in the new AiiDAlab docker stack we use |
||||||||
|
||||||||
OLD_PGSQL_DB_VERSION=`cat ${DB_FOLDER}/PG_VERSION` # The version of the database before the upgrade is taken from the PG_VERSION file. | ||||||||
NEW_PGSQL_DB_VERSION=`conda run -n pgsql psql -V | awk '{ print int( $3 ) }'` # The new version of PGSQL is given by the `psql -V` command installed in the pgsql conda environment. | ||||||||
|
||||||||
TIMESTAMP=$(date +%Y%m%d%H%M%S) | ||||||||
BACKUP_DB_FOLDER=/home/${SYSTEM_USER}/.postgresql-bak-${TIMESTAMP} | ||||||||
OLD_DB_CONDA_ENV_NAME="pgsql-backup-${OLD_PGSQL_DB_VERSION}-${TIMESTAMP}" | ||||||||
DUMP_FILE_NAME="/home/${SYSTEM_USER}/pgsql-dump-${OLD_PGSQL_DB_VERSION}-${TIMESTAMP}.sql" | ||||||||
|
||||||||
# Make sure the new PostgreSQL server is shut down. | ||||||||
conda run -n pgsql pg_ctl -D ${DB_FOLDER} stop || true # Ignore errors, as the database might not be running. | ||||||||
|
||||||||
# Part 1: dump the old database. | ||||||||
|
||||||||
# Make a backup of the database. | ||||||||
mv ${DB_FOLDER} ${BACKUP_DB_FOLDER} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would this script show it's output? (although when the script is executed in the debug mode (
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can retrieve the debug information, yes, I agree it is more clear to print this massage. But I don't know where to check these printed-out messages. Is it write to some log files? |
||||||||
|
||||||||
# Install the old version of PostgreSQL. | ||||||||
conda create -c conda-forge --yes -n ${OLD_DB_CONDA_ENV_NAME} postgresql=${OLD_PGSQL_DB_VERSION} && conda clean --all -f -y | ||||||||
|
||||||||
# Below is a solution to a strange bug with PGSQL=10 | ||||||||
# Taken from https://github.com/tethysplatform/tethys/issues/667. | ||||||||
# This bug is not present for PGSQL=14. So as soon as we migrate, the line below can be removed. | ||||||||
if [ ${OLD_PGSQL_DB_VERSION} -eq 10 ]; then | ||||||||
cp /usr/share/zoneinfo /home/aiida/.conda/envs/${OLD_DB_CONDA_ENV_NAME}/share/ -R | ||||||||
fi | ||||||||
|
||||||||
# Start the old version of PostgreSQL. | ||||||||
conda run -n ${OLD_DB_CONDA_ENV_NAME} pg_ctl -D ${BACKUP_DB_FOLDER} -l ${BACKUP_DB_FOLDER}/logfile start | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind, I misunderstood how the backup folder works. |
||||||||
|
||||||||
# Dump the old database. | ||||||||
conda run -n ${OLD_DB_CONDA_ENV_NAME} pg_dumpall -f ${DUMP_FILE_NAME} | ||||||||
|
||||||||
# Stop the old version of PostgreSQL. | ||||||||
conda run -n ${OLD_DB_CONDA_ENV_NAME} pg_ctl -D ${BACKUP_DB_FOLDER} stop | ||||||||
|
||||||||
# Delete the environment with old version of PostgreSQL. | ||||||||
conda env remove -n ${OLD_DB_CONDA_ENV_NAME} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this command be run only at the very and once we're sure the migration was successfull. Otherwise might be good to keep around for debugging? |
||||||||
|
||||||||
# Part 2: restore the old database to the new version. | ||||||||
|
||||||||
# Create a new database. | ||||||||
conda run -n pgsql initdb -D ${DB_FOLDER} | ||||||||
|
||||||||
# Start the new version of PostgreSQL. | ||||||||
conda run -n pgsql pg_ctl -w -D ${DB_FOLDER} -l ${DB_FOLDER}/logfile start | ||||||||
|
||||||||
# Restore the old database. | ||||||||
conda run -n pgsql psql -f ${DUMP_FILE_NAME} postgres | ||||||||
|
||||||||
# Stop the new version of PostgreSQL to return everything to the original state. | ||||||||
conda run -n pgsql pg_ctl -D ${DB_FOLDER} stop |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,11 @@ else | |
if ! $running ; then | ||
echo "" > /home/${SYSTEM_USER}/.postgresql/logfile # empty log files | ||
rm -vf /home/${SYSTEM_USER}/.postgresql/postmaster.pid | ||
${PSQL_START_CMD} | ||
${PSQL_START_CMD} || cant_start=true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @danielhollas suggested, we should distinguish the "migration required" case from other failures and properly handle it here. |
||
fi | ||
|
||
if $cant_start; then | ||
echo "Postgresql could not be started. Maybe the database needs to be migrated." | ||
touch /home/$SYSTEM_USER/.PGSQL_MIGRATION_REQUIRED | ||
fi | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why not jump straight to version 14? (just asking, I think it is probably better to do it incrementally)