Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ RUN cd /tmp && \
conda clean --all -f -y

# Install PostgreSQL in a dedicated conda environment.
RUN conda create -c conda-forge -n pgsql postgresql=10 && conda clean --all -f -y
RUN conda create -c conda-forge -n pgsql postgresql=12 && conda clean --all -f -y

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)


# Below is a solution to a strange bug with PGSQL=10
# Taken from https://github.com/tethysplatform/tethys/issues/667.
Expand Down Expand Up @@ -140,5 +140,8 @@ RUN sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' /etc/skel/.bashr
# Always activate conda.
COPY profile.d/activate_conda.sh /etc/profile.d/

# Add database migration script.
COPY opt/migrate-database.sh /opt/migrate-database.sh

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]
59 changes: 59 additions & 0 deletions opt/migrate-database.sh
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

Choose a reason for hiding this comment

The 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 ${NB_USER}


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}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would this script show it's output?
I would put an echo here to show where the backup is

(although when the script is executed in the debug mode (set -x), each command is echoed, so I guess and extra echo make it a bit more clear what is happening)

Suggested change
mv ${DB_FOLDER} ${BACKUP_DB_FOLDER}
echo "Creating a DB backup to ${BACKUP_DB_FOLDER}"
mv ${DB_FOLDER} ${BACKUP_DB_FOLDER}

Copy link
Member

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the ${BACKUP_DB_FOLDER} for this makes me a bit nervous, I don't think we should touch it at all during the migration. But perhaps I am misunderstanding....

Suggested change
conda run -n ${OLD_DB_CONDA_ENV_NAME} pg_ctl -D ${BACKUP_DB_FOLDER} -l ${BACKUP_DB_FOLDER}/logfile start
conda run -n ${OLD_DB_CONDA_ENV_NAME} pg_ctl -D ${DB_FOLDER} -l ${DB_FOLDER}/logfile start

Choose a reason for hiding this comment

The 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}

Choose a reason for hiding this comment

The 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
7 changes: 6 additions & 1 deletion opt/start-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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