Skip to content

Commit ef9fb08

Browse files
committed
python: tools for writing upgrades and downgrade
adapted from [the ruby instructions](https://github.com/github/codeql/blob/main/ruby/doc/prepare-db-upgrade.md)
1 parent 36e18d5 commit ef9fb08

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

python/tools/prepare-db-downgrade.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/sh
2+
#
3+
# Prepare the downgrade script directory for a Python database schema downgrade.
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 downgrade script.
18+
Usage: ${app_name} [--prev_hash <COMMITISH>]"
19+
20+
--prev-hash <COMMITISH>
21+
Hash/branch to use to get SHA1 for previous DB scheme.
22+
Default: origin/main
23+
24+
Must be run within the git repo needing an update.
25+
EOF
26+
exit "${exit_code}"
27+
}
28+
29+
prev_hash="origin/main"
30+
31+
while [ $# -gt 0 ]; do
32+
case "$1" in
33+
-x)
34+
set -x
35+
;;
36+
-h | --help)
37+
usage 0
38+
;;
39+
--prev-hash)
40+
if [ $# -eq 1 ]; then
41+
usage 2 "--prev-hash requires Commit/Branch option"
42+
fi
43+
shift
44+
prev_hash="$1"
45+
;;
46+
--)
47+
shift
48+
break
49+
;;
50+
-*)
51+
usage 2 "Unrecognised option: $1"
52+
;;
53+
*)
54+
break
55+
;;
56+
esac
57+
shift
58+
done
59+
60+
if [ $# -gt 0 ]; then
61+
usage 2 "Unrecognised operand: $1"
62+
fi
63+
64+
scheme_file_name="semmlecode.python.dbscheme"
65+
scheme_file="ql/lib/${scheme_file_name}"
66+
downgrade_root="ql/downgrades"
67+
68+
check_hash_valid()
69+
{
70+
if [ ${#2} -ne 40 ]; then
71+
echo "Did not get expected $1 hash: $2" >&2
72+
exit 2
73+
fi
74+
}
75+
76+
# Get the hash of the previous and current DB Schema files
77+
prev_hash="$(git show "${prev_hash}:python/${scheme_file}" | git hash-object --stdin)"
78+
check_hash_valid previous "${prev_hash}"
79+
current_hash="$(git hash-object "${scheme_file}")"
80+
check_hash_valid current "${current_hash}"
81+
if [ "${current_hash}" = "${prev_hash}" ]; then
82+
echo "No work to be done."
83+
exit
84+
fi
85+
86+
# Copy current and new dbscheme into the downgrade dir
87+
downgradedir="${downgrade_root}/${current_hash}"
88+
mkdir -p "${downgradedir}"
89+
90+
cp "${scheme_file}" "${downgradedir}/old.dbscheme"
91+
git cat-file blob "${prev_hash}" > "${downgradedir}/${scheme_file_name}"
92+
93+
# Create the template downgrade.properties file.
94+
cat <<EOF > "${downgradedir}/downgrade.properties"
95+
description: <INSERT DESCRIPTION HERE>
96+
compatibility: full|backwards|partial|breaking
97+
EOF
98+
99+
# Tell user what we've done
100+
cat <<EOF
101+
Created downgrade directory here:
102+
${downgradedir}
103+
104+
Please update:
105+
${downgradedir}/downgrade.properties
106+
with appropriate downgrade instructions
107+
EOF

python/tools/prepare-db-upgrade.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/sh
2+
#
3+
# Prepare the upgrade script directory for a Python database 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>]"
19+
20+
--prev-hash <COMMITISH>
21+
Hash/branch to use to get SHA1 for previous DB scheme.
22+
Default: origin/main
23+
24+
Must be run within the git repo needing an update.
25+
EOF
26+
exit "${exit_code}"
27+
}
28+
29+
prev_hash="origin/main"
30+
31+
while [ $# -gt 0 ]; do
32+
case "$1" in
33+
-x)
34+
set -x
35+
;;
36+
-h | --help)
37+
usage 0
38+
;;
39+
--prev-hash)
40+
if [ $# -eq 1 ]; then
41+
usage 2 "--prev-hash requires Commit/Branch option"
42+
fi
43+
shift
44+
prev_hash="$1"
45+
;;
46+
--)
47+
shift
48+
break
49+
;;
50+
-*)
51+
usage 2 "Unrecognised option: $1"
52+
;;
53+
*)
54+
break
55+
;;
56+
esac
57+
shift
58+
done
59+
60+
if [ $# -gt 0 ]; then
61+
usage 2 "Unrecognised operand: $1"
62+
fi
63+
64+
scheme_file="ql/lib/semmlecode.python.dbscheme"
65+
upgrade_root="ql/upgrades"
66+
67+
check_hash_valid()
68+
{
69+
if [ ${#2} -ne 40 ]; then
70+
echo "Did not get expected $1 hash: $2" >&2
71+
exit 2
72+
fi
73+
}
74+
75+
# Get the hash of the previous and current DB Schema files
76+
prev_hash="$(git show "${prev_hash}:python/${scheme_file}" | git hash-object --stdin)"
77+
check_hash_valid previous "${prev_hash}"
78+
current_hash="$(git hash-object "${scheme_file}")"
79+
check_hash_valid current "${current_hash}"
80+
if [ "${current_hash}" = "${prev_hash}" ]; then
81+
echo "No work to be done."
82+
exit
83+
fi
84+
85+
# Copy current and new dbscheme into the upgrade dir
86+
upgradedir="${upgrade_root}/${prev_hash}"
87+
mkdir -p "${upgradedir}"
88+
89+
cp "${scheme_file}" "${upgradedir}"
90+
git cat-file blob "${prev_hash}" > "${upgradedir}/old.dbscheme"
91+
92+
# Create the template upgrade.properties file.
93+
cat <<EOF > "${upgradedir}/upgrade.properties"
94+
description: <INSERT DESCRIPTION HERE>
95+
compatibility: full|backwards|partial|breaking
96+
EOF
97+
98+
# Tell user what we've done
99+
cat <<EOF
100+
Created upgrade directory here:
101+
${upgradedir}
102+
103+
Please update:
104+
${upgradedir}/upgrade.properties
105+
with appropriate upgrade instructions
106+
EOF

0 commit comments

Comments
 (0)