fix: retain the worst effect of compound migration statements

This commit is contained in:
Jaeyeon Kim 2026-10-28 09:21:53 +00:00
parent 95a9d41626
commit 114128e5e3
2 changed files with 26 additions and 9 deletions

View file

@ -572,6 +572,8 @@ def _rule_rename(site: _Site) -> Finding | None:
def _rule_not_null(site: _Site) -> Finding | None:
if _ADD_COLUMN.search(site.statement) is not None and _adds_not_null_without_default(site.statement):
return site.rate(BREAKING, "Adds a NOT NULL column with no default, so inserts from the previous version fail.")
required: Final = [match.group("column") for match in _SET_NOT_NULL_COLUMN.finditer(site.statement)]
if required:
unguarded: Final = [
@ -586,8 +588,6 @@ def _rule_not_null(site: _Site) -> Finding | None:
"Those writes are now rejected.",
)
return site.rate(INFO, "The previous version already always writes this column, so nothing changes for it.")
if _ADD_COLUMN.search(site.statement) is not None and _adds_not_null_without_default(site.statement):
return site.rate(BREAKING, "Adds a NOT NULL column with no default, so inserts from the previous version fail.")
return None
@ -687,9 +687,7 @@ def _rule_rest(site: _Site) -> Finding:
return site.rate(INFO, "No effect on the previous version.")
# Worst-first: the first rule that has something to say about a statement decides its rating.
RULES: Final = (
_rule_new_table,
_rule_drop,
_rule_rename,
_rule_not_null,
@ -704,11 +702,11 @@ def classify(migration: str, statement: str, context: UpgradeContext) -> Finding
"""Rate one statement by what it does to a pod still running the base version."""
table: Final = statement_table(statement)
site: Final = _Site(migration, statement, table, context, _star_read_for(table, context.star_reads))
for rule in RULES:
found: Final = rule(site)
if found is not None:
return found
return _rule_rest(site)
new_table: Final = _rule_new_table(site)
if new_table is not None:
return new_table
findings: Final = tuple(found for rule in RULES if (found := rule(site)) is not None)
return min(findings, key=lambda finding: SEVERITY_ORDER.index(finding.severity), default=_rule_rest(site))
def _adds_not_null_without_default(statement: str) -> bool:

View file

@ -13,6 +13,8 @@ from pathlib import Path
from types import ModuleType
from typing import Final
import pytest
ROOT: Final = Path(__file__).resolve().parents[2]
CI_CD: Final = ROOT / "ci_cd"
@ -371,6 +373,23 @@ def test_not_null_is_judged_per_add_column_clause() -> None:
assert one_default_is_not_enough.severity == impact.BREAKING
@pytest.mark.parametrize("benign", ['DROP COLUMN "unknown"', 'ALTER COLUMN "token" SET NOT NULL'])
@pytest.mark.parametrize(
"change,severity,effect",
[
('ADD COLUMN "required_new" TEXT NOT NULL', impact.BREAKING, "no default"),
('ALTER COLUMN "token" TYPE INTEGER', impact.BREAKING, "to INTEGER"),
('ADD COLUMN "optional_new" TEXT', impact.PREPARED_PLAN, "cached plan"),
],
)
def test_compound_alter_keeps_the_worst_effect(benign: str, change: str, severity: str, effect: str) -> None:
statement: Final = f'ALTER TABLE "LiteLLM_VerificationToken" {benign}, {change}'
finding: Final = _classify(statement, schema=_base_schema(), reads=(_auth_read(),))
assert finding.severity == severity
assert effect in finding.effect
assert finding.statement == statement
def test_drop_not_null_beside_an_add_column_is_not_breaking() -> None:
# 20250707230009_add_mcp_namespaced_tool_name: the `NOT NULL` here belongs to `DROP NOT NULL`.
finding = _classify(