-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtable_version-loader.bash
108 lines (99 loc) · 2.33 KB
/
table_version-loader.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
tgt_schema=table_version
tgt_db=
ext_mode=on
ext_name=table_version
ext_dir=@@LOCAL_SHAREDIR@@
tpl_file=
ver=
if test -n "$TABLE_VERSION_EXT_DIR"
then
ext_dir="$TABLE_VERSION_EXT_DIR"
fi
while test -n "$1"
do
if test "$1" = "--no-extension"
then
ext_mode=off
elif test "$1" = "--version"
then
shift
ver=$1
elif test -z "${tgt_db}"
then
tgt_db=$1
else
echo "Unused argument $1" >&2
fi
shift
done
if test -z "${ver}"
then
# tpl_file is expected to have the following format:
# table_version-1.4.0dev.sql.tpl
ver="$(echo "${ext_dir}/${ext_name}"-*.sql.tpl | sed --expression="s/^.*${ext_name}-//;s/\.sql\.tpl//" | sort --version-sort | tail --lines=1)"
if test -z "${ver}"
then
echo "Cannot find template loader, maybe set TABLE_VERSION_EXT_DIR?" >&2
exit 1
fi
fi
if test -z "$tgt_db"
then
echo "Usage: $0 [--no-extension] [--version <ver>] { <dbname> | - }" >&2
exit 1
fi
dblbl=${tgt_db}
if [ "$dblbl" = "-" ]
then
dblbl="(stdout)"
fi
echo "Loading ${ext_name} ${ver} in ${dblbl}.${tgt_schema} (ext_mode ${ext_mode})" >&2
{
if test "${ext_mode}" = 'on'
then cat<<EOF
DO \$\$
DECLARE
OLDVER TEXT;
BEGIN
IF EXISTS (
SELECT * FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
WHERE c.relnamespace = n.oid AND n.nspname = 'table_version'
AND c.relname = 'revision'
)
THEN
SELECT extversion FROM pg_catalog.pg_extension
WHERE extname = 'table_version'
INTO OLDVER;
IF OLDVER IS NOT NULL
THEN
IF OLDVER = '${ver}' THEN
ALTER EXTENSION ${ext_name} UPDATE TO '${ver}next';
END IF;
ALTER EXTENSION ${ext_name} UPDATE TO '${ver}';
ELSE
CREATE EXTENSION ${ext_name} VERSION '${ver}';
END IF;
ELSE
CREATE EXTENSION ${ext_name} VERSION '${ver}';
END IF;
END
\$\$ LANGUAGE 'plpgsql';
EOF
else
tpl_file=${ext_dir}/${ext_name}-${ver}.sql.tpl
if test -r "$tpl_file"
then
echo "Using template file ${tpl_file}" >&2
sed --expression="s/@extschema@/${tgt_schema}/g" "$tpl_file"
else
echo "Template file ${tpl_file} is not readable or does not exist" >&2
exit 1
fi
fi
} | if [ "$tgt_db" = "-" ]
then
cat
else
psql -XtA --set ON_ERROR_STOP=1 "$tgt_db" -o /dev/null
fi