Skip to content

Commit ae89d20

Browse files
committed
Fixed problems with order of update scripts. Added --check mode into pgpro_upgrade
1 parent 6c469b6 commit ae89d20

File tree

8 files changed

+43
-8
lines changed

8 files changed

+43
-8
lines changed

src/pgpro-upgrade/README

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ because it starts postgres in single-user mode to perform upgrades.
2727
It is safe to invoke script multiple times, because it does check for
2828
features it is going to add.
2929

30+
If script is invoked with **--check** argument, it doesn't do any
31+
changes in the database, just returns 0 if no changes is needed,
32+
and 1 if base needs to be upgraded.
33+
3034
HOW TO ADD NEW FEATURE
3135
----------------------
3236

@@ -44,3 +48,16 @@ which returns single boolean value (i.e. one column and one row). This
4448
value should be 'f' if feature is not found in the database and should
4549
be installed, and 't' if it already exists.
4650

51+
Note that scirpts are invoked using postgres single-user mode, not using
52+
psql. So, each SQL statement should be in one line or line endings
53+
should be \ esaped.
54+
55+
Last line of the script should contain newline at the end.
56+
57+
Script naming convention
58+
------------------------
59+
60+
Scripts are executed in the lexicographical order of their names.
61+
So, please start name of script with 3-digit number, next one to last
62+
used, followed by dash.
63+

src/pgpro-upgrade/pgpro_upgrade

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
# 3. sed
55
if [ -z "$PGDATA" ]; then
66
echo "PGDATA environment variable is not set. Stop." 1>&2
7-
exit 1
7+
exit 2
88
fi
99
PGBIN="`echo $0|sed 's![^/\\]*$!!'`"
10-
10+
if [ "$1" = "--check" ]; then
11+
check=1
12+
else
13+
check=
14+
fi
1115
echo "$PGBIN"
1216

1317
case "$PGBIN" in
@@ -38,17 +42,31 @@ if [ -z "$BASELIST" ]; then
3842
exit 1
3943
fi
4044

41-
echo "Upgrading databases $BASELIST"
45+
[ -z "$check" ]&& echo "Upgrading databases $BASELIST"
4246

4347
#Search for upgrade scripts
48+
need_upgrade=0
4449
for i in "$DIR"/*.test; do
45-
create="`echo "$i" |sed 's/\.test$/.sql/'`"
4650
found=`< "$i" "${PGBIN}postgres" --single template0 |
4751
sed -n 's/^[ ]*1: [^ ]* = "\([ft]\)"[ ].*$/\1/p'`
4852
if [ "$found" = "f" ]; then
49-
for base in $BASELIST; do
50-
echo "Executing $create in $base"
51-
< "$create" "${PGBIN}postgres" --single "$base"
52-
done
53+
if [ -z "$check" ]; then
54+
create="`echo "$i" |sed 's/\.test$/.sql/'`"
55+
for base in $BASELIST; do
56+
echo "Executing $create in $base"
57+
< "$create" "${PGBIN}postgres" --single "$base" >/dev/null
58+
59+
done
60+
else
61+
need_upgrade=1
62+
fi
5363
fi
5464
done
65+
if [ -n "$check" ]; then
66+
if [ $need_upgrade -eq 0 ]; then
67+
echo "All Postgres Pro specific updates are appied" 1>&2
68+
else
69+
echo "Database needs upgrade"
70+
fi
71+
exit $need_upgrade
72+
fi

0 commit comments

Comments
 (0)