Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(ci): preserve offsets when re-lexing executed SQL literals

mateo-berri9a0c541b4975
diff --git a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
index b3f60febaba..7a6f1894349 100644
--- a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
+++ b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
@@ -249,6 +249,16 @@ def undouble(literal: str) -> str:
return literal.replace("''", "'")
+def defuse_escapes(literal: str) -> str:
+ """The literal made safe to re-lex without moving anything: each doubled quote becomes a real
+ quote and a space, so a `--` or `/*` in a nested string stays inside its string the way
+ `undouble` achieves it, while the pair keeps its two characters. Every newline and every
+ character after a resolved escape then holds the offset it had in the document, so a rewrite
+ scanned out of the literal reports its true file line and lines up with the file's markers,
+ which `undouble` cannot promise because it shrinks the text as it collapses each pair."""
+ return literal.replace("''", "' ")
+
+
def mask(sql: str) -> tuple[str, tuple[tuple[int, int], ...], tuple[tuple[int, int], ...]]:
"""Blank comments and quoted text, keeping offsets, and locate the spans that can still
hold SQL: dollar-quoted bodies, and the single-quoted literals `EXECUTE` runs."""
@@ -632,8 +642,10 @@ def scan_region(
"""Violations in one region of `document`, whose text begins at `offset`. Positions are
always counted against the whole document, so a statement nested in a dollar-quoted body
reports its real file line and lines up with the markers read from that file. A single-quoted
- literal that `DO` or `EXECUTE` runs as SQL is undoubled before it is scanned, so a `--` or `/*`
- in one of its nested strings blanks nothing and the statement after it stays visible."""
+ literal that `DO` or `EXECUTE` runs as SQL has each doubled quote turned into a quote and a space
+ before it is scanned, so a `--` or `/*` in one of its nested strings blanks nothing and the
+ statement after it stays visible, and since that keeps every character on its offset, the
+ statement reports its true file line and lines up with the markers."""
masked, bodies, literals = mask(region)
executed = executed_names(masked)
runnable = executed_literals(masked, literals, executed)
@@ -648,7 +660,7 @@ def scan_region(
if base <= start and end <= commands_end:
yield from scan_region(
document,
- undouble(region[start:end]),
+ defuse_escapes(region[start:end]),
migration,
markers,
offset + start,
diff --git a/tests/test_litellm/test_check_migrations_no_data_rewrites.py b/tests/test_litellm/test_check_migrations_no_data_rewrites.py
index f44e9d7e3aa..3a6cdd2af32 100644
--- a/tests/test_litellm/test_check_migrations_no_data_rewrites.py
+++ b/tests/test_litellm/test_check_migrations_no_data_rewrites.py
@@ -878,6 +878,15 @@ class TestDynamicSql:
sql = "DO $$\nBEGIN\n EXECUTE 'SELECT 1 -- UPDATE \"Foo\" SET \"a\" = 1';\nEND $$;"
assert _keywords(tmp_path, sql) == ()
+ def test_a_rewrite_below_escaped_quotes_in_a_multiline_payload_reports_its_own_line(self, tmp_path):
+ sql = (
+ "DO $$\nBEGIN\n EXECUTE '\n"
+ "SELECT ''a'', ''b'', ''c'', ''d'', ''e''\n"
+ "; UPDATE \"Foo\" SET \"a\" = 1';\nEND $$;"
+ )
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+ assert _scan(tmp_path, sql)[0].line == 5
+
def test_a_rewrite_in_a_later_command_before_bind_values_is_flagged(self, tmp_path):
sql = "DO $$\nBEGIN\n EXECUTE 'SELECT 1; DELETE FROM \"Foo\" WHERE \"a\" = $1' USING 1;\nEND $$;"
assert _keywords(tmp_path, sql) == ("DELETE",)