Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: migrate column_disallow_drop_in_index (#9258)
zll6009845afbe9823
diff --git a/backend/plugin/advisor/mysqlwip/advisor_column_disallow_drop_in_index.go b/backend/plugin/advisor/mysqlwip/advisor_column_disallow_drop_in_index.goindex 7ff2d0ce36..2b2502c4b5 100644--- a/backend/plugin/advisor/mysqlwip/advisor_column_disallow_drop_in_index.go+++ b/backend/plugin/advisor/mysqlwip/advisor_column_disallow_drop_in_index.go@@ -5,27 +5,35 @@ package mysqlwipimport ("fmt"- "github.com/pingcap/tidb/parser/ast"+ "github.com/antlr4-go/antlr/v4""github.com/pkg/errors"+ mysql "github.com/bytebase/mysql-parser"+"github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")var (_ advisor.Advisor = (*ColumnDisallowDropInIndexAdvisor)(nil)- _ ast.Visitor = (*columnDisallowDropInIndexChecker)(nil))+func init() {+ // only for mysqlwip test.+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLColumnDisallowDropInIndex, &ColumnDisallowDropInIndexAdvisor{})+}+// ColumnDisallowDropInIndexAdvisor is the advisor checking for disallow DROP COLUMN in index.type ColumnDisallowDropInIndexAdvisor struct {}// Check checks for disallow Drop COLUMN in index statement.func (*ColumnDisallowDropInIndexAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {- stmtList, ok := ctx.AST.([]ast.StmtNode)+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)if !ok {- return nil, errors.Errorf("failed to convert to StmtNode")+ return nil, errors.Errorf("failed to convert to mysql parser result")}level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)@@ -40,10 +48,9 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(ctx advisor.Context, _ string) ([catalog: ctx.Catalog,}- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ for _, stmtNode := range stmtList {+ checker.baseLine = stmtNode.BaseLine+ antlr.ParseTreeWalkerDefault.Walk(checker, stmtNode.Tree)}if len(checker.adviceList) == 0 {@@ -59,81 +66,87 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(ctx advisor.Context, _ string) ([}type columnDisallowDropInIndexChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine intadviceList []advisor.Advicelevel advisor.Statustitle string- text stringtables tableState // the variable mean whether the column in index.catalog *catalog.Finder- line int}-func (checker *columnDisallowDropInIndexChecker) Enter(in ast.Node) (ast.Node, bool) {- switch node := in.(type) {- case *ast.CreateTableStmt:- checker.addIndexColumn(node)- case *ast.AlterTableStmt:- return checker.dropColumn(node)+func (checker *columnDisallowDropInIndexChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {+ if ctx.TableName() == nil {+ return+ }+ if ctx.TableElementList() == nil {+ return}- return in, false-}--func (*columnDisallowDropInIndexChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true-}-func (checker *columnDisallowDropInIndexChecker) dropColumn(in ast.Node) (ast.Node, bool) {- if node, ok := in.(*ast.AlterTableStmt); ok {- for _, spec := range node.Specs {- if spec.Tp == ast.AlterTableDropColumn {- table := node.Table.Name.O-- index := checker.catalog.Origin.Index(&catalog.TableIndexFind{- // In MySQL, the SchemaName is "".- SchemaName: "",- TableName: table,- })-- if index != nil {- if checker.tables[table] == nil {- checker.tables[table] = make(columnSet)- }- for _, indexColumn := range *index {- for _, column := range indexColumn.ExpressionList() {- checker.tables[table][column] = true- }- }- }+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())+ for _, tableElement := range ctx.TableElementList().AllTableElement() {+ if tableElement == nil || tableElement.TableConstraintDef() == nil {+ continue+ }- colName := spec.OldColumnName.Name.String()- if !checker.canDrop(table, colName) {- checker.adviceList = append(checker.adviceList, advisor.Advice{- Status: checker.level,- Code: advisor.DropIndexColumn,- Title: checker.title,- Content: fmt.Sprintf("`%s`.`%s` cannot drop index column", table, colName),- Line: checker.line,- })+ if tableElement.TableConstraintDef().GetType_().GetTokenType() != mysql.MySQLParserINDEX_SYMBOL || tableElement.TableConstraintDef().GetType_().GetTokenType() != mysql.MySQLParserKEYS_SYMBOL {+ columnList := mysqlparser.NormalizeKeyListVariants(tableElement.TableConstraintDef().KeyListVariants())+ for _, column := range columnList {+ if checker.tables[tableName] == nil {+ checker.tables[tableName] = make(columnSet)}+ checker.tables[tableName][column] = true}}}- return in, false}-func (checker *columnDisallowDropInIndexChecker) addIndexColumn(in ast.Node) {- if node, ok := in.(*ast.CreateTableStmt); ok {- for _, spec := range node.Constraints {- if spec.Tp == ast.ConstraintIndex {- for _, key := range spec.Keys {- table := node.Table.Name.O- if checker.tables[table] == nil {- checker.tables[table] = make(columnSet)- }- checker.tables[table][key.Column.Name.O] = true+func (checker *columnDisallowDropInIndexChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {+ if ctx.AlterTableActions() == nil {+ return+ }+ if ctx.AlterTableActions().AlterCommandList() == nil {+ return+ }+ if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {+ return+ }++ _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())+ // alter table add column, change column, modify column.+ for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {+ if item == nil || item.DROP_SYMBOL() == nil || item.ColumnInternalRef() == nil {+ continue+ }++ index := checker.catalog.Origin.Index(&catalog.TableIndexFind{+ // In MySQL, the SchemaName is "".+ SchemaName: "",+ TableName: tableName,+ })++ if index != nil {+ if checker.tables[tableName] == nil {+ checker.tables[tableName] = make(columnSet)+ }+ for _, indexColumn := range *index {+ for _, column := range indexColumn.ExpressionList() {+ checker.tables[tableName][column] = true}}}++ columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())+ if !checker.canDrop(tableName, columnName) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.DropIndexColumn,+ Title: checker.title,+ Content: fmt.Sprintf("`%s`.`%s` cannot drop index column", tableName, columnName),+ Line: checker.baseLine + item.GetStart().GetLine(),+ })+ }}}diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.goindex e076ea7c11..5edd097771 100644--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go@@ -75,9 +75,10 @@ func TestMySQLWIPRules(t *testing.T) {advisor.SchemaRuleColumnDisallowChange,// advisor.SchemaRuleColumnDisallowChangingOrder disallow changing column order.advisor.SchemaRuleColumnDisallowChangingOrder,+ // advisor.SchemaRuleColumnDisallowDropIndex disallow drop index column.+ advisor.SchemaRuleColumnDisallowDropInIndex,// advisor.SchemaRuleColumnCommentConvention enforce the column comment convention.advisor.SchemaRuleColumnCommentConvention,-// advisor.SchemaRuleColumnDisallowSetCharset disallow set column charset.advisor.SchemaRuleColumnDisallowSetCharset,// advisor.SchemaRuleColumnMaximumCharacterLength enforce the maximum character length.diff --git a/backend/plugin/advisor/mysqlwip/test/column_disallow_drop_in_index.yaml b/backend/plugin/advisor/mysqlwip/test/column_disallow_drop_in_index.yamlindex 2b3c284b70..ed4294e6f1 100644--- a/backend/plugin/advisor/mysqlwip/test/column_disallow_drop_in_index.yaml+++ b/backend/plugin/advisor/mysqlwip/test/column_disallow_drop_in_index.yaml@@ -1,6 +1,6 @@- statement: |-CREATE TABLE t(a int, b int, INDEX idx_a(a));- ALTER TABLE t DROP COLUMN b+ ALTER TABLE t DROP COLUMN b;want:- status: SUCCESScode: 0@@ -8,32 +8,30 @@content: ""line: 0details: ""-- statement: |-CREATE TABLE t(a int, b int, INDEX idx_b(b));- ALTER TABLE t DROP COLUMN b+ ALTER TABLE t DROP COLUMN b;want:- status: WARNcode: 424title: "column.disallow-drop-in-index"content: "`t`.`b` cannot drop index column"- line: 3+ line: 2details: ""- statement: |-CREATE TABLE t(a int, c int, INDEX idx_ac(a,c));- ALTER TABLE t DROP COLUMN c+ ALTER TABLE t DROP COLUMN c;want:- status: WARNcode: 424title: "column.disallow-drop-in-index"content: "`t`.`c` cannot drop index column"- line: 3+ line: 2details: ""-- statement: |-CREATE TABLE t(a int, b int, INDEX idx_a(a));CREATE TABLE y(a int, b int);- ALTER TABLE y DROP COLUMN a+ ALTER TABLE y DROP COLUMN a;want:- status: SUCCESScode: 0@@ -41,11 +39,10 @@content: ""line: 0details: ""-- statement: |-CREATE TABLE t(a int,b int, c int, INDEX idx_ac(a,c));ALTER TABLE t DROP COLUMN a;- ALTER TABLE t DROP COLUMN c+ ALTER TABLE t DROP COLUMN c;want:- status: WARNcode: 424@@ -57,5 +54,5 @@code: 424title: "column.disallow-drop-in-index"content: "`t`.`c` cannot drop index column"- line: 4+ line: 3details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 44ea6be1ef..f101a0798b 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1240,6 +1240,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty}case SchemaRuleColumnDisallowDropInIndex:switch engine {+ // only for mysqlwip test.+ case storepb.Engine_ENGINE_UNSPECIFIED:+ return MySQLColumnDisallowDropInIndex, nilcase storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return MySQLColumnDisallowDropInIndex, nil}