feat: Phase2影响分析 — EXPLAIN预检+大表检测(简化版)+执行策略+风险评估table_sizes支持 (1141测试)
新增: - explain_preview.py: EXPLAIN预检服务(MySQL/PG), 影响行数预估, 全表扫描检测 - ticket.executor: 三种执行策略(stop_on_error/continue/transaction) - risk_evaluator: table_sizes参数, 大表ALTER/UPDATE/DELETE自动升级high - test_phase2_features.py: 22个测试(EXPLAIN+执行策略+大表检测) 简化: - 砍掉CTX-001上下文感知规则(复杂二次扫描), 合并大表检测到risk_evaluator - 审核引擎保持纯静态分析, 不再导入_RULE_REGISTRY做二次扫描 模型: - Ticket加exec_strategy字段 - TicketSqlItem模型 - schema加sql_type/exec_strategy/risk_level/workflow_id
This commit is contained in:
1 parent
7be0bf1a7c
commit
f6f9142ce8
7 files changed
+574
-22
No files matched your search
@@ -19,6 +19,7 @@ def evaluate_risk_level(
|
||||
sql_type: str = "",
|
||||
audit_findings: Optional[list] = None,
|
||||
statement_count: int = 1,
|
||||
table_sizes: Optional[dict] = None,
|
||||
) -> str:
|
||||
"""评估 SQL 的风险等级。
|
||||
|
||||
@@ -28,6 +29,7 @@ def evaluate_risk_level(
|
||||
sql_type: 前端传入的 SQL 类型标注 DDL/DML/DQL 等
|
||||
audit_findings: 审核引擎返回的 findings 列表
|
||||
statement_count: SQL 语句条数
|
||||
table_sizes: EXPLAIN 预检返回的表大小 {table_name: row_count}
|
||||
|
||||
Returns:
|
||||
risk_level: low / medium / high / critical
|
||||
@@ -93,6 +95,13 @@ def evaluate_risk_level(
|
||||
if high_indicators > 0:
|
||||
return "high"
|
||||
|
||||
# 2.5 大表检测 — EXPLAIN 预检返回的 table_sizes
|
||||
# ALTER/UPDATE/DELETE 涉及大表(>10万行)时升级为 high
|
||||
if table_sizes:
|
||||
max_rows = max(table_sizes.values()) if table_sizes else 0
|
||||
if max_rows > 100_000 and re.search(r"\b(ALTER\s+TABLE|UPDATE|DELETE\s+FROM)\b", sql_upper):
|
||||
return "high"
|
||||
|
||||
# 3. medium 级别 — 常规变更
|
||||
medium_types = {"DDL", "DML", "SET", "HSET", "HMSET"}
|
||||
if sql_type in medium_types:
|
||||
|
||||
Reference in new issue
Block a user