Skip to content

Commit 5b6732d

Browse files
committed
chore: update pg upgrade scripts
1 parent 1e29a31 commit 5b6732d

File tree

4 files changed

+30
-57
lines changed

4 files changed

+30
-57
lines changed

ansible/files/admin_api_scripts/pg_upgrade_complete.sh

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ EXTENSIONS_TO_REENABLE=(
1313
"pg_graphql"
1414
)
1515

16-
set -euo pipefail
16+
set -eEuo pipefail
1717

1818
run_sql() {
19-
STATEMENT=$1
20-
psql -h localhost -U supabase_admin -d postgres -c "$STATEMENT"
19+
psql -h localhost -U supabase_admin -d postgres "$@"
2120
}
2221

2322
cleanup() {
@@ -30,6 +29,11 @@ cleanup() {
3029
}
3130

3231
function complete_pg_upgrade {
32+
if [ -f /tmp/pg-upgrade-status ]; then
33+
echo "Upgrade job already started. Bailing."
34+
exit 0
35+
fi
36+
3337
echo "running" > /tmp/pg-upgrade-status
3438

3539
mount -a -v
@@ -42,21 +46,19 @@ function complete_pg_upgrade {
4246
service postgresql start
4347

4448
for EXTENSION in "${EXTENSIONS_TO_REENABLE[@]}"; do
45-
run_sql "CREATE EXTENSION IF NOT EXISTS ${EXTENSION} CASCADE;"
49+
run_sql -c "CREATE EXTENSION IF NOT EXISTS ${EXTENSION} CASCADE;"
4650
done
4751

52+
if [ -d /data/sql ]; then
53+
for FILE in /data/sql/*.sql; do
54+
run_sql -f $FILE
55+
done
56+
fi
57+
4858
sleep 5
4959
service postgresql restart
5060

51-
if [[ $(systemctl is-active gotrue) == "inactive" ]]; then
52-
echo "starting gotrue"
53-
systemctl start --no-block gotrue || true
54-
fi
55-
56-
if [[ $(systemctl is-active postgrest) == "inactive" ]]; then
57-
echo "starting postgrest"
58-
systemctl start --no-block postgrest || true
59-
fi
61+
start_vacuum_analyze
6062

6163
echo "Upgrade job completed"
6264
}
@@ -68,5 +70,4 @@ function start_vacuum_analyze {
6870

6971
trap cleanup ERR
7072

71-
complete_pg_upgrade >>/var/log/pg-upgrade-complete.log 2>&1
72-
start_vacuum_analyze >>/var/log/pg-upgrade-complete.log 2>&1 &
73+
complete_pg_upgrade >>/var/log/pg-upgrade-complete.log 2>&1 &

ansible/files/admin_api_scripts/pg_upgrade_initiate.sh

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,6 @@ run_sql() {
2424
psql -h localhost -U supabase_admin -d postgres -c "$STATEMENT"
2525
}
2626

27-
function shutdown_services {
28-
if [[ $(systemctl is-active gotrue) == "active" ]]; then
29-
echo "stopping gotrue"
30-
systemctl stop gotrue || true
31-
fi
32-
33-
if [[ $(systemctl is-active postgrest) == "active" ]]; then
34-
echo "stopping postgrest"
35-
systemctl stop postgrest || true
36-
fi
37-
}
38-
39-
function start_services {
40-
if [[ $(systemctl is-active gotrue) == "inactive" ]]; then
41-
echo "starting gotrue"
42-
systemctl start gotrue || true
43-
fi
44-
45-
if [[ $(systemctl is-active postgrest) == "inactive" ]]; then
46-
echo "starting postgrest"
47-
systemctl start postgrest || true
48-
fi
49-
}
50-
5127
cleanup() {
5228
UPGRADE_STATUS=${1:-"failed"}
5329
EXIT_CODE=${?:-0}
@@ -71,8 +47,6 @@ cleanup() {
7147

7248
run_sql "ALTER USER postgres WITH NOSUPERUSER;"
7349

74-
start_services
75-
7650
umount $MOUNT_POINT
7751
echo "${UPGRADE_STATUS}" > /tmp/pg-upgrade-status
7852

@@ -82,12 +56,14 @@ cleanup() {
8256
function initiate_upgrade {
8357
echo "running" > /tmp/pg-upgrade-status
8458

85-
shutdown_services
86-
8759
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
8860
# excluding nvme0 since it is the root disk
8961
BLOCK_DEVICE=$(lsblk -dprno name,size,mountpoint,type | grep "disk" | grep -v "nvme0" | awk 'NF==3 { print $1; }')
9062

63+
if [ -x "$(command -v blockdev)" ]; then
64+
blockdev --rereadpt "$BLOCK_DEVICE"
65+
fi
66+
9167
mkdir -p "$MOUNT_POINT"
9268
mount "$BLOCK_DEVICE" "$MOUNT_POINT"
9369
resize2fs "$BLOCK_DEVICE"
@@ -129,7 +105,9 @@ function initiate_upgrade {
129105
WORKERS=$(nproc | awk '{ print ($1 == 1 ? 1 : $1 - 1) }')
130106

131107
# upgrade job outputs a log in the cwd; needs write permissions
132-
cd /tmp
108+
mkdir -p /tmp/pg_upgrade
109+
chown -R postgres:postgres /tmp/pg_upgrade
110+
cd /tmp/pg_upgrade
133111

134112
UPGRADE_COMMAND=$(cat <<EOF
135113
time ${PGBINNEW}/pg_upgrade \
@@ -158,8 +136,12 @@ EOF
158136
su -c "$UPGRADE_COMMAND" -s $SHELL postgres
159137

160138
# copying custom configurations
161-
mkdir -p $MOUNT_POINT/conf
162-
cp -R /etc/postgresql-custom/* $MOUNT_POINT/conf/
139+
mkdir -p "$MOUNT_POINT/conf"
140+
cp -R /etc/postgresql-custom/* "$MOUNT_POINT/conf/"
141+
142+
# copy sql files generated by pg_upgrade
143+
mkdir -p "$MOUNT_POINT/sql"
144+
cp /tmp/pg_upgrade/*.sql "$MOUNT_POINT/sql/" || true
163145

164146
cleanup "complete"
165147
}

ansible/files/admin_api_scripts/pg_upgrade_prepare.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99

1010
set -euo pipefail
1111

12-
if [[ $(systemctl is-active gotrue) == "active" ]]; then
13-
echo "stopping gotrue"
14-
systemctl stop gotrue || true
15-
fi
16-
17-
if [[ $(systemctl is-active postgrest) == "active" ]]; then
18-
echo "stopping postgrest"
19-
systemctl stop postgrest || true
20-
fi
21-
2212
systemctl stop postgresql
2313

2414
cp /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key

common.vars.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
postgres-version = "15.1.0.28"
1+
postgres-version = "15.1.0.29"

0 commit comments

Comments
 (0)