11#! /usr/bin/env bash
22# Squawk (.squawk.toml) on the SQL of each migration added since a base commit, or of the migrations named. The SQL
3- # lives in Rust raw strings (`r"..."`, `r#"..."#`), so each file's strings are copied into a .sql file first. Old
3+ # lives in Rust strings (raw-string consts, or literals passed straight to tx.query), so each file's are copied into a
4+ # .sql file first; a migration whose SQL can't be found that way (a format! string) fails. Old
45# migrations aren't checked: production ran them long ago. Silence one finding with `-- squawk-ignore <rule>` above the
56# statement, listed with its reason in scripts/ci/suppressions.txt.
67#
@@ -22,12 +23,29 @@ out=$(mktemp -d)
2223trap ' rm -rf "$out"' EXIT
2324python3 - " $out " " ${files[@]} " << 'PY '
2425import os, re, sys
25- out = sys.argv[1]
26+
27+ # Rust string escapes in a plain "..." literal: a backslash-newline drops the newline and the next line's indent.
28+ ESCAPES = {"n": "\n", "t": "\t", "r": "\r", "0": "\0", "\\": "\\", '"': '"', "'": "'"}
29+
30+
31+ def plain(body):
32+ body = re.sub(r"\\\n\s*", "", body)
33+ return re.sub(r"\\(.)", lambda m: ESCAPES.get(m.group(1), m.group(0)), body)
34+
35+
36+ out, failed = sys.argv[1], False
2637for path in sys.argv[2:]:
2738 source = open(path).read()
39+ # Raw strings anywhere (the SQL consts), and plain strings passed straight to .query(...).
2840 parts = [m.group(2) for m in re.finditer(r'\br(#*)"(.*?)"\1', source, re.S)]
41+ parts += [plain(m.group(1)) for m in re.finditer(r'\.query\(\s*"((?:[^"\\]|\\.)*)"', source, re.S)]
2942 sql = "\n".join(p if p.rstrip().endswith(";") else p.rstrip() + ";" for p in parts if p.strip())
43+ if not sql.strip():
44+ print(f"{path}: no SQL found. Write it as a literal string, in a raw-string const or passed straight to "
45+ "tx.query(...), so Squawk can read it (a format! string can't be checked).", file=sys.stderr)
46+ failed = True
3047 open(os.path.join(out, os.path.basename(path)[:-3] + ".sql"), "w").write(sql + "\n")
48+ sys.exit(1 if failed else 0)
3149PY
3250squawk --config .squawk.toml " $out " /* .sql
3351echo " Squawk: ${# files[@]} new migrations clean."
0 commit comments