Skip to content

Commit 5cd4eb6

Browse files
[linting] Catch unintended errors in check-sql.sh (#13745)
I really borked our SQL linting. This PR is short but it catches a few critical problems. 1. The point of `check-sql.sh` is to detect modifications or deletions of SQL files in PRs and fail if such a change occurs. Currently on `main` it does not detect modifications. In #13456, I removed the `delete-<service>-tables.sql` files (intentionally), so added the `^D` to the `grep` regex to indicate that it is OK to have a deletion. What I inadvertently did though is change the rule from "It's ok to have Additions of any file OR Modifications of estimated-current.sql / delete-<service>-tables.sql" to "It's ok to have Additions OR Modifications OR Deletions of estimated-current.sql / delete-<service>-tables.sql". Really this should have been "It's ok to have Additions OR Modifications of estimated-current.sql OR Deletions of delete-<service>-tables.sql". I've changed it to reflect that rule. 2. Rules currently silently *pass* in CI with an error message that git is not installed. In #13437 I changed the image used to run the linters and inadvertently didn't include `git` which `check-sql.sh` needs to run. Here's how it failed in a sneaky way: - Since `git` is not installed, all calls to `git` fail, but the script is not run with `set -e` so every line of the script is executed - Since `git` lines fail, `modified_sql_file_list` remains empty - Since `modified_sql_file_list` remains empty, it appears to the check at the end that everything checked out - The if statement runs successfully and the script returns with error code 0 To fix this I do a few things: - installed `git` in the linting image - `set -e` by default and only enable `set +e` later on when necessary (because we don't want a failed `git diff` to immediately exit) - Do away with the file checking and instead check the error code of the grep. If nothing survives the grep filter, which means there were no illegal changes made, grep will return with exit code 1. So we treat that exit code as a success.
1 parent d40ac24 commit 5cd4eb6

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

build.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ steps:
14621462
inline: |
14631463
ARG BASE_IMAGE={{ hail_ubuntu_image.image }}
14641464
FROM $BASE_IMAGE
1465-
RUN hail-apt-get-install make
1465+
RUN hail-apt-get-install git make
14661466
COPY hail/python/pinned-requirements.txt hail-requirements.txt
14671467
COPY hail/python/dev/pinned-requirements.txt dev-requirements.txt
14681468
COPY gear/pinned-requirements.txt gear-requirements.txt
@@ -2791,6 +2791,7 @@ steps:
27912791
cp /io/repo/tls/create_certs.py ./ci/test/resources/
27922792
cp /io/repo/tls/create_test_db_config.sh ./ci/test/resources/
27932793
cp /io/repo/pylintrc ./
2794+
cp /io/repo/check-sql.sh ./
27942795
cp /io/repo/pyproject.toml ./
27952796
cp -R /io/repo/docker ./
27962797
cp -R /io/repo/gear ./

check-sql.sh

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
#!/bin/bash
22

3-
# Verify the the working tree modifices no sql files relative to the main
3+
set -e
4+
set -o pipefail
5+
6+
# Verify the the working tree modifies no sql files relative to the main
47
# branch. This will always pass on a deploy because the working tree is an
58
# unmodified copy of the main branch.
69

710
target_treeish=${HAIL_TARGET_SHA:-$(git merge-base main HEAD)}
811

9-
modified_sql_file_list=$(mktemp)
10-
1112
if [ ! -d sql ]; then
1213
echo 'No migrations to check, exiting.'
1314
exit 0
1415
fi
1516

17+
set +e
1618
git diff --name-status $target_treeish sql \
17-
| grep -Ev $'^A|^M|^D\t[^/]+/sql/(estimated-current.sql|delete-[^ ]+-tables.sql)' \
18-
> $modified_sql_file_list
19+
| grep -Ev $'^A|^M\t[^/]+/sql/estimated-current.sql|^D\t[^/]+/sql/delete-[^ ]+-tables.sql'
20+
grep_exit_code=$?
21+
1922

20-
if [ "$(cat $modified_sql_file_list | wc -l)" -ne 0 ]
23+
if [[ $grep_exit_code -eq 0 ]]
2124
then
22-
cat $modified_sql_file_list
2325
echo 'At least one migration file was modified. See above.'
2426
exit 1
27+
elif [[ $grep_exit_code -eq 1 ]]
28+
then
29+
# Exit code 1 means nothing survived grep's filter, so no illegal changes were made
30+
# https://www.gnu.org/software/grep/manual/html_node/Exit-Status.html#Exit-Status-1
31+
exit 0
2532
fi
33+
34+
exit $grep_exit_code

ci/test/resources/build.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@ steps:
219219
to: /io/pyproject.toml
220220
dependsOn:
221221
- hello_image
222+
- kind: runImage
223+
name: check_sql_linting
224+
image:
225+
valueFrom: git_make_bash_image.image
226+
script: |
227+
set -ex
228+
cd /io/repo/ci/test/resources
229+
echo "foo" >> sql/create-hello-tables.sql
230+
231+
set +e
232+
bash /io/repo/check-sql.sh
233+
234+
if [[ $? -eq 0 ]]
235+
then
236+
echo "check-sql.sh passed when it should have failed"
237+
fi
238+
inputs:
239+
- from: /repo
240+
to: /io/repo
241+
dependsOn:
242+
- git_make_bash_image
243+
- merge_code
222244
- kind: createDatabase2
223245
name: hello_database
224246
databaseName: hello

0 commit comments

Comments
 (0)