Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(advisor/tidb): migrate builtin_prior_backup_check to omni AST — last migratable advisor (Phase 1.5 §1.5.3 batch 19) (#20325)

Vincent Huange6e36c65ddf0
diff --git a/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
index 9d64b30372..dc734f30d7 100644
--- a/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
+++ b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
@@ -3,283 +3,556 @@ package tidb
import (
"context"
"fmt"
- "log/slog"
+ "slices"
"strings"
- "github.com/pingcap/tidb/pkg/parser/ast"
- "github.com/pingcap/tidb/pkg/parser/opcode"
- "github.com/pkg/errors"
+ "github.com/bytebase/omni/tidb/ast"
+ pingcapast "github.com/pingcap/tidb/pkg/parser/ast"
"github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/common/log"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
-const (
- maxMixedDMLCount = 5
-)
-
var (
_ advisor.Advisor = (*StatementPriorBackupCheckAdvisor)(nil)
)
+// maxMixedDMLCount must stay in sync with the backup transformer's
+// constant at backend/plugin/parser/tidb/backup.go:23. The advisor's
+// count-cap gate (Codex-fix-1g) uses the same threshold to predict
+// transformer behavior at pre-execution time.
+const maxMixedDMLCount = 5
+
func init() {
advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_BUILTIN_PRIOR_BACKUP_CHECK, &StatementPriorBackupCheckAdvisor{})
}
-// StatementPriorBackupCheckAdvisor is the advisor checking for no mixed DDL and DML.
-type StatementPriorBackupCheckAdvisor struct {
-}
-
-// Check checks for no mixed DDL and DML.
+// StatementPriorBackupCheckAdvisor flags inputs incompatible with the
+// prior-backup workflow. Aligned with the mysql analog's modernized
+// shape (per-table DML-type mixing detection + size cap), NOT the
+// stale pre-omni tidb logic (count cap + unique-WHERE short-circuit).
+//
+// The reshape decision (batch 19) — instead of mechanically porting
+// the pre-omni tidb logic per invariant #7, we align with the mysql
+// analog's modernized shape because:
+// 1. Pre-omni tidb wasn't being tested (orphan fixture, not in
+// tidb_rules_test.go) — "preserving" untested behavior preserves
+// unknowns.
+// 2. Mysql's per-table DML-type mixing is more semantically accurate
+// for backup feasibility than the count-cap heuristic.
+// 3. Phase 1.5 closes after this batch; deferring the alignment to a
+// future ticket would leave tidb on stale logic indefinitely.
+//
+// Behavior matrix (vs pre-omni tidb):
+//
+// UPDATE t × 6 (no unique-WHERE) — pre: fires (count cap). new: skips.
+// UPDATE t WHERE id=5 × 10 — pre: skips. new: skips.
+// UPDATE t; DELETE FROM t — pre: skips. new: FIRES (per-table mixing).
+// Statements > MaxSheetCheckSize — pre: skips. new: FIRES (size cap).
+// Missing bbdataarchive db — pre: fires w/ code.BuiltinPriorBackupCheck.
+// new: fires w/ code.DatabaseNotExists
+// (public-API delta, mysql-aligned).
+//
+// Audit axes applied:
+// - #7 (preserve pre-omni): NOT applied here — see reshape rationale.
+// - #19 (case-sensitivity): table-name grouping uses
+// strings.EqualFold-equivalent via lowercased key.
+// - #26 (UNION-root): omni's UNION-rooted UpdateStmt sources are
+// reached only via SubqueryExpr in TableExpr; the SubqueryExpr
+// arm returns nil — derived tables aren't base-table candidates.
+// - #29 (filter-effect): no expression-tree filter here. Dropped
+// omniIsConstantLit (was for the unique-WHERE machinery).
+type StatementPriorBackupCheckAdvisor struct{}
+
+// Check evaluates the prior-backup compatibility of the reviewed
+// statements. Gated on `checkCtx.EnablePriorBackup`.
func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
if !checkCtx.EnablePriorBackup {
return nil, nil
}
- var adviceList []*storepb.Advice
- root, err := getTiDBNodes(checkCtx)
-
- if err != nil {
- return nil, err
- }
-
level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
if err != nil {
return nil, err
}
title := checkCtx.Rule.Type.String()
- var updateStatements []*ast.UpdateStmt
- var deleteStatements []*ast.DeleteStmt
-
- for _, stmtNode := range root {
- var isDDL bool
- if _, ok := stmtNode.(ast.DDLNode); ok {
- isDDL = true
- }
-
- if u, ok := stmtNode.(*ast.UpdateStmt); ok {
- updateStatements = append(updateStatements, u)
- }
+ var adviceList []*storepb.Advice
- if d, ok := stmtNode.(*ast.DeleteStmt); ok {
- deleteStatements = append(deleteStatements, d)
- }
+ // 1. Size cap.
+ if checkCtx.StatementsTotalSize > common.MaxSheetCheckSize {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("The size of the SQL statements exceeds the maximum limit of %d bytes for backup", common.MaxSheetCheckSize),
+ Code: code.BuiltinPriorBackupCheck.Int32(),
+ StartPosition: nil,
+ })
+ }
- if isDDL {
+ // Cumulative #30 Codex-fix-1f: DDL detection MUST go through
+ // pingcap (`getTiDBNodes`), not omni (`getTiDBOmniNodes`).
+ // Reason: invariant #2 soft-fail at the omni wrapper silently
+ // drops Tier-4-deferred grammar (Sequence trio, FlashBackDatabase,
+ // FlashBackTable). For a safety gate, that's a real regression —
+ // pingcap's full DDLNode interface catches all 22 implementers
+ // via `ddlNode` struct embedding. The omni path is still used
+ // below for per-table DML-mixing detection (where omni AST shape
+ // is required); DDL detection sidesteps omni's grammar gaps via
+ // the pingcap-bridge path.
+ //
+ // Architectural note: this dual-path approach mirrors mysql's
+ // analog (mysqlparser.ExtractTables + isOmniDDL). Eight rounds
+ // of Codex catches on omni-only DDL enumeration (cumulative #30
+ // Codex-fix-1c through 1e) demonstrated the brittle-enumeration
+ // pattern; switching DDL detection to pingcap's authoritative
+ // DDLNode interface eliminates the enumeration entirely.
+ pingcapStmts, err := getTiDBNodes(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+ for _, p := range pingcapStmts {
+ if _, isDDL := p.(pingcapast.DDLNode); isDDL {
adviceList = append(adviceList, &storepb.Advice{
Status: level,
Title: title,
Content: "Prior backup cannot deal with mixed DDL and DML statements",
Code: code.BuiltinPriorBackupCheck.Int32(),
- StartPosition: common.ConvertANTLRLineToPosition(stmtNode.OriginTextPosition()),
+ StartPosition: common.ConvertANTLRLineToPosition(p.OriginTextPosition()),
})
}
}
+ stmts, err := getTiDBOmniNodes(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ // 2. Per-table DML mixing detection (omni path — needs full
+ // AST-shape access for SET-clause analysis).
+ // Cumulative #30 Codex-fix-2 (revised): resolve unqualified table
+ // references to a default database at EXTRACTION time, so the
+ // priorBackupTable.database field is always populated. This makes
+ // the grouping key consistent across qualified vs unqualified
+ // references to the same table. Use DBSchema.Name as the default
+ // (the schema being checked; populated reliably across review
+ // paths including plancheck where CurrentDatabase is not set —
+ // per statement_advise_executor.go:168-180). Fall back to
+ // CurrentDatabase, then empty string, in that order.
+ defaultDB := ""
+ if checkCtx.DBSchema != nil {
+ defaultDB = checkCtx.DBSchema.GetName()
+ }
+ if defaultDB == "" {
+ defaultDB = checkCtx.CurrentDatabase
+ }
+ type dmlRef struct {
+ table priorBackupTable
+ stmtType string // "UPDATE" or "DELETE"
+ }
+ var dmlRefs []dmlRef
+
+ for _, ostmt := range stmts {
+ node := ostmt.Node
+ switch n := node.(type) {
+ case *ast.UpdateStmt:
+ // Cumulative #30 Codex-fix-1: derive UPDATE mutation
+ // targets from SET-clause LHS qualifiers, NOT from the
+ // full Tables list (which includes JOIN-only read-only
+ // tables). For `UPDATE t1 JOIN t2 ON ... SET t1.col = ...`
+ // the mutation target is t1; t2 must NOT be tagged.
+ aliasMap := omniBuildTableAliasMap(n.Tables, defaultDB)
+ for _, t := range omniExtractUpdateTargets(n.SetList, aliasMap, checkCtx.DBSchema) {
+ dmlRefs = append(dmlRefs, dmlRef{table: t, stmtType: "UPDATE"})
+ }
+ case *ast.DeleteStmt:
+ // DELETE's Tables field IS the mutation target set (per
+ // omni parsenodes.go:123); Using[] is the filter-only
+ // joins. Use Tables directly.
+ for _, t := range omniExtractDMLTables(n.Tables, defaultDB) {
+ dmlRefs = append(dmlRefs, dmlRef{table: t, stmtType: "DELETE"})
+ }
+ default:
+ }
+ }
+
+ // 3. Backup database existence.
databaseName := common.BackupDatabaseNameOfEngine(storepb.Engine_TIDB)
if !advisor.DatabaseExists(ctx, checkCtx, databaseName) {
adviceList = append(adviceList, &storepb.Advice{
Status: level,
Title: title,
- Content: fmt.Sprintf("Prior backup check failed: need database %q to do prior backup but it does not exist", databaseName),
- Code: code.BuiltinPriorBackupCheck.Int32(),
+ Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", databaseName),
+ Code: code.DatabaseNotExists.Int32(),
StartPosition: nil,
})
}
- if len(updateStatements)+len(deleteStatements) > maxMixedDMLCount && !updateForOneTableWithUnique(checkCtx.DBSchema, updateStatements, deleteStatements) {
- adviceList = append(adviceList, &storepb.Advice{
- Status: level,
- Title: title,
- Content: fmt.Sprintf("Prior backup is feasible only with up to %d statements that are either UPDATE or DELETE, or if all UPDATEs target the same table with a PRIMARY or UNIQUE KEY in the WHERE clause", maxMixedDMLCount),
- Code: code.BuiltinPriorBackupCheck.Int32(),
- StartPosition: nil,
- })
+ // 4. Per-table DML-type mixing. Group by `db.table` key; if a
+ // single table has more than one DML type observed, emit advice.
+ // Mysql-aligned: more accurate than the pre-omni count cap.
+ type tableTypes struct {
+ seen map[string]bool
+ any string // first-seen type, for stable advice-content sentinel
}
-
- return adviceList, nil
-}
-
-func updateForOneTableWithUnique(dbMetadata *storepb.DatabaseSchemaMetadata, updates []*ast.UpdateStmt, deletes []*ast.DeleteStmt) bool {
- if len(deletes) > 0 {
- return false
+ groups := make(map[string]*tableTypes)
+ for _, ref := range dmlRefs {
+ // Cumulative #30 Codex-fix-2 (revised): database is already
+ // resolved to defaultDB at extraction time (see omniExtractDMLTables
+ // + omniBuildTableAliasMap), so equivalent references (qualified
+ // vs unqualified) share the same database segment. Key is
+ // lowercased for case-insensitive grouping.
+ key := strings.ToLower(ref.table.database) + "." + strings.ToLower(ref.table.table)
+ g := groups[key]
+ if g == nil {
+ g = &tableTypes{seen: make(map[string]bool)}
+ groups[key] = g
+ }
+ g.seen[ref.stmtType] = true
+ if g.any == "" {
+ g.any = ref.stmtType
+ }
}
- var table *table
- for _, update := range updates {
- tables, err := extractTableRefs(update.TableRefs)
- if err != nil {
- slog.Debug("failed to extract table reference", log.BBError(err))
- return false
- }
- if len(tables) != 1 {
- return false
+ // Deterministic order: sort keys lexicographically before emitting.
+ keys := make([]string, 0, len(groups))
+ for k := range groups {
+ keys = append(keys, k)
+ }
+ slices.Sort(keys)
+ for _, key := range keys {
+ g := groups[key]
+ if len(g.seen) > 1 {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Prior backup cannot handle mixed DML statements on the same table %q", key),
+ Code: code.BuiltinPriorBackupCheck.Int32(),
+ StartPosition: nil,
+ })
}
- if table == nil {
- table = &tables[0]
- } else if !equalTable(table, &tables[0]) {
- return false
+ }
+
+ // Cumulative #30 Codex-fix-1g: count-cap with multi-table gate.
+ // The backup transformer at backend/plugin/parser/tidb/backup.go:
+ // 96-110 routes > maxMixedDMLCount DML statements into
+ // generateSQLForSingleTable which errors on multi-table inputs
+ // ("prior backup cannot handle statements on different tables
+ // more than 5"). My reshape (cumulative #30) dropped this gate
+ // under the "modernize-away-pre-omni-logic" framing — but the
+ // transformer constraint is current, not legacy. Reinstating
+ // the count gate prevents the advisor from approving inputs
+ // that the transformer rejects at runtime.
+ //
+ // Single-table batches above the threshold are intentionally
+ // allowed — the transformer's generateSQLForSingleTable
+ // successfully handles them.
+ if len(dmlRefs) > maxMixedDMLCount {
+ distinctDMLTables := make(map[string]struct{})
+ for _, ref := range dmlRefs {
+ distinctKey := strings.ToLower(ref.table.database) + "." + strings.ToLower(ref.table.table)
+ distinctDMLTables[distinctKey] = struct{}{}
}
- if !hasUniqueInWhereClause(dbMetadata, update, table) {
- return false
+ if len(distinctDMLTables) > 1 {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Prior backup cannot handle more than %d DML statements across different tables", maxMixedDMLCount),
+ Code: code.BuiltinPriorBackupCheck.Int32(),
+ StartPosition: nil,
+ })
}
}
- return true
+ return adviceList, nil
}
-func hasUniqueInWhereClause(dbMetadata *storepb.DatabaseSchemaMetadata, update *ast.UpdateStmt, table *table) bool {
- if update.Where == nil {
- return false
- }
- list := extractColumnsInEqualCondition(table, update.Where)
- columnMap := make(map[string]bool)
- for _, column := range list {
- columnMap[strings.ToLower(column)] = true
- }
+// priorBackupTable is the (database, table) qualifier captured from
+// UPDATE/DELETE table references. File-local — only this advisor uses it.
+type priorBackupTable struct {
+ database string
+ table string
+}
- if dbMetadata != nil {
- for _, schema := range dbMetadata.Schemas {
- for _, tableSchema := range schema.Tables {
- if strings.EqualFold(tableSchema.Name, table.table) {
- for _, index := range tableSchema.Indexes {
- if index.Unique || index.Primary {
- exists := true
- for _, column := range index.Expressions {
- if !columnMap[strings.ToLower(column)] {
- exists = false
- break
- }
- }
- if exists {
- return true
- }
- }
- }
- }
- }
- }
+// omniExtractDMLTables walks an UpdateStmt.Tables or DeleteStmt.Tables
+// slice (each element is a TableExpr — *TableRef, *JoinClause, or
+// *SubqueryExpr) and returns the base-table references reached.
+// Unqualified references get defaultDB filled in (cumulative #30
+// Codex-fix-2 revised: resolve at extraction time so grouping keys
+// are consistent across qualified vs unqualified references).
+// Cumulative #26 — UNION-rooted derived tables (accessed via
+// SubqueryExpr at this layer) return nil. Derived tables aren't
+// base-table candidates for backup-type mixing.
+func omniExtractDMLTables(tables []ast.TableExpr, defaultDB string) []priorBackupTable {
+ var result []priorBackupTable
+ for _, t := range tables {
+ result = append(result, omniExtractTableExprRefs(t, defaultDB)...)
}
-
- return false
+ return result
}
-func extractColumnsInEqualCondition(table *table, node ast.ExprNode) []string {
- if node == nil {
+// omniExtractTableExprRefs walks a single omni TableExpr, returning
+// the base-table references reached. Empty Schema is resolved to
+// defaultDB (typically checkCtx.DBSchema.Name).
+func omniExtractTableExprRefs(t ast.TableExpr, defaultDB string) []priorBackupTable {
+ if t == nil {
return nil
}
-
- switch n := node.(type) {
- case *ast.BinaryOperationExpr:
- switch n.Op {
- case opcode.LogicAnd:
- return append(extractColumnsInEqualCondition(table, n.L), extractColumnsInEqualCondition(table, n.R)...)
- case opcode.EQ:
- if isConstant(n.R) {
- return extractColumnsInEqualCondition(table, n.L)
- } else if isConstant(n.L) {
- return extractColumnsInEqualCondition(table, n.R)
- }
-
- return nil
- default:
- return nil
- }
- case *ast.ColumnNameExpr:
- if n.Name == nil {
- return nil
+ switch n := t.(type) {
+ case *ast.TableRef:
+ db := n.Schema
+ if db == "" {
+ db = defaultDB
}
-
- if n.Name.Schema.String() != "" && table.database != "" && !strings.EqualFold(n.Name.Schema.String(), table.database) {
- return nil
- }
- if n.Name.Table.String() != "" && table.table != "" && !strings.EqualFold(n.Name.Table.String(), table.table) {
- return nil
- }
- return []string{n.Name.Name.L}
+ return []priorBackupTable{{
+ table: n.Name,
+ database: db,
+ }}
+ case *ast.JoinClause:
+ left := omniExtractTableExprRefs(n.Left, defaultDB)
+ right := omniExtractTableExprRefs(n.Right, defaultDB)
+ return append(left, right...)
+ case *ast.SubqueryExpr:
+ // Cumulative #26: derived tables aren't base-table candidates;
+ // nil matches mysql's modernized behavior and pre-omni tidb's
+ // *SubqueryExpr nil-return arm.
+ return nil
default:
return nil
}
}
-func isConstant(n ast.ExprNode) bool {
- switch n.(type) {
- case ast.ValueExpr:
- return true
- default:
- return false
- }
+// updateTableAliasMap holds resolution state for an UpdateStmt's
+// SET-clause target extraction. Three lookup paths plus a single-
+// target-detection list:
+//
+// - bySchemaName: canonical "schema.name" key → base. Used for
+// fully-qualified SET LHS (`SET db1.t.col = ...`). Cumulative
+// #30 Codex-fix-1d: required because joined tables with the
+// same bare name in different schemas (e.g.
+// `UPDATE db1.tech_book JOIN db2.tech_book ...`) collide in a
+// bare-name-only lookup. Schema-qualified lookup disambiguates.
+//
+// - byAlias: canonical alias key → base. Only registered for
+// aliased TableRefs. Aliases are unique within a query (parser-
+// enforced) so no ambiguity.
+//
+// - byBareName: canonical bare-name key → list of bases. Multiple
+// bases under the same bare name means the bare-name reference
+// is ambiguous (resolution skips to avoid misattribution).
+//
+// - distinctBases: deduplicated base tables in the FROM clause.
+// Used to detect single-target UPDATE for unqualified-column
+// SET attribution. Pre-Codex-fix-1b counted lookup-map entries
+// directly, which double-counted aliased TableRefs.
+type updateTableAliasMap struct {
+ bySchemaName map[string]priorBackupTable
+ byAlias map[string]priorBackupTable
+ byBareName map[string][]priorBackupTable
+ distinctBases []priorBackupTable
}
-func equalTable(t1, t2 *table) bool {
- if t1 == nil || t2 == nil {
- return false
+// omniBuildTableAliasMap walks an UpdateStmt's Tables slice and
+// builds resolution state for SET-clause target extraction.
+// Unqualified TableRef Schema is resolved to defaultDB.
+// JoinClause recurses into Left+Right. SubqueryExpr contributes
+// nothing — derived tables aren't base-table candidates (cumulative
+// #26).
+func omniBuildTableAliasMap(tables []ast.TableExpr, defaultDB string) *updateTableAliasMap {
+ m := &updateTableAliasMap{
+ bySchemaName: make(map[string]priorBackupTable),
+ byAlias: make(map[string]priorBackupTable),
+ byBareName: make(map[string][]priorBackupTable),
}
- return t1.database == t2.database && t1.table == t2.table
-}
-
-type table struct {
- database string
- table string
+ for _, t := range tables {
+ omniCollectTableAliases(t, defaultDB, m)
+ }
+ return m
}
-func extractResultSetNode(n ast.ResultSetNode) ([]table, error) {
- if n == nil {
- return nil, nil
+func omniCollectTableAliases(t ast.TableExpr, defaultDB string, m *updateTableAliasMap) {
+ if t == nil {
+ return
}
- switch n := n.(type) {
- case *ast.SelectStmt:
- return nil, nil
+ switch n := t.(type) {
+ case *ast.TableRef:
+ db := n.Schema
+ if db == "" {
+ db = defaultDB
+ }
+ base := priorBackupTable{database: db, table: n.Name}
+ nameLower := strings.ToLower(n.Name)
+ dbLower := strings.ToLower(db)
+ m.bySchemaName[dbLower+"."+nameLower] = base
+ if n.Alias != "" {
+ m.byAlias[strings.ToLower(n.Alias)] = base
+ }
+ m.byBareName[nameLower] = append(m.byBareName[nameLower], base)
+ // distinctBases dedup by canonicalized (db, table).
+ distinctKey := dbLower + "." + nameLower
+ alreadyTracked := false
+ for _, b := range m.distinctBases {
+ if strings.ToLower(b.database)+"."+strings.ToLower(b.table) == distinctKey {
+ alreadyTracked = true
+ break
+ }
+ }
+ if !alreadyTracked {
+ m.distinctBases = append(m.distinctBases, base)
+ }
+ case *ast.JoinClause:
+ omniCollectTableAliases(n.Left, defaultDB, m)
+ omniCollectTableAliases(n.Right, defaultDB, m)
case *ast.SubqueryExpr:
- return nil, nil
- case *ast.TableSource:
- return extractTableSource(n)
- case *ast.TableName:
- return extractTableName(n)
- case *ast.Join:
- return extractJoin(n)
- case *ast.SetOprStmt:
- return nil, nil
+ // Derived table — not a base-table candidate.
+ default:
}
- return nil, nil
-}
-
-func extractTableRefs(n *ast.TableRefsClause) ([]table, error) {
- return extractJoin(n.TableRefs)
}
-func extractJoin(n *ast.Join) ([]table, error) {
- l, err := extractResultSetNode(n.Left)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract left node in join")
+// omniExtractUpdateTargets walks an UpdateStmt's SetList and returns
+// the distinct base-table references that are actual mutation
+// targets. Resolution by qualifier type:
+//
+// 1. Column.Schema != "" AND Column.Table != "" → fully-qualified
+// lookup via bySchemaName. Cumulative #30 Codex-fix-1d.
+// 2. Column.Schema == "" AND Column.Table != "" → byAlias first
+// (aliases unambiguous); on miss, byBareName. If byBareName
+// has multiple candidates (joined same-named tables across
+// schemas), the reference is ambiguous → skip.
+// 3. Both empty → single-target shortcut via distinctBases.
+// Multi-target falls through to schema-aware column resolution
+// (Codex-fix-1e): walk dbMetadata for each candidate base to
+// find which one owns the column; single match → use; multiple
+// or zero → skip (MySQL itself errors on ambiguous unqualified
+// refs at execution time).
+//
+// Returns deduplicated targets.
+func omniExtractUpdateTargets(setList []*ast.Assignment, m *updateTableAliasMap, dbMetadata *storepb.DatabaseSchemaMetadata) []priorBackupTable {
+ var result []priorBackupTable
+ seen := make(map[string]bool)
+ add := func(t priorBackupTable) {
+ key := strings.ToLower(t.database) + "." + strings.ToLower(t.table)
+ if seen[key] {
+ return
+ }
+ seen[key] = true
+ result = append(result, t)
}
- r, err := extractResultSetNode(n.Right)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to extract right node in join")
+ for _, a := range setList {
+ if a == nil || a.Column == nil {
+ continue
+ }
+ col := a.Column
+ switch {
+ case col.Schema != "" && col.Table != "":
+ // Cumulative #30 Codex-fix-1d: schema-qualified lookup
+ // disambiguates same-bare-name joined tables across schemas.
+ key := strings.ToLower(col.Schema) + "." + strings.ToLower(col.Table)
+ if base, ok := m.bySchemaName[key]; ok {
+ add(base)
+ }
+ case col.Table != "":
+ // Try alias first (always unambiguous).
+ if base, ok := m.byAlias[strings.ToLower(col.Table)]; ok {
+ add(base)
+ continue
+ }
+ // Bare-name lookup. Single match → use; multiple → skip
+ // (ambiguous user reference under joined same-named
+ // tables; without schema info we can't disambiguate).
+ bases := m.byBareName[strings.ToLower(col.Table)]
+ if len(bases) == 1 {
+ add(bases[0])
+ }
+ default:
+ // Unqualified SET column.
+ // Single-target shortcut (Codex-fix-1b: count DISTINCT
+ // base tables, not lookup-map entries).
+ if len(m.distinctBases) == 1 {
+ add(m.distinctBases[0])
+ continue
+ }
+ // Multi-target: schema-aware column resolution
+ // (Codex-fix-1e, refined by Codex-fix-1h to mirror
+ // transformer semantics — any-match attributes; zero-
+ // match or no-metadata falls back to all distinctBases).
+ for _, t := range omniResolveUnqualifiedSETColumn(col.Column, m.distinctBases, dbMetadata) {
+ add(t)
+ }
+ }
}
- l = append(l, r...)
- return l, nil
+ return result
}
-func extractTableSource(n *ast.TableSource) ([]table, error) {
- if n == nil {
- return nil, nil
+// omniResolveUnqualifiedSETColumn resolves an unqualified SET column
+// name to its owning base table(s). Mirrors the transformer's
+// resolveUnqualifiedColumns semantics at
+// parser/tidb/backup.go:539-576 (cumulative #30 Codex-fix-1h):
+//
+// - No metadata available → return ALL distinctBases (fallback).
+// - Column found in N≥1 distinct bases → return THOSE N bases.
+// Single-match is the common case (Codex-fix-1e — typical
+// joins-for-filtering pattern); multi-match means the column
+// is ambiguous (MySQL itself errors at execution time, but
+// the advisor signals the broader risk surface).
+// - Column not found in ANY base → return ALL distinctBases
+// (fallback — column likely added by an earlier statement
+// or schema lag; the transformer treats this conservatively
+// and the advisor must too to avoid approving inputs the
+// transformer rejects).
+//
+// Earlier Codex-fix-1e returned nil on ambiguous/zero matches —
+// that diverged from transformer behavior and let multi-table
+// count-cap violations slip through (Codex P1 #10). Refined here
+// to mirror the transformer; the advisor stays as the prediction
+// layer for the transformer's runtime constraint.
+//
+// Schema-match policy: a base whose database doesn't match
+// dbMetadata.Name is excluded from the catalog walk — we have
+// no catalog info for it. Cross-database UPDATEs with unqualified
+// SET fall through to the zero-match path → all distinctBases
+// fallback. Case-insensitive matching on table/column names per
+// MySQL convention.
+func omniResolveUnqualifiedSETColumn(colName string, distinctBases []priorBackupTable, dbMetadata *storepb.DatabaseSchemaMetadata) []priorBackupTable {
+ if colName == "" {
+ return distinctBases
}
- return extractResultSetNode(n.Source)
-}
-
-func extractTableName(n *ast.TableName) ([]table, error) {
- if n == nil {
- return nil, nil
+ if dbMetadata == nil {
+ return distinctBases
+ }
+ dbName := dbMetadata.GetName()
+ var matches []priorBackupTable
+ for _, base := range distinctBases {
+ // Cross-DB bases: no catalog info; skip the catalog walk
+ // for these. They still participate in the fallback path
+ // when overall match count is zero.
+ if base.database != "" && dbName != "" && !strings.EqualFold(base.database, dbName) {
+ continue
+ }
+ for _, schema := range dbMetadata.Schemas {
+ for _, table := range schema.Tables {
+ if !strings.EqualFold(table.Name, base.table) {
+ continue
+ }
+ for _, c := range table.Columns {
+ if strings.EqualFold(c.Name, colName) {
+ matches = append(matches, base)
+ break
+ }
+ }
+ }
+ }
}
- return []table{
- {
- table: n.Name.O,
- database: n.Schema.O,
- },
- }, nil
+ if len(matches) > 0 {
+ return matches
+ }
+ return distinctBases
}
+
+// (omniIsDDLStmt removed in cumulative #30 Codex-fix-1f. DDL
+// detection uses pingcap's DDLNode interface directly via the
+// getTiDBNodes path — handles Tier-4-deferred grammar that omni
+// rejects at parse time. Full lesson + 9-sub-fix algorithm-
+// corrections lineage lives in plan-doc cumulative #30.)
diff --git a/backend/plugin/advisor/tidb/test/statement_prior_backup_check.yaml b/backend/plugin/advisor/tidb/test/statement_prior_backup_check.yaml
deleted file mode 100644
index 4f06939948..0000000000
--- a/backend/plugin/advisor/tidb/test/statement_prior_backup_check.yaml
+++ /dev/null
@@ -1,27 +0,0 @@
-- statement: DELETE FROM tech_book WHERE a > 1;
- want:
- - status: 1
- code: 0
- title: OK
- content: ""
- line: 0
- column: 0
- details: ""
-- statement: UPDATE tech_book SET id = 1;
- want:
- - status: 1
- code: 0
- title: OK
- content: ""
- line: 0
- column: 0
- details: ""
-- statement: UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
- want:
- - status: 2
- code: 227
- title: BUILTIN_PRIOR_BACKUP_CHECK
- content: Found 1 DELETE, 1 UPDATE on table ``.`tech_book`, disallow mixing different types of DML statements
- line: 0
- column: 0
- details: ""
diff --git a/backend/plugin/advisor/tidb/tidb_rules_test.go b/backend/plugin/advisor/tidb/tidb_rules_test.go
index 0504eee3c4..57b301cb5e 100644
--- a/backend/plugin/advisor/tidb/tidb_rules_test.go
+++ b/backend/plugin/advisor/tidb/tidb_rules_test.go
@@ -1,8 +1,13 @@
package tidb
import (
+ "context"
+ "strings"
"testing"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/component/sheet"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
)
@@ -64,3 +69,377 @@ func TestTiDBRules(t *testing.T) {
advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_TIDB, false /* record */)
}
}
+
+// TestTiDBPriorBackupCheckAdvisor exercises the BUILTIN_PRIOR_BACKUP_CHECK
+// advisor's full check pipeline. Modeled on the mysql analog's
+// TestMariaDBPriorBackupCheckAdvisor — the standard fixture driver
+// doesn't set ListDatabaseNamesFunc, so backup-database-existence and
+// per-table DML-mixing checks need dedicated context construction.
+//
+// Coverage:
+// - Mixed DDL + DML: triggers "mixed DDL and DML" advice
+// - Backup db missing: triggers "Need database ... does not exist"
+// - Backup db present + plain UPDATE: no DML-mixing advice
+// - Per-table DML-type mixing (UPDATE + DELETE on same table):
+// triggers "mixed DML statements on the same table" advice
+// - Size cap exceeded: triggers size-limit advice
+func TestTiDBPriorBackupCheckAdvisor(t *testing.T) {
+ sm := sheet.NewManager()
+ // Large statement for size-cap testing: must exceed
+ // common.MaxSheetCheckSize (2 * 1024 * 1024 bytes). Padded
+ // with a SQL comment so the statement parses cleanly.
+ largeStatement := "UPDATE tech_book SET id = 1 WHERE id = 2; -- " + strings.Repeat("x", 2*1024*1024+100)
+ cases := []struct {
+ name string
+ statement string
+ backupDBPresent bool
+ wantContentSubstr []string
+ wantNoneSubstr []string
+ }{
+ {
+ name: "mixed DDL and DML fires",
+ statement: "CREATE TABLE t(id INT);\nUPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DDL and DML",
+ },
+ wantNoneSubstr: []string{
+ "does not exist",
+ },
+ },
+ {
+ name: "backup db missing fires",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: false,
+ wantContentSubstr: []string{
+ "does not exist",
+ },
+ },
+ {
+ name: "single UPDATE on table — clean",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: true,
+ wantNoneSubstr: []string{
+ "mixed DDL and DML",
+ "mixed DML statements",
+ "does not exist",
+ "exceeds the maximum limit",
+ },
+ },
+ {
+ name: "per-table DML mixing (UPDATE + DELETE on same table) fires",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 2;\nDELETE FROM tech_book WHERE id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ "tech_book",
+ },
+ },
+ {
+ name: "UPDATE + DELETE on DIFFERENT tables — clean",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 2;\nDELETE FROM orders WHERE order_id = 3;",
+ backupDBPresent: true,
+ wantNoneSubstr: []string{
+ "mixed DML statements",
+ },
+ },
+ {
+ name: "size cap exceeded fires",
+ statement: largeStatement,
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "exceeds the maximum limit",
+ "for backup",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1: UPDATE JOIN should NOT
+ // false-positive on the joined-only read table.
+ // `UPDATE t1 JOIN t2 ON ... SET t1.col=...` mutates t1
+ // only; t2 is read-only. Following DELETE FROM t2 is
+ // pure DELETE — no mixing on either table. Pre-fix code
+ // tagged BOTH t1 and t2 as UPDATE targets, then matched
+ // t2's UPDATE+DELETE → false-positive. Post-fix:
+ // SET-clause-based target extraction → only t1 tagged
+ // for UPDATE → no mixing.
+ name: "UPDATE-JOIN + DELETE-on-joined-table — no false-positive (Codex-fix-1)",
+ statement: "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET tech_book.name = 'x';\nDELETE FROM orders WHERE order_id = 5;",
+ backupDBPresent: true,
+ wantNoneSubstr: []string{
+ "mixed DML statements",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-2: case-insensitive grouping.
+ // `UPDATE tech_book ...; DELETE FROM Tech_Book ...`
+ // references the same logical table with different
+ // casing; pre-fix code split into two buckets and missed
+ // the mixing. Post-fix: lowercased grouping key.
+ name: "case-insensitive grouping — Tech_Book ≡ tech_book (Codex-fix-2)",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 2;\nDELETE FROM Tech_Book WHERE id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1b: aliased single-table
+ // UPDATE with unqualified SET. The lookup map contains
+ // 2 entries (alias + bare name) but only 1 distinct
+ // base table. Pre-fix-1b code gated on `len(lookup) == 1`
+ // → skipped attribution → following DELETE on same
+ // table wasn't flagged as mixed. Post-fix-1b: gate on
+ // `len(distinctBases) == 1` → attribution correctly
+ // records UPDATE on tech_book → DELETE → mixed-DML fires.
+ name: "aliased single-table UPDATE with unqualified SET (Codex-fix-1b)",
+ statement: "UPDATE tech_book AS t SET id = 1 WHERE id = 2;\nDELETE FROM tech_book WHERE id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-2 (revised): qualified vs
+ // unqualified split when CurrentDatabase is unset
+ // (plancheck path). Pre-fix used CurrentDatabase as the
+ // default fallback, which is empty in
+ // statement_advise_executor.go:168-180. Post-fix uses
+ // DBSchema.Name as the default (the schema being checked,
+ // reliably populated across review paths). Mock catalog's
+ // DBSchema.Name is "test", so unqualified `tech_book`
+ // resolves to `test.tech_book` and matches qualified
+ // `test.tech_book` (case-insensitive).
+ name: "qualified vs unqualified same-table mixing (Codex-fix-2 revised)",
+ statement: "UPDATE test.tech_book SET id = 1 WHERE id = 2;\nDELETE FROM tech_book WHERE id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1c: DROP VIEW must be classified
+ // as DDL. Pingcap parses `DROP VIEW v` as *ast.DropTableStmt
+ // (already in DDL list); omni splits to *ast.DropViewStmt
+ // which initial port excluded → regression. Post-fix:
+ // DropViewStmt added to DDL set.
+ name: "DROP VIEW + UPDATE fires mixed DDL+DML (Codex-fix-1c)",
+ statement: "DROP VIEW v;\nUPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DDL and DML",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1c: ALTER DATABASE must be
+ // classified as DDL. Pingcap implements DDLNode via
+ // ddlNode struct embedding (ast/base.go:81); peer's
+ // compile-time `_ DDLNode = ...` grep missed this →
+ // initial port excluded AlterDatabaseStmt → regression.
+ // Empirical verification: parse + .(ast.DDLNode) returns
+ // true for AlterDatabaseStmt.
+ name: "ALTER DATABASE + UPDATE fires mixed DDL+DML (Codex-fix-1c)",
+ statement: "ALTER DATABASE test COLLATE = utf8mb4_bin;\nUPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DDL and DML",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1d: schema-qualified UPDATE
+ // target disambiguation. Joined same-bare-name tables
+ // across schemas (`db1.tech_book JOIN db2.tech_book`):
+ // pre-fix lookup-by-bare-name overwrote the first entry
+ // with the second, so `SET db1.tech_book.id = 1` resolved
+ // to db2.tech_book (wrong). Following `DELETE FROM
+ // db1.tech_book` → DELETE on db1; UPDATE went to db2 →
+ // no mixing detected → false-negative. Post-fix: separate
+ // bySchemaName lookup keyed by "schema.name" disambiguates.
+ //
+ // Note: this fixture uses two parsed-but-non-existent
+ // databases (db1, db2) — the advisor's grouping logic
+ // runs purely on AST-level table identifiers, not on
+ // catalog schema existence. The mixed-DML detection
+ // fires regardless of whether the named databases exist.
+ name: "schema-qualified UPDATE target disambiguation (Codex-fix-1d)",
+ statement: "UPDATE db1.tech_book JOIN db2.tech_book ON db1.tech_book.id = db2.tech_book.id SET db1.tech_book.id = 1;\nDELETE FROM db1.tech_book WHERE id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ "db1.tech_book",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1g: count-cap with multi-table
+ // gate. The backup transformer at parser/tidb/backup.go:
+ // 96-110 routes >5 DML statements into single-table-only
+ // path which errors on multi-table inputs. The advisor
+ // must catch this pre-execution.
+ //
+ // 6 UPDATEs across two tables (tech_book × 3 + orders × 3)
+ // → must fire "more than 5 DML statements across different
+ // tables".
+ name: "6+ DML across multiple tables fires count-cap (Codex-fix-1g)",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 1;\n" +
+ "UPDATE tech_book SET id = 2 WHERE id = 2;\n" +
+ "UPDATE tech_book SET id = 3 WHERE id = 3;\n" +
+ "UPDATE orders SET order_id = 4 WHERE order_id = 4;\n" +
+ "UPDATE orders SET order_id = 5 WHERE order_id = 5;\n" +
+ "UPDATE orders SET order_id = 6 WHERE order_id = 6;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "more than 5 DML statements across different tables",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1g: single-table batches >5
+ // are still OK — the transformer's
+ // generateSQLForSingleTable handles them successfully.
+ // 6 UPDATEs on tech_book only → no count-cap advice.
+ name: "6+ DML on single table — no count-cap advice (Codex-fix-1g)",
+ statement: "UPDATE tech_book SET id = 1 WHERE id = 1;\n" +
+ "UPDATE tech_book SET id = 2 WHERE id = 2;\n" +
+ "UPDATE tech_book SET id = 3 WHERE id = 3;\n" +
+ "UPDATE tech_book SET id = 4 WHERE id = 4;\n" +
+ "UPDATE tech_book SET id = 5 WHERE id = 5;\n" +
+ "UPDATE tech_book SET id = 6 WHERE id = 6;",
+ backupDBPresent: true,
+ wantNoneSubstr: []string{
+ "more than 5 DML statements",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1f: Tier-4-deferred DDL
+ // (CREATE/ALTER/DROP SEQUENCE) is omni-rejected at
+ // parse time. Pre-fix-1f used omniIsDDLStmt on
+ // omni-parsed stmts → soft-fail skipped the SEQUENCE
+ // stmt → DDL detection missed → no mixed-DDL advice
+ // despite the DML+DDL mixing. Post-fix-1f: DDL
+ // detection uses pingcap's DDLNode interface via
+ // `getTiDBNodes` → pingcap parses CreateSequenceStmt
+ // successfully → DDL detected → advice fires.
+ name: "CREATE SEQUENCE + UPDATE fires mixed DDL+DML via pingcap path (Codex-fix-1f)",
+ statement: "CREATE SEQUENCE seq1 START 1 INCREMENT BY 1;\nUPDATE tech_book SET id = 1 WHERE id = 2;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DDL and DML",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1f: FLASHBACK family is
+ // Tier-4-deferred grammar in omni. Pingcap handles
+ // FLASHBACK TABLE / FLASHBACK DATABASE as DDL via
+ // the DDLNode interface. Post-fix-1f: pingcap path
+ // catches it; pre-fix-1f silently skipped.
+ //
+ // Note: pingcap classifies FLASHBACK TABLE / DATABASE
+ // as DDL via ddlNode struct embedding. Verified by
+ // parse-test in batch 19 reshape investigation.
+ name: "FLASHBACK TABLE + DELETE fires mixed DDL+DML via pingcap path (Codex-fix-1f)",
+ statement: "FLASHBACK TABLE tech_book TO tech_book_old;\nDELETE FROM orders WHERE order_id = 5;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DDL and DML",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1h: multi-match fallback to
+ // all matched tables (mirrors transformer's
+ // resolveUnqualifiedColumns at backup.go:539-576).
+ // `name` column exists on BOTH tech_book and orders in
+ // MockMySQLDatabase. `UPDATE tech_book JOIN orders SET
+ // name = 'x'` → resolver returns BOTH tables. Then
+ // `DELETE FROM tech_book` makes tech_book mixed
+ // (UPDATE+DELETE) → mixed-DML fires on tech_book.
+ // Pre-fix-1h: resolver returned nil for multi-match →
+ // no UPDATE targets → no mixing detected → false-negative.
+ name: "multi-match unqualified SET attributes to all (Codex-fix-1h)",
+ statement: "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET name = 'x';\nDELETE FROM tech_book WHERE id = 5;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ "tech_book",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1h: zero-match fallback to
+ // all distinctBases. `notarealcolumn` doesn't exist on
+ // any joined table; transformer falls back to all
+ // tables. Six unqualified-SET multi-table UPDATEs with
+ // no-catalog-match column → count-cap fires (each
+ // contributes 2 distinctBases entries; total > 5 with
+ // distinct tables > 1).
+ //
+ // Pre-fix-1h: resolver returned nil → 0 dmlRefs → no
+ // count-cap fires → advisor approves SQL that the
+ // transformer would reject at runtime.
+ //
+ // Note: omni's parser permits unknown column names in
+ // SET (lazy column validation); the catalog walk simply
+ // fails to find them and falls back.
+ name: "zero-match unqualified SET falls back to all (Codex-fix-1h count-cap path)",
+ statement: "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET notarealcolumn = 1 WHERE tech_book.id = 1;\n" +
+ "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET notarealcolumn = 2 WHERE tech_book.id = 2;\n" +
+ "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET notarealcolumn = 3 WHERE tech_book.id = 3;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "more than 5 DML statements across different tables",
+ },
+ },
+ {
+ // Cumulative #30 Codex-fix-1e: schema-aware column
+ // resolution for unqualified SET in multi-table UPDATE.
+ // `UPDATE tech_book JOIN orders SET customer_name = 'x'`
+ // has unqualified `customer_name` — which exists on
+ // `orders` only (per MockMySQLDatabase). Pre-fix-1e:
+ // multi-target UPDATE with unqualified SET → skip → no
+ // UPDATE target recorded → following DELETE on orders
+ // would not fire as mixed-DML (false-negative).
+ // Post-fix-1e: omniResolveUnqualifiedSETColumn walks
+ // dbMetadata, finds customer_name on orders → UPDATE
+ // attributed to orders → DELETE on orders → mixed-DML
+ // fires.
+ name: "unqualified SET in multi-table UPDATE resolves via catalog (Codex-fix-1e)",
+ statement: "UPDATE tech_book INNER JOIN orders ON tech_book.id = orders.order_id SET customer_name = 'x';\nDELETE FROM orders WHERE order_id = 5;",
+ backupDBPresent: true,
+ wantContentSubstr: []string{
+ "mixed DML statements on the same table",
+ "orders",
+ },
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ ctx := advisor.Context{
+ DBType: storepb.Engine_TIDB,
+ DBSchema: advisor.MockMySQLDatabase, // tidb tests reuse mysql mock catalog
+ EnablePriorBackup: true,
+ InstanceID: "instance",
+ ListDatabaseNamesFunc: func(context.Context, string) ([]string, error) {
+ if tc.backupDBPresent {
+ return []string{"bbdataarchive"}, nil
+ }
+ return nil, nil
+ },
+ }
+ rule := &storepb.SQLReviewRule{
+ Type: storepb.SQLReviewRule_BUILTIN_PRIOR_BACKUP_CHECK,
+ Level: storepb.SQLReviewRule_WARNING,
+ }
+ adviceList, err := advisor.SQLReviewCheck(context.Background(), sm, tc.statement, []*storepb.SQLReviewRule{rule}, ctx)
+ require.NoError(t, err)
+ joined := ""
+ for _, a := range adviceList {
+ joined += a.Content + "\n"
+ }
+ for _, want := range tc.wantContentSubstr {
+ require.True(t, strings.Contains(joined, want),
+ "expected advice content containing %q, got: %s", want, joined)
+ }
+ for _, unwanted := range tc.wantNoneSubstr {
+ require.False(t, strings.Contains(joined, unwanted),
+ "expected NO advice content containing %q, got: %s", unwanted, joined)
+ }
+ })
+ }
+}
diff --git a/backend/plugin/advisor/tidb/utils_test.go b/backend/plugin/advisor/tidb/utils_test.go
index 0ce446ecdb..d85d83945e 100644
--- a/backend/plugin/advisor/tidb/utils_test.go
+++ b/backend/plugin/advisor/tidb/utils_test.go
@@ -429,3 +429,13 @@ func TestOmniCharLength(t *testing.T) {
})
}
}
+
+// TestOmniIsDDLStmt was REMOVED in cumulative #30 Codex-fix-1f:
+// the omniIsDDLStmt helper itself was removed when DDL detection
+// switched to pingcap's authoritative DDLNode interface via the
+// `getTiDBNodes` path. The omni-only enumeration was brittle (4
+// rounds of corrections across Codex-fix-1c/1d/1e) and fundamentally
+// gap-prone for Tier-4-deferred grammar (Sequence, FlashBackDatabase)
+// that omni's parser rejects but pingcap classifies as DDL. The
+// switch eliminates the enumeration. End-to-end coverage of the
+// DDL-detection path is now in TestTiDBPriorBackupCheckAdvisor.