Skip to content

Commit 3598375

Browse files
Add script that automatically updates postgres databases to a later version (#635)
## Overview In anticipation of upgrading postgres databases in the future, this introduces a script that automatically upgrades postgres databases using the backup/restore process. This includes magpie and all WPS birds that use the postgres component. This does not include test component like `optional-components/generic_bird` and will not update custom components (ones not from this repository). Test components are not assumed to have persistent data that needs to be updated and we cannot guarantee that other postgres databases used by components outside this repository do not require additional steps (data migrations) in order to comply with a different version of postgres. It will update postgres databases to the version specified by the `POSTGRES_VERSION_UPDATE` environment variable. All of the old database files will be copied to a temporary directory in case you want to inspect them or revert this operation later on. To specify which directory to write these backups to set the `DATA_BACKUP_DIR` variable (default: `${TMPDIR:-/tmp}/birdhouse-postgres-migrate-backup/`) Note that backups in the form of database dumps will also be written to the named volume or directory specified by the `BIRDHOUSE_BACKUP_VOLUME` variable. For example, to update the current postgres databases to version 18.1 and write backups to `/tmp/test/` ```sh $ POSTGRES_VERSION_UPDATE=18.1 DATA_BACKUP_DIR=/tmp/test/ birdhouse/scripts/update-postgresh.sh ``` In a future update we can update the postgres versions and tell users to run this script first in order to safely migrate data from one version to the next. ## Changes **Non-breaking changes** - adds new script **Breaking changes** - None ## Related Issue / Discussion ## Additional Information I have tested this by upgrading postgres databases from the current version (9.6) to the latest version (18.1) and all data in the magpie and WPS bird databases are not affected. ## CI Operations <!-- The test suite can be run using a different DACCS config with ``birdhouse_daccs_configs_branch: branch_name`` in the PR description. To globally skip the test suite regardless of the commit message use ``birdhouse_skip_ci`` set to ``true`` in the PR description. Using ``[<cmd>]`` (with the brackets) where ``<cmd> = skip ci`` in the commit message will override ``birdhouse_skip_ci`` from the PR description. Such commit command can be used to override the PR description behavior for a specific commit update. However, a commit message cannot 'force run' a PR which the description turns off the CI. To run the CI, the PR should instead be updated with a ``true`` value, and a running message can be posted in following PR comments to trigger tests once again. --> birdhouse_daccs_configs_branch: master birdhouse_skip_ci: false
2 parents d5a81a2 + 92b23f3 commit 3598375

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

CHANGES.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,36 @@
1515
[Unreleased](https://github.com/bird-house/birdhouse-deploy/tree/master) (latest)
1616
------------------------------------------------------------------------------------------------------------------
1717

18-
[//]: # (list changes here, using '-' for each new entry, remove this when items are added)
18+
## Changes
19+
20+
- Add script that automatically updates postgres databases to a later version
21+
22+
In anticipation of upgrading postgres databases in the future, this introduces a script that automatically
23+
upgrades postgres databases using the backup/restore process.
24+
25+
This includes magpie and all WPS birds that use the postgres component. This does not include test component
26+
like `optional-components/generic_bird` and will not update custom components (ones not from this repository).
27+
28+
Test components are not assumed to have persistent data that needs to be updated and we cannot guarantee that
29+
other postgres databases used by components outside this repository do not require additional steps (data
30+
migrations) in order to comply with a different version of postgres.
31+
32+
It will update postgres databases to the version specified by the `POSTGRES_VERSION_UPDATE` environment variable.
33+
All of the old database files will be copied to a temporary directory in case you want to inspect them or revert
34+
this operation later on. To specify which directory to write these backups to set the `BIRDHOUSE_BACKUP_DATA_DIR` variable
35+
(default: `${TMPDIR:-/tmp}/birdhouse-postgres-migrate-backup/`)
36+
37+
Note that backups in the form of database dumps will also be written to the named volume or directory specified
38+
by the `BIRDHOUSE_BACKUP_VOLUME` variable.
39+
40+
For example, to update the current postgres databases to version 18.1 and write backups to `/tmp/test/`
41+
42+
```sh
43+
$ POSTGRES_VERSION_UPDATE=18.1 BIRDHOUSE_BACKUP_DATA_DIR=/tmp/test/ birdhouse/scripts/update-postgresh.sh
44+
```
45+
46+
In a future update we can update the postgres versions and tell users to run this script first in order to safely
47+
migrate data from one version to the next.
1948

2049
[2.20.4](https://github.com/bird-house/birdhouse-deploy/tree/2.20.4) (2026-01-16)
2150
------------------------------------------------------------------------------------------------------------------

birdhouse/components/magpie/default.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export MAGPIE_VERSION=4.2.0
99
export MAGPIE_IMAGE='pavics/magpie:${MAGPIE_VERSION}'
1010
export MAGPIE_IMAGE_URI='registry.hub.docker.com/${MAGPIE_IMAGE}'
1111

12+
export MAGPIE_POSTGRES_VERSION="9.6"
13+
export MAGPIE_POSTGRES_DOCKER=postgres
14+
export MAGPIE_POSTGRES_IMAGE='${MAGPIE_POSTGRES_DOCKER}:${MAGPIE_POSTGRES_VERSION}'
15+
1216
export MAGPIE_DB_NAME="magpiedb"
1317

1418
export MAGPIE_PERSIST_DIR='${BIRDHOUSE_DATA_PERSIST_ROOT}/magpie_persist'
@@ -35,6 +39,7 @@ export DELAYED_EVAL="
3539
MAGPIE_PERSIST_DIR
3640
MAGPIE_IMAGE
3741
MAGPIE_IMAGE_URI
42+
MAGPIE_POSTGRES_IMAGE
3843
"
3944

4045
# add any new variables not already in 'VARS' or 'OPTIONAL_VARS' that must be replaced in templates here

birdhouse/components/magpie/docker-compose-extra.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ services:
4343
start_interval: 5s
4444

4545
postgres-magpie:
46-
image: postgres:9.6
46+
image: ${MAGPIE_POSTGRES_IMAGE}
4747
container_name: postgres-magpie
4848
# ports: # should not be exposed directly, enable 'optional-components/database-external-ports' as needed
4949
env_file:

birdhouse/components/postgres/default.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# must use single quotes to avoid early expansion before overrides in env.local
55
# are applied and must be added to the list of DELAYED_EVAL.
66

7+
export POSTGRES_VERSION="9.6"
8+
export POSTGRES_DOCKER=postgres
9+
export POSTGRES_IMAGE='${POSTGRES_DOCKER}:${POSTGRES_VERSION}'
10+
711
export POSTGRES_DATA_DIR='${BIRDHOUSE_DATA_PERSIST_ROOT}/frontend_persist'
812
export __DEFAULT_BIRDHOUSE_POSTGRES_DB=birdhouse
913
export BIRDHOUSE_POSTGRES_DB='${__DEFAULT_BIRDHOUSE_POSTGRES_DB}'
@@ -12,6 +16,7 @@ export DELAYED_EVAL="
1216
$DELAYED_EVAL
1317
POSTGRES_DATA_DIR
1418
BIRDHOUSE_POSTGRES_DB
19+
POSTGRES_IMAGE
1520
"
1621

1722
# add any new variables not already in 'VARS' or 'OPTIONAL_VARS' that must be replaced in templates here

birdhouse/components/postgres/docker-compose-extra.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ x-logging:
88

99
services:
1010
postgres:
11-
image: postgres:9.6
11+
image: ${POSTGRES_IMAGE}
1212
container_name: postgres
1313
# ports: # should not be exposed directly, enable 'optional-components/database-external-ports' as needed
1414
volumes:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env sh
2+
3+
# This script updates all postgres databases that are used by components in this repository.
4+
# This includes magpie and all WPS birds that use the postgres component.
5+
# This does not include test component like optional-components/generic_bird and will not update
6+
# custom components (ones not from this repository).
7+
#
8+
# It will update postgres databases to the version specified by the POSTGRES_VERSION_UPDATE
9+
# environment variable.
10+
# All of the old database files will be copied to a temporary directory in case you want to inspect
11+
# them or revert this operation later on. To specify which directory to write these backups to
12+
# set the BIRDHOUSE_BACKUP_DATA_DIR variable (default: ${TMPDIR:-/tmp}/birdhouse-postgres-migrate-backup/)
13+
# Note that backups in the form of database dumps will also be written to the named volume or directory
14+
# specified by the BIRDHOUSE_BACKUP_VOLUME variable.
15+
#
16+
# For example, to update the current postgres databases to version 18.1 and write backups to /tmp/test/
17+
#
18+
# $ POSTGRES_VERSION_UPDATE=18.1 BIRDHOUSE_BACKUP_DATA_DIR=/tmp/test/ ./update-postgresh.sh
19+
#
20+
# This script will not update other postgres databases that may be present in this deployment but it can
21+
# be used as a starting point to figure out how to update other databases. For example, it may be possible
22+
# to update the postgres database used by generic_bird with (untested, please proceed with caution):
23+
#
24+
# $ birdhouse backup create --no-restic -a generic_bird
25+
# $ birdhouse compose down
26+
# $ ${THIS_DIR}/backup-datavolume.sh birdhouse_postgres_generic_bird "${BIRDHOUSE_BACKUP_DATA_DIR}"
27+
# $ docker volume rm birdhouse_postgres_generic_bird
28+
# $ GENERIC_BIRD_POSTGRES_IMAGE=postgres:18.1 birdhouse compose up -d
29+
# $ birdhouse backup restore --no-restic -a generic_bird
30+
#
31+
32+
THIS_FILE="$(readlink -f "$0" || realpath "$0")"
33+
THIS_DIR="$(dirname "${THIS_FILE}")"
34+
BIRDHOUSE_EXE="${THIS_DIR}/../../bin/birdhouse"
35+
36+
# load logging functions
37+
eval $(${BIRDHOUSE_EXE} configs --print-log-command)
38+
39+
: ${POSTGRES_VERSION_UPDATE:?"$(log ERROR "POSTGRES_VERSION_UPDATE must be set")"}
40+
41+
BIRDHOUSE_BACKUP_DATA_DIR="${BIRDHOUSE_BACKUP_DATA_DIR:-"${TMPDIR:-/tmp}"/birdhouse-postgres-migrate-backup/}"
42+
POSTGRES_COMPONENTS="-a magpie -a $(birdhouse -q configs -c 'echo $POSTGRES_DATABASES_TO_CREATE' | sed 's/ / -a /g')"
43+
44+
log INFO "Migrating postgres databases to version ${POSTGRES_VERSION_UPDATE}"
45+
log INFO "Postgres databases for the following components will be updated: $(echo "${POSTGRES_COMPONENTS}" | sed 's/[[:space:]]*-a[[:space:]]*/ /g')"
46+
log INFO "Current databases files will be backed up to the directory: ${BIRDHOUSE_BACKUP_DATA_DIR}"
47+
48+
log INFO "Starting the birdhouse stack in order to backup existing databases."
49+
${BIRDHOUSE_EXE} compose up -d
50+
51+
log INFO "Backing up postgres databases from the following components: $(echo "${POSTGRES_COMPONENTS}" | sed 's/[[:space:]]*-a[[:space:]]*/ /g')"
52+
${BIRDHOUSE_EXE} backup create --no-restic ${POSTGRES_COMPONENTS}
53+
54+
log INFO "Stopping the birdhouse stack in order to update postgres version"
55+
${BIRDHOUSE_EXE} compose down
56+
57+
log INFO "Backing up all postgres data to ${BIRDHOUSE_BACKUP_DATA_DIR}."
58+
eval "$(${BIRDHOUSE_EXE} -q configs -c 'echo "MAGPIE_PERSIST_DIR=$MAGPIE_PERSIST_DIR; POSTGRES_DATA_DIR=$POSTGRES_DATA_DIR"')"
59+
60+
mkdir -p ${BIRDHOUSE_BACKUP_DATA_DIR}
61+
mv "${MAGPIE_PERSIST_DIR}" "${BIRDHOUSE_BACKUP_DATA_DIR}"
62+
mv "${POSTGRES_DATA_DIR}" "${BIRDHOUSE_BACKUP_DATA_DIR}"
63+
64+
MAGPIE_POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} ${BIRDHOUSE_EXE} compose up -d
65+
66+
${BIRDHOUSE_EXE} backup restore --no-restic ${POSTGRES_COMPONENTS}
67+
68+
log WARN "Migration is now complete. Please ensure that the data has been upgraded properly.
69+
If you are satisfied that the databases have been updated properly please add the following to your local environment file:
70+
71+
export MAGPIE_POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE}
72+
export POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE}
73+
74+
If you are not satified that the databases have been updated properly and you wish to revert these changes, you can do so by running:
75+
76+
${BIRDHOUSE_EXE} compose down
77+
rm -r "${MAGPIE_PERSIST_DIR}"
78+
rm -r "${POSTGRES_DATA_DIR}"
79+
mv "${BIRDHOUSE_BACKUP_DATA_DIR}/$(basename "${MAGPIE_PERSIST_DIR}")" "${MAGPIE_PERSIST_DIR}"
80+
mv "${BIRDHOUSE_BACKUP_DATA_DIR}/$(basename "${POSTGRES_DATA_DIR}")" "${POSTGRES_DATA_DIR}"
81+
${BIRDHOUSE_EXE} compose up -d
82+
"

0 commit comments

Comments
 (0)