Skip to content

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

chore: implement disallow set charset in table level (#11083)

boojack3b67b9055c47
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 553d3125be..e52b135c3f 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -110,6 +110,7 @@ const (
TableIsReferencedByView Code = 609
CreateTableTrigger Code = 610
TotalTextLengthExceedsLimit Code = 611
+ DisallowSetCharset Code = 612
// 701 ~ 799 database advisor error code.
DatabaseNotEmpty Code = 701
diff --git a/backend/plugin/advisor/mysql/advisor_table_disallow_set_charset.go b/backend/plugin/advisor/mysql/advisor_table_disallow_set_charset.go
index 763a7c3798..50239673e5 100644
--- a/backend/plugin/advisor/mysql/advisor_table_disallow_set_charset.go
+++ b/backend/plugin/advisor/mysql/advisor_table_disallow_set_charset.go
@@ -1,6 +1,8 @@
package mysql
import (
+ "fmt"
+
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -67,3 +69,45 @@ type tableDisallowSetCharsetChecker struct {
func (checker *tableDisallowSetCharsetChecker) EnterQuery(ctx *mysql.QueryContext) {
checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
}
+
+func (checker *tableDisallowSetCharsetChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {
+ if ctx.CreateTableOptions() != nil {
+ for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {
+ if option.DefaultCharset() != nil {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DisallowSetCharset,
+ Title: checker.title,
+ Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", checker.text),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+ }
+ }
+}
+
+func (checker *tableDisallowSetCharsetChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {
+ if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
+ return
+ }
+
+ alterList := ctx.AlterTableActions().AlterCommandList().AlterList()
+ if alterList == nil {
+ return
+ }
+ for _, alterListItem := range alterList.AllAlterListItem() {
+ if alterListItem == nil {
+ continue
+ }
+
+ if alterListItem.Charset() != nil {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DisallowSetCharset,
+ Title: checker.title,
+ Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", checker.text),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 9e08e894d6..a1d8c2da91 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -81,6 +81,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleTableDisallowTrigger,
// advisor.SchemaRuleTableNoDuplicateIndex require the table no duplicate index.
advisor.SchemaRuleTableNoDuplicateIndex,
+ // advisor.SchemaRuleTableDisallowSetCharset disallow set table charset when creating/altering table.
+ advisor.SchemaRuleTableDisallowSetCharset,
// Column related rules.
// advisor.SchemaRuleRequiredColumn enforce the required columns in each table.
diff --git a/backend/plugin/advisor/mysql/test/table_disallow_set_charset.yaml b/backend/plugin/advisor/mysql/test/table_disallow_set_charset.yaml
new file mode 100644
index 0000000000..cc53ad78be
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/table_disallow_set_charset.yaml
@@ -0,0 +1,30 @@
+- statement: |-
+ CREATE TABLE employee (
+ id INT PRIMARY KEY,
+ name VARCHAR(50)
+ ) CHARSET=utf8;
+ want:
+ - status: WARN
+ code: 612
+ title: table.disallow-set-charset
+ content: |-
+ Set charset on tables is disallowed, but "CREATE TABLE employee (
+ id INT PRIMARY KEY,
+ name VARCHAR(50)
+ ) CHARSET=utf8;" uses
+ line: 1
+ details: ""
+- statement: |-
+ CREATE TABLE employee (
+ id INT PRIMARY KEY,
+ name VARCHAR(50)
+ );
+ ALTER TABLE employee CONVERT TO CHARACTER SET utf8;
+ want:
+ - status: WARN
+ code: 612
+ title: table.disallow-set-charset
+ content: |-
+ Set charset on tables is disallowed, but "ALTER TABLE employee CONVERT TO CHARACTER SET utf8;" uses
+ line: 5
+ details: ""
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index cffd84a9a3..b96593fcbe 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -364,7 +364,8 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleFunctionDisallowCreate,
SchemaRuleStatementCreateSpecifySchema,
SchemaRuleStatementCheckSetRoleVariable,
- SchemaRuleStatementWhereDisallowUsingFunction:
+ SchemaRuleStatementWhereDisallowUsingFunction,
+ SchemaRuleTableDisallowSetCharset:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{
Format: "_delete$",