Skip to content

Commit 1209bbd

Browse files
committed
Add version of prepare-db-upgrade.sh supporting multiple languages
1 parent 5f0ab52 commit 1209bbd

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

misc/scripts/prepare-db-upgrade.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/bin/sh
2+
#
3+
# Prepare the upgrade and downgrade script directories for a DB Schema upgrade.
4+
5+
set -e
6+
set -u
7+
8+
app_name="$(basename "$0")"
9+
10+
usage()
11+
{
12+
exit_code="$1"
13+
shift
14+
15+
cat >&2 <<EOF
16+
${app_name}: $@
17+
${app_name}: Generate skeleton upgrade script.
18+
Usage: ${app_name} [--prev_hash <COMMITISH>] [--lang <LANG>]"
19+
20+
--prev-hash <COMMITISH>
21+
Hash/branch to use to get SHA1 for previous DB scheme.
22+
Default: origin/main
23+
24+
--lang <LANG>
25+
Language to update the schema for.
26+
27+
Must be run within the git repo needing an update.
28+
EOF
29+
exit "${exit_code}"
30+
}
31+
32+
prev_hash="origin/main"
33+
lang="cpp"
34+
35+
while [ $# -gt 0 ]; do
36+
case "$1" in
37+
-x)
38+
set -x
39+
;;
40+
-h | --help)
41+
usage 0
42+
;;
43+
--prev-hash)
44+
if [ $# -eq 1 ]; then
45+
usage 2 "--prev-hash requires Commit/Branch option"
46+
fi
47+
shift
48+
prev_hash="$1"
49+
;;
50+
--lang)
51+
if [ $# -eq 1 ]; then
52+
usage 2 "--lang requires a language option"
53+
fi
54+
shift
55+
lang="$1"
56+
;;
57+
--)
58+
shift
59+
break
60+
;;
61+
-*)
62+
usage 2 "Unrecognised option: $1"
63+
;;
64+
*)
65+
break
66+
;;
67+
esac
68+
shift
69+
done
70+
71+
if [ $# -gt 0 ]; then
72+
usage 2 "Unrecognised operand: $1"
73+
fi
74+
75+
top_level="$(git rev-parse --show-superproject-working-tree)"
76+
77+
if [ "x${top_level}" = "x" ]; then
78+
top_level="$(git rev-parse --show-toplevel)"
79+
fi
80+
81+
if [ "x${top_level}" = "x" ]; then
82+
usage 2 "Not in a code repository."
83+
fi
84+
85+
case "${lang}" in
86+
java)
87+
scheme_file="${lang}/ql/lib/config/semmlecode.dbscheme"
88+
;;
89+
csharp | cpp | javascript | python)
90+
scheme_file="${lang}/ql/lib/semmlecode.${lang}.dbscheme"
91+
;;
92+
ruby)
93+
scheme_file="${lang}/ql/lib/${lang}.dbscheme"
94+
;;
95+
*)
96+
usage 2 "Unrecognised language: ${lang}"
97+
;;
98+
esac
99+
100+
qldir="${top_level}/ql"
101+
102+
upgrade_root="${qldir}/${lang}/ql/lib/upgrades"
103+
downgrade_root="${qldir}/${lang}/downgrades"
104+
105+
check_hash_valid()
106+
{
107+
len=0
108+
checking="$2"
109+
while [ "x${checking}" != "x" ]; do
110+
len=$((len + 1))
111+
checking="${checking%?}"
112+
done
113+
114+
if [ ${len} -ne 40 ]; then
115+
echo "Did not get expected $1 hash: $2" >&2
116+
exit 2
117+
fi
118+
}
119+
120+
# Get the hash of the previous and current DB Schema files
121+
cd "${qldir}"
122+
prev_hash="$(git show "${prev_hash}:${scheme_file}" | git hash-object --stdin)"
123+
check_hash_valid previous "${prev_hash}"
124+
current_hash="$(git hash-object "${scheme_file}")"
125+
check_hash_valid current "${current_hash}"
126+
if [ "${current_hash}" = "${prev_hash}" ]; then
127+
echo "No work to be done."
128+
exit
129+
fi
130+
131+
# Copy current and new dbscheme into the upgrade dir
132+
upgradedir="${upgrade_root}/${prev_hash}"
133+
mkdir -p "${upgradedir}"
134+
135+
cp "${scheme_file}" "${upgradedir}"
136+
git cat-file blob "${prev_hash}" > "${upgradedir}/old.dbscheme"
137+
138+
# Create the template upgrade.properties file in the upgrade dir.
139+
cat <<EOF > "${upgradedir}/upgrade.properties"
140+
description: <INSERT DESCRIPTION HERE>
141+
compatibility: full|backwards|partial|breaking
142+
EOF
143+
144+
# Copy current and new dbscheme into the downgrade dir
145+
downgradedir="${downgrade_root}/${current_hash}"
146+
mkdir -p "${downgradedir}"
147+
148+
cp "${scheme_file}" "${downgradedir}/old.dbscheme"
149+
git cat-file blob "${prev_hash}" > "${downgradedir}/$(basename "${scheme_file}")"
150+
151+
# Create the template upgrade.properties file in the downgrade dir.
152+
cat <<EOF > "${downgradedir}/upgrade.properties"
153+
description: <INSERT DESCRIPTION HERE>
154+
compatibility: full|backwards|partial|breaking
155+
EOF
156+
157+
# Tell user what we've done
158+
cat <<EOF
159+
Created upgrade directory here:
160+
${upgradedir}
161+
Created downgrade directory here:
162+
${downgradedir}
163+
164+
Please update:
165+
${upgradedir}/upgrade.properties
166+
${downgradedir}/upgrade.properties
167+
with appropriate instructions
168+
EOF

0 commit comments

Comments
 (0)