Skip to content

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

feat(sql-review): implement MySQL disallow-set-column-charset rule (#2827)

rebelice274f767b929e
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index a50485223e..66581a4d79 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -106,6 +106,9 @@ const (
// MySQLColumnTypeRestriction is an advisor type for MySQL column type restriction.
MySQLColumnTypeRestriction Type = "bb.plugin.advisor.mysql.column.type-restriction"
+ // MySQLDisallowSetColumnCharset is an advisor type for MySQL disallow set column charset.
+ MySQLDisallowSetColumnCharset Type = "bb.plugin.advisor.mysql.column.disallow-set-charset"
+
// 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/code.go b/plugin/advisor/code.go
index ae0c579435..9de622c81b 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -59,6 +59,7 @@ const (
DisabledColumnType Code = 411
ColumnExists Code = 412
DropAllColumns Code = 413
+ SetColumnCharset Code = 414
// 501 engine error code.
NotInnoDBEngine Code = 501
diff --git a/plugin/advisor/mysql/advisor_column_disallow_set_charset.go b/plugin/advisor/mysql/advisor_column_disallow_set_charset.go
new file mode 100644
index 0000000000..433d3fc80e
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_disallow_set_charset.go
@@ -0,0 +1,119 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/pingcap/tidb/parser/ast"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnDisallowSetCharsetAdvisor)(nil)
+ _ ast.Visitor = (*columnDisallowSetCharsetChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLDisallowSetColumnCharset, &ColumnDisallowSetCharsetAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLDisallowSetColumnCharset, &ColumnDisallowSetCharsetAdvisor{})
+}
+
+// ColumnDisallowSetCharsetAdvisor is the advisor checking for disallow set column charset.
+type ColumnDisallowSetCharsetAdvisor struct {
+}
+
+// Check checks for disallow set column charset.
+func (*ColumnDisallowSetCharsetAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ checker := &columnDisallowSetCharsetChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmtList {
+ checker.text = stmt.Text()
+ checker.line = stmt.OriginTextPosition()
+ (stmt).Accept(checker)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type columnDisallowSetCharsetChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *columnDisallowSetCharsetChecker) Enter(in ast.Node) (ast.Node, bool) {
+ code := advisor.Ok
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ for _, column := range node.Cols {
+ charset := getColumnCharset(column)
+ if charset != "" {
+ code = advisor.SetColumnCharset
+ break
+ }
+ }
+ case *ast.AlterTableStmt:
+ for _, spec := range node.Specs {
+ switch spec.Tp {
+ case ast.AlterTableAddColumns:
+ for _, column := range spec.NewColumns {
+ charset := getColumnCharset(column)
+ if charset != "" {
+ code = advisor.SetColumnCharset
+ }
+ }
+ case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
+ charset := getColumnCharset(spec.NewColumns[0])
+ if charset != "" {
+ code = advisor.SetColumnCharset
+ }
+ }
+ if code != advisor.Ok {
+ break
+ }
+ }
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.SetColumnCharset,
+ Title: checker.title,
+ Content: fmt.Sprintf("Disallow set column charset but \"%s\" does", checker.text),
+ Line: checker.line,
+ })
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*columnDisallowSetCharsetChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_column_disallow_set_charset_test.go b/plugin/advisor/mysql/advisor_column_disallow_set_charset_test.go
new file mode 100644
index 0000000000..3c906581ab
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_disallow_set_charset_test.go
@@ -0,0 +1,79 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestColumnDisallowSetCharset(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `CREATE TABLE t(a varchar(20))`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE t(a varchar(20) CHARSET ascii)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.SetColumnCharset,
+ Title: "column.disallow-set-charset",
+ Content: "Disallow set column charset but \"CREATE TABLE t(a varchar(20) CHARSET ascii)\" does",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE tech_book ADD COLUMN a varchar(20) CHARSET ascii`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.SetColumnCharset,
+ Title: "column.disallow-set-charset",
+ Content: "Disallow set column charset but \"ALTER TABLE tech_book ADD COLUMN a varchar(20) CHARSET ascii\" does",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE tech_book MODIFY COLUMN id varchar(20) CHARSET ascii`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.SetColumnCharset,
+ Title: "column.disallow-set-charset",
+ Content: "Disallow set column charset but \"ALTER TABLE tech_book MODIFY COLUMN id varchar(20) CHARSET ascii\" does",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE tech_book CHANGE COLUMN name name varchar(20) CHARSET ascii`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.SetColumnCharset,
+ Title: "column.disallow-set-charset",
+ Content: "Disallow set column charset but \"ALTER TABLE tech_book CHANGE COLUMN name name varchar(20) CHARSET ascii\" does",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnDisallowSetCharsetAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnDisallowSetCharset,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 21d9eee4a8..84e052356b 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -87,6 +87,8 @@ const (
SchemaRuleColumnAutoIncrementMustInteger SQLReviewRuleType = "column.auto-increment-must-integer"
// SchemaRuleColumnTypeRestriction enforce the column type restriction.
SchemaRuleColumnTypeRestriction SQLReviewRuleType = "column.type-restriction"
+ // SchemaRuleColumnDisallowSetCharset disallow set column charset.
+ SchemaRuleColumnDisallowSetCharset SQLReviewRuleType = "column.disallow-set-charset"
// SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.
SchemaRuleSchemaBackwardCompatibility SQLReviewRuleType = "schema.backward-compatibility"
@@ -689,6 +691,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLColumnTypeRestriction, nil
}
+ case SchemaRuleColumnDisallowSetCharset:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLDisallowSetColumnCharset, nil
+ }
case SchemaRuleTableRequirePK:
switch engine {
case db.MySQL, db.TiDB: