mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(ci): read bare constraint columns and judge unique keys by what they replace
Second review round on #41744. A CHECK written without quotes named no columns, so it rated info; and a unique index was exempted as a replacement on the strength of its name prefix, which would also have exempted a narrower key. Names in a constraint are now read quoted or bare and kept only when the base schema declares them for that table, so `CHECK (status = 'active')` finds `status` and ignores the literal. Without a base schema every new constraint is write-reject. A column this upgrade adds with a DEFAULT counts as written by the old pods (they insert the default), so a composite unique index over it still binds them. A unique index is exempt only when a unique index dropped in the same migration existed at the base on the same table with a subset of its columns: the new key then rejects nothing the old one did not. The base's unique indexes come from its migrations. The jwt_issuer migration cited in review drops (jwt_claim_name, jwt_claim_value) and recreates (jwt_issuer, jwt_claim_name, jwt_claim_value), so it stays a lock on evidence rather than on a name prefix. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
886b59b56d
commit
fb9fcb1615
2 changed files with 160 additions and 26 deletions
|
|
@ -41,7 +41,7 @@ import re
|
|||
import subprocess
|
||||
import sys
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
|
|
@ -167,6 +167,11 @@ _DROP_INDEX_NAME: Final = re.compile(
|
|||
r'\bDROP\s+INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+EXISTS\s+)?"?(?P<index>\w+)"?', re.IGNORECASE
|
||||
)
|
||||
_QUOTED_IDENTIFIER: Final = re.compile(r'"(?P<name>\w+)"')
|
||||
_BARE_WORD: Final = re.compile(r"\b(?P<name>[A-Za-z_]\w*)\b")
|
||||
_ADDED_COLUMN_NAME: Final = re.compile(r'\bADD\s+COLUMN\s+(?:IF\s+NOT\s+EXISTS\s+)?"?(?P<column>\w+)"?', re.IGNORECASE)
|
||||
_INDEX_NAME: Final = re.compile(
|
||||
r'\bCREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?"?(?P<index>\w+)"?', re.IGNORECASE
|
||||
)
|
||||
|
||||
_MODEL_BLOCK: Final = re.compile(r"^\s*model\s+(?P<model>\w+)\s*\{(?P<body>.*?)^\s*\}", re.MULTILINE | re.DOTALL)
|
||||
_ENUM_NAME: Final = re.compile(r"^\s*enum\s+(?P<name>\w+)", re.MULTILINE)
|
||||
|
|
@ -215,6 +220,10 @@ class UpgradeContext:
|
|||
star_reads: tuple[StarRead, ...]
|
||||
schema: Schema | None
|
||||
dropped_indexes: frozenset[str] = frozenset()
|
||||
# (table, column) added by this upgrade with a DEFAULT: the old pods write that default on every insert.
|
||||
defaulted_columns: frozenset[tuple[str, str]] = frozenset()
|
||||
# Unique indexes that exist at the base, by name: what a dropped-and-recreated key used to cover.
|
||||
base_unique_indexes: dict[str, tuple[str, tuple[str, ...]]] = field(default_factory=dict)
|
||||
|
||||
def column(self, table: str, column: str) -> Column | None:
|
||||
return None if self.schema is None else self.schema.get(table, {}).get(column)
|
||||
|
|
@ -226,6 +235,19 @@ class UpgradeContext:
|
|||
"""Whether the old client selects or writes this column. Without a schema, assume it does."""
|
||||
return self.schema is None or self.column(table, column) is not None
|
||||
|
||||
def writes(self, table: str, column: str) -> bool:
|
||||
"""Whether rows the old pods insert carry a value here: a column they know, or a default they get."""
|
||||
return self.knows_column(table, column) or (table, column) in self.defaulted_columns
|
||||
|
||||
def replaced_key(self, table: str, columns: Sequence[str]) -> bool:
|
||||
"""Whether a dropped unique index on this table covered a subset of `columns`: the new key then rejects
|
||||
nothing the old key did not already reject."""
|
||||
for name in self.dropped_indexes:
|
||||
found: Final = self.base_unique_indexes.get(name)
|
||||
if found is not None and found[0] == table and set(found[1]) <= set(columns):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Finding:
|
||||
|
|
@ -347,13 +369,13 @@ def parse_prisma_schema(text: str) -> Schema:
|
|||
table_map: Final = _TABLE_MAP.search(body)
|
||||
table: Final = table_map.group("name") if table_map is not None else block.group("model")
|
||||
columns: Final[dict[str, Column]] = {}
|
||||
for field in _PRISMA_FIELD.finditer(body):
|
||||
prisma_type: Final = field.group("type")
|
||||
for declared in _PRISMA_FIELD.finditer(body):
|
||||
prisma_type: Final = declared.group("type")
|
||||
if prisma_type not in PRISMA_SCALARS and prisma_type not in enums:
|
||||
continue
|
||||
field_map: Final = _FIELD_MAP.search(field.group("rest"))
|
||||
name: Final = field_map.group("name") if field_map is not None else field.group("name")
|
||||
columns[name] = Column(required=field.group("optional") is None, prisma_type=prisma_type)
|
||||
field_map: Final = _FIELD_MAP.search(declared.group("rest"))
|
||||
name: Final = field_map.group("name") if field_map is not None else declared.group("name")
|
||||
columns[name] = Column(required=declared.group("optional") is None, prisma_type=prisma_type)
|
||||
schema[table] = columns
|
||||
return schema
|
||||
|
||||
|
|
@ -369,11 +391,41 @@ def load_base_schema(repo: Path, ref: str) -> Schema | None:
|
|||
|
||||
|
||||
def _identifiers(fragment: str) -> tuple[str, ...]:
|
||||
return tuple(dict.fromkeys(match.group("name") for match in _QUOTED_IDENTIFIER.finditer(fragment)))
|
||||
"""Every name in the fragment, quoted or bare. Bare words include keywords and literals, so callers keep
|
||||
only the ones that are columns of the table at hand."""
|
||||
quoted: Final = [match.group("name") for match in _QUOTED_IDENTIFIER.finditer(fragment)]
|
||||
unquoted: Final = _QUOTED_IDENTIFIER.sub(" ", re.sub(r"'[^']*'", " ", fragment))
|
||||
bare: Final = [match.group("name") for match in _BARE_WORD.finditer(unquoted)]
|
||||
return tuple(dict.fromkeys(quoted + bare))
|
||||
|
||||
|
||||
def _defaulted_columns(statements: Sequence[str]) -> frozenset[tuple[str, str]]:
|
||||
"""(table, column) pairs these statements add with a DEFAULT."""
|
||||
found: Final[set[tuple[str, str]]] = set()
|
||||
for statement in statements:
|
||||
table: Final = statement_table(statement)
|
||||
for clause in _CLAUSE_SPLIT.split(statement):
|
||||
added: Final = _ADDED_COLUMN_NAME.search(clause)
|
||||
if added is not None and _DEFAULT.search(clause) is not None:
|
||||
found.add((table, added.group("column")))
|
||||
return frozenset(found)
|
||||
|
||||
|
||||
def unique_indexes_at(repo: Path, ref: str) -> dict[str, tuple[str, tuple[str, ...]]]:
|
||||
"""Every unique index some migration at `ref` creates, by name: its table and columns."""
|
||||
listing: Final = _git_or_none(
|
||||
repo, "grep", "-h", "-i", "--fixed-strings", "CREATE UNIQUE INDEX", ref, "--", f"{MIGRATIONS_DIR}/"
|
||||
)
|
||||
indexes: Final[dict[str, tuple[str, tuple[str, ...]]]] = {}
|
||||
for line in (listing or "").splitlines():
|
||||
named: Final = _INDEX_NAME.search(line)
|
||||
if named is not None:
|
||||
indexes[named.group("index")] = (statement_table(line), _index_columns(line))
|
||||
return indexes
|
||||
|
||||
|
||||
def _constraint_columns(statement: str) -> tuple[str, ...]:
|
||||
"""The columns of this table that a new constraint checks on every write."""
|
||||
"""Names a new constraint mentions: the listed columns, and every name inside a CHECK expression."""
|
||||
listed: Final = [
|
||||
name for match in _CONSTRAINT_COLUMNS.finditer(statement) for name in _identifiers(match.group("columns"))
|
||||
]
|
||||
|
|
@ -574,7 +626,15 @@ def _rule_type_change(site: _Site) -> Finding | None:
|
|||
|
||||
def _rule_constraint(site: _Site) -> Finding | None:
|
||||
if _ADD_CONSTRAINT.search(site.statement) is not None:
|
||||
checked: Final = [column for column in _constraint_columns(site.statement) if site.knows(column)]
|
||||
if site.context.schema is None:
|
||||
return site.rate(
|
||||
WRITE_REJECT,
|
||||
"Adds a rule the previous version never enforced. Rows it writes that violate the rule are now "
|
||||
"rejected; whether any do depends on the data.",
|
||||
)
|
||||
checked: Final = [
|
||||
column for column in _constraint_columns(site.statement) if site.context.writes(site.table, column)
|
||||
]
|
||||
if checked:
|
||||
return site.rate(
|
||||
WRITE_REJECT,
|
||||
|
|
@ -589,14 +649,15 @@ def _rule_constraint(site: _Site) -> Finding | None:
|
|||
if _UNIQUE_INDEX.search(site.statement) is None:
|
||||
return None
|
||||
covered: Final = _index_columns(site.statement)
|
||||
replaces: Final = any(name.startswith(f"{site.table}_") for name in site.context.dropped_indexes)
|
||||
if covered and all(site.knows(column) for column in covered) and not replaces:
|
||||
return site.rate(
|
||||
WRITE_REJECT,
|
||||
f"Makes `{'`, `'.join(covered)}` unique, which the previous version never enforced. Duplicate rows it "
|
||||
"writes are now rejected; whether any are depends on the data.",
|
||||
)
|
||||
return None
|
||||
if not covered or not all(site.context.writes(site.table, column) for column in covered):
|
||||
return None
|
||||
if site.context.replaced_key(site.table, covered):
|
||||
return None # the key it replaces already rejected everything this one does
|
||||
return site.rate(
|
||||
WRITE_REJECT,
|
||||
f"Makes `{'`, `'.join(covered)}` unique, which the previous version never enforced. Duplicate rows it "
|
||||
"writes are now rejected; whether any are depends on the data.",
|
||||
)
|
||||
|
||||
|
||||
def _rule_whole_row_read(site: _Site) -> Finding | None:
|
||||
|
|
@ -686,14 +747,16 @@ def build_report(repo: Path, base: str, head: str) -> Report:
|
|||
# A re-declared `CREATE TABLE IF NOT EXISTS` of a table the old pods already use is not a new table.
|
||||
fresh_tables: Final = created_here - tables_created_before(repo, base)
|
||||
schema: Final = load_base_schema(repo, base)
|
||||
statements_by_migration: Final = {name: split_statements(sql) for name, sql in sql_by_migration.items()}
|
||||
defaulted: Final = _defaulted_columns([s for statements in statements_by_migration.values() for s in statements])
|
||||
base_unique: Final = unique_indexes_at(repo, base)
|
||||
|
||||
findings: Final[list[Finding]] = []
|
||||
for name, sql in sql_by_migration.items():
|
||||
statements: Final = split_statements(sql)
|
||||
for name, statements in statements_by_migration.items():
|
||||
dropped: Final = frozenset(
|
||||
match.group("index") for statement in statements if (match := _DROP_INDEX_NAME.search(statement))
|
||||
)
|
||||
context: Final = UpgradeContext(fresh_tables, star_reads, schema, dropped)
|
||||
context: Final = UpgradeContext(fresh_tables, star_reads, schema, dropped, defaulted, base_unique)
|
||||
findings.extend(classify(name, statement, context) for statement in statements)
|
||||
|
||||
ranked: Final = sorted(findings, key=lambda finding: (SEVERITY_ORDER.index(finding.severity), finding.migration))
|
||||
|
|
|
|||
|
|
@ -114,8 +114,10 @@ def _context(
|
|||
reads: tuple[object, ...] = (),
|
||||
schema: object = None,
|
||||
dropped: frozenset[str] = frozenset(),
|
||||
defaulted: frozenset[tuple[str, str]] = frozenset(),
|
||||
base_unique: dict[str, tuple[str, tuple[str, ...]]] | None = None,
|
||||
) -> object:
|
||||
return impact.UpgradeContext(fresh, reads, schema, dropped)
|
||||
return impact.UpgradeContext(fresh, reads, schema, dropped, defaulted, base_unique or {})
|
||||
|
||||
|
||||
def _classify(
|
||||
|
|
@ -124,8 +126,11 @@ def _classify(
|
|||
reads: tuple[object, ...] = (),
|
||||
schema: object = None,
|
||||
dropped: frozenset[str] = frozenset(),
|
||||
defaulted: frozenset[tuple[str, str]] = frozenset(),
|
||||
base_unique: dict[str, tuple[str, tuple[str, ...]]] | None = None,
|
||||
) -> object:
|
||||
return impact.classify("20260713230852_add_key_type", statement, _context(fresh, reads, schema, dropped))
|
||||
context = _context(fresh, reads, schema, dropped, defaulted, base_unique)
|
||||
return impact.classify("20260713230852_add_key_type", statement, context)
|
||||
|
||||
|
||||
def _base_schema() -> object:
|
||||
|
|
@ -426,7 +431,8 @@ def _two_version_repo(tmp_path: Path) -> Path:
|
|||
repo,
|
||||
"20250101000000_baseline",
|
||||
'CREATE TABLE "LiteLLM_VerificationToken" ("token" TEXT PRIMARY KEY);\n'
|
||||
'CREATE TABLE "LiteLLM_TeamTable" ("team_id" TEXT PRIMARY KEY);\n',
|
||||
'CREATE TABLE "LiteLLM_TeamTable" ("team_id" TEXT PRIMARY KEY);\n'
|
||||
'CREATE UNIQUE INDEX "LiteLLM_VerificationToken_key_name_key" ON "LiteLLM_VerificationToken"("key_name");\n',
|
||||
)
|
||||
(repo / impact.MIGRATIONS_DIR / "migration_lock.toml").write_text('provider = "postgresql"\n', encoding="utf-8")
|
||||
_git(repo, "add", ".")
|
||||
|
|
@ -447,6 +453,15 @@ def _two_version_repo(tmp_path: Path) -> Path:
|
|||
# A column it does declare: the old client selects it on every read.
|
||||
'ALTER TABLE "LiteLLM_VerificationToken" DROP COLUMN "key_name";\n',
|
||||
)
|
||||
_write_migration(
|
||||
repo,
|
||||
"20250202000000_scope_key_name_by_issuer",
|
||||
# The jwt_issuer pattern: drop the old key, add a defaulted column, recreate the key wider.
|
||||
'DROP INDEX IF EXISTS "LiteLLM_VerificationToken_key_name_key";\n'
|
||||
'ALTER TABLE "LiteLLM_VerificationToken" ADD COLUMN IF NOT EXISTS "issuer" TEXT NOT NULL DEFAULT \'\';\n'
|
||||
'CREATE UNIQUE INDEX "LiteLLM_VerificationToken_issuer_key_name_key" ON "LiteLLM_VerificationToken"'
|
||||
'("issuer", "key_name");\n',
|
||||
)
|
||||
_git(repo, "add", ".")
|
||||
_git(repo, "commit", "-q", "-m", "head")
|
||||
_git(repo, "tag", "v1.1.0")
|
||||
|
|
@ -457,7 +472,7 @@ def test_report_rates_migrations_against_the_queries_the_old_pods_run(tmp_path:
|
|||
repo = _two_version_repo(tmp_path)
|
||||
report = impact.build_report(repo, "v1.0.0", "v1.1.0")
|
||||
|
||||
assert report.migrations == ("20250201000000_add_key_type",)
|
||||
assert report.migrations == ("20250201000000_add_key_type", "20250202000000_scope_key_name_by_issuer")
|
||||
# The whole-row read exists only at v1.0.0; that is the version whose plans go stale.
|
||||
assert [read.table for read in report.star_reads] == ["LiteLLM_VerificationToken"]
|
||||
assert report.star_reads[0].location.startswith("litellm/proxy/utils.py:")
|
||||
|
|
@ -471,6 +486,11 @@ def test_report_rates_migrations_against_the_queries_the_old_pods_run(tmp_path:
|
|||
assert unknown_drop.severity == impact.INFO
|
||||
known_drop = by_statement['ALTER TABLE "LiteLLM_VerificationToken" DROP COLUMN "key_name"']
|
||||
assert known_drop.severity == impact.BREAKING
|
||||
widened_key = by_statement[
|
||||
'CREATE UNIQUE INDEX "LiteLLM_VerificationToken_issuer_key_name_key" ON "LiteLLM_VerificationToken"'
|
||||
'("issuer", "key_name")'
|
||||
]
|
||||
assert widened_key.severity == impact.LOCK # widens the key it drops, so nothing new is rejected
|
||||
assert report.schema_tables == 4
|
||||
assert report.worst == impact.BREAKING
|
||||
|
||||
|
|
@ -542,17 +562,68 @@ def test_unique_index_on_columns_the_old_client_writes_can_reject_duplicates() -
|
|||
assert finding.severity == impact.WRITE_REJECT
|
||||
|
||||
|
||||
def test_unique_index_that_replaces_a_dropped_one_is_only_a_lock() -> None:
|
||||
# The Daily*Spend migrations drop the old unique key and create a wider one in the same file.
|
||||
TOKEN_KEY_NAME_KEY: Final = {"LiteLLM_VerificationToken_key_name_key": ("LiteLLM_VerificationToken", ("key_name",))}
|
||||
|
||||
|
||||
def test_unique_index_that_widens_a_dropped_key_is_only_a_lock() -> None:
|
||||
# The Daily*Spend migrations drop the old unique key and create a wider one in the same file: every
|
||||
# duplicate the new key rejects, the old key already rejected.
|
||||
finding = _classify(
|
||||
'CREATE UNIQUE INDEX "LiteLLM_VerificationToken_key_name_team_id_key" ON "LiteLLM_VerificationToken"'
|
||||
'("key_name", "team_id")',
|
||||
schema=_base_schema(),
|
||||
dropped=frozenset({"LiteLLM_VerificationToken_key_name_key"}),
|
||||
base_unique=TOKEN_KEY_NAME_KEY,
|
||||
)
|
||||
assert finding.severity == impact.LOCK
|
||||
|
||||
|
||||
def test_unique_index_that_narrows_a_dropped_key_can_reject_writes() -> None:
|
||||
# Dropping (key_name, team_id) for (key_name) is stricter: rows that only differed by team now collide.
|
||||
finding = _classify(
|
||||
'CREATE UNIQUE INDEX "LiteLLM_VerificationToken_key_name_key" ON "LiteLLM_VerificationToken"("key_name")',
|
||||
schema=_base_schema(),
|
||||
dropped=frozenset({"LiteLLM_VerificationToken_key_name_team_id_key"}),
|
||||
base_unique={
|
||||
"LiteLLM_VerificationToken_key_name_team_id_key": ("LiteLLM_VerificationToken", ("key_name", "team_id"))
|
||||
},
|
||||
)
|
||||
assert finding.severity == impact.WRITE_REJECT
|
||||
|
||||
|
||||
def test_unique_index_over_a_new_defaulted_column_still_binds_the_old_pods() -> None:
|
||||
# Old pods insert the default, so the new column does not make their rows distinct.
|
||||
finding = _classify(
|
||||
'CREATE UNIQUE INDEX "idx" ON "LiteLLM_VerificationToken"("key_name", "jwt_issuer")',
|
||||
schema=_base_schema(),
|
||||
defaulted=frozenset({("LiteLLM_VerificationToken", "jwt_issuer")}),
|
||||
)
|
||||
assert finding.severity == impact.WRITE_REJECT
|
||||
|
||||
|
||||
def test_check_constraint_with_unquoted_columns_is_matched_against_the_schema() -> None:
|
||||
finding = _classify(
|
||||
'ALTER TABLE "LiteLLM_VerificationToken" ADD CONSTRAINT "chk" CHECK (key_name <> \'active\') NOT VALID',
|
||||
schema=_base_schema(),
|
||||
)
|
||||
assert finding.severity == impact.WRITE_REJECT
|
||||
assert "`key_name`" in finding.effect
|
||||
assert "active" not in finding.effect # a string literal is not a column
|
||||
|
||||
|
||||
def test_constraint_without_a_schema_to_check_against_is_write_reject() -> None:
|
||||
finding = _classify('ALTER TABLE "LiteLLM_VerificationToken" ADD CONSTRAINT "chk" CHECK (status = \'active\')')
|
||||
assert finding.severity == impact.WRITE_REJECT
|
||||
|
||||
|
||||
def test_defaulted_columns_are_collected_per_clause() -> None:
|
||||
statements = [
|
||||
'ALTER TABLE "T" ADD COLUMN "a" TEXT NOT NULL DEFAULT \'\', ADD COLUMN "b" TEXT',
|
||||
'ALTER TABLE "U" ADD COLUMN IF NOT EXISTS "c" INT DEFAULT 0',
|
||||
]
|
||||
assert impact._defaulted_columns(statements) == frozenset({("T", "a"), ("U", "c")})
|
||||
|
||||
|
||||
def test_unique_index_on_a_new_column_is_only_a_lock() -> None:
|
||||
finding = _classify(
|
||||
'CREATE UNIQUE INDEX "idx" ON "LiteLLM_VerificationToken"("settings_updated_at")', schema=_base_schema()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue