Skip to content

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

feat(advisor): support PK type allowlist for MySQL (#5195)

rebelice01b021331832
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 6bfd34ad34..860e8fd809 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -156,6 +156,9 @@ const (
// MySQLIndexPKType is an advisor type for MySQL correct type of PK.
MySQLIndexPKType Type = "bb.plugin.advisor.mysql.index.pk-type"
+ // MySQLPrimaryKeyTypeAllowlist is an advisor type for MySQL primary key type allowlist.
+ MySQLPrimaryKeyTypeAllowlist Type = "bb.plugin.advisor.mysql.index.primary-key-type-allowlist"
+
// MySQLIndexKeyNumberLimit is an advisor type for MySQL index key number limit.
MySQLIndexKeyNumberLimit Type = "bb.plugin.advisor.mysql.index.key-number-limit"
diff --git a/backend/plugin/advisor/mysql/advisor_index_primary_key_type_allowlist.go b/backend/plugin/advisor/mysql/advisor_index_primary_key_type_allowlist.go
new file mode 100644
index 0000000000..1df67f5f0c
--- /dev/null
+++ b/backend/plugin/advisor/mysql/advisor_index_primary_key_type_allowlist.go
@@ -0,0 +1,215 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/pingcap/tidb/parser/ast"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser"
+)
+
+var (
+ _ advisor.Advisor = (*IndexPrimaryKeyTypeAllowlistAdvisor)(nil)
+ _ ast.Visitor = (*indexPrimaryKeyTypeAllowlistChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLPrimaryKeyTypeAllowlist, &IndexPrimaryKeyTypeAllowlistAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLPrimaryKeyTypeAllowlist, &IndexPrimaryKeyTypeAllowlistAdvisor{})
+}
+
+// IndexPrimaryKeyTypeAllowlistAdvisor is the advisor checking for primary key type allowlist.
+type IndexPrimaryKeyTypeAllowlistAdvisor struct {
+}
+
+// Check checks for primary key type allowlist.
+func (*IndexPrimaryKeyTypeAllowlistAdvisor) 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
+ }
+ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ allowlist := make(map[string]bool)
+ for _, tp := range payload.List {
+ allowlist[strings.ToLower(tp)] = true
+ }
+ checker := &indexPrimaryKeyTypeAllowlistChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ allowlist: allowlist,
+ catalog: ctx.Catalog,
+ tablesNewColumns: make(map[string]columnNameToColumnDef),
+ }
+
+ 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 indexPrimaryKeyTypeAllowlistChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+ allowlist map[string]bool
+ catalog *catalog.Finder
+ tablesNewColumns tableNewColumn
+}
+
+// Enter implements the ast.Visitor interface.
+func (v *indexPrimaryKeyTypeAllowlistChecker) Enter(in ast.Node) (ast.Node, bool) {
+ var pkDataList []pkData
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ tableName := node.Table.Name.String()
+ for _, column := range node.Cols {
+ pds := v.addNewColumn(tableName, column.OriginTextPosition(), column)
+ pkDataList = append(pkDataList, pds...)
+ }
+ for _, constraint := range node.Constraints {
+ pds := v.addConstraint(tableName, constraint.OriginTextPosition(), constraint)
+ pkDataList = append(pkDataList, pds...)
+ }
+ case *ast.AlterTableStmt:
+ tableName := node.Table.Name.String()
+ for _, spec := range node.Specs {
+ switch spec.Tp {
+ case ast.AlterTableAddColumns:
+ for _, column := range spec.NewColumns {
+ pds := v.addNewColumn(tableName, node.OriginTextPosition(), column)
+ pkDataList = append(pkDataList, pds...)
+ }
+ case ast.AlterTableAddConstraint:
+ pds := v.addConstraint(tableName, node.OriginTextPosition(), spec.Constraint)
+ pkDataList = append(pkDataList, pds...)
+ case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
+ newColumnDef := spec.NewColumns[0]
+ oldColumnName := newColumnDef.Name.Name.String()
+ if spec.OldColumnName != nil {
+ oldColumnName = spec.OldColumnName.Name.String()
+ }
+ pds := v.changeColumn(tableName, oldColumnName, node.OriginTextPosition(), newColumnDef)
+ pkDataList = append(pkDataList, pds...)
+ }
+ }
+ }
+ for _, pd := range pkDataList {
+ v.adviceList = append(v.adviceList, advisor.Advice{
+ Status: v.level,
+ Code: advisor.IndexPKType,
+ Title: v.title,
+ Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", pd.column, pd.table, pd.columnType),
+ Line: pd.line,
+ })
+ }
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*indexPrimaryKeyTypeAllowlistChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func (v *indexPrimaryKeyTypeAllowlistChecker) addNewColumn(tableName string, line int, colDef *ast.ColumnDef) []pkData {
+ var pkDataList []pkData
+ for _, option := range colDef.Options {
+ if option.Tp == ast.ColumnOptionPrimaryKey {
+ tp := bbparser.TypeString(colDef.Tp.GetType())
+ if _, exists := v.allowlist[strings.ToLower(tp)]; !exists {
+ pkDataList = append(pkDataList, pkData{
+ table: tableName,
+ column: colDef.Name.String(),
+ columnType: tp,
+ line: line,
+ })
+ }
+ }
+ }
+ v.tablesNewColumns.set(tableName, colDef.Name.String(), colDef)
+ return pkDataList
+}
+
+func (v *indexPrimaryKeyTypeAllowlistChecker) changeColumn(tableName, oldColumnName string, line int, newColumnDef *ast.ColumnDef) []pkData {
+ var pkDataList []pkData
+ v.tablesNewColumns.delete(tableName, oldColumnName)
+ for _, option := range newColumnDef.Options {
+ if option.Tp == ast.ColumnOptionPrimaryKey {
+ tp := bbparser.TypeString(newColumnDef.Tp.GetType())
+ if _, exists := v.allowlist[strings.ToLower(tp)]; !exists {
+ pkDataList = append(pkDataList, pkData{
+ table: tableName,
+ column: newColumnDef.Name.String(),
+ columnType: tp,
+ line: line,
+ })
+ }
+ }
+ }
+ v.tablesNewColumns.set(tableName, newColumnDef.Name.String(), newColumnDef)
+ return pkDataList
+}
+
+func (v *indexPrimaryKeyTypeAllowlistChecker) addConstraint(tableName string, line int, constraint *ast.Constraint) []pkData {
+ var pkDataList []pkData
+ if constraint.Tp == ast.ConstraintPrimaryKey {
+ for _, key := range constraint.Keys {
+ columnName := key.Column.Name.String()
+ columnType, err := v.getPKColumnType(tableName, columnName)
+ if err != nil {
+ continue
+ }
+ if _, exists := v.allowlist[strings.ToLower(columnType)]; !exists {
+ pkDataList = append(pkDataList, pkData{
+ table: tableName,
+ column: columnName,
+ columnType: columnType,
+ line: line,
+ })
+ }
+ }
+ }
+ return pkDataList
+}
+
+// getPKColumnType gets the column type string from v.tablesNewColumns or catalog, returns empty string and non-nil error if cannot find the column in given table.
+func (v *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName string, columnName string) (string, error) {
+ if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {
+ return bbparser.TypeString(colDef.Tp.GetType()), nil
+ }
+ column := v.catalog.Origin.FindColumn(&catalog.ColumnFind{
+ TableName: tableName,
+ ColumnName: columnName,
+ })
+ if column != nil {
+ return column.Type(), nil
+ }
+ return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 56c0dae770..042ad1a16b 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -109,6 +109,7 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleIndexTypeNoBlob,
// advisor.SchemaRuleIndexTotalNumberLimit enforce the index total number limit.
advisor.SchemaRuleIndexTotalNumberLimit,
+ advisor.SchemaRuleIndexPrimaryKeyTypeAllowlist,
// advisor.SchemaRuleCharsetAllowlist enforce the charset allowlist.
advisor.SchemaRuleCharsetAllowlist,
diff --git a/backend/plugin/advisor/mysql/test/column_auto_increment_must_integer.yaml b/backend/plugin/advisor/mysql/test/column_auto_increment_must_integer.yaml
index e2068c3783..daaf4696ad 100644
--- a/backend/plugin/advisor/mysql/test/column_auto_increment_must_integer.yaml
+++ b/backend/plugin/advisor/mysql/test/column_auto_increment_must_integer.yaml
@@ -12,27 +12,27 @@
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
line: 1
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT
want:
- status: WARN
code: 410
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a varchar(255) AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a varchar(255) AUTO_INCREMENT
want:
- status: WARN
code: 410
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
line: 2
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t CHANGE COLUMN b a varchar(255) AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t CHANGE COLUMN b a varchar(255) AUTO_INCREMENT
want:
- status: WARN
code: 410
diff --git a/backend/plugin/advisor/mysql/test/column_auto_increment_must_unsigned.yaml b/backend/plugin/advisor/mysql/test/column_auto_increment_must_unsigned.yaml
index d6654427dd..3a46434ced 100644
--- a/backend/plugin/advisor/mysql/test/column_auto_increment_must_unsigned.yaml
+++ b/backend/plugin/advisor/mysql/test/column_auto_increment_must_unsigned.yaml
@@ -12,27 +12,27 @@
title: column.auto-increment-must-unsigned
content: Auto-increment column `t`.`a` is not UNSIGNED type
line: 1
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a INT AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a INT AUTO_INCREMENT
want:
- status: WARN
code: 417
title: column.auto-increment-must-unsigned
content: Auto-increment column `t`.`a` is not UNSIGNED type
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a INT AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a INT AUTO_INCREMENT
want:
- status: WARN
code: 417
title: column.auto-increment-must-unsigned
content: Auto-increment column `t`.`a` is not UNSIGNED type
line: 2
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t CHANGE COLUMN b a INT AUTO_INCREMENT
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t CHANGE COLUMN b a INT AUTO_INCREMENT
want:
- status: WARN
code: 417
diff --git a/backend/plugin/advisor/mysql/test/column_comment.yaml b/backend/plugin/advisor/mysql/test/column_comment.yaml
index 17dee44a0e..eb82551753 100644
--- a/backend/plugin/advisor/mysql/test/column_comment.yaml
+++ b/backend/plugin/advisor/mysql/test/column_comment.yaml
@@ -26,18 +26,18 @@
title: column.comment
content: Column `t`.`c` requires comments
line: 4
-- statement: |4-
- CREATE TABLE t(a int COMMENT 'comment');
- ALTER TABLE t ADD COLUMN b int
+- statement: |-
+ CREATE TABLE t(a int COMMENT 'comment');
+ ALTER TABLE t ADD COLUMN b int
want:
- status: WARN
code: 408
title: column.comment
content: Column `t`.`b` requires comments
line: 2
-- statement: |4-
- CREATE TABLE t(a int COMMENT 'this is comment');
- ALTER TABLE t CHANGE COLUMN a b int
+- statement: |-
+ CREATE TABLE t(a int COMMENT 'this is comment');
+ ALTER TABLE t CHANGE COLUMN a b int
want:
- status: WARN
code: 409
@@ -49,9 +49,9 @@
title: column.comment
content: Column `t`.`b` requires comments
line: 2
-- statement: |4-
- CREATE TABLE t(b int COMMENT 'It is comment');
- ALTER TABLE t MODIFY COLUMN b int
+- statement: |-
+ CREATE TABLE t(b int COMMENT 'It is comment');
+ ALTER TABLE t MODIFY COLUMN b int
want:
- status: WARN
code: 409
@@ -63,9 +63,9 @@
title: column.comment
content: Column `t`.`b` requires comments
line: 2
-- statement: |4-
- CREATE TABLE t(b int COMMENT 'It is COMMENT');
- ALTER TABLE t MODIFY COLUMN b int COMMENT 'abcdefghiakljhakljdsfalugelkhnabsdguelkadf'
+- statement: |-
+ CREATE TABLE t(b int COMMENT 'It is COMMENT');
+ ALTER TABLE t MODIFY COLUMN b int COMMENT 'abcdefghiakljhakljdsfalugelkhnabsdguelkadf'
want:
- status: WARN
code: 409
diff --git a/backend/plugin/advisor/mysql/test/column_current_time_count_limit.yaml b/backend/plugin/advisor/mysql/test/column_current_time_count_limit.yaml
index 56f9481f1e..1d31c96f8f 100644
--- a/backend/plugin/advisor/mysql/test/column_current_time_count_limit.yaml
+++ b/backend/plugin/advisor/mysql/test/column_current_time_count_limit.yaml
@@ -5,32 +5,32 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(
- a timestamp default now(),
- b timestamp default current_timestamp(),
- c timestamp default localtime
- )
+- statement: |-
+ CREATE TABLE t(
+ a timestamp default now(),
+ b timestamp default current_timestamp(),
+ c timestamp default localtime
+ )
want:
- status: WARN
code: 418
title: column.current-time-count-limit
content: Table `t` has 3 DEFAULT CURRENT_TIMESTAMP() columns. The count greater than 2.
line: 5
-- statement: |4-
- CREATE TABLE t(
- a timestamp default now() on update localtime,
- b timestamp default current_timestamp() on update localtimestamp()
- )
+- statement: |-
+ CREATE TABLE t(
+ a timestamp default now() on update localtime,
+ b timestamp default current_timestamp() on update localtimestamp()
+ )
want:
- status: WARN
code: 419
title: column.current-time-count-limit
content: Table `t` has 2 ON UPDATE CURRENT_TIMESTAMP() columns. The count greater than 1.
line: 4
-- statement: |4-
- ALTER TABLE tech_book ADD COLUMN a timestamp default now() on update localtime;
- ALTER TABLE tech_book ADD COLUMN b timestamp default current_timestamp() on update localtimestamp();
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN a timestamp default now() on update localtime;
+ ALTER TABLE tech_book ADD COLUMN b timestamp default current_timestamp() on update localtimestamp();
want:
- status: WARN
code: 419
diff --git a/backend/plugin/advisor/mysql/test/column_disallow_change.yaml b/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
index ad6d770554..e3f11e3953 100644
--- a/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
+++ b/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
@@ -1,15 +1,15 @@
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a int
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a int
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t CHANGE COLUMN b a int
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t CHANGE COLUMN b a int
want:
- status: WARN
code: 406
diff --git a/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml b/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
index d7cfe4801d..f7ec6d05f9 100644
--- a/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
+++ b/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
@@ -1,42 +1,42 @@
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a int
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a int
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a int FIRST
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a int FIRST
want:
- status: WARN
code: 407
title: column.disallow-changing-order
content: '"ALTER TABLE t MODIFY COLUMN a int FIRST" changes column order'
line: 2
-- statement: |4-
- CREATE TABLE t(b int, a1 int);
- ALTER TABLE t CHANGE COLUMN a1 a int FIRST
+- statement: |-
+ CREATE TABLE t(b int, a1 int);
+ ALTER TABLE t CHANGE COLUMN a1 a int FIRST
want:
- status: WARN
code: 407
title: column.disallow-changing-order
content: '"ALTER TABLE t CHANGE COLUMN a1 a int FIRST" changes column order'
line: 2
-- statement: |4-
- CREATE TABLE t(a int, b int);
- ALTER TABLE t MODIFY COLUMN a int AFTER b
+- statement: |-
+ CREATE TABLE t(a int, b int);
+ ALTER TABLE t MODIFY COLUMN a int AFTER b
want:
- status: WARN
code: 407
title: column.disallow-changing-order
content: '"ALTER TABLE t MODIFY COLUMN a int AFTER b" changes column order'
line: 2
-- statement: |4-
- CREATE TABLE t(a1 int, b int);
- ALTER TABLE t CHANGE COLUMN a1 a int AFTER b
+- statement: |-
+ CREATE TABLE t(a1 int, b int);
+ ALTER TABLE t CHANGE COLUMN a1 a int AFTER b
want:
- status: WARN
code: 407
diff --git a/backend/plugin/advisor/mysql/test/column_no_null.yaml b/backend/plugin/advisor/mysql/test/column_no_null.yaml
index 0848ac06f1..1ab9977242 100644
--- a/backend/plugin/advisor/mysql/test/column_no_null.yaml
+++ b/backend/plugin/advisor/mysql/test/column_no_null.yaml
@@ -34,36 +34,36 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE book(a int NOT NULL);
- ALTER TABLE book ADD COLUMN (id int, name varchar(255) NOT NULL);
+- statement: |-
+ CREATE TABLE book(a int NOT NULL);
+ ALTER TABLE book ADD COLUMN (id int, name varchar(255) NOT NULL);
want:
- status: WARN
code: 402
title: column.no-null
content: '`book`.`id` cannot have NULL value'
line: 2
-- statement: |4-
- CREATE TABLE book(a int NOT NULL);
- ALTER TABLE book ADD COLUMN (id int PRIMARY KEY, name varchar(255) NOT NULL);
+- statement: |-
+ CREATE TABLE book(a int NOT NULL);
+ ALTER TABLE book ADD COLUMN (id int PRIMARY KEY, name varchar(255) NOT NULL);
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE book(id int NOT NULL);
- ALTER TABLE book CHANGE COLUMN id name varchar(255);
+- statement: |-
+ CREATE TABLE book(id int NOT NULL);
+ ALTER TABLE book CHANGE COLUMN id name varchar(255);
want:
- status: WARN
code: 402
title: column.no-null
content: '`book`.`name` cannot have NULL value'
line: 2
-- statement: |4-
- CREATE TABLE book(id int NOT NULL);
- ALTER TABLE book CHANGE COLUMN id name varchar(255) NOT NULL;
+- statement: |-
+ CREATE TABLE book(id int NOT NULL);
+ ALTER TABLE book CHANGE COLUMN id name varchar(255) NOT NULL;
want:
- status: SUCCESS
code: 0
diff --git a/backend/plugin/advisor/mysql/test/column_require_default.yaml b/backend/plugin/advisor/mysql/test/column_require_default.yaml
index 3d54a477b5..3e02ab3101 100644
--- a/backend/plugin/advisor/mysql/test/column_require_default.yaml
+++ b/backend/plugin/advisor/mysql/test/column_require_default.yaml
@@ -5,21 +5,21 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(
- a int,
- b int default 1
- )
+- statement: |-
+ CREATE TABLE t(
+ a int,
+ b int default 1
+ )
want:
- status: WARN
code: 420
title: column.require-default
content: Column `t`.`a` doesn't have DEFAULT.
line: 2
-- statement: |4-
- ALTER TABLE tech_book ADD COLUMN a BLOB;
- ALTER TABLE tech_book ADD COLUMN b timestamp;
- ALTER TABLE tech_book MODIFY COLUMN a varchar(220);
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN a BLOB;
+ ALTER TABLE tech_book ADD COLUMN b timestamp;
+ ALTER TABLE tech_book MODIFY COLUMN a varchar(220);
want:
- status: WARN
code: 420
diff --git a/backend/plugin/advisor/mysql/test/column_set_default_for_not_null.yaml b/backend/plugin/advisor/mysql/test/column_set_default_for_not_null.yaml
index ce85bd3051..29af326aec 100644
--- a/backend/plugin/advisor/mysql/test/column_set_default_for_not_null.yaml
+++ b/backend/plugin/advisor/mysql/test/column_set_default_for_not_null.yaml
@@ -39,54 +39,54 @@
title: column.set-default-for-not-null
content: Column `book`.`id` is NOT NULL but doesn't have DEFAULT
line: 2
-- statement: |4-
- CREATE TABLE book(a int);
- ALTER TABLE book ADD COLUMN id int PRIMARY KEY
+- statement: |-
+ CREATE TABLE book(a int);
+ ALTER TABLE book ADD COLUMN id int PRIMARY KEY
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE book(a int);
- ALTER TABLE book ADD COLUMN id int NOT NULL
+- statement: |-
+ CREATE TABLE book(a int);
+ ALTER TABLE book ADD COLUMN id int NOT NULL
want:
- status: WARN
code: 404
title: column.set-default-for-not-null
content: Column `book`.`id` is NOT NULL but doesn't have DEFAULT
line: 2
-- statement: |4-
- CREATE TABLE book(id int);
- ALTER TABLE book MODIFY COLUMN id int NOT NULL
+- statement: |-
+ CREATE TABLE book(id int);
+ ALTER TABLE book MODIFY COLUMN id int NOT NULL
want:
- status: WARN
code: 404
title: column.set-default-for-not-null
content: Column `book`.`id` is NOT NULL but doesn't have DEFAULT
line: 2
-- statement: |4-
- CREATE TABLE book(id int);
- ALTER TABLE book MODIFY COLUMN id int PRIMARY KEY
+- statement: |-
+ CREATE TABLE book(id int);
+ ALTER TABLE book MODIFY COLUMN id int PRIMARY KEY
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE book(uid int);
- ALTER TABLE book CHANGE COLUMN uid id int PRIMARY KEY
+- statement: |-
+ CREATE TABLE book(uid int);
+ ALTER TABLE book CHANGE COLUMN uid id int PRIMARY KEY
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE book(uid int);
- ALTER TABLE book CHANGE COLUMN uid id int NOT NULL
+- statement: |-
+ CREATE TABLE book(uid int);
+ ALTER TABLE book CHANGE COLUMN uid id int NOT NULL
want:
- status: WARN
code: 404
diff --git a/backend/plugin/advisor/mysql/test/column_type_disallow_list.yaml b/backend/plugin/advisor/mysql/test/column_type_disallow_list.yaml
index 620066096b..1f90918f1c 100644
--- a/backend/plugin/advisor/mysql/test/column_type_disallow_list.yaml
+++ b/backend/plugin/advisor/mysql/test/column_type_disallow_list.yaml
@@ -12,18 +12,18 @@
title: column.type-disallow-list
content: Disallow column type JSON but column `t`.`a` is
line: 1
-- statement: |4-
- CREATE TABLE t(d int);
- ALTER TABLE t add COLUMN (a JSON, b BLOB, c TEXT)
+- statement: |-
+ CREATE TABLE t(d int);
+ ALTER TABLE t add COLUMN (a JSON, b BLOB, c TEXT)
want:
- status: WARN
code: 411
title: column.type-disallow-list
content: Disallow column type JSON but column `t`.`a` is
line: 2
-- statement: |4-
- CREATE TABLE t(c int, b int);
- ALTER TABLE t CHANGE COLUMN c a JSON, MODIFY b BLOB
+- statement: |-
+ CREATE TABLE t(c int, b int);
+ ALTER TABLE t CHANGE COLUMN c a JSON, MODIFY b BLOB
want:
- status: WARN
code: 411
diff --git a/backend/plugin/advisor/mysql/test/index_key_number_limit.yaml b/backend/plugin/advisor/mysql/test/index_key_number_limit.yaml
index 64cf0f02d2..1cd22615c2 100644
--- a/backend/plugin/advisor/mysql/test/index_key_number_limit.yaml
+++ b/backend/plugin/advisor/mysql/test/index_key_number_limit.yaml
@@ -16,27 +16,27 @@
title: index.key-number-limit
content: The number of index `pk` in table `t` should be not greater than 5
line: 4
-- statement: |4-
- CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
- CREATE INDEX idx ON t(a, b, c, d, e, f, g)
+- statement: |-
+ CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
+ CREATE INDEX idx ON t(a, b, c, d, e, f, g)
want:
- status: WARN
code: 802
title: index.key-number-limit
content: The number of index `idx` in table `t` should be not greater than 5
line: 2
-- statement: |4-
- CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
- ALTER TABLE t ADD UNIQUE KEY uk (a, b, c, d, e, f, g)
+- statement: |-
+ CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
+ ALTER TABLE t ADD UNIQUE KEY uk (a, b, c, d, e, f, g)
want:
- status: WARN
code: 802
title: index.key-number-limit
content: The number of index `uk` in table `t` should be not greater than 5
line: 2
-- statement: |4-
- CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
- ALTER TABLE t ADD FOREIGN KEY uk (a, b, c, d, e, f, g) REFERENCES t1(a, b, c, d, e, f, g)
+- statement: |-
+ CREATE TABLE t(a int, b int, c int, d int, e int, f int, g int);
+ ALTER TABLE t ADD FOREIGN KEY uk (a, b, c, d, e, f, g) REFERENCES t1(a, b, c, d, e, f, g)
want:
- status: WARN
code: 802
diff --git a/backend/plugin/advisor/mysql/test/index_no_duplicate_column.yaml b/backend/plugin/advisor/mysql/test/index_no_duplicate_column.yaml
index 0ce0a3e342..62045e5c5d 100644
--- a/backend/plugin/advisor/mysql/test/index_no_duplicate_column.yaml
+++ b/backend/plugin/advisor/mysql/test/index_no_duplicate_column.yaml
@@ -15,45 +15,45 @@
title: index.no-duplicate-column
content: INDEX `idx_a` has duplicate column `t`.`a`
line: 3
-- statement: |4-
- CREATE TABLE t(a int);
- CREATE INDEX idx_a on t(a, a)
+- statement: |-
+ CREATE TABLE t(a int);
+ CREATE INDEX idx_a on t(a, a)
want:
- status: WARN
code: 812
title: index.no-duplicate-column
content: INDEX `idx_a` has duplicate column `t`.`a`
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD INDEX idx_a (a, a)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD INDEX idx_a (a, a)
want:
- status: WARN
code: 812
title: index.no-duplicate-column
content: INDEX `idx_a` has duplicate column `t`.`a`
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD PRIMARY KEY pk_a (a, a)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD PRIMARY KEY pk_a (a, a)
want:
- status: WARN
code: 812
title: index.no-duplicate-column
content: PRIMARY KEY `pk_a` has duplicate column `t`.`a`
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD UNIQUE KEY uk_a (a, a)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD UNIQUE KEY uk_a (a, a)
want:
- status: WARN
code: 812
title: index.no-duplicate-column
content: UNIQUE KEY `uk_a` has duplicate column `t`.`a`
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD FOREIGN KEY fk_a (a, a) REFERENCES t1(a, b)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD FOREIGN KEY fk_a (a, a) REFERENCES t1(a, b)
want:
- status: WARN
code: 812
diff --git a/backend/plugin/advisor/mysql/test/index_pk_type_limit.yaml b/backend/plugin/advisor/mysql/test/index_pk_type_limit.yaml
index 92c82d8bcb..2817902c47 100644
--- a/backend/plugin/advisor/mysql/test/index_pk_type_limit.yaml
+++ b/backend/plugin/advisor/mysql/test/index_pk_type_limit.yaml
@@ -45,36 +45,36 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id VARCHAR(5) PRIMARY KEY
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id VARCHAR(5) PRIMARY KEY
want:
- status: WARN
code: 803
title: index.pk-type-limit
content: Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id INT PRIMARY KEY
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT PRIMARY KEY
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id VARCHAR(5), ADD PRIMARY KEY (id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id VARCHAR(5), ADD PRIMARY KEY (id)
want:
- status: WARN
code: 803
title: index.pk-type-limit
content: Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 VARCHAR(5), ADD COLUMN id3 VARCHAR(5), ADD PRIMARY KEY (id, id2, id3)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 VARCHAR(5), ADD COLUMN id3 VARCHAR(5), ADD PRIMARY KEY (id, id2, id3)
want:
- status: WARN
code: 803
@@ -86,18 +86,18 @@
title: index.pk-type-limit
content: Columns in primary key must be INT/BIGINT but `t`.`id3` is varchar(5)
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 BIGINT, ADD PRIMARY KEY (id, id2)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 BIGINT, ADD PRIMARY KEY (id, id2)
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN id BIGINT, ADD PRIMARY KEY (id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id BIGINT, ADD PRIMARY KEY (id)
want:
- status: SUCCESS
code: 0
@@ -145,36 +145,36 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(id int);
- ALTER TABLE t MODIFY COLUMN id VARCHAR(5) PRIMARY KEY;
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t MODIFY COLUMN id VARCHAR(5) PRIMARY KEY;
want:
- status: WARN
code: 803
title: index.pk-type-limit
content: Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)
line: 2
-- statement: |4-
- CREATE TABLE t(id int);
- ALTER TABLE t MODIFY COLUMN id INT(5) PRIMARY KEY;
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t MODIFY COLUMN id INT(5) PRIMARY KEY;
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(id int);
- ALTER TABLE t CHANGE COLUMN id id2 VARCHAR(5) PRIMARY KEY
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t CHANGE COLUMN id id2 VARCHAR(5) PRIMARY KEY
want:
- status: WARN
code: 803
title: index.pk-type-limit
content: Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)
line: 2
-- statement: |4-
- CREATE TABLE t(id int);
- ALTER TABLE t CHANGE COLUMN id id2 INT(5) PRIMARY KEY
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t CHANGE COLUMN id id2 INT(5) PRIMARY KEY
want:
- status: SUCCESS
code: 0
diff --git a/backend/plugin/advisor/mysql/test/index_primary_key_type_allowlist.yaml b/backend/plugin/advisor/mysql/test/index_primary_key_type_allowlist.yaml
new file mode 100644
index 0000000000..94806560dd
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/index_primary_key_type_allowlist.yaml
@@ -0,0 +1,183 @@
+- statement: CREATE TABLE t(id VARCHAR(5) PRIMARY KEY)
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 1
+- statement: CREATE TABLE t(id INT PRIMARY KEY)
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: CREATE TABLE t(id VARCHAR(5), PRIMARY KEY(id))
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 1
+- statement: CREATE TABLE t(id INT, id2 VARCHAR(5), id3 VARCHAR(5), PRIMARY KEY(id, id2, id3))
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id2` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 1
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id3` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 1
+- statement: CREATE TABLE t(id INT, id2 BIGINT, PRIMARY KEY(id, id2))
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: CREATE TABLE t(id INT, PRIMARY KEY(id))
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id VARCHAR(5) PRIMARY KEY
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT PRIMARY KEY
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id VARCHAR(5), ADD PRIMARY KEY (id)
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 VARCHAR(5), ADD COLUMN id3 VARCHAR(5), ADD PRIMARY KEY (id, id2, id3)
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id2` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id3` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id INT, ADD COLUMN id2 BIGINT, ADD PRIMARY KEY (id, id2)
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN id BIGINT, ADD PRIMARY KEY (id)
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(id VARCHAR(5));
+ ALTER TABLE t ADD PRIMARY KEY(id);
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(id INT, id2 VARCHAR(5), id3 VARCHAR(5));
+ ALTER TABLE t ADD PRIMARY KEY(id, id2, id3);
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id2` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id3` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(id INT, id2 BIGINT);
+ ALTER TABLE t ADD PRIMARY KEY(id, id2);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(id BIGINT);
+ ALTER TABLE t ADD PRIMARY KEY(id);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t MODIFY COLUMN id VARCHAR(5) PRIMARY KEY;
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t MODIFY COLUMN id INT(5) PRIMARY KEY;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t CHANGE COLUMN id id2 VARCHAR(5) PRIMARY KEY
+ want:
+ - status: WARN
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column `id2` in table `t` is one of the primary key, but its type "varchar" is not in allowlist
+ line: 2
+- statement: |-
+ CREATE TABLE t(id int);
+ ALTER TABLE t CHANGE COLUMN id id2 INT(5) PRIMARY KEY
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
diff --git a/backend/plugin/advisor/mysql/test/index_total_number_limit.yaml b/backend/plugin/advisor/mysql/test/index_total_number_limit.yaml
index c960a19a0a..45edd91c35 100644
--- a/backend/plugin/advisor/mysql/test/index_total_number_limit.yaml
+++ b/backend/plugin/advisor/mysql/test/index_total_number_limit.yaml
@@ -12,41 +12,41 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE INDEX idx_id_name_1 on tech_book(id, name);
- CREATE INDEX idx_id_name_2 on tech_book(id, name);
- CREATE INDEX idx_id_name_3 on tech_book(id, name);
- CREATE INDEX idx_id_name_4 on tech_book(id, name);
- CREATE INDEX idx_id_name_5 on tech_book(id, name);
+- statement: |-
+ CREATE INDEX idx_id_name_1 on tech_book(id, name);
+ CREATE INDEX idx_id_name_2 on tech_book(id, name);
+ CREATE INDEX idx_id_name_3 on tech_book(id, name);
+ CREATE INDEX idx_id_name_4 on tech_book(id, name);
+ CREATE INDEX idx_id_name_5 on tech_book(id, name);
want:
- status: WARN
code: 813
title: index.total-number-limit
content: The count of index in table `tech_book` should be no more than 5, but found 8
line: 5
-- statement: |4-
- CREATE TABLE t(a int PRIMARY KEY, b int, c int);
- CREATE INDEX idx_a_b_1 on t(a, b);
- CREATE INDEX idx_a_b_2 on t(a, b);
- CREATE INDEX idx_a_b_3 on t(a, b);
- CREATE INDEX idx_a_b_4 on t(a, b);
- CREATE INDEX idx_a_b_5 on t(a, b);
+- statement: |-
+ CREATE TABLE t(a int PRIMARY KEY, b int, c int);
+ CREATE INDEX idx_a_b_1 on t(a, b);
+ CREATE INDEX idx_a_b_2 on t(a, b);
+ CREATE INDEX idx_a_b_3 on t(a, b);
+ CREATE INDEX idx_a_b_4 on t(a, b);
+ CREATE INDEX idx_a_b_5 on t(a, b);
want:
- status: WARN
code: 813
title: index.total-number-limit
content: The count of index in table `t` should be no more than 5, but found 6
line: 6
-- statement: |4-
- CREATE TABLE t(
- a int PRIMARY KEY,
- b int,
- c int,
- INDEX idx_a_b_1 (a, b),
- INDEX idx_a_b_2 (a, b),
- INDEX idx_a_b_3 (a, b),
- INDEX idx_a_b_4 (a, b),
- INDEX idx_a_b_5 (a, b));
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY,
+ b int,
+ c int,
+ INDEX idx_a_b_1 (a, b),
+ INDEX idx_a_b_2 (a, b),
+ INDEX idx_a_b_3 (a, b),
+ INDEX idx_a_b_4 (a, b),
+ INDEX idx_a_b_5 (a, b));
want:
- status: WARN
code: 813
diff --git a/backend/plugin/advisor/mysql/test/index_type_no_blob.yaml b/backend/plugin/advisor/mysql/test/index_type_no_blob.yaml
index 682302a183..dc2d342e28 100644
--- a/backend/plugin/advisor/mysql/test/index_type_no_blob.yaml
+++ b/backend/plugin/advisor/mysql/test/index_type_no_blob.yaml
@@ -63,18 +63,18 @@
title: OK
content: ""
line: 0
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN id INT, ADD PRIMARY KEY(b(1), id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN id INT, ADD PRIMARY KEY(b(1), id)
want:
- status: WARN
code: 804
title: index.type-no-blob
content: Columns in index must not be BLOB but `t`.`b` is blob
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD UNIQUE INDEX(b(1), mb(2), lb(3), id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD UNIQUE INDEX(b(1), mb(2), lb(3), id)
want:
- status: WARN
code: 804
@@ -91,9 +91,9 @@
title: index.type-no-blob
content: Columns in index must not be BLOB but `t`.`lb` is longblob
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD INDEX(b(1), mb(2), lb(3), id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD INDEX(b(1), mb(2), lb(3), id)
want:
- status: WARN
code: 804
@@ -110,9 +110,9 @@
title: index.type-no-blob
content: Columns in index must not be BLOB but `t`.`lb` is longblob
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD INDEX(id)
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD COLUMN b BLOB, ADD COLUMN mb MEDIUMBLOB, ADD COLUMN lb LONGBLOB, ADD COLUMN id INT, ADD INDEX(id)
want:
- status: SUCCESS
code: 0
diff --git a/backend/plugin/advisor/mysql/test/statement_merge_alter_table.yaml b/backend/plugin/advisor/mysql/test/statement_merge_alter_table.yaml
index a878c9ae74..d33f1bfc34 100644
--- a/backend/plugin/advisor/mysql/test/statement_merge_alter_table.yaml
+++ b/backend/plugin/advisor/mysql/test/statement_merge_alter_table.yaml
@@ -5,20 +5,20 @@
title: OK
content: ""
line: 0
-- statement: |4-
- ALTER TABLE tech_book ADD COLUMN a int;
- ALTER TABLE tech_book ADD COLUMN b int;
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN a int;
+ ALTER TABLE tech_book ADD COLUMN b int;
want:
- status: WARN
code: 207
title: statement.merge-alter-table
content: There are 2 statements to modify table `tech_book`
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE tech_book ADD COLUMN a int;
- ALTER TABLE t ADD COLUMN b int;
- ALTER TABLE tech_book ADD COLUMN b int;
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE tech_book ADD COLUMN a int;
+ ALTER TABLE t ADD COLUMN b int;
+ ALTER TABLE tech_book ADD COLUMN b int;
want:
- status: WARN
code: 207
@@ -30,11 +30,11 @@
title: statement.merge-alter-table
content: There are 2 statements to modify table `tech_book`
line: 4
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE tech_book ADD COLUMN a int;
- ALTER TABLE tech_book ADD COLUMN b int;
- ALTER TABLE t ADD COLUMN b int;
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE tech_book ADD COLUMN a int;
+ ALTER TABLE tech_book ADD COLUMN b int;
+ ALTER TABLE t ADD COLUMN b int;
want:
- status: WARN
code: 207
diff --git a/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml b/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
index 9635c42a58..ef9e429aae 100644
--- a/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
+++ b/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
@@ -19,9 +19,9 @@
title: system.charset.allowlist
content: '"CREATE TABLE t(a int) CHARSET ascii" used disabled charset ''ascii'''
line: 1
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t CHARSET ascii
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t CHARSET ascii
want:
- status: WARN
code: 1001
@@ -42,27 +42,27 @@
title: system.charset.allowlist
content: '"CREATE TABLE t(a varchar(255) CHARSET ascii)" used disabled charset ''ascii'''
line: 1
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii
want:
- status: WARN
code: 1001
title: system.charset.allowlist
content: '"ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii" used disabled charset ''ascii'''
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii
want:
- status: WARN
code: 1001
title: system.charset.allowlist
content: '"ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii" used disabled charset ''ascii'''
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii
want:
- status: WARN
code: 1001
diff --git a/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml b/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
index fabe7e8476..ce69200e64 100644
--- a/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
+++ b/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
@@ -19,9 +19,9 @@
title: system.collation.allowlist
content: '"CREATE TABLE t(a int) COLLATE latin1_bin" used disabled collation ''latin1_bin'''
line: 1
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t COLLATE latin1_bin
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t COLLATE latin1_bin
want:
- status: WARN
code: 1201
@@ -42,27 +42,27 @@
title: system.collation.allowlist
content: '"CREATE TABLE t(a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin)" used disabled collation ''latin1_bin'''
line: 1
-- statement: |4-
- CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
want:
- status: WARN
code: 1201
title: system.collation.allowlist
content: '"ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin" used disabled collation ''latin1_bin'''
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
want:
- status: WARN
code: 1201
title: system.collation.allowlist
content: '"ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin" used disabled collation ''latin1_bin'''
line: 2
-- statement: |4-
- CREATE TABLE t(a int);
- ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
want:
- status: WARN
code: 1201
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 25d3423a99..e447da6bb9 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -917,7 +917,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return MySQLIndexTypeNoBlob, nil
}
case SchemaRuleIndexPrimaryKeyTypeAllowlist:
- if engine == db.Postgres {
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLPrimaryKeyTypeAllowlist, nil
+ case db.Postgres:
return PostgreSQLPrimaryKeyTypeAllowlist, nil
}
case SchemaRuleCreateIndexConcurrently:
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 31cccffdcb..3cc992e2f6 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -470,7 +470,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {
})
case SchemaRuleIndexPrimaryKeyTypeAllowlist:
payload, err = json.Marshal(StringArrayTypeRulePayload{
- List: []string{"serial", "bigserial"},
+ List: []string{"serial", "bigserial", "int", "bigint"},
})
default:
return "", errors.Errorf("unknown SQL review type for default payload: %s", ruleTp)
diff --git a/backend/plugin/parser/utils.go b/backend/plugin/parser/utils.go
index 6104bf4561..6424e65edc 100644
--- a/backend/plugin/parser/utils.go
+++ b/backend/plugin/parser/utils.go
@@ -11,6 +11,7 @@ import (
tidbparser "github.com/pingcap/tidb/parser"
tidbast "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
+ "github.com/pingcap/tidb/parser/mysql"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/parser/ast"
@@ -200,6 +201,61 @@ func ExtractDelimiter(stmt string) (string, error) {
return "", errors.Errorf("cannot extract delimiter from %q", stmt)
}
+// TypeString returns the string representation of the type for MySQL.
+func TypeString(tp byte) string {
+ switch tp {
+ case mysql.TypeTiny:
+ return "tinyint"
+ case mysql.TypeShort:
+ return "smallint"
+ case mysql.TypeInt24:
+ return "mediumint"
+ case mysql.TypeLong:
+ return "int"
+ case mysql.TypeLonglong:
+ return "bigint"
+ case mysql.TypeFloat:
+ return "float"
+ case mysql.TypeDouble:
+ return "double"
+ case mysql.TypeNewDecimal:
+ return "decimal"
+ case mysql.TypeVarchar:
+ return "varchar"
+ case mysql.TypeBit:
+ return "bit"
+ case mysql.TypeTimestamp:
+ return "timestamp"
+ case mysql.TypeDatetime:
+ return "datetime"
+ case mysql.TypeDate:
+ return "date"
+ case mysql.TypeDuration:
+ return "time"
+ case mysql.TypeJSON:
+ return "json"
+ case mysql.TypeEnum:
+ return "enum"
+ case mysql.TypeSet:
+ return "set"
+ case mysql.TypeTinyBlob:
+ return "tinyblob"
+ case mysql.TypeMediumBlob:
+ return "mediumblob"
+ case mysql.TypeLongBlob:
+ return "longblob"
+ case mysql.TypeBlob:
+ return "blob"
+ case mysql.TypeVarString:
+ return "varbinary"
+ case mysql.TypeString:
+ return "binary"
+ case mysql.TypeGeometry:
+ return "geometry"
+ }
+ return "unknown"
+}
+
// ExtractDatabaseList extracts all databases from statement.
func ExtractDatabaseList(engineType EngineType, statement string) ([]string, error) {
switch engineType {