Skip to content

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

feat(sql-review): implement MySQL disallow CHANGE COLUMN statement (#2456)

rebelicee3c04719043c
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index b273338ce0..7a076d1aba 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -91,6 +91,9 @@ const (
// MySQLColumnSetDefaultForNotNull is an advisor type for MySQL set default value for not null column.
MySQLColumnSetDefaultForNotNull Type = "bb.plugin.advisor.mysql.column.set-default-for-not-null"
+ // MySQLColumnDisallowChanging is an advisor type for MySQL disallow CHANGE COLUMN statement.
+ MySQLColumnDisallowChanging Type = "bb.plugin.advisor.mysql.column.disallow-change"
+
// MySQLColumnExists is an advisor type for MySQL column existence.
MySQLColumnExists Type = "bb.plugin.advisor.mysql.column.exists"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 44306ffd52..4bd4a0181a 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -48,6 +48,7 @@ const (
ChangeColumnType Code = 403
NotNullColumnWithNullDefault Code = 404
ColumnNotExists Code = 405
+ UseChangeColumnStatement Code = 406
// 501 engine error code.
NotInnoDBEngine Code = 501
diff --git a/plugin/advisor/generator/main.go b/plugin/advisor/generator/main.go
index 2458e05a00..6fd082fc00 100644
--- a/plugin/advisor/generator/main.go
+++ b/plugin/advisor/generator/main.go
@@ -43,7 +43,7 @@ var (
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
- if !strings.Contains(line, flags.rule) {
+ if !strings.Contains(line, flags.rule+" ") {
continue
}
wordList := strings.Fields(line)
diff --git a/plugin/advisor/generator/mysql.template b/plugin/advisor/generator/mysql.template
index 3a48338e06..822cc40af6 100644
--- a/plugin/advisor/generator/mysql.template
+++ b/plugin/advisor/generator/mysql.template
@@ -18,11 +18,11 @@ func init() {
advisor.Register(db.TiDB, advisor.%AdvisorType, &%AdvisorName{})
}
-// %AdvisorName is the advisor checking for %AdvisorComment.
+// %AdvisorName is the advisor checking for %AdvisorComment
type %AdvisorName struct {
}
-// Check checks for %AdvisorComment.
+// Check checks for %AdvisorComment
func (*%AdvisorName) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
if errAdvice != nil {
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing.go b/plugin/advisor/mysql/advisor_column_disallow_changing.go
new file mode 100644
index 0000000000..404713edba
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing.go
@@ -0,0 +1,92 @@
+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 = (*ColumnDisallowChangingAdvisor)(nil)
+ _ ast.Visitor = (*columnDisallowChangingChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLColumnDisallowChanging, &ColumnDisallowChangingAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLColumnDisallowChanging, &ColumnDisallowChangingAdvisor{})
+}
+
+// ColumnDisallowChangingAdvisor is the advisor checking for disallow CHANGE COLUMN statement.
+type ColumnDisallowChangingAdvisor struct {
+}
+
+// Check checks for disallow CHANGE COLUMN statement.
+func (*ColumnDisallowChangingAdvisor) 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 := &columnDisallowChangingChecker{
+ 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 columnDisallowChangingChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *columnDisallowChangingChecker) Enter(in ast.Node) (ast.Node, bool) {
+ if node, ok := in.(*ast.AlterTableStmt); ok {
+ for _, spec := range node.Specs {
+ if spec.Tp == ast.AlterTableChangeColumn {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.UseChangeColumnStatement,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" contains CHANGE COLUMN statement", checker.text),
+ Line: checker.line,
+ })
+ break
+ }
+ }
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*columnDisallowChangingChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_test.go b/plugin/advisor/mysql/advisor_column_disallow_changing_test.go
new file mode 100644
index 0000000000..596d001c91
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_test.go
@@ -0,0 +1,43 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestColumnDisallowChanging(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `ALTER TABLE t ADD COLUMN a int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t CHANGE COLUMN b a int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.UseChangeColumnStatement,
+ Title: "column.disallow-change",
+ Content: "\"ALTER TABLE t CHANGE COLUMN b a int\" contains CHANGE COLUMN statement",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnDisallowChangingAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnDisallowChange,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index bfbc5f30f2..3a062c046c 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -72,6 +72,8 @@ const (
SchemaRuleColumnDisallowChangeType SQLReviewRuleType = "column.disallow-change-type"
// SchemaRuleColumnSetDefaultForNotNull require the not null column to set default value.
SchemaRuleColumnSetDefaultForNotNull SQLReviewRuleType = "column.set-default-for-not-null"
+ // SchemaRuleColumnDisallowChange disallow CHANGE COLUMN statement.
+ SchemaRuleColumnDisallowChange SQLReviewRuleType = "column.disallow-change"
// SchemaRuleColumnExists require the column existence.
SchemaRuleColumnExists SQLReviewRuleType = "column.exists"
@@ -417,6 +419,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLColumnSetDefaultForNotNull, nil
}
+ case SchemaRuleColumnDisallowChange:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLColumnDisallowChanging, nil
+ }
case SchemaRuleColumnExists:
switch engine {
case db.MySQL, db.TiDB: