-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: Add script to upgrade Postgres 13 data to 14 #34317
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9ff20c
chore: Add script to upgrade Postgres 13 data to 14
sharat87 d843647
Don't do anything if Postgres is v13
sharat87 0cfc63c
Assert sample table contents in the tester script
sharat87 b38f794
revert
sharat87 65d90fe
Discard changes to Dockerfile
sharat87 b3e24d5
Revert "Discard changes to Dockerfile"
sharat87 9c75da5
as on ee
sharat87 54e9157
Discard changes to Dockerfile
sharat87 8a62f96
Small fixes in tester script
sharat87 7c5ab1e
Check for running Postgres in the upgrade script
sharat87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
srix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # This script will upgrade Postgres to the "current" version of Postgres, if needed. | ||
|
|
||
| # Assumptions: | ||
| # 1. Postgres is currently not running. The caller of this script ensures that _no_ version of Postgres server is currently running. | ||
| # 2. The newest version of Postgres we want to use, is already installed. | ||
| # 3. Postgres is installed via apt package manager only. | ||
|
|
||
| # Contract: | ||
| # 1. Don't install old version of Postgres, if it's already installed. | ||
| # 2. Use absolute paths to all Postgres executables, don't rely on any of them to be coming from "\$PATH". | ||
| # 3. Be idempotent across versions. | ||
| # 4. When we can't proceed due to any exceptional scenarios, communicate clearly. | ||
| # 5. Mark old/stale/deprecated data with a date, so it can be deleted with confidence later. | ||
|
|
||
| # Check if any Postgres server is running | ||
| if pgrep -x "postgres" > /dev/null; then | ||
| echo "Error: A Postgres server is currently running. Please stop it before proceeding with the upgrade." | ||
| exit 1 | ||
| fi | ||
|
|
||
| postgres_path=/usr/lib/postgresql | ||
|
|
||
| pg_data_dir=/appsmith-stacks/data/postgres/main | ||
|
|
||
| old_version="" | ||
| if [[ -f "$pg_data_dir/PG_VERSION" ]]; then | ||
| old_version="$(cat "$pg_data_dir/PG_VERSION")" | ||
| fi | ||
|
|
||
| if [[ -z "$old_version" ]]; then | ||
| tlog "No existing Postgres data found, not upgrading anything." >&2 | ||
| exit | ||
| fi | ||
|
|
||
| if [[ -f "$pg_data_dir/postmaster.pid" ]]; then | ||
| tlog "Previous Postgres was not shutdown cleanly. Please start and stop Postgres $old_version properly with 'supervisorctl' only." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| top_available_version="$(postgres --version | grep -o '[[:digit:]]\+' | head -1)" | ||
|
|
||
| declare -a to_uninstall | ||
| to_uninstall=() | ||
|
|
||
| # 13 to 14 | ||
| if [[ "$old_version" == 13 && "$top_available_version" > "$old_version" ]]; then | ||
| if [[ ! -e "$postgres_path/$old_version" ]]; then | ||
| apt-get update | ||
| apt-get install --yes "postgresql-$old_version" | ||
| to_uninstall+=("postgresql-$old_version") | ||
| fi | ||
|
|
||
| new_version="$((old_version + 1))" | ||
| new_data_dir="$pg_data_dir-$new_version" | ||
|
|
||
| # `pg_upgrade` writes log to current folder. So change to a temp folder first. | ||
| rm -rf "$TMP/pg_upgrade" "$new_data_dir" | ||
| mkdir -p "$TMP/pg_upgrade" "$new_data_dir" | ||
| chown -R postgres "$TMP/pg_upgrade" "$new_data_dir" | ||
| cd "$TMP/pg_upgrade" | ||
|
|
||
| # Required by the temporary Postgres server started by `pg_upgrade`. | ||
| chown postgres /etc/ssl/private/ssl-cert-snakeoil.key | ||
| chmod 0600 /etc/ssl/private/ssl-cert-snakeoil.key | ||
|
|
||
| su postgres --command " | ||
| set -o errexit | ||
| set -o xtrace | ||
| '$postgres_path/$new_version/bin/initdb' --pgdata='$new_data_dir' | ||
| '$postgres_path/$new_version/bin/pg_upgrade' \ | ||
| --old-datadir='$pg_data_dir' \ | ||
| --new-datadir='$new_data_dir' \ | ||
| --old-bindir='$postgres_path/$old_version/bin' \ | ||
| --new-bindir='$postgres_path/$new_version/bin' | ||
| " | ||
|
|
||
| date -u '+%FT%T.%3NZ' > "$pg_data_dir/deprecated-on.txt" | ||
| mv -v "$pg_data_dir" "$pg_data_dir-$old_version" | ||
| mv -v "$new_data_dir" "$pg_data_dir" | ||
|
|
||
| # Dangerous generated script that deletes the now updated data folder. | ||
| rm -fv "$TMP/pg_upgrade/delete_old_cluster.sh" | ||
| fi | ||
|
|
||
| if [[ -n "${#to_uninstall[@]}" ]]; then | ||
| apt-get purge "${to_uninstall[@]}" | ||
| apt-get clean | ||
| fi | ||
|
|
||
| echo "== Fin ==" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #!/bin/bash | ||
|
|
||
| # A script to test Postgres upgrades. WIP. | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
| set -o xtrace | ||
|
|
||
| from_tag=appsmith/appsmith-ce:v1.28 | ||
srix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| to_tag=appsmith/appsmith-ce:latest | ||
|
|
||
| container_name=appsmith-pg-upgrade-test | ||
| port=20080 | ||
|
|
||
| docker rm -f "$container_name" | ||
| docker volume rm --force "$container_name" | ||
sharat87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # TODO: Add `--pull always` for images that have a manifest? | ||
|
|
||
| docker volume create "$container_name" | ||
| docker run \ | ||
| --name "$container_name" \ | ||
| --detach \ | ||
| --publish "$port":80 \ | ||
| --volume "$container_name":/appsmith-stacks \ | ||
| "$from_tag" | ||
|
|
||
| wait-for-supervisor() { | ||
| while ! docker exec "$container_name" test -e /tmp/appsmith/supervisor.sock; do | ||
| sleep 1 | ||
| done | ||
| sleep 2 | ||
| } | ||
|
|
||
| wait-for-supervisor | ||
|
|
||
| docker exec "$container_name" bash -exc ' | ||
| supervisorctl status \ | ||
| | awk '\''$1 != "postgres" && $1 != "stdout" {print $1}'\'' \ | ||
| | xargs supervisorctl stop | ||
|
|
||
| # Insert some sample data | ||
| su postgres -c "psql -h 127.0.0.1 -c \" | ||
| create table t (id serial, name text); | ||
| insert into t values (1, '\''one'\''); | ||
| insert into t values (2, '\''two'\''); | ||
| insert into t values (3, '\''three'\''); | ||
| \"" | ||
|
|
||
| supervisorctl stop postgres | ||
|
|
||
| cat /appsmith-stacks/data/postgres/main/PG_VERSION | ||
sharat87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ' | ||
|
|
||
| docker rm -f "$container_name" | ||
|
|
||
| docker run \ | ||
| --name "$container_name" \ | ||
| --detach \ | ||
| --publish "$port":80 \ | ||
| --volume "$container_name":/appsmith-stacks \ | ||
| "$to_tag" | ||
|
|
||
| wait-for-supervisor | ||
|
|
||
| status=0 | ||
|
|
||
| if [[ 14 != "$(docker exec "$container_name" cat /appsmith-stacks/data/postgres/main/PG_VERSION)" ]]; then | ||
| echo "Version isn't 14" | ||
| status=1 | ||
| else | ||
| sample_table_contents="$(su postgres -c 'psql -h 127.0.0.1 -c "select * from t"')" | ||
| expected_contents=' id | name | ||
| ----+------- | ||
| 1 | one | ||
| 2 | two | ||
| 3 | three | ||
| (3 rows)' | ||
| if ! diff <(echo "$expected_contents") <(su postgres -c 'psql -h 127.0.0.1 -c "select * from t"'); then | ||
| status=1 | ||
| echo "Table contents mismatch. Found this:" | ||
| su postgres -c 'psql -h 127.0.0.1 -c "select * from t"' | ||
| echo "Instead of this:" | ||
| echo "$expected_contents" | ||
| fi | ||
| fi | ||
sharat87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| docker exec -it "$container_name" bash | ||
|
|
||
| docker rm --force "$container_name" | ||
| docker volume rm --force "$container_name" | ||
|
|
||
| exit "$status" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.