Skip to content

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

feat: implement MySQL disallow changing column type (#2413)

rebelice3ce88c8d565e
diff --git a/plugin/advisor/catalog/catalog.go b/plugin/advisor/catalog/catalog.go
index b13fcaea02..e5d8429ee9 100644
--- a/plugin/advisor/catalog/catalog.go
+++ b/plugin/advisor/catalog/catalog.go
@@ -172,3 +172,30 @@ func (d *Database) FindPrimaryKey(find *PrimaryKeyFind) *Index {
}
return nil
}
+
+// ColumnFind is for find column.
+type ColumnFind struct {
+ SchemaName string
+ TableName string
+ ColumnName string
+}
+
+// FindColumn finds the column.
+func (d *Database) FindColumn(find *ColumnFind) *Column {
+ for _, schema := range d.SchemaList {
+ if schema.Name != find.SchemaName {
+ continue
+ }
+ for _, table := range schema.TableList {
+ if table.Name != find.TableName {
+ continue
+ }
+ for _, column := range table.ColumnList {
+ if column.Name == find.ColumnName {
+ return column
+ }
+ }
+ }
+ }
+ return nil
+}
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 8ec2fa94ab..f372327d06 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -45,6 +45,7 @@ const (
// 401 ~ 499 column error code.
NoRequiredColumn Code = 401
ColumnCanNotNull Code = 402
+ ChangeColumnType Code = 403
NotNullColumnWithNullDefault Code = 404
// 501 engine error code.
diff --git a/plugin/advisor/generator/mysql.template b/plugin/advisor/generator/mysql.template
index d966849209..3a48338e06 100644
--- a/plugin/advisor/generator/mysql.template
+++ b/plugin/advisor/generator/mysql.template
@@ -1,5 +1,7 @@
package mysql
+// Framework code is generated by the generator.
+
import (
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/advisor/db"
diff --git a/plugin/advisor/generator/mysql_test.template b/plugin/advisor/generator/mysql_test.template
index 65bef3925a..9a5c806f7f 100644
--- a/plugin/advisor/generator/mysql_test.template
+++ b/plugin/advisor/generator/mysql_test.template
@@ -1,5 +1,7 @@
package mysql
+// Framework code is generated by the generator.
+
import (
"testing"
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_type.go b/plugin/advisor/mysql/advisor_column_disallow_changing_type.go
index 367297e417..55197ab612 100644
--- a/plugin/advisor/mysql/advisor_column_disallow_changing_type.go
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_type.go
@@ -1,9 +1,17 @@
package mysql
+// Framework code is generated by the generator.
+
import (
+ "fmt"
+ "strings"
+
"github.com/pingcap/tidb/parser/ast"
+ "github.com/pingcap/tidb/parser/mysql"
+ "github.com/pingcap/tidb/parser/types"
"github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/bytebase/bytebase/plugin/advisor/db"
)
@@ -33,8 +41,9 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(ctx advisor.Context, statement s
return nil, err
}
checker := &columnDisallowChangingTypeChecker{
- level: level,
- title: string(ctx.Rule.Type),
+ level: level,
+ title: string(ctx.Rule.Type),
+ database: ctx.Database,
}
for _, stmt := range stmtList {
@@ -60,13 +69,35 @@ type columnDisallowChangingTypeChecker struct {
title string
text string
line int
+ database *catalog.Database
}
// Enter implements the ast.Visitor interface.
-func (*columnDisallowChangingTypeChecker) Enter(in ast.Node) (ast.Node, bool) {
- // TODO: implement it
- // switch node := in.(type) {
- // }
+func (checker *columnDisallowChangingTypeChecker) Enter(in ast.Node) (ast.Node, bool) {
+ changeType := false
+ if node, ok := in.(*ast.AlterTableStmt); ok {
+ for _, spec := range node.Specs {
+ switch spec.Tp {
+ case ast.AlterTableChangeColumn:
+ changeType = checker.changeColumnType(node.Table.Name.O, spec.OldColumnName.Name.O, getTypeString(spec.NewColumns[0].Tp))
+ case ast.AlterTableModifyColumn:
+ changeType = checker.changeColumnType(node.Table.Name.O, spec.NewColumns[0].Name.Name.O, getTypeString(spec.NewColumns[0].Tp))
+ }
+ if changeType {
+ break
+ }
+ }
+ }
+
+ if changeType {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.ChangeColumnType,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" changes column type", checker.text),
+ Line: checker.line,
+ })
+ }
return in, false
}
@@ -75,3 +106,31 @@ func (*columnDisallowChangingTypeChecker) Enter(in ast.Node) (ast.Node, bool) {
func (*columnDisallowChangingTypeChecker) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
+
+func getTypeString(tp *types.FieldType) string {
+ switch tp.Tp {
+ case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
+ // Referring this issue tidb#6688, the integer max display length is deprecated in MySQL 8.0.
+ // Since the length doesn't take any effect in TiDB storage or showing result, we remove it here.
+ s := tp.String()
+ if idx := strings.Index(s, "("); idx != -1 {
+ s = s[:idx]
+ }
+ return s
+ default:
+ return tp.String()
+ }
+}
+
+func (checker *columnDisallowChangingTypeChecker) changeColumnType(tableName string, columName string, newType string) bool {
+ column := checker.database.FindColumn(&catalog.ColumnFind{
+ TableName: tableName,
+ ColumnName: columName,
+ })
+
+ if column == nil {
+ return false
+ }
+
+ return !strings.EqualFold(column.Type, newType)
+}
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_type_test.go b/plugin/advisor/mysql/advisor_column_disallow_changing_type_test.go
index dcf097b3bc..a4f51b8b6d 100644
--- a/plugin/advisor/mysql/advisor_column_disallow_changing_type_test.go
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_type_test.go
@@ -1,5 +1,7 @@
package mysql
+// Framework code is generated by the generator.
+
import (
"testing"
@@ -9,7 +11,7 @@ import (
func TestColumnDisallowChangingType(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: ``,
+ Statement: `ALTER TABLE tech_book MODIFY id int`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -19,9 +21,34 @@ func TestColumnDisallowChangingType(t *testing.T) {
},
},
},
+ {
+ Statement: `ALTER TABLE tech_book MODIFY id bigint`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.ChangeColumnType,
+ Title: "column.disallow-change-type",
+ Content: "\"ALTER TABLE tech_book MODIFY id bigint\" changes column type",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE tech_book CHANGE name uname varchar(20)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.ChangeColumnType,
+ Title: "column.disallow-change-type",
+ Content: "\"ALTER TABLE tech_book CHANGE name uname varchar(20)\" changes column type",
+ Line: 1,
+ },
+ },
+ },
}
advisor.RunSQLReviewRuleTests(t, tests, &ColumnDisallowChangingTypeAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnDisallowChangeType,
Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, advisor.MockMySQLDatabase)
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index a8d72b39e5..8af11fa157 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -66,6 +66,8 @@ const (
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
// SchemaRuleColumnNotNull enforce the columns cannot have NULL value.
SchemaRuleColumnNotNull SQLReviewRuleType = "column.no-null"
+ // SchemaRuleColumnDisallowChangeType disallow change column type.
+ SchemaRuleColumnDisallowChangeType SQLReviewRuleType = "column.disallow-change-type"
// SchemaRuleColumnSetDefaultForNotNull require the not null column to set default value.
SchemaRuleColumnSetDefaultForNotNull SQLReviewRuleType = "column.set-default-for-not-null"
@@ -401,6 +403,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.Postgres:
return PostgreSQLColumnNoNull, nil
}
+ case SchemaRuleColumnDisallowChangeType:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLColumnDisallowChangingType, nil
+ }
case SchemaRuleColumnSetDefaultForNotNull:
switch engine {
case db.MySQL, db.TiDB:
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index c49c10a395..50da3ff999 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -38,8 +38,14 @@ var (
{
Name: MockTableName,
ColumnList: []*catalog.Column{
- {Name: "id"},
- {Name: "name"},
+ {
+ Name: "id",
+ Type: "int",
+ },
+ {
+ Name: "name",
+ Type: "varchar(255)",
+ },
},
IndexList: []*catalog.Index{
{