Skip to content

Commit cdfc88f

Browse files
committed
Squawk: plain strings passed to tx.query are read too, a migration with no readable SQL fails, and require-concurrent-index-creation is back on (review)
1 parent 8bcae24 commit cdfc88f

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎.squawk.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Squawk (scripts/ci/squawk_migrations.sh): Postgres migration lint on the SQL of each new migration in
22
# app/database/migrations, in CI's migrations job. Every rule applies except these, each with its reason.
33
pg_version = "17.0"
4-
# laravel-rust runs each migration in a transaction.
4+
# laravel-rust runs each migration in a transaction, so CREATE INDEX CONCURRENTLY isn't possible: an index on an existing
5+
# table blocks its writes while it builds. On a small table, silence require-concurrent-index-creation with a listed
6+
# `-- squawk-ignore` and the table's size as the reason.
57
assume_in_transaction = true
68
excluded_rules = [
7-
# CREATE INDEX CONCURRENTLY can't run in a transaction, and every migration runs in one; lock_timeout bounds the wait.
8-
"require-concurrent-index-creation",
99
# scripts/ci/migration_safety.py owns dropped columns: it fails one main's code still reads and allows the rest.
1010
"ban-drop-column",
1111
# BIGSERIAL keys and integer counters are the schema's convention since the initial migration.

‎scripts/ci/squawk_migrations.sh‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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)
2223
trap 'rm -rf "$out"' EXIT
2324
python3 - "$out" "${files[@]}" <<'PY'
2425
import 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
2637
for 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)
3149
PY
3250
squawk --config .squawk.toml "$out"/*.sql
3351
echo "Squawk: ${#files[@]} new migrations clean."

0 commit comments

Comments
 (0)