Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(advisor/tidb): migrate index_type_no_blob + index_primary_key_type_allowlist to omni AST (Phase 1.5 §1.5.3 batch 8) (#20229)
Vincent Huang399c9d5e0053
diff --git a/backend/plugin/advisor/tidb/advisor_index_pk_type.go b/backend/plugin/advisor/tidb/advisor_index_pk_type.goindex 6c184b55db..bc0fd2780b 100644--- a/backend/plugin/advisor/tidb/advisor_index_pk_type.go+++ b/backend/plugin/advisor/tidb/advisor_index_pk_type.go@@ -55,8 +55,11 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([return checker.adviceList, nil}-// omni-AST analog of columnNameToColumnDef / tableNewColumn in utils.go.-// (Pingcap-typed siblings remain there for un-migrated advisors.)+// tableNewOmniColumn tracks per-review column definitions by table+name+// for the index-family advisors that need to resolve column types referenced+// later in the same review (e.g. an ALTER TABLE ADD INDEX over a column+// just added). Shared by advisor_index_pk_type, advisor_index_type_no_blob,+// and advisor_index_primary_key_type_allowlist.type omniColumnNameToColumnDef map[string]*ast.ColumnDeftype tableNewOmniColumn map[string]omniColumnNameToColumnDefdiff --git a/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go b/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.goindex 7083dcfc20..48f279b195 100644--- a/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go+++ b/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go@@ -1,27 +1,22 @@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/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"- tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code""github.com/bytebase/bytebase/backend/store/model")var (_ advisor.Advisor = (*IndexPrimaryKeyTypeAllowlistAdvisor)(nil)- _ ast.Visitor = (*indexPrimaryKeyTypeAllowlistChecker)(nil))func init() {@@ -34,8 +29,7 @@ type IndexPrimaryKeyTypeAllowlistAdvisor struct {// Check checks for primary key type allowlist.func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -54,13 +48,11 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx adtitle: checkCtx.Rule.Type.String(),allowlist: allowlist,originalMetadata: checkCtx.OriginalMetadata,- tablesNewColumns: make(map[string]columnNameToColumnDef),+ tablesNewColumns: make(tableNewOmniColumn),}- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ for _, ostmt := range stmts {+ checker.checkStmt(ostmt)}return checker.adviceList, nil@@ -70,139 +62,168 @@ type indexPrimaryKeyTypeAllowlistChecker struct {adviceList []*storepb.Advicelevel storepb.Advice_Statustitle string- text string- line intallowlist map[string]booloriginalMetadata *model.DatabaseMetadata- tablesNewColumns tableNewColumn+ tablesNewColumns tableNewOmniColumn}-// Enter implements the ast.Visitor interface.-func (v *indexPrimaryKeyTypeAllowlistChecker) Enter(in ast.Node) (ast.Node, bool) {+func (c *indexPrimaryKeyTypeAllowlistChecker) 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, ast.ATAddIndex:+ // Empirically tidb omni emits only ATAddConstraint for+ // `ALTER TABLE ... ADD …` forms (bare and named); ATAddIndex+ // is reserved (not emitted). Dual arm preserved for sibling+ // parity with the naming-convention advisors and mysql analog.+ 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}- pds := v.changeColumn(tableName, oldColumnName, node.OriginTextPosition(), newColumnDef)+ 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 := c.changeColumn(tableName, oldColumnName, stmtLine, newColumnDef)pkDataList = append(pkDataList, pds...)default:- // Ignore other alter table specs}}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("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", pd.column, pd.table, pd.columnType),StartPosition: common.ConvertANTLRLineToPosition(pd.line),})}- return in, false-}--// Leave implements the ast.Visitor interface.-func (*indexPrimaryKeyTypeAllowlistChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true}-func (v *indexPrimaryKeyTypeAllowlistChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {+func (c *indexPrimaryKeyTypeAllowlistChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {var pkDataList []pkData- for _, option := range colDef.Options {- if option.Tp == ast.ColumnOptionPrimaryKey {- tp := tidbparser.TypeString(colDef.Tp.GetType())- if _, exists := v.allowlist[strings.ToLower(tp)]; !exists {+ for _, constraint := range colDef.Constraints {+ if constraint != nil && constraint.Type == ast.ColConstrPrimaryKey {+ tp := omniDataTypeNameCompact(colDef.TypeName)+ if _, exists := c.allowlist[tp]; !exists {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 *indexPrimaryKeyTypeAllowlistChecker) changeColumn(tableName, oldColumnName string, line int, newColumnDef *ast.ColumnDef) []pkData {+func (c *indexPrimaryKeyTypeAllowlistChecker) 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 := tidbparser.TypeString(newColumnDef.Tp.GetType())- if _, exists := v.allowlist[strings.ToLower(tp)]; !exists {+ c.tablesNewColumns.delete(tableName, oldColumnName)+ for _, constraint := range newColumnDef.Constraints {+ if constraint != nil && constraint.Type == ast.ColConstrPrimaryKey {+ tp := omniDataTypeNameCompact(newColumnDef.TypeName)+ if _, exists := c.allowlist[tp]; !exists {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 *indexPrimaryKeyTypeAllowlistChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {+func (c *indexPrimaryKeyTypeAllowlistChecker) 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 _, exists := v.allowlist[strings.ToLower(columnType)]; !exists {- 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 _, exists := c.allowlist[columnType]; !exists {+ 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 *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName string, columnName string) (string, error) {- if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {- return tidbparser.TypeString(colDef.Tp.GetType()), nil+// getPKColumnType returns the rendered type for tableName.columnName,+// preferring the column we observed in this check pass and falling back+// to the catalog. Always returns lowercase to match advice fixture rendering+// and the case-insensitive allowlist comparison.+func (c *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName, columnName string) (string, error) {+ if colDef, ok := c.tablesNewColumns.get(tableName, columnName); ok {+ return omniDataTypeNameCompact(colDef.TypeName), nil+ }+ schema := c.originalMetadata.GetSchemaMetadata("")+ if schema == nil {+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)+ }+ table := schema.GetTable(tableName)+ if table == nil {+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)}- column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)- if column != nil {- return column.GetProto().Type, nil+ column := table.GetColumn(columnName)+ if column == nil {+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)}- return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)+ return strings.ToLower(column.GetProto().Type), nil}diff --git a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.goindex f2ca5cb479..053f421e9a 100644--- a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go+++ b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go@@ -1,28 +1,22 @@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 = (*IndexTypeNoBlobAdvisor)(nil)- _ ast.Visitor = (*indexTypeNoBlobChecker)(nil))func init() {@@ -35,8 +29,7 @@ type IndexTypeNoBlobAdvisor struct {// Check checks for index type no blob.func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -49,13 +42,11 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Contextlevel: level,title: checkCtx.Rule.Type.String(),originalMetadata: checkCtx.OriginalMetadata,- tablesNewColumns: make(map[string]columnNameToColumnDef),+ tablesNewColumns: make(tableNewOmniColumn),}- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ for _, ostmt := range stmts {+ checker.checkStmt(ostmt)}return checker.adviceList, nil@@ -65,190 +56,205 @@ type indexTypeNoBlobChecker struct {adviceList []*storepb.Advicelevel storepb.Advice_Statustitle string- text string- line intoriginalMetadata *model.DatabaseMetadata- tablesNewColumns tableNewColumn+ tablesNewColumns tableNewOmniColumn}-// Enter implements the ast.Visitor interface.-func (v *indexTypeNoBlobChecker) Enter(in ast.Node) (ast.Node, bool) {+func (c *indexTypeNoBlobChecker) 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, ast.ATAddIndex:+ // Empirically tidb omni emits only ATAddConstraint for+ // `ALTER TABLE ... ADD …` forms (bare and named); ATAddIndex+ // is reserved (not emitted). Dual arm preserved for sibling+ // parity with the naming-convention advisors and mysql analog.+ if cmd.Constraint != nil {+ pds := c.addConstraint(tableName, stmtLine, cmd.Constraint)+ pkDataList = append(pkDataList, pds...)}- pds := v.changeColumn(tableName, oldColumnName, node.OriginTextPosition(), newColumnDef)+ 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 := c.changeColumn(tableName, oldColumnName, stmtLine, newColumnDef)pkDataList = append(pkDataList, pds...)default:- // Other alter table types}}case *ast.CreateIndexStmt:- tableName := node.Table.Name.String()- for _, indexSpec := range node.IndexPartSpecifications {- pds := v.addIndex(tableName, node.OriginTextPosition(), indexSpec)- pkDataList = append(pkDataList, pds...)+ if n.Table == nil {+ return+ }+ // Note: pingcap-typed advisor does NOT exclude FULLTEXT/SPATIAL+ // indexes from the BLOB check, even though the mysql omni rule does.+ // Preserve pingcap behavior; aligning to mysql's stricter exclusion+ // is a separate intentional behavior change (cumulative #9 territory).+ tableName := n.Table.Name+ stmtLine := ostmt.AbsoluteLine(n.Loc.Start)+ for _, columnName := range omniIndexColumns(n.Columns) {+ columnType, err := c.getColumnType(tableName, columnName)+ if err != nil {+ continue+ }+ if isBlobType(columnType) {+ pkDataList = append(pkDataList, pkData{+ table: tableName,+ column: columnName,+ columnType: columnType,+ line: stmtLine,+ })+ }}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.IndexTypeNoBlob.Int32(),- Title: v.title,+ Title: c.title,Content: fmt.Sprintf("Columns in index must not be BLOB 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 (*indexTypeNoBlobChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true-}--func (v *indexTypeNoBlobChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {+func (c *indexTypeNoBlobChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {var pkDataList []pkData- for _, option := range colDef.Options {- if option.Tp == ast.ColumnOptionUniqKey {- tp := v.getBlobStr(colDef.Tp)- if v.isBlob(tp) {+ // Note: pingcap-typed addNewColumn checks ColumnOptionUniqKey only (NOT+ // ColumnOptionPrimaryKey), so column-level `PRIMARY KEY` on a BLOB inside+ // CREATE TABLE is NOT flagged by the pingcap version. Preserve that+ // behavior. The change-column path checks both (matching the pingcap+ // inconsistency); replicate as-is.+ tp := omniDataTypeNameCompact(colDef.TypeName)+ for _, constraint := range colDef.Constraints {+ if constraint != nil && constraint.Type == ast.ColConstrUnique {+ if isBlobType(tp) {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)- return pkDataList-}--func (v *indexTypeNoBlobChecker) addIndex(tableName string, line int, indexSpec *ast.IndexPartSpecification) []pkData {- if indexSpec.Column == nil {- return nil- }- var pkDataList []pkData- columnName := indexSpec.Column.Name.String()- columnType, err := v.getColumnType(tableName, columnName)- if err != nil {- return nil- }- if v.isBlob(columnType) {- pkDataList = append(pkDataList, pkData{- table: tableName,- column: columnName,- columnType: columnType,- line: line,- })- }+ c.tablesNewColumns.set(tableName, colDef.Name, colDef)return pkDataList}-func (v *indexTypeNoBlobChecker) changeColumn(tableName, oldColumnName string, line int, newColumnDef *ast.ColumnDef) []pkData {+func (c *indexTypeNoBlobChecker) 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 || option.Tp == ast.ColumnOptionUniqKey {- tp := v.getBlobStr(newColumnDef.Tp)- if v.isBlob(tp) {+ c.tablesNewColumns.delete(tableName, oldColumnName)+ tp := omniDataTypeNameCompact(newColumnDef.TypeName)+ for _, constraint := range newColumnDef.Constraints {+ if constraint != nil && (constraint.Type == ast.ColConstrPrimaryKey || constraint.Type == ast.ColConstrUnique) {+ if isBlobType(tp) {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 *indexTypeNoBlobChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {+func (c *indexTypeNoBlobChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {var pkDataList []pkData- if constraint.Tp == ast.ConstraintPrimaryKey || constraint.Tp == ast.ConstraintUniqKey || constraint.Tp == ast.ConstraintKey ||- constraint.Tp == ast.ConstraintIndex || constraint.Tp == ast.ConstraintUniqIndex || constraint.Tp == ast.ConstraintUniq {- for _, key := range constraint.Keys {- if key.Column == nil {- continue- }- columnName := key.Column.Name.String()- columnType, err := v.getColumnType(tableName, columnName)- if err != nil {- continue- }- if v.isBlob(columnType) {- pkDataList = append(pkDataList, pkData{- table: tableName,- column: columnName,- columnType: columnType,- line: line,- })- }+ // Mirror pingcap match: PRIMARY KEY + UNIQUE family + INDEX/KEY family.+ // Pingcap had 6 distinct ConstraintTp values (ConstraintPrimaryKey,+ // ConstraintUniqKey, ConstraintKey, ConstraintIndex, ConstraintUniqIndex,+ // ConstraintUniq); omni unifies the unique forms under ConstrUnique and+ // the index/key forms under ConstrIndex (cumulative #2). Pingcap-typed+ // advisor does NOT include ConstraintForeignKey; the mysql omni analog+ // does. Preserve pingcap behavior — FK is a separate behavior change.+ switch constraint.Type {+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex:+ default:+ return pkDataList+ }+ for _, columnName := range constraint.Columns {+ columnType, err := c.getColumnType(tableName, columnName)+ if err != nil {+ continue+ }+ if isBlobType(columnType) {+ pkDataList = append(pkDataList, pkData{+ table: tableName,+ column: columnName,+ columnType: columnType,+ line: line,+ })}}return pkDataList}-// getColumnType 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 *indexTypeNoBlobChecker) getColumnType(tableName string, columnName string) (string, error) {- if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {- return v.getBlobStr(colDef.Tp), nil+// getColumnType returns the lowercase rendered type for tableName.columnName,+// preferring the column we observed in this check pass and falling back to+// the catalog.+func (c *indexTypeNoBlobChecker) getColumnType(tableName, columnName string) (string, error) {+ if colDef, ok := c.tablesNewColumns.get(tableName, columnName); ok {+ return omniDataTypeNameCompact(colDef.TypeName), nil}- column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)+ column := c.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)if column != nil {- return column.GetProto().Type, nil+ return strings.ToLower(column.GetProto().Type), nil}- return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)+ return "", errors.Errorf(errCannotFindColumnTypeFmt, tableName, columnName)}-// getBlobStr returns the type string of tp.-func (*indexTypeNoBlobChecker) getBlobStr(tp *types.FieldType) string {- switch tp.GetType() {- case mysql.TypeTinyBlob:- return "tinyblob"- case mysql.TypeBlob:- return "blob"- case mysql.TypeMediumBlob:- return "mediumblob"- case mysql.TypeLongBlob:- return "longblob"+func isBlobType(columnType string) bool {+ switch strings.ToLower(columnType) {+ case "blob", "tinyblob", "mediumblob", "longblob":+ return truedefault:+ return false}- return tp.String()-}--func (*indexTypeNoBlobChecker) isBlob(tp string) bool {- up := strings.ToUpper(tp)- return up == "TINYBLOB" || up == "BLOB" || up == "MEDIUMBLOB" || up == "LONGBLOB"}diff --git a/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml b/backend/plugin/advisor/tidb/test/index_pk_type_limit.yamlindex 62d6a6f762..f5f847ff88 100644--- a/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml+++ b/backend/plugin/advisor/tidb/test/index_pk_type_limit.yaml@@ -184,3 +184,21 @@changeType: 1- statement: CREATE TABLE t(id INTEGER, PRIMARY KEY(id))changeType: 1+# Named CONSTRAINT form pin (empirical cumulative #17): omni emits+# ATAddConstraint for `ADD CONSTRAINT <name> PRIMARY KEY (...)` (same arm+# as bare `ADD PRIMARY KEY`). Pins the contract that batch-2's+# `case ATAddConstraint:` covers both bare and named forms — no+# ATAddIndex split exists for this construct in current tidb omni.+- statement: |-+ CREATE TABLE t(id VARCHAR(5));+ ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY (id);+ changeType: 1+ want:+ - status: 2+ code: 803+ title: INDEX_PK_TYPE_LIMIT+ content: Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)+ startposition:+ line: 2+ column: 0+ endposition: nulldiff --git a/backend/plugin/advisor/tidb/test/index_primary_key_type_allowlist.yaml b/backend/plugin/advisor/tidb/test/index_primary_key_type_allowlist.yamlindex 25e543e8e6..e1a21e157c 100644--- a/backend/plugin/advisor/tidb/test/index_primary_key_type_allowlist.yaml+++ b/backend/plugin/advisor/tidb/test/index_primary_key_type_allowlist.yaml@@ -180,3 +180,19 @@CREATE TABLE t(id int);ALTER TABLE t CHANGE COLUMN id id2 INT(5) PRIMARY KEYchangeType: 1+# Named CONSTRAINT form pin (empirical cumulative #17 — batch 8): omni emits+# ATAddConstraint for `ADD CONSTRAINT <name> PRIMARY KEY (...)` (same arm as+# bare `ADD PRIMARY KEY`). Pins cross-form parity.+- statement: |-+ CREATE TABLE t(id VARCHAR(5));+ ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY (id);+ changeType: 1+ want:+ - status: 2+ code: 803+ title: INDEX_PRIMARY_KEY_TYPE_ALLOWLIST+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist+ startposition:+ line: 2+ column: 0+ endposition: nulldiff --git a/backend/plugin/advisor/tidb/test/index_type_no_blob.yaml b/backend/plugin/advisor/tidb/test/index_type_no_blob.yamlindex 4102da6937..2d5296154e 100644--- a/backend/plugin/advisor/tidb/test/index_type_no_blob.yaml+++ b/backend/plugin/advisor/tidb/test/index_type_no_blob.yaml@@ -203,3 +203,19 @@CREATE TABLE t(b blob, mb mediumblob, lb longblob, id int);CREATE INDEX idx_id ON t(id);changeType: 1+# Named CONSTRAINT form pin (empirical cumulative #17 — batch 8): omni emits+# ATAddConstraint with cmd.Constraint.Name set for `ADD CONSTRAINT <name>+# UNIQUE (...)`. Verifies the named arm reaches the same case as bare forms.+- statement: |-+ CREATE TABLE t(b blob);+ ALTER TABLE t ADD CONSTRAINT uq_b UNIQUE (b);+ changeType: 1+ want:+ - status: 2+ code: 804+ title: INDEX_TYPE_NO_BLOB+ content: Columns in index must not be BLOB but `t`.`b` is blob+ startposition:+ line: 2+ column: 0+ endposition: nulldiff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.goindex 84073b4a48..8d2e11d8a3 100644--- a/backend/plugin/advisor/tidb/utils.go+++ b/backend/plugin/advisor/tidb/utils.go@@ -59,34 +59,6 @@ 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 needDefault(column *ast.ColumnDef) bool {for _, option := range column.Options {switch option.Tp {@@ -300,6 +272,18 @@ func omniColumnHasComment(col *omniast.ColumnDef) bool {return false}+// omniDataTypeNameCompact returns a compact, lowercase type-name string+// for use in advice content + allowlist comparisons. Mirrors the mysql+// helper of the same name (mysql/utils_omni.go). Length/scale info is+// intentionally omitted — pingcap's `tidbparser.TypeString` and the+// canonical fixture rendering both elide it (e.g. `varchar(5)` → "varchar").+func omniDataTypeNameCompact(dt *omniast.DataType) string {+ if dt == nil {+ return ""+ }+ return strings.ToLower(dt.Name)+}+// 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.