Skip to content

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

refactor(advisor/tidb): migrate 5 more rules to omni AST (Phase 1.5 §1.5.3 batch 2) (#20198)

Vincent Huang87bb291e335d
diff --git a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
index d15daadb84..eb24036611 100644
--- a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
+++ b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
@@ -4,19 +4,17 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/pingcap/tidb/pkg/parser/ast"
+ "github.com/bytebase/omni/tidb/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
var (
_ advisor.Advisor = (*DatabaseAllowDropIfEmptyAdvisor)(nil)
- _ ast.Visitor = (*allowDropEmptyDBChecker)(nil)
)
func init() {
@@ -29,8 +27,7 @@ type DatabaseAllowDropIfEmptyAdvisor struct {
// Check checks for drop table naming convention.
func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- root, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -45,8 +42,9 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso
title: checkCtx.Rule.Type.String(),
originalMetadata: checkCtx.OriginalMetadata,
}
- for _, stmtNode := range root {
- (stmtNode).Accept(checker)
+
+ for _, ostmt := range stmts {
+ checker.checkStmt(ostmt)
}
return checker.adviceList, nil
@@ -59,31 +57,27 @@ type allowDropEmptyDBChecker struct {
originalMetadata *model.DatabaseMetadata
}
-// Enter implements the ast.Visitor interface.
-func (v *allowDropEmptyDBChecker) Enter(in ast.Node) (ast.Node, bool) {
- if node, ok := in.(*ast.DropDatabaseStmt); ok {
- if v.originalMetadata.DatabaseName() != node.Name.O {
- v.adviceList = append(v.adviceList, &storepb.Advice{
- Status: v.level,
- Code: code.NotCurrentDatabase.Int32(),
- Title: v.title,
- Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.originalMetadata.DatabaseName()),
- StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),
- })
- } else if !v.originalMetadata.HasNoTable() {
- v.adviceList = append(v.adviceList, &storepb.Advice{
- Status: v.level,
- Code: code.DatabaseNotEmpty.Int32(),
- Title: v.title,
- Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", node.Name),
- StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),
- })
- }
+func (c *allowDropEmptyDBChecker) checkStmt(ostmt OmniStmt) {
+ node, ok := ostmt.Node.(*ast.DropDatabaseStmt)
+ if !ok {
+ return
+ }
+ line := ostmt.AbsoluteLine(node.Loc.Start)
+ if c.originalMetadata.DatabaseName() != node.Name {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.NotCurrentDatabase.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, c.originalMetadata.DatabaseName()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if !c.originalMetadata.HasNoTable() {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.DatabaseNotEmpty.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", node.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
}
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*allowDropEmptyDBChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
}
diff --git a/backend/plugin/advisor/tidb/advisor_index_pk_type.go b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
index a30f7dc192..6c184b55db 100644
--- a/backend/plugin/advisor/tidb/advisor_index_pk_type.go
+++ b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
@@ -1,29 +1,26 @@
package tidb
-// Framework code is generated by the generator.
-
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/pingcap/tidb/pkg/parser/ast"
- "github.com/pingcap/tidb/pkg/parser/mysql"
- "github.com/pingcap/tidb/pkg/parser/types"
+ "github.com/bytebase/omni/tidb/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
var (
_ advisor.Advisor = (*IndexPkTypeAdvisor)(nil)
- _ ast.Visitor = (*indexPkTypeChecker)(nil)
)
+const errCannotFindColumnTypeFmt = "cannot find the type of `%s`.`%s`"
+
func init() {
advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_INDEX_PK_TYPE_LIMIT, &IndexPkTypeAdvisor{})
}
@@ -34,8 +31,7 @@ type IndexPkTypeAdvisor struct {
// Check checks for correct type of PK.
func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- stmtList, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -49,27 +45,29 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
title: checkCtx.Rule.Type.String(),
line: make(map[string]int),
originalMetadata: checkCtx.OriginalMetadata,
- tablesNewColumns: make(map[string]columnNameToColumnDef),
+ tablesNewColumns: make(tableNewOmniColumn),
}
- for _, stmt := range stmtList {
- (stmt).Accept(checker)
+ for _, ostmt := range stmts {
+ checker.checkStmt(ostmt)
}
return checker.adviceList, nil
}
-type columnNameToColumnDef map[string]*ast.ColumnDef
-type tableNewColumn map[string]columnNameToColumnDef
+// omni-AST analog of columnNameToColumnDef / tableNewColumn in utils.go.
+// (Pingcap-typed siblings remain there for un-migrated advisors.)
+type omniColumnNameToColumnDef map[string]*ast.ColumnDef
+type tableNewOmniColumn map[string]omniColumnNameToColumnDef
-func (t tableNewColumn) set(tableName string, columnName string, colDef *ast.ColumnDef) {
+func (t tableNewOmniColumn) set(tableName, columnName string, colDef *ast.ColumnDef) {
if _, ok := t[tableName]; !ok {
- t[tableName] = make(map[string]*ast.ColumnDef)
+ t[tableName] = make(omniColumnNameToColumnDef)
}
t[tableName][columnName] = colDef
}
-func (t tableNewColumn) get(tableName string, columnName string) (colDef *ast.ColumnDef, ok bool) {
+func (t tableNewOmniColumn) get(tableName, columnName string) (colDef *ast.ColumnDef, ok bool) {
if _, ok := t[tableName]; !ok {
return nil, false
}
@@ -77,7 +75,7 @@ func (t tableNewColumn) get(tableName string, columnName string) (colDef *ast.Co
return col, ok
}
-func (t tableNewColumn) delete(tableName string, columnName string) {
+func (t tableNewOmniColumn) delete(tableName, columnName string) {
if _, ok := t[tableName]; !ok {
return
}
@@ -90,7 +88,7 @@ type indexPkTypeChecker struct {
title string
line map[string]int
originalMetadata *model.DatabaseMetadata
- tablesNewColumns tableNewColumn
+ tablesNewColumns tableNewOmniColumn
}
type pkData struct {
@@ -100,147 +98,185 @@ type pkData struct {
line int
}
-// Enter implements the ast.Visitor interface.
-func (v *indexPkTypeChecker) Enter(in ast.Node) (ast.Node, bool) {
+func (c *indexPkTypeChecker) checkStmt(ostmt OmniStmt) {
var pkDataList []pkData
- switch node := in.(type) {
+ switch n := ostmt.Node.(type) {
case *ast.CreateTableStmt:
- tableName := node.Table.Name.String()
- for _, column := range node.Cols {
- pds := v.addNewColumn(tableName, column.OriginTextPosition(), column)
+ if n.Table == nil {
+ return
+ }
+ tableName := n.Table.Name
+ for _, column := range n.Columns {
+ if column == nil {
+ continue
+ }
+ pds := c.addNewColumn(tableName, ostmt.AbsoluteLine(column.Loc.Start), column)
pkDataList = append(pkDataList, pds...)
}
- for _, constraint := range node.Constraints {
- pds := v.addConstraint(tableName, constraint.OriginTextPosition(), constraint)
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ pds := c.addConstraint(tableName, ostmt.AbsoluteLine(constraint.Loc.Start), constraint)
pkDataList = append(pkDataList, pds...)
}
case *ast.AlterTableStmt:
- tableName := node.Table.Name.String()
- for _, spec := range node.Specs {
- switch spec.Tp {
- case ast.AlterTableAddColumns:
- for _, column := range spec.NewColumns {
- pds := v.addNewColumn(tableName, node.OriginTextPosition(), column)
+ if n.Table == nil {
+ return
+ }
+ tableName := n.Table.Name
+ stmtLine := ostmt.AbsoluteLine(n.Loc.Start)
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, column := range addColumnTargets(cmd) {
+ pds := c.addNewColumn(tableName, stmtLine, column)
pkDataList = append(pkDataList, pds...)
}
- case ast.AlterTableAddConstraint:
- pds := v.addConstraint(tableName, node.OriginTextPosition(), spec.Constraint)
- pkDataList = append(pkDataList, pds...)
- case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
- newColumnDef := spec.NewColumns[0]
- oldColumnName := newColumnDef.Name.Name.String()
- if spec.OldColumnName != nil {
- oldColumnName = spec.OldColumnName.Name.String()
+ case ast.ATAddConstraint:
+ if cmd.Constraint != nil {
+ pds := c.addConstraint(tableName, stmtLine, cmd.Constraint)
+ pkDataList = append(pkDataList, pds...)
+ }
+ case ast.ATChangeColumn, ast.ATModifyColumn:
+ if cmd.Column == nil {
+ continue
+ }
+ newColumnDef := cmd.Column
+ oldColumnName := newColumnDef.Name
+ if cmd.Type == ast.ATChangeColumn && cmd.Name != "" {
+ // CHANGE COLUMN: cmd.Name is the OLD column name.
+ oldColumnName = cmd.Name
}
- pds := v.changeColumn(tableName, oldColumnName, node.OriginTextPosition(), newColumnDef)
+ pds := c.changeColumn(tableName, oldColumnName, stmtLine, newColumnDef)
pkDataList = append(pkDataList, pds...)
default:
}
}
default:
+ return
}
+
for _, pd := range pkDataList {
- v.adviceList = append(v.adviceList, &storepb.Advice{
- Status: v.level,
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.IndexPKType.Int32(),
- Title: v.title,
+ Title: c.title,
Content: fmt.Sprintf("Columns in primary key must be INT/BIGINT but `%s`.`%s` is %s", pd.table, pd.column, pd.columnType),
StartPosition: common.ConvertANTLRLineToPosition(pd.line),
})
}
- return in, false
}
-// Leave implements the ast.Visitor interface.
-func (*indexPkTypeChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
-}
-
-func (v *indexPkTypeChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {
+func (c *indexPkTypeChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {
var pkDataList []pkData
- for _, option := range colDef.Options {
- if option.Tp == ast.ColumnOptionPrimaryKey {
- tp := v.getIntOrBigIntStr(colDef.Tp)
+ for _, constraint := range colDef.Constraints {
+ if constraint != nil && constraint.Type == ast.ColConstrPrimaryKey {
+ tp := formatColumnType(colDef.TypeName)
if tp != "INT" && tp != "BIGINT" {
pkDataList = append(pkDataList, pkData{
table: tableName,
- column: colDef.Name.String(),
+ column: colDef.Name,
columnType: tp,
line: line,
})
}
}
}
- v.tablesNewColumns.set(tableName, colDef.Name.String(), colDef)
+ c.tablesNewColumns.set(tableName, colDef.Name, colDef)
return pkDataList
}
-func (v *indexPkTypeChecker) changeColumn(tableName, oldColumnName string, line int, newColumnDef *ast.ColumnDef) []pkData {
+func (c *indexPkTypeChecker) changeColumn(tableName, oldColumnName string, line int, newColumnDef *ast.ColumnDef) []pkData {
var pkDataList []pkData
- v.tablesNewColumns.delete(tableName, oldColumnName)
- for _, option := range newColumnDef.Options {
- if option.Tp == ast.ColumnOptionPrimaryKey {
- tp := v.getIntOrBigIntStr(newColumnDef.Tp)
+ c.tablesNewColumns.delete(tableName, oldColumnName)
+ for _, constraint := range newColumnDef.Constraints {
+ if constraint != nil && constraint.Type == ast.ColConstrPrimaryKey {
+ tp := formatColumnType(newColumnDef.TypeName)
if tp != "INT" && tp != "BIGINT" {
pkDataList = append(pkDataList, pkData{
table: tableName,
- column: newColumnDef.Name.String(),
+ column: newColumnDef.Name,
columnType: tp,
line: line,
})
}
}
}
- v.tablesNewColumns.set(tableName, newColumnDef.Name.String(), newColumnDef)
+ c.tablesNewColumns.set(tableName, newColumnDef.Name, newColumnDef)
return pkDataList
}
-func (v *indexPkTypeChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {
+func (c *indexPkTypeChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {
var pkDataList []pkData
- if constraint.Tp == ast.ConstraintPrimaryKey {
- for _, key := range constraint.Keys {
- columnName := key.Column.Name.String()
- columnType, err := v.getPKColumnType(tableName, columnName)
- if err != nil {
- continue
- }
- if columnType != "INT" && columnType != "BIGINT" {
- pkDataList = append(pkDataList, pkData{
- table: tableName,
- column: columnName,
- columnType: columnType,
- line: line,
- })
- }
+ if constraint.Type != ast.ConstrPrimaryKey {
+ return pkDataList
+ }
+ for _, columnName := range constraint.Columns {
+ columnType, err := c.getPKColumnType(tableName, columnName)
+ if err != nil {
+ continue
+ }
+ if columnType != "INT" && columnType != "BIGINT" {
+ pkDataList = append(pkDataList, pkData{
+ table: tableName,
+ column: columnName,
+ columnType: columnType,
+ line: line,
+ })
}
}
return pkDataList
}
-// getPKColumnType gets the column type string from v.tablesNewColumns or catalog, returns empty string and non-nil error if cannot find the column in given table.
-func (v *indexPkTypeChecker) getPKColumnType(tableName string, columnName string) (string, error) {
- if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {
- return v.getIntOrBigIntStr(colDef.Tp), nil
+// getPKColumnType returns the rendered type string for tableName.columnName,
+// preferring the column we observed in this check pass and falling back to
+// the catalog. Returns an empty string and a non-nil error if the column
+// cannot be located in either.
+func (c *indexPkTypeChecker) getPKColumnType(tableName, columnName string) (string, error) {
+ if colDef, ok := c.tablesNewColumns.get(tableName, columnName); ok {
+ return formatColumnType(colDef.TypeName), nil
}
- column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
- if column != nil {
- return column.GetProto().Type, nil
+ schema := c.originalMetadata.GetSchemaMetadata("")
+ if schema == nil {
+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)
}
- return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
+ table := schema.GetTable(tableName)
+ if table == nil {
+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)
+ }
+ column := table.GetColumn(columnName)
+ if column == nil {
+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)
+ }
+ return column.GetProto().Type, nil
}
-// getIntOrBigIntStr returns the type string of tp.
-func (*indexPkTypeChecker) getIntOrBigIntStr(tp *types.FieldType) string {
- switch tp.GetType() {
- // https://pkg.go.dev/github.com/pingcap/tidb/pkg/parser/mysql#TypeLong
- case mysql.TypeLong:
- // tp.String() return int(11)
+// formatColumnType renders a column type for advice content:
+// "INT"/"BIGINT" uppercase (matched against the rule), everything else
+// lowercase Name with optional "(Length[,Scale])". Types with non-trivial
+// rendering (ENUM/SET value lists, BIT, DECIMAL implicit-Scale) are not
+// special-cased; extend the switch if a fixture exposes a divergence.
+func formatColumnType(t *ast.DataType) string {
+ if t == nil {
+ return ""
+ }
+ switch strings.ToUpper(t.Name) {
+ case "INT":
return "INT"
- // https://pkg.go.dev/github.com/pingcap/tidb/pkg/parser/mysql#TypeLonglong
- case mysql.TypeLonglong:
- // tp.String() return bigint(20)
+ case "BIGINT":
return "BIGINT"
+ }
+ lower := strings.ToLower(t.Name)
+ switch {
+ case t.Length > 0 && t.Scale > 0:
+ return fmt.Sprintf("%s(%d,%d)", lower, t.Length, t.Scale)
+ case t.Length > 0:
+ return fmt.Sprintf("%s(%d)", lower, t.Length)
default:
+ return lower
}
- return tp.String()
}
diff --git a/backend/plugin/advisor/tidb/advisor_migration_compatibility.go b/backend/plugin/advisor/tidb/advisor_migration_compatibility.go
index dc2a827744..9114dd5371 100644
--- a/backend/plugin/advisor/tidb/advisor_migration_compatibility.go
+++ b/backend/plugin/advisor/tidb/advisor_migration_compatibility.go
@@ -4,17 +4,16 @@ import (
"context"
"fmt"
+ "github.com/bytebase/omni/tidb/ast"
+
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/pingcap/tidb/pkg/parser/ast"
)
var (
_ advisor.Advisor = (*CompatibilityAdvisor)(nil)
- _ ast.Visitor = (*compatibilityChecker)(nil)
)
func init() {
@@ -27,8 +26,7 @@ type CompatibilityAdvisor struct {
// Check checks schema backward compatibility.
func (*CompatibilityAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- root, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -42,8 +40,8 @@ func (*CompatibilityAdvisor) Check(_ context.Context, checkCtx advisor.Context)
level: level,
title: checkCtx.Rule.Type.String(),
}
- for _, stmtNode := range root {
- (stmtNode).Accept(c)
+ for _, ostmt := range stmts {
+ c.checkStmt(ostmt)
}
return c.adviceList, nil
@@ -56,114 +54,101 @@ type compatibilityChecker struct {
lastCreateTable string
}
-// Enter implements the ast.Visitor interface.
-func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
+func (c *compatibilityChecker) checkStmt(ostmt OmniStmt) {
+ // Line and code captured in the same arm so they cannot drift.
code := advisorcode.Ok
- switch node := in.(type) {
- // CREATE TABLE
+ line := 0
+ switch n := ostmt.Node.(type) {
case *ast.CreateTableStmt:
- v.lastCreateTable = node.Table.Name.O
- // DROP DATABASE
+ if n.Table != nil {
+ c.lastCreateTable = n.Table.Name
+ }
case *ast.DropDatabaseStmt:
code = advisorcode.CompatibilityDropDatabase
- // RENAME TABLE
+ line = ostmt.AbsoluteLine(n.Loc.Start)
case *ast.RenameTableStmt:
code = advisorcode.CompatibilityRenameTable
- // DROP TABLE/VIEW
+ line = ostmt.AbsoluteLine(n.Loc.Start)
case *ast.DropTableStmt:
code = advisorcode.CompatibilityDropTable
- // ALTER TABLE
+ line = ostmt.AbsoluteLine(n.Loc.Start)
case *ast.AlterTableStmt:
- if node.Table.Name.O == v.lastCreateTable {
+ if n.Table != nil && n.Table.Name == c.lastCreateTable {
break
}
- for _, spec := range node.Specs {
- // RENAME COLUMN
- if spec.Tp == ast.AlterTableRenameColumn {
- code = advisorcode.CompatibilityRenameColumn
- break
- }
- // DROP COLUMN
- if spec.Tp == ast.AlterTableDropColumn {
- code = advisorcode.CompatibilityDropColumn
- break
- }
- // RENAME TABLE
- if spec.Tp == ast.AlterTableRenameTable {
- code = advisorcode.CompatibilityRenameTable
- break
- }
-
- if spec.Tp == ast.AlterTableAddConstraint {
- // ADD PRIMARY KEY
- if spec.Constraint.Tp == ast.ConstraintPrimaryKey {
- code = advisorcode.CompatibilityAddPrimaryKey
- break
- }
- // ADD UNIQUE/UNIQUE KEY/UNIQUE INDEX
- if spec.Constraint.Tp == ast.ConstraintPrimaryKey ||
- spec.Constraint.Tp == ast.ConstraintUniq ||
- spec.Constraint.Tp == ast.ConstraintUniqKey {
- code = advisorcode.CompatibilityAddUniqueKey
- break
- }
- // ADD FOREIGN KEY
- if spec.Constraint.Tp == ast.ConstraintForeignKey {
- code = advisorcode.CompatibilityAddForeignKey
- break
- }
- // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
- // ADD CHECK ENFORCED
- if spec.Constraint.Tp == ast.ConstraintCheck && spec.Constraint.Enforced {
- code = advisorcode.CompatibilityAddCheck
- break
- }
- }
-
- // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
- // ALTER CHECK ENFORCED
- if spec.Tp == ast.AlterTableAlterCheck {
- if spec.Constraint.Enforced {
- code = advisorcode.CompatibilityAlterCheck
- break
- }
- }
-
- // MODIFY COLUMN / CHANGE COLUMN
- // Due to the limitation that we don't know the current data type of the column before the change,
- // so we treat all as incompatible. This generates false positive when:
- // 1. Change to a compatible data type such as INT to BIGINT
- // 2. Change properties such as comment, change it to NULL
- if spec.Tp == ast.AlterTableModifyColumn || spec.Tp == ast.AlterTableChangeColumn {
- code = advisorcode.CompatibilityAlterColumn
- break
- }
- }
-
- // ALTER VIEW TBD: https://github.com/pingcap/parser/pull/1252
- // case *ast.AlterViewStmt:
-
- // CREATE UNIQUE INDEX
+ code = c.classifyAlterTable(n)
+ line = ostmt.AbsoluteLine(n.Loc.Start)
case *ast.CreateIndexStmt:
- if v.lastCreateTable != node.Table.Name.O && node.KeyType == ast.IndexKeyTypeUnique {
+ if n.Table != nil && n.Table.Name != c.lastCreateTable && n.Unique {
code = advisorcode.CompatibilityAddUniqueKey
+ line = ostmt.AbsoluteLine(n.Loc.Start)
}
default:
}
if code != advisorcode.Ok {
- v.adviceList = append(v.adviceList, &storepb.Advice{
- Status: v.level,
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.Int32(),
- Title: v.title,
- Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", in.Text()),
- StartPosition: common.ConvertANTLRLineToPosition(in.OriginTextPosition()),
+ Title: c.title,
+ Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", ostmt.Text),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
})
}
- return in, false
}
-// Leave implements the ast.Visitor interface.
-func (*compatibilityChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+// classifyAlterTable returns the first compatibility-impacting code among
+// the ALTER TABLE commands, or Ok if none.
+func (*compatibilityChecker) classifyAlterTable(n *ast.AlterTableStmt) advisorcode.Code {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATRenameColumn:
+ return advisorcode.CompatibilityRenameColumn
+ case ast.ATDropColumn:
+ return advisorcode.CompatibilityDropColumn
+ case ast.ATRenameTable:
+ return advisorcode.CompatibilityRenameTable
+ case ast.ATAddConstraint:
+ if c := classifyAddConstraint(cmd.Constraint); c != advisorcode.Ok {
+ return c
+ }
+ case ast.ATAlterCheckEnforced:
+ // omni populates cmd.NewName with "ENFORCED" / "NOT ENFORCED";
+ // cmd.Constraint is not set. Flag only the ENFORCED transition.
+ if cmd.NewName == "ENFORCED" {
+ return advisorcode.CompatibilityAlterCheck
+ }
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ // Treat all MODIFY/CHANGE COLUMN as incompatible: column's
+ // current type is unknown at advice time, so type-widening and
+ // metadata-only changes false-positive.
+ return advisorcode.CompatibilityAlterColumn
+ default:
+ }
+ }
+ return advisorcode.Ok
+}
+
+func classifyAddConstraint(constraint *ast.Constraint) advisorcode.Code {
+ if constraint == nil {
+ return advisorcode.Ok
+ }
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey:
+ return advisorcode.CompatibilityAddPrimaryKey
+ case ast.ConstrUnique:
+ // All UNIQUE forms (UNIQUE / UNIQUE KEY / UNIQUE INDEX) route here.
+ return advisorcode.CompatibilityAddUniqueKey
+ case ast.ConstrForeignKey:
+ return advisorcode.CompatibilityAddForeignKey
+ case ast.ConstrCheck:
+ if !constraint.NotEnforced {
+ return advisorcode.CompatibilityAddCheck
+ }
+ default:
+ }
+ return advisorcode.Ok
}
diff --git a/backend/plugin/advisor/tidb/advisor_table_no_fk.go b/backend/plugin/advisor/tidb/advisor_table_no_fk.go
index da12256808..504fc6bb71 100644
--- a/backend/plugin/advisor/tidb/advisor_table_no_fk.go
+++ b/backend/plugin/advisor/tidb/advisor_table_no_fk.go
@@ -4,18 +4,16 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/pingcap/tidb/pkg/parser/ast"
+ "github.com/bytebase/omni/tidb/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
_ advisor.Advisor = (*TableNoFKAdvisor)(nil)
- _ ast.Visitor = (*tableNoFKChecker)(nil)
)
func init() {
@@ -28,8 +26,7 @@ type TableNoFKAdvisor struct {
// Check checks table disallow foreign key.
func (*TableNoFKAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- root, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -42,8 +39,8 @@ func (*TableNoFKAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*
level: level,
title: checkCtx.Rule.Type.String(),
}
- for _, stmtNode := range root {
- (stmtNode).Accept(checker)
+ for _, ostmt := range stmts {
+ checker.checkStmt(ostmt)
}
return checker.adviceList, nil
@@ -55,40 +52,47 @@ type tableNoFKChecker struct {
title string
}
-// Enter implements the ast.Visitor interface.
-func (checker *tableNoFKChecker) Enter(in ast.Node) (ast.Node, bool) {
- switch node := in.(type) {
+func (c *tableNoFKChecker) checkStmt(ostmt OmniStmt) {
+ switch n := ostmt.Node.(type) {
case *ast.CreateTableStmt:
- for _, constraint := range node.Constraints {
- if constraint.Tp == ast.ConstraintForeignKey {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: code.TableHasFK.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", node.Table.Name),
- StartPosition: common.ConvertANTLRLineToPosition(constraint.OriginTextPosition()),
- })
+ if n.Table == nil {
+ return
+ }
+ for _, constraint := range n.Constraints {
+ if constraint == nil || constraint.Type != ast.ConstrForeignKey {
+ continue
}
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.TableHasFK.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", n.Table.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(ostmt.AbsoluteLine(constraint.Loc.Start)),
+ })
}
case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- if spec.Tp == ast.AlterTableAddConstraint && spec.Constraint.Tp == ast.ConstraintForeignKey {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: code.TableHasFK.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", node.Table.Name),
- StartPosition: common.ConvertANTLRLineToPosition(in.OriginTextPosition()),
- })
+ if n.Table == nil {
+ return
+ }
+ stmtLine := ostmt.AbsoluteLine(n.Loc.Start)
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
}
+ if cmd.Type != ast.ATAddConstraint || cmd.Constraint == nil {
+ continue
+ }
+ if cmd.Constraint.Type != ast.ConstrForeignKey {
+ continue
+ }
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: code.TableHasFK.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", n.Table.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(stmtLine),
+ })
}
default:
}
-
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*tableNoFKChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
}
diff --git a/backend/plugin/advisor/tidb/advisor_table_require_pk.go b/backend/plugin/advisor/tidb/advisor_table_require_pk.go
index 27df9bfe2b..31396eada2 100644
--- a/backend/plugin/advisor/tidb/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/tidb/advisor_table_require_pk.go
@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
- "github.com/pingcap/tidb/pkg/parser/ast"
+ "github.com/bytebase/omni/tidb/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
@@ -20,7 +20,6 @@ const (
var (
_ advisor.Advisor = (*TableRequirePKAdvisor)(nil)
- _ ast.Visitor = (*tableRequirePKChecker)(nil)
)
func init() {
@@ -33,8 +32,7 @@ type TableRequirePKAdvisor struct {
// Check checks table requires PK.
func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- root, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -52,8 +50,8 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
finalMetadata: checkCtx.FinalMetadata,
}
- for _, stmtNode := range root {
- (stmtNode).Accept(checker)
+ for _, ostmt := range stmts {
+ checker.checkStmt(ostmt)
}
return checker.generateAdviceList(), nil
@@ -69,172 +67,206 @@ type tableRequirePKChecker struct {
finalMetadata *model.DatabaseMetadata
}
-// Enter implements the ast.Visitor interface.
-func (v *tableRequirePKChecker) Enter(in ast.Node) (ast.Node, bool) {
- switch node := in.(type) {
- // CREATE TABLE
+func (c *tableRequirePKChecker) checkStmt(ostmt OmniStmt) {
+ switch n := ostmt.Node.(type) {
case *ast.CreateTableStmt:
- if node.ReferTable != nil {
- v.createTableLike(node)
- } else {
- v.createTable(node)
- }
- v.line[node.Table.Name.O] = node.OriginTextPosition()
- // DROP TABLE
+ c.handleCreateTable(n, ostmt)
case *ast.DropTableStmt:
- for _, table := range node.Tables {
- delete(v.tables, table.Name.String())
- }
- // ALTER TABLE
- case *ast.AlterTableStmt:
- tableName := node.Table.Name.O
- for _, spec := range node.Specs {
- switch spec.Tp {
- // ADD CONSTRAINT
- case ast.AlterTableAddConstraint:
- if spec.Constraint.Tp == ast.ConstraintPrimaryKey {
- v.tables[tableName] = newColumnSet(convertConstraintToKeySlice(spec.Constraint))
- }
- // DROP PRIMARY KEY
- case ast.AlterTableDropPrimaryKey:
- v.initEmptyTable(tableName)
- v.line[tableName] = node.OriginTextPosition()
- // DROP INDEX
- case ast.AlterTableDropIndex:
- if strings.ToUpper(spec.Name) == primaryKeyName {
- v.initEmptyTable(tableName)
- v.line[tableName] = node.OriginTextPosition()
- }
- // ADD COLUMNS
- case ast.AlterTableAddColumns:
- v.addPKIfExistByCols(tableName, spec.NewColumns)
- // CHANGE COLUMN
- case ast.AlterTableChangeColumn:
- if v.changeColumn(tableName, spec.OldColumnName.Name.String(), spec.NewColumns[0].Name.Name.String()) {
- v.line[tableName] = node.OriginTextPosition()
- }
- v.addPKIfExistByCols(tableName, spec.NewColumns[:1])
- // MODIFY COLUMN
- case ast.AlterTableModifyColumn:
- v.addPKIfExistByCols(tableName, spec.NewColumns[:1])
- // DROP COLUMN
- case ast.AlterTableDropColumn:
- if v.dropColumn(tableName, spec.OldColumnName.Name.String()) {
- v.line[tableName] = node.OriginTextPosition()
- }
- default:
- // Skip other alter table specification types
+ for _, table := range n.Tables {
+ if table != nil {
+ delete(c.tables, table.Name)
}
}
+ case *ast.AlterTableStmt:
+ c.handleAlterTable(n, ostmt)
default:
}
+}
- return in, false
+func (c *tableRequirePKChecker) handleCreateTable(n *ast.CreateTableStmt, ostmt OmniStmt) {
+ if n.Table == nil {
+ return
+ }
+ if n.Like != nil {
+ c.createTableLike(n)
+ } else {
+ c.createTable(n)
+ }
+ c.line[n.Table.Name] = ostmt.AbsoluteLine(n.Loc.Start)
}
-// Leave implements the ast.Visitor interface.
-func (*tableRequirePKChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+func (c *tableRequirePKChecker) handleAlterTable(n *ast.AlterTableStmt, ostmt OmniStmt) {
+ if n.Table == nil {
+ return
+ }
+ tableName := n.Table.Name
+ stmtLine := ostmt.AbsoluteLine(n.Loc.Start)
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddConstraint:
+ if cmd.Constraint != nil && cmd.Constraint.Type == ast.ConstrPrimaryKey {
+ c.tables[tableName] = newColumnSet(constraintPKColumns(cmd.Constraint))
+ }
+ case ast.ATDropConstraint:
+ // DROP PRIMARY KEY arrives here with cmd.Name == "PRIMARY"
+ // in current omni grammar; the empty-name fallback mirrors
+ // mysql/rule_table_require_pk.go's defensive guard against
+ // grammar changes that might emit it without a name.
+ if strings.EqualFold(cmd.Name, primaryKeyName) || cmd.Name == "" {
+ c.initEmptyTable(tableName)
+ c.line[tableName] = stmtLine
+ }
+ case ast.ATDropIndex:
+ // DROP INDEX `PRIMARY` is also accepted as an alias for DROP PRIMARY KEY.
+ if strings.ToUpper(cmd.Name) == primaryKeyName {
+ c.initEmptyTable(tableName)
+ c.line[tableName] = stmtLine
+ }
+ case ast.ATAddColumn:
+ c.addPKIfExistByCols(tableName, addColumnTargets(cmd))
+ case ast.ATChangeColumn:
+ // cmd.Name is the old name; cmd.Column.Name is the new name.
+ // cmd.NewName is for RENAME ops, not ATChangeColumn.
+ if cmd.Column == nil {
+ continue
+ }
+ if c.changeColumn(tableName, cmd.Name, cmd.Column.Name) {
+ c.line[tableName] = stmtLine
+ }
+ c.addPKIfExistByCols(tableName, []*ast.ColumnDef{cmd.Column})
+ case ast.ATModifyColumn:
+ if cmd.Column != nil {
+ c.addPKIfExistByCols(tableName, []*ast.ColumnDef{cmd.Column})
+ }
+ case ast.ATDropColumn:
+ if c.dropColumn(tableName, cmd.Name) {
+ c.line[tableName] = stmtLine
+ }
+ default:
+ // Skip other alter table specification types.
+ }
+ }
}
-func (v *tableRequirePKChecker) generateAdviceList() []*storepb.Advice {
- tableList := v.tables.tableList()
+func (c *tableRequirePKChecker) generateAdviceList() []*storepb.Advice {
+ tableList := c.tables.tableList()
for _, tableName := range tableList {
- if len(v.tables[tableName]) == 0 {
- v.adviceList = append(v.adviceList, &storepb.Advice{
- Status: v.level,
+ if len(c.tables[tableName]) == 0 {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
Code: code.TableNoPK.Int32(),
- Title: v.title,
+ Title: c.title,
Content: fmt.Sprintf("Table `%s` requires PRIMARY KEY", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(v.line[tableName]),
+ StartPosition: common.ConvertANTLRLineToPosition(c.line[tableName]),
})
}
}
- return v.adviceList
+ return c.adviceList
}
-func (v *tableRequirePKChecker) initEmptyTable(name string) {
- v.tables[name] = make(columnSet)
+func (c *tableRequirePKChecker) initEmptyTable(name string) {
+ c.tables[name] = make(columnSet)
}
-func (v *tableRequirePKChecker) createTable(node *ast.CreateTableStmt) {
- table := node.Table.Name.String()
- v.initEmptyTable(table)
- v.addPKIfExistByCols(table, node.Cols)
+func (c *tableRequirePKChecker) createTable(n *ast.CreateTableStmt) {
+ table := n.Table.Name
+ c.initEmptyTable(table)
+ c.addPKIfExistByCols(table, n.Columns)
- for _, constraint := range node.Constraints {
- if constraint.Tp == ast.ConstraintPrimaryKey {
- v.tables[table] = newColumnSet(convertConstraintToKeySlice(constraint))
+ for _, constraint := range n.Constraints {
+ if constraint != nil && constraint.Type == ast.ConstrPrimaryKey {
+ c.tables[table] = newColumnSet(constraintPKColumns(constraint))
}
}
}
-func (v *tableRequirePKChecker) createTableLike(node *ast.CreateTableStmt) {
- table := node.Table.Name.String()
- v.initEmptyTable(table)
- referTableName := node.ReferTable.Name.String()
- // Switch to check refer table's primary key.
- // Find refer table in context first, and then find it in catalog.
- if referTablePk, ok := v.tables[referTableName]; ok {
+func (c *tableRequirePKChecker) createTableLike(n *ast.CreateTableStmt) {
+ table := n.Table.Name
+ c.initEmptyTable(table)
+ if n.Like == nil {
+ return
+ }
+ referTableName := n.Like.Name
+ if referTablePK, ok := c.tables[referTableName]; ok {
var columns []string
- for column := range referTablePk {
+ for column := range referTablePK {
columns = append(columns, column)
}
- v.tables[table] = newColumnSet(columns)
- } else {
- schema := v.originalMetadata.GetSchemaMetadata("")
- if schema != nil {
- referTableMetadata := schema.GetTable(referTableName)
- if referTableMetadata != nil {
- primaryKey := referTableMetadata.GetPrimaryKey()
- if primaryKey != nil {
- v.tables[table] = newColumnSet(primaryKey.GetProto().GetExpressions())
- }
- }
- }
+ c.tables[table] = newColumnSet(columns)
+ return
}
+ schema := c.originalMetadata.GetSchemaMetadata("")
+ if schema == nil {
+ return
+ }
+ referTableMetadata := schema.GetTable(referTableName)
+ if referTableMetadata == nil {
+ return
+ }
+ primaryKey := referTableMetadata.GetPrimaryKey()
+ if primaryKey == nil {
+ return
+ }
+ c.tables[table] = newColumnSet(primaryKey.GetProto().GetExpressions())
}
-func (v *tableRequirePKChecker) dropColumn(table string, column string) bool {
- if _, ok := v.tables[table]; !ok {
- pk := v.originalMetadata.GetSchemaMetadata("").GetTable(table).GetIndex(primaryKeyName)
+func (c *tableRequirePKChecker) dropColumn(table, column string) bool {
+ if _, ok := c.tables[table]; !ok {
+ schema := c.originalMetadata.GetSchemaMetadata("")
+ if schema == nil {
+ return false
+ }
+ t := schema.GetTable(table)
+ if t == nil {
+ return false
+ }
+ pk := t.GetIndex(primaryKeyName)
if pk == nil {
return false
}
- v.tables[table] = newColumnSet(pk.GetProto().GetExpressions())
+ c.tables[table] = newColumnSet(pk.GetProto().GetExpressions())
}
- pk := v.tables[table]
+ pk := c.tables[table]
_, columnInPK := pk[column]
- delete(v.tables[table], column)
+ delete(c.tables[table], column)
return columnInPK
}
-func (v *tableRequirePKChecker) changeColumn(table string, oldColumn string, newColumn string) bool {
- if v.dropColumn(table, oldColumn) {
- pk := v.tables[table]
+func (c *tableRequirePKChecker) changeColumn(table, oldColumn, newColumn string) bool {
+ if c.dropColumn(table, oldColumn) {
+ pk := c.tables[table]
pk[newColumn] = true
return true
}
return false
}
-func (v *tableRequirePKChecker) addPKIfExistByCols(table string, columns []*ast.ColumnDef) {
+func (c *tableRequirePKChecker) addPKIfExistByCols(table string, columns []*ast.ColumnDef) {
for _, column := range columns {
- for _, option := range column.Options {
- if option.Tp == ast.ColumnOptionPrimaryKey {
- v.tables[table] = newColumnSet([]string{column.Name.Name.String()})
+ if column == nil {
+ continue
+ }
+ for _, constraint := range column.Constraints {
+ if constraint != nil && constraint.Type == ast.ColConstrPrimaryKey {
+ c.tables[table] = newColumnSet([]string{column.Name})
return
}
}
}
}
-func convertConstraintToKeySlice(constraint *ast.Constraint) []string {
- var columns []string
- for _, key := range constraint.Keys {
- columns = append(columns, key.Column.Name.String())
+// constraintPKColumns returns the simple column names of a PRIMARY KEY.
+// Functional PRIMARY KEYs (`PRIMARY KEY ((expr))`) populate IndexColumns
+// with expression nodes instead of Columns; this returns an empty slice
+// for that form, causing the advisor to report no PK columns.
+func constraintPKColumns(constraint *ast.Constraint) []string {
+ if constraint == nil {
+ return nil
}
- return columns
+ return constraint.Columns
}
diff --git a/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml b/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml
index d20cf8b831..62d6a6f762 100644
--- a/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml
+++ b/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml
@@ -180,3 +180,7 @@
CREATE TABLE t(id int);
ALTER TABLE t CHANGE COLUMN id id2 INT(5) PRIMARY KEY
changeType: 1
+- statement: CREATE TABLE t(id INTEGER PRIMARY KEY)
+ changeType: 1
+- statement: CREATE TABLE t(id INTEGER, PRIMARY KEY(id))
+ changeType: 1
diff --git a/backend/plugin/advisor/tidb/test/schema_backward_compatibility.yaml b/backend/plugin/advisor/tidb/test/schema_backward_compatibility.yaml
index 471e18f998..883c150052 100644
--- a/backend/plugin/advisor/tidb/test/schema_backward_compatibility.yaml
+++ b/backend/plugin/advisor/tidb/test/schema_backward_compatibility.yaml
@@ -53,3 +53,48 @@
line: 1
column: 0
endposition: null
+- statement: ALTER TABLE tech_book ALTER CHECK chk ENFORCED
+ changeType: 1
+ want:
+ - status: 2
+ code: 110
+ title: SCHEMA_BACKWARD_COMPATIBILITY
+ content: '"ALTER TABLE tech_book ALTER CHECK chk ENFORCED" may cause incompatibility with the existing data and code'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ALTER CHECK chk NOT ENFORCED
+ changeType: 1
+# Case-insensitivity: omni's tidb lexer lowercases identifiers before keyword
+# lookup (lexer.go:2267) and the ALTER CHECK arms assign canonical literal
+# strings to cmd.NewName ("ENFORCED" / "NOT ENFORCED"; alter_table.go:762-765,
+# 785-788). The advisor's `cmd.NewName == "ENFORCED"` check therefore matches
+# regardless of user case. These fixtures pin the contract — if omni ever
+# stops normalizing, the lowercase/mixed cases will start regressing.
+- statement: ALTER TABLE tech_book ALTER CHECK chk enforced
+ changeType: 1
+ want:
+ - status: 2
+ code: 110
+ title: SCHEMA_BACKWARD_COMPATIBILITY
+ content: '"ALTER TABLE tech_book ALTER CHECK chk enforced" may cause incompatibility with the existing data and code'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ALTER CHECK chk Enforced
+ changeType: 1
+ want:
+ - status: 2
+ code: 110
+ title: SCHEMA_BACKWARD_COMPATIBILITY
+ content: '"ALTER TABLE tech_book ALTER CHECK chk Enforced" may cause incompatibility with the existing data and code'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ALTER CHECK chk not enforced
+ changeType: 1
+- statement: ALTER TABLE tech_book ALTER CHECK chk Not Enforced
+ changeType: 1
diff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.go
index cf4cda32fa..746572075c 100644
--- a/backend/plugin/advisor/tidb/utils.go
+++ b/backend/plugin/advisor/tidb/utils.go
@@ -58,6 +58,34 @@ func (t tablePK) tableList() []string {
return tableList
}
+// tableNewColumn tracks per-statement column definitions by name, scoped
+// to the un-migrated pingcap-AST index advisors. Delete when those migrate.
+// Tracked: https://linear.app/bytebase/issue/BYT-9395
+type columnNameToColumnDef map[string]*ast.ColumnDef
+type tableNewColumn map[string]columnNameToColumnDef
+
+func (t tableNewColumn) set(tableName, columnName string, colDef *ast.ColumnDef) {
+ if _, ok := t[tableName]; !ok {
+ t[tableName] = make(columnNameToColumnDef)
+ }
+ t[tableName][columnName] = colDef
+}
+
+func (t tableNewColumn) get(tableName, columnName string) (colDef *ast.ColumnDef, ok bool) {
+ if _, ok := t[tableName]; !ok {
+ return nil, false
+ }
+ col, ok := t[tableName][columnName]
+ return col, ok
+}
+
+func (t tableNewColumn) delete(tableName, columnName string) {
+ if _, ok := t[tableName]; !ok {
+ return
+ }
+ delete(t[tableName], columnName)
+}
+
func restoreNode(node ast.Node, flag format.RestoreFlags) (string, error) {
var buffer strings.Builder
ctx := format.NewRestoreCtx(flag, &buffer)
@@ -89,19 +117,12 @@ func needDefault(column *ast.ColumnDef) bool {
return true
}
-// getTiDBNodes extracts TiDB AST nodes from the advisor context.
-//
-// Soft-fail on bridge miss: when stmt.AST is a PingCapASTProvider
-// (post-dispatcher-flip *OmniAST) whose AsPingCapAST() returns (nil, false),
-// the statement is skipped rather than surfaced as an error. This honors
-// Phase 1.5 invariant #2 from the omni-tidb completion plan — un-migrated
-// advisors emit no advice for the skipped statement; the review continues.
-// The bridge has already logged the parse failure at debug level
-// (omni.go:AsPingCapAST).
+// getTiDBNodes extracts pingcap-AST nodes for un-migrated advisors.
//
-// True engine-mismatch (stmt.AST is some unrelated type, neither *tidb.AST
-// nor a PingCapASTProvider) is still surfaced as an error — that's a
-// programmer error in registration, not a soft-fail case.
+// On a PingCapASTProvider whose AsPingCapAST returns (nil, false) — i.e.
+// the bridge tried and pingcap rejected the statement — the statement is
+// skipped, not surfaced as an error. A non-provider, non-*AST input is
+// still surfaced as an engine-mismatch error.
func getTiDBNodes(checkCtx advisor.Context) ([]ast.StmtNode, error) {
if checkCtx.ParsedStatements == nil {
return nil, errors.New("ParsedStatements is not provided in context")
@@ -114,10 +135,6 @@ func getTiDBNodes(checkCtx advisor.Context) ([]ast.StmtNode, error) {
}
tidbAST, ok := tidbparser.GetTiDBAST(stmt.AST)
if !ok {
- // Bridge miss → soft-fail (skip). Engine mismatch → error.
- // PingCapASTProvider implementations route through GetTiDBAST →
- // AsPingCapAST; a (nil, false) here means the bridge tried and
- // pingcap rejected the statement. Skip rather than abort the rule.
if _, isProvider := stmt.AST.(tidbparser.PingCapASTProvider); isProvider {
continue
}
@@ -167,12 +184,25 @@ func (s OmniStmt) FirstTokenLine() int {
return s.AbsoluteLine(0)
}
-// canNull reports whether the given pingcap-AST column may have NULL values
-// (i.e. the column has no NOT NULL or PRIMARY KEY constraint).
-//
-// Moved here from advisor_column_no_null.go during its migration to omni AST.
-// Still consumed by advisor_column_set_default_for_not_null.go (un-migrated).
-// Delete when set_default_for_not_null migrates to omni AST.
+// addColumnTargets returns the column definitions added by an ATAddColumn
+// cmd. omni populates either cmd.Columns (multi-column ADD COLUMN (...))
+// or cmd.Column (single ADD COLUMN); read both defensively.
+func addColumnTargets(cmd *omniast.AlterTableCmd) []*omniast.ColumnDef {
+ if cmd == nil {
+ return nil
+ }
+ if len(cmd.Columns) > 0 {
+ return cmd.Columns
+ }
+ if cmd.Column != nil {
+ return []*omniast.ColumnDef{cmd.Column}
+ }
+ return nil
+}
+
+// canNull reports whether a pingcap-AST column may hold NULL (no NOT NULL
+// or PRIMARY KEY option). Scoped to un-migrated advisors; delete when
+// advisor_column_set_default_for_not_null.go migrates.
// Tracked: https://linear.app/bytebase/issue/BYT-9362
func canNull(column *ast.ColumnDef) bool {
for _, option := range column.Options {
@@ -188,29 +218,13 @@ func canNull(column *ast.ColumnDef) bool {
// this cache.
const omniStmtsCacheKey = "tidb.omniStmts"
-// getTiDBOmniNodes extracts omni/tidb AST nodes from the advisor context by
-// re-parsing each statement's text with omni. Used by advisors during the
-// migration off the native pingcap parser.
-//
-// While the registered ParseStatementsFunc still returns native pingcap ASTs
-// (preserved by Phase 1.5 for backward compat with un-migrated advisors), this
-// helper re-parses with omni. Migrated advisors call this; un-migrated
-// advisors continue to call getTiDBNodes.
-//
-// Phase 1.5 invariants enforced here:
-//
-// - Single-parse-per-review: result is cached on advisor.Context.Memo so
-// subsequent advisors in the same review reuse the parse work. Cost stays
-// 1× regardless of how many advisors migrate.
-// - Soft-fail on omni grammar gaps: a statement that fails to parse with
-// omni is logged and skipped. Other statements (and other advisors) keep
-// working. This protects review continuity while omni grammar catches up
-// to pingcap on Tier 4 deferred features (FLASHBACK, SEQUENCE, BATCH DML,
-// etc. — see plans/2026-04-23-omni-tidb-completion-plan.md §Phase 2).
+// getTiDBOmniNodes returns omni-parsed statements for migrated advisors.
//
-// After all advisors are migrated and the dispatcher is flipped to omni, this
-// helper can be simplified to read directly from checkCtx.ParsedStatements
-// without re-parsing.
+// Two invariants:
+// - Single-parse-per-review: result is cached on checkCtx.Memo, so all
+// migrated advisors in one review share one parse pass.
+// - Soft-fail per statement: omni parse errors are logged at debug and
+// the statement is skipped; the review never breaks on grammar gaps.
func getTiDBOmniNodes(checkCtx advisor.Context) ([]OmniStmt, error) {
if cached, ok := checkCtx.Memo(omniStmtsCacheKey); ok {
if stmts, typeOK := cached.([]OmniStmt); typeOK {
@@ -229,10 +243,6 @@ func getTiDBOmniNodes(checkCtx advisor.Context) ([]OmniStmt, error) {
}
list, err := tidbparser.ParseTiDBOmni(stmt.Text)
if err != nil {
- // Soft-fail: omni may not yet support every TiDB grammar feature
- // pingcap accepts. Skip this statement; advisors emit no advice
- // for it but the review continues. Promote to higher log level
- // when this signals a real omni regression vs. an expected gap.
slog.Debug("omni/tidb parse failed; skipping statement for omni-aware advisors",
slog.String("error", err.Error()),
)