Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(schema system): support MySQL COLUMN no NULL rule (#1288)
rebelicea06d337cdf73
diff --git a/common/error.go b/common/error.goindex 534f98d064..973c2586dd 100644--- a/common/error.go+++ b/common/error.go@@ -67,6 +67,7 @@ const (// 10301 column rule advisor error codeNoRequiredColumn Code = 10301+ ColumnCanNull Code = 10302NotInnoDBEngine Code = 10401)diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 304d8464c7..f4939adab2 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -91,6 +91,9 @@ const (// MySQLColumnRequirement is an advisor type for MySQL column requirement.MySQLColumnRequirement Type = "bb.plugin.advisor.mysql.column.require"+ // MySQLColumnNoNull is an advisor type for MySQL column no NULL value.+ MySQLColumnNoNull Type = "bb.plugin.advisor.mysql.column.no-null"+// MySQLNoSelectAll is an advisor type for MySQL no select all.MySQLNoSelectAll Type = "bb.plugin.advisor.mysql.select.no-select-all")diff --git a/plugin/advisor/mysql/advisor_column_no_null.go b/plugin/advisor/mysql/advisor_column_no_null.gonew file mode 100644index 0000000000..1931c552a6--- /dev/null+++ b/plugin/advisor/mysql/advisor_column_no_null.go@@ -0,0 +1,127 @@+package mysql++import (+ "fmt"++ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/db"+ "github.com/pingcap/tidb/parser/ast"+)++var (+ _ advisor.Advisor = (*ColumnNoNullAdvisor)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})+}++// ColumnNoNullAdvisor is the advisor checking for column no NULL value.+type ColumnNoNullAdvisor struct {+}++// Check checks for column no NULL value.+func (adv *ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ root, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &columnNoNullChecker{level: level}++ for _, stmtNode := range root {+ (stmtNode).Accept(checker)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type columnNoNullChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+}++type columnName struct {+ tableName string+ columnName string+}++// Enter implements the ast.Visitor interface+func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {+ var columns []columnName+ switch node := in.(type) {+ // CREATE TABLE+ case *ast.CreateTableStmt:+ for _, column := range node.Cols {+ if canNull(column) {+ columns = append(columns, columnName{+ tableName: node.Table.Name.String(),+ columnName: column.Name.Name.String(),+ })+ }+ }+ // ALTER TABLE+ case *ast.AlterTableStmt:+ for _, spec := range node.Specs {+ switch spec.Tp {+ // ADD COLUMNS+ case ast.AlterTableAddColumns:+ for _, column := range spec.NewColumns {+ if canNull(column) {+ columns = append(columns, columnName{+ tableName: node.Table.Name.String(),+ columnName: column.Name.Name.String(),+ })+ }+ }+ // CHANGE COLUMN+ case ast.AlterTableChangeColumn:+ if canNull(spec.NewColumns[0]) {+ columns = append(columns, columnName{+ tableName: node.Table.Name.String(),+ columnName: spec.NewColumns[0].Name.Name.String(),+ })+ }+ }+ }+ }++ for _, column := range columns {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: fmt.Sprintf("`%s`.`%s` can have NULL value", column.tableName, column.columnName),+ })+ }++ return in, false+}++// Leave implements the ast.Visitor interface+func (v *columnNoNullChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}++func canNull(column *ast.ColumnDef) bool {+ for _, option := range column.Options {+ if option.Tp == ast.ColumnOptionNotNull || option.Tp == ast.ColumnOptionPrimaryKey {+ return false+ }+ }+ return true+}diff --git a/plugin/advisor/mysql/advisor_column_no_null_test.go b/plugin/advisor/mysql/advisor_column_no_null_test.gonew file mode 100644index 0000000000..e88ccaa42f--- /dev/null+++ b/plugin/advisor/mysql/advisor_column_no_null_test.go@@ -0,0 +1,114 @@+package mysql++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestColumnNoNull(t *testing.T) {+ tests := []test{+ {+ statement: "CREATE TABLE book(id int, name varchar(255))",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`id` can have NULL value",+ },+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`name` can have NULL value",+ },+ },+ },+ {+ statement: "CREATE TABLE book(id int PRIMARY KEY, name varchar(255))",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`name` can have NULL value",+ },+ },+ },+ {+ statement: "CREATE TABLE book(id int NOT NULL, name varchar(255))",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`name` can have NULL value",+ },+ },+ },+ {+ statement: "CREATE TABLE book(id int PRIMARY KEY, name varchar(255) NOT NULL)",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "ALTER TABLE book ADD COLUMN (id int, name varchar(255) NOT NULL)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`id` can have NULL value",+ },+ },+ },+ {+ statement: "ALTER TABLE book ADD COLUMN (id int PRIMARY KEY, name varchar(255) NOT NULL)",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "ALTER TABLE book CHANGE COLUMN id name varchar(255)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: common.ColumnCanNull,+ Title: "Column can have NULL value",+ Content: "`book`.`name` can have NULL value",+ },+ },+ },+ {+ statement: "ALTER TABLE book CHANGE COLUMN id name varchar(255) NOT NULL",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ }++ runSchemaReviewRuleTests(t, tests, &ColumnNoNullAdvisor{}, &api.SchemaReviewRule{+ Type: api.SchemaRuleColumnNotNull,+ Level: api.SchemaRuleLevelWarning,+ Payload: "",+ }, &MockCatalogService{})+}diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.goindex cb31a2d890..de90c20edb 100644--- a/server/task_check_executor_statement_advisor_composite.go+++ b/server/task_check_executor_statement_advisor_composite.go@@ -174,6 +174,11 @@ func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (adcase db.MySQL, db.TiDB:return advisor.MySQLColumnRequirement, nil}+ case api.SchemaRuleColumnNotNull:+ switch engine {+ case db.MySQL, db.TiDB:+ return advisor.MySQLColumnNoNull, nil+ }case api.SchemaRuleMySQLEngine:switch engine {case db.MySQL: