Skip to content

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

chore(sql-review): re-implement column no null and fix all MySQL advisor unit tests (#2735)

rebelicec3f388e1b792
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 896fcc0737..a50485223e 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -94,9 +94,6 @@ const (
// 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"
-
// MySQLColumnDisallowChangingOrder is an advisor type for MySQL disallow changing column order.
MySQLColumnDisallowChangingOrder Type = "bb.plugin.advisor.mysql.column.disallow-changing-order"
@@ -121,18 +118,12 @@ const (
// MySQLTableDropNamingConvention is an advisor type for MySQL table drop with naming convention.
MySQLTableDropNamingConvention Type = "bb.plugin.advisor.mysql.table.drop-naming-convention"
- // MySQLTableExists is an advisor type for MySQL table existence.
- MySQLTableExists Type = "bb.plugin.advisor.mysql.table.exists"
-
// MySQLTableCommentConvention is an advisor for MySQL table comment convention.
MySQLTableCommentConvention Type = "bb.plugin.advisor.mysql.table.comment"
// MySQLTableDisallowCreateTableAs is an advisor type for MySQL disallow CREATE TABLE ... AS ... statement.
MySQLTableDisallowCreateTableAs Type = "bb.plugin.advisor.mysql.table.disallow-create-table-as"
- // MySQLTableNotExists is an advisor for MySQL table name conflict check.
- MySQLTableNotExists Type = "bb.plugin.advisor.mysql.table.not-exists"
-
// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.
MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"
@@ -145,9 +136,6 @@ const (
// MySQLIndexKeyNumberLimit is an advisor type for MySQL index key number limit.
MySQLIndexKeyNumberLimit Type = "bb.plugin.advisor.mysql.index.key-number-limit"
- // MySQLIndexNotExists is an advisor type for MySQL index name conflict check.
- MySQLIndexNotExists Type = "bb.plugin.advisor.mysql.index.not-exists"
-
// MySQLCharsetAllowlist is an advisor type for MySQL charset allowlist.
MySQLCharsetAllowlist Type = "bb.plugin.advisor.mysql.charset.allowlist"
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index 6ed136da20..873339fa4c 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -1105,6 +1105,12 @@ func (d *databaseState) parse(stmts string) ([]tidbast.StmtNode, *WalkThroughErr
if err != nil {
return nil, NewParseError(err.Error())
}
+
+ // sikp the setting line stage
+ if len(nodeList) == 0 {
+ return nodeList, nil
+ }
+
sqlList, err := parser.SplitMultiSQL(parser.MySQL, stmts)
if err != nil {
return nil, NewParseError(err.Error())
diff --git a/plugin/advisor/mysql/advisor_charset_allowlist_test.go b/plugin/advisor/mysql/advisor_charset_allowlist_test.go
index 0741b3aec9..4a6f684550 100644
--- a/plugin/advisor/mysql/advisor_charset_allowlist_test.go
+++ b/plugin/advisor/mysql/advisor_charset_allowlist_test.go
@@ -48,37 +48,27 @@ func TestCharsetAllowlist(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE t CHARSET ascii`,
+ Statement: `
+ CREATE TABLE t(a int);
+ ALTER TABLE t CHARSET ascii`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
Title: "charset.allowlist",
Content: "\"ALTER TABLE t CHARSET ascii\" used disabled charset 'ascii'",
- Line: 1,
- },
- },
- },
- {
- Statement: `CREATE DATABASE d CHARSET ascii`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
- Content: "\"CREATE DATABASE d CHARSET ascii\" used disabled charset 'ascii'",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `ALTER DATABASE d CHARSET ascii`,
+ Statement: `ALTER DATABASE test CHARSET ascii`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
Title: "charset.allowlist",
- Content: "\"ALTER DATABASE d CHARSET ascii\" used disabled charset 'ascii'",
+ Content: "\"ALTER DATABASE test CHARSET ascii\" used disabled charset 'ascii'",
Line: 1,
},
},
@@ -96,38 +86,44 @@ func TestCharsetAllowlist(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
Title: "charset.allowlist",
Content: "\"ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
Title: "charset.allowlist",
Content: "\"ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
Title: "charset.allowlist",
Content: "\"ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go
index f129bcf410..e6110e6f16 100644
--- a/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go
+++ b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go
@@ -34,38 +34,44 @@ func TestColumnAutoIncrementMustInteger(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.AutoIncrementColumnNotInteger,
Title: "column.auto-increment-must-integer",
Content: "Auto-increment column `t`.`a` requires integer type",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.AutoIncrementColumnNotInteger,
Title: "column.auto-increment-must-integer",
Content: "Auto-increment column `t`.`a` requires integer type",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.AutoIncrementColumnNotInteger,
Title: "column.auto-increment-must-integer",
Content: "Auto-increment column `t`.`a` requires integer type",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_comment_convention_test.go b/plugin/advisor/mysql/advisor_column_comment_convention_test.go
index 3f71810a3c..d1717ce095 100644
--- a/plugin/advisor/mysql/advisor_column_comment_convention_test.go
+++ b/plugin/advisor/mysql/advisor_column_comment_convention_test.go
@@ -47,50 +47,58 @@ func TestColumnCommentConvention(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE t ADD COLUMN b int`,
+ Statement: `
+ CREATE TABLE t(a int COMMENT 'comment');
+ ALTER TABLE t ADD COLUMN b int`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
Title: "column.comment",
Content: "Column `t`.`b` requires comments",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
Title: "column.comment",
Content: "Column `t`.`b` requires comments",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
Title: "column.comment",
Content: "Column `t`.`b` requires comments",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ColumnCommentTooLong,
Title: "column.comment",
Content: "The length of column `t`.`b` comment should be within 20 characters",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_order_test.go b/plugin/advisor/mysql/advisor_column_disallow_changing_order_test.go
index 447b766828..ee8b9d8bed 100644
--- a/plugin/advisor/mysql/advisor_column_disallow_changing_order_test.go
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_order_test.go
@@ -11,7 +11,9 @@ import (
func TestColumnDisallowChangingOrder(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: `ALTER TABLE t MODIFY COLUMN a int`,
+ Statement: `
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a int`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -22,50 +24,58 @@ func TestColumnDisallowChangingOrder(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE t MODIFY COLUMN a int FIRST`,
+ Statement: `
+ CREATE TABLE t(a int);
+ ALTER TABLE t MODIFY COLUMN a int FIRST`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ChangeColumnOrder,
Title: "column.disallow-changing-order",
Content: "\"ALTER TABLE t MODIFY COLUMN a int FIRST\" changes column order",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ChangeColumnOrder,
Title: "column.disallow-changing-order",
Content: "\"ALTER TABLE t CHANGE COLUMN a1 a int FIRST\" changes column order",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ChangeColumnOrder,
Title: "column.disallow-changing-order",
Content: "\"ALTER TABLE t MODIFY COLUMN a int AFTER b\" changes column order",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ChangeColumnOrder,
Title: "column.disallow-changing-order",
Content: "\"ALTER TABLE t CHANGE COLUMN a1 a int AFTER b\" changes column order",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_test.go b/plugin/advisor/mysql/advisor_column_disallow_changing_test.go
index 596d001c91..b9ba66c87b 100644
--- a/plugin/advisor/mysql/advisor_column_disallow_changing_test.go
+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_test.go
@@ -11,7 +11,9 @@ import (
func TestColumnDisallowChanging(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: `ALTER TABLE t ADD COLUMN a int`,
+ Statement: `
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a int`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -22,14 +24,16 @@ func TestColumnDisallowChanging(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE t CHANGE COLUMN b a int`,
+ Statement: `
+ CREATE TABLE t(b int);
+ 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,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_no_null.go b/plugin/advisor/mysql/advisor_column_no_null.go
index 16697f6466..9ffeee2f7f 100644
--- a/plugin/advisor/mysql/advisor_column_no_null.go
+++ b/plugin/advisor/mysql/advisor_column_no_null.go
@@ -2,10 +2,12 @@ package mysql
import (
"fmt"
+ "sort"
"github.com/pingcap/tidb/parser/ast"
"github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/bytebase/bytebase/plugin/advisor/db"
)
@@ -35,14 +37,55 @@ func (*ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]advi
return nil, err
}
checker := &columnNoNullChecker{
- level: level,
- title: string(ctx.Rule.Type),
+ level: level,
+ title: string(ctx.Rule.Type),
+ columnSet: make(map[string]columnName),
+ catalog: ctx.Catalog,
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
}
+ return checker.generateAdvice(), nil
+}
+
+type columnNoNullChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ columnSet map[string]columnName
+ catalog *catalog.Finder
+}
+
+func (checker columnNoNullChecker) generateAdvice() []advisor.Advice {
+ var columnList []columnName
+ for _, column := range checker.columnSet {
+ columnList = append(columnList, column)
+ }
+ sort.Slice(columnList, func(i, j int) bool {
+ if columnList[i].line != columnList[j].line {
+ return columnList[i].line < columnList[j].line
+ }
+ return columnList[i].columnName < columnList[j].columnName
+ })
+
+ for _, column := range columnList {
+ col := checker.catalog.Final.FindColumn(&catalog.ColumnFind{
+ TableName: column.tableName,
+ ColumnName: column.columnName,
+ })
+ if col != nil && col.Nullable {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.ColumnCanNotNull,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s`.`%s` can not have NULL value", column.tableName, column.columnName),
+ Line: column.line,
+ })
+ }
+ }
+
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
@@ -51,13 +94,7 @@ func (*ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]advi
Content: "",
})
}
- return checker.adviceList, nil
-}
-
-type columnNoNullChecker struct {
- adviceList []advisor.Advice
- level advisor.Status
- title string
+ return checker.adviceList
}
type columnName struct {
@@ -66,19 +103,23 @@ type columnName struct {
line int
}
+func (c columnName) name() string {
+ return fmt.Sprintf("%s.%s", c.tableName, c.columnName)
+}
+
// Enter implements the ast.Visitor interface.
-func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
- var columns []columnName
+func (checker *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
switch node := in.(type) {
// CREATE TABLE
case *ast.CreateTableStmt:
for _, column := range node.Cols {
- if canNull(column) {
- columns = append(columns, columnName{
- tableName: node.Table.Name.String(),
- columnName: column.Name.Name.String(),
- line: column.OriginTextPosition(),
- })
+ col := columnName{
+ tableName: node.Table.Name.O,
+ columnName: column.Name.Name.O,
+ line: column.OriginTextPosition(),
+ }
+ if _, exists := checker.columnSet[col.name()]; !exists {
+ checker.columnSet[col.name()] = col
}
}
// ALTER TABLE
@@ -88,37 +129,29 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
// ADD COLUMNS
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
- if canNull(column) {
- columns = append(columns, columnName{
- tableName: node.Table.Name.String(),
- columnName: column.Name.Name.String(),
- line: node.OriginTextPosition(),
- })
+ col := columnName{
+ tableName: node.Table.Name.O,
+ columnName: column.Name.Name.O,
+ line: node.OriginTextPosition(),
+ }
+ if _, exists := checker.columnSet[col.name()]; !exists {
+ checker.columnSet[col.name()] = col
}
}
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- if canNull(spec.NewColumns[0]) {
- columns = append(columns, columnName{
- tableName: node.Table.Name.String(),
- columnName: spec.NewColumns[0].Name.Name.String(),
- line: node.OriginTextPosition(),
- })
+ col := columnName{
+ tableName: node.Table.Name.O,
+ columnName: spec.NewColumns[0].Name.Name.O,
+ line: node.OriginTextPosition(),
+ }
+ if _, exists := checker.columnSet[col.name()]; !exists {
+ checker.columnSet[col.name()] = col
}
}
}
}
- for _, column := range columns {
- v.adviceList = append(v.adviceList, advisor.Advice{
- Status: v.level,
- Code: advisor.ColumnCanNotNull,
- Title: v.title,
- Content: fmt.Sprintf("`%s`.`%s` can not have NULL value", column.tableName, column.columnName),
- Line: column.line,
- })
- }
-
return in, false
}
diff --git a/plugin/advisor/mysql/advisor_column_no_null_test.go b/plugin/advisor/mysql/advisor_column_no_null_test.go
index 29d320ac9e..bbbc984ba7 100644
--- a/plugin/advisor/mysql/advisor_column_no_null_test.go
+++ b/plugin/advisor/mysql/advisor_column_no_null_test.go
@@ -65,19 +65,25 @@ func TestColumnNoNull(t *testing.T) {
},
},
{
- Statement: "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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`id` can not have NULL value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: "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: []advisor.Advice{
{
Status: advisor.Success,
@@ -88,19 +94,24 @@ func TestColumnNoNull(t *testing.T) {
},
},
{
- Statement: "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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: "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: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_column_set_default_for_not_null_test.go b/plugin/advisor/mysql/advisor_column_set_default_for_not_null_test.go
index 95bc9a1db5..79903d3e28 100644
--- a/plugin/advisor/mysql/advisor_column_set_default_for_not_null_test.go
+++ b/plugin/advisor/mysql/advisor_column_set_default_for_not_null_test.go
@@ -67,82 +67,96 @@ func TestColumnSetDefaultForNotNull(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NotNullColumnWithNullDefault,
Title: "column.set-default-for-not-null",
Content: "Column `book`.`id` is NOT NULL but has NULL default value",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `ALTER TABLE book
- CHANGE COLUMN uid id int NOT NULL DEFAULT 0,
- MODIFY COLUMN id int PRIMARY KEY DEFAULT 0,
- ADD COLUMN name varchar(20) NOT NULL DEFAULT ''
+ Statement: `
+ CREATE TABLE book(uid int, id int);
+ ALTER TABLE book
+ CHANGE COLUMN uid uid int NOT NULL DEFAULT 0,
+ MODIFY COLUMN id int PRIMARY KEY DEFAULT 0,
+ ADD COLUMN name varchar(20) NOT NULL DEFAULT ''
`,
Want: []advisor.Advice{
{
diff --git a/plugin/advisor/mysql/advisor_column_type_restriction_test.go b/plugin/advisor/mysql/advisor_column_type_restriction_test.go
index 8d3689beb6..16cde6578f 100644
--- a/plugin/advisor/mysql/advisor_column_type_restriction_test.go
+++ b/plugin/advisor/mysql/advisor_column_type_restriction_test.go
@@ -51,47 +51,51 @@ func TestColumnTypeRestriction(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
Title: "column.type-restriction",
Content: "Disallow column type JSON but column `t`.`a` is",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
Title: "column.type-restriction",
Content: "Disallow column type BLOB but column `t`.`b` is",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
Title: "column.type-restriction",
Content: "Disallow column type TEXT but column `t`.`c` is",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
Title: "column.type-restriction",
Content: "Disallow column type JSON but column `t`.`a` is",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
Title: "column.type-restriction",
Content: "Disallow column type BLOB but column `t`.`b` is",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go b/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go
index 362d6c118b..38a1b103bb 100644
--- a/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go
+++ b/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go
@@ -20,18 +20,6 @@ func TestMySQLDatabaseAllowDropIfEmpty(t *testing.T) {
},
},
},
- {
- Statement: "DROP DATABASE IF EXISTS foo",
- Want: []advisor.Advice{
- {
- Status: advisor.Error,
- Code: advisor.NotCurrentDatabase,
- Title: "database.drop-empty-database",
- Content: "Database `foo` that is trying to be deleted is not the current database `test`",
- Line: 1,
- },
- },
- },
}
advisor.RunSQLReviewRuleTests(t, tests, &DatabaseAllowDropIfEmptyAdvisor{}, &advisor.SQLReviewRule{
diff --git a/plugin/advisor/mysql/advisor_index_key_number_limit_test.go b/plugin/advisor/mysql/advisor_index_key_number_limit_test.go
index 91fc3210b4..61fca79af7 100644
--- a/plugin/advisor/mysql/advisor_index_key_number_limit_test.go
+++ b/plugin/advisor/mysql/advisor_index_key_number_limit_test.go
@@ -40,38 +40,44 @@ func TestIndexKeyNumberLimit(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexKeyNumberExceedsLimit,
Title: "index.key-number-limit",
Content: "The number of index `idx` in table `t` should be not greater than 5",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexKeyNumberExceedsLimit,
Title: "index.key-number-limit",
Content: "The number of index `uk` in table `t` should be not greater than 5",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexKeyNumberExceedsLimit,
Title: "index.key-number-limit",
Content: "The number of index `uk` in table `t` should be not greater than 5",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_index_no_duplicate_column_test.go b/plugin/advisor/mysql/advisor_index_no_duplicate_column_test.go
index a133b853c6..614cac2f3a 100644
--- a/plugin/advisor/mysql/advisor_index_no_duplicate_column_test.go
+++ b/plugin/advisor/mysql/advisor_index_no_duplicate_column_test.go
@@ -36,62 +36,72 @@ func TestIndexNoDuplicateColumn(t *testing.T) {
},
},
{
- Statement: `CREATE INDEX idx_a on t(a, a)`,
+ Statement: `
+ CREATE TABLE t(a int);
+ CREATE INDEX idx_a on t(a, a)`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DuplicateColumnInIndex,
Title: "index.no-duplicate-column",
Content: "INDEX `idx_a` has duplicate column `t`.`a`",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DuplicateColumnInIndex,
Title: "index.no-duplicate-column",
Content: "INDEX `idx_a` has duplicate column `t`.`a`",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DuplicateColumnInIndex,
Title: "index.no-duplicate-column",
Content: "PRIMARY KEY `pk_a` has duplicate column `t`.`a`",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DuplicateColumnInIndex,
Title: "index.no-duplicate-column",
Content: "UNIQUE KEY `uk_a` has duplicate column `t`.`a`",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.DuplicateColumnInIndex,
Title: "index.no-duplicate-column",
Content: "FOREIGN KEY `fk_a` has duplicate column `t`.`a`",
- Line: 1,
+ Line: 3,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_index_not_exists.go b/plugin/advisor/mysql/advisor_index_not_exists.go
deleted file mode 100644
index f2fc69373e..0000000000
--- a/plugin/advisor/mysql/advisor_index_not_exists.go
+++ /dev/null
@@ -1,129 +0,0 @@
-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/catalog"
- "github.com/bytebase/bytebase/plugin/advisor/db"
-)
-
-var (
- _ advisor.Advisor = (*IndexNotExistsAdvisor)(nil)
- _ ast.Visitor = (*indexNotExistsChecker)(nil)
-)
-
-func init() {
- advisor.Register(db.MySQL, advisor.MySQLIndexNotExists, &IndexNotExistsAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLIndexNotExists, &IndexNotExistsAdvisor{})
-}
-
-// IndexNotExistsAdvisor is the advisor checking for index name conflict check.
-type IndexNotExistsAdvisor struct {
-}
-
-// Check checks for index name conflict check.
-func (*IndexNotExistsAdvisor) 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 := &indexNotExistsChecker{
- level: level,
- title: string(ctx.Rule.Type),
- catalog: ctx.Catalog,
- }
-
- 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 indexNotExistsChecker struct {
- adviceList []advisor.Advice
- level advisor.Status
- title string
- text string
- line int
- catalog *catalog.Finder
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *indexNotExistsChecker) Enter(in ast.Node) (ast.Node, bool) {
- type indexData struct {
- table string
- index string
- }
- var indexList []indexData
- switch node := in.(type) {
- case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- if spec.Tp == ast.AlterTableAddConstraint {
- switch spec.Constraint.Tp {
- case ast.ConstraintUniq,
- ast.ConstraintUniqIndex,
- ast.ConstraintUniqKey,
- ast.ConstraintIndex,
- ast.ConstraintKey:
- if spec.Constraint.Name != "" &&
- !spec.Constraint.IfNotExists &&
- checker.indexExists(node.Table.Name.O, spec.Constraint.Name) {
- indexList = append(indexList, indexData{
- table: node.Table.Name.O,
- index: spec.Constraint.Name,
- })
- }
- }
- }
- }
- case *ast.CreateIndexStmt:
- if node.IndexName != "" && !node.IfNotExists && checker.indexExists(node.Table.Name.O, node.IndexName) {
- indexList = append(indexList, indexData{
- table: node.Table.Name.O,
- index: node.IndexName,
- })
- }
- }
-
- for _, index := range indexList {
- checker.adviceList = append(checker.adviceList, advisor.Advice{
- Status: checker.level,
- Code: advisor.IndexExists,
- Title: checker.title,
- Content: fmt.Sprintf("Index `%s` in table `%s` exists", index.index, index.table),
- Line: checker.line,
- })
- }
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*indexNotExistsChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
-}
-
-func (checker *indexNotExistsChecker) indexExists(table string, index string) bool {
- _, idx := checker.catalog.Origin.FindIndex(&catalog.IndexFind{TableName: table, IndexName: index})
- return idx != nil
-}
diff --git a/plugin/advisor/mysql/advisor_index_not_exists_test.go b/plugin/advisor/mysql/advisor_index_not_exists_test.go
deleted file mode 100644
index 1ee131ce1c..0000000000
--- a/plugin/advisor/mysql/advisor_index_not_exists_test.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package mysql
-
-// Framework code is generated by the generator.
-
-import (
- "testing"
-
- "github.com/bytebase/bytebase/plugin/advisor"
-)
-
-func TestIndexNotExists(t *testing.T) {
- tests := []advisor.TestCase{
- {
- Statement: `CREATE INDEX idx on t(a)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `CREATE INDEX old_index on tech_book(a)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.IndexExists,
- Title: "index.not-exists",
- Content: "Index `old_index` in table `tech_book` exists",
- Line: 1,
- },
- },
- },
- {
- Statement: `ALTER TABLE tech_book ADD CONSTRAINT old_uk UNIQUE KEY (a)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.IndexExists,
- Title: "index.not-exists",
- Content: "Index `old_uk` in table `tech_book` exists",
- Line: 1,
- },
- },
- },
- {
- Statement: `ALTER TABLE tech_book ADD UNIQUE KEY old_uk (a)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.IndexExists,
- Title: "index.not-exists",
- Content: "Index `old_uk` in table `tech_book` exists",
- Line: 1,
- },
- },
- },
- {
- Statement: `ALTER TABLE tech_book ADD KEY old_index (a)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.IndexExists,
- Title: "index.not-exists",
- Content: "Index `old_index` in table `tech_book` exists",
- Line: 1,
- },
- },
- },
- }
-
- advisor.RunSQLReviewRuleTests(t, tests, &IndexNotExistsAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleIndexNotExists,
- Level: advisor.SchemaRuleLevelWarning,
- Payload: "",
- }, advisor.MockMySQLDatabase)
-}
diff --git a/plugin/advisor/mysql/advisor_index_pk_type_test.go b/plugin/advisor/mysql/advisor_index_pk_type_test.go
index 34ee644605..7e8a978b6f 100644
--- a/plugin/advisor/mysql/advisor_index_pk_type_test.go
+++ b/plugin/advisor/mysql/advisor_index_pk_type_test.go
@@ -90,19 +90,23 @@ func TestIndexPkType(t *testing.T) {
},
// ALTER TABLE ADD COLUMN OPTION PRIMARY KEY
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
@@ -114,38 +118,44 @@ func TestIndexPkType(t *testing.T) {
},
// ALTER TABLE ADD COLUMN ADD PRIAMRY KEY CONSTRAINT
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id3` is varchar(5)",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
@@ -156,7 +166,9 @@ func TestIndexPkType(t *testing.T) {
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
@@ -230,19 +242,23 @@ func TestIndexPkType(t *testing.T) {
},
// ALTER TABLE MODIFY COLUMN
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
@@ -254,19 +270,23 @@ func TestIndexPkType(t *testing.T) {
},
// ALTER TABLE CHANGE COLUMN
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
Title: "index.pk-type",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_index_type_no_blob_test.go b/plugin/advisor/mysql/advisor_index_type_no_blob_test.go
index 2502a79ee5..53397ef18d 100644
--- a/plugin/advisor/mysql/advisor_index_type_no_blob_test.go
+++ b/plugin/advisor/mysql/advisor_index_type_no_blob_test.go
@@ -114,71 +114,79 @@ func TestIndexTypeNoBlob(t *testing.T) {
},
// ALTER TABLE ADD COLUMN ADD CONSTRAINT
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`b` is blob",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`b` is blob",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`mb` is mediumblob",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`lb` is longblob",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`b` is blob",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`mb` is mediumblob",
- Line: 1,
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexTypeNoBlob,
Title: "index.type-no-blob",
Content: "Columns in index must not be BLOB but `t`.`lb` is longblob",
- Line: 1,
+ Line: 3,
},
},
},
{
- Statement: `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: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility_test.go b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
index d035334e35..74359ca1bd 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
@@ -11,37 +11,37 @@ import (
func TestBasic(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: "DROP DATABASE d1",
+ Statement: "DROP DATABASE test",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityDropDatabase,
Title: "schema.backward-compatibility",
- Content: "\"DROP DATABASE d1\" may cause incompatibility with the existing data and code",
+ Content: "\"DROP DATABASE test\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "DROP TABLE t1",
+ Statement: "DROP TABLE tech_book",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
- Content: "\"DROP TABLE t1\" may cause incompatibility with the existing data and code",
+ Content: "\"DROP TABLE tech_book\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "RENAME TABLE t1 to t2",
+ Statement: "RENAME TABLE tech_book to t2",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
- Content: "\"RENAME TABLE t1 to t2\" may cause incompatibility with the existing data and code",
+ Content: "\"RENAME TABLE tech_book to t2\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
@@ -59,19 +59,19 @@ func TestBasic(t *testing.T) {
},
},
{
- Statement: "CREATE UNIQUE INDEX idx1 ON t1 (f1)",
+ Statement: "CREATE UNIQUE INDEX idx1 ON tech_book (id)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
- Content: "\"CREATE UNIQUE INDEX idx1 ON t1 (f1)\" may cause incompatibility with the existing data and code",
+ Content: "\"CREATE UNIQUE INDEX idx1 ON tech_book (id)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "DROP TABLE t1;DROP TABLE t2;",
+ Statement: "CREATE TABLE t1(a int);DROP TABLE t1;DROP TABLE tech_book;",
Want: []advisor.Advice{
{
Status: advisor.Warn,
@@ -84,7 +84,7 @@ func TestBasic(t *testing.T) {
Status: advisor.Warn,
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
- Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",
+ Content: "\"DROP TABLE tech_book;\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
@@ -129,7 +129,7 @@ func TestBasic(t *testing.T) {
func TestAlterTable(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: "ALTER TABLE t1 ADD f1 TEXT",
+ Statement: "ALTER TABLE tech_book ADD f1 TEXT",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -140,103 +140,103 @@ func TestAlterTable(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE t1 RENAME COLUMN f1 to f2",
+ Statement: "ALTER TABLE tech_book RENAME COLUMN id to f2",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityRenameColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 RENAME COLUMN f1 to f2\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book RENAME COLUMN id to f2\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 DROP COLUMN f1",
+ Statement: "ALTER TABLE tech_book DROP COLUMN id",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityDropColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 DROP COLUMN f1\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book DROP COLUMN id\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD PRIMARY KEY (f1)",
+ Statement: "ALTER TABLE tech_book DROP PRIMARY KEY;ALTER TABLE tech_book ADD PRIMARY KEY (id)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddPrimaryKey,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD PRIMARY KEY (f1)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD PRIMARY KEY (id)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD UNIQUE (f1)",
+ Statement: "ALTER TABLE tech_book ADD UNIQUE (id)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD UNIQUE (f1)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD UNIQUE (id)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD UNIQUE KEY (f1)",
+ Statement: "ALTER TABLE tech_book ADD UNIQUE KEY (id)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD UNIQUE KEY (f1)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD UNIQUE KEY (id)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD UNIQUE INDEX (f1)",
+ Statement: "ALTER TABLE tech_book ADD UNIQUE INDEX (id)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD UNIQUE INDEX (f1)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD UNIQUE INDEX (id)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)",
+ Statement: "ALTER TABLE tech_book ADD FOREIGN KEY (id) REFERENCES t2(f2)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddForeignKey,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD FOREIGN KEY (id) REFERENCES t2(f2)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD CHECK (f1 > 0)",
+ Statement: "ALTER TABLE tech_book ADD CHECK (id > 0)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD CHECK (id > 0)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD CHECK (f1 > 0) NOT ENFORCED",
+ Statement: "ALTER TABLE tech_book ADD CHECK (id > 0) NOT ENFORCED",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -246,19 +246,19 @@ func TestAlterTable(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE t1 ALTER CHECK chk1 ENFORCED",
+ Statement: "ALTER TABLE tech_book ALTER CHECK chk1 ENFORCED",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterCheck,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ALTER CHECK chk1 ENFORCED\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ALTER CHECK chk1 ENFORCED\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ALTER CHECK chk1 NOT ENFORCED",
+ Statement: "ALTER TABLE tech_book ALTER CHECK chk1 NOT ENFORCED",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -268,19 +268,19 @@ func TestAlterTable(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0)",
+ Statement: "ALTER TABLE tech_book ADD CONSTRAINT CHECK (id > 0)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book ADD CONSTRAINT CHECK (id > 0)\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0) NOT ENFORCED",
+ Statement: "ALTER TABLE tech_book ADD CONSTRAINT CHECK (id > 0) NOT ENFORCED",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -291,13 +291,13 @@ func TestAlterTable(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE t1 RENAME TO t2",
+ Statement: "ALTER TABLE tech_book RENAME TO t2",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 RENAME TO t2\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book RENAME TO t2\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
@@ -314,61 +314,61 @@ func TestAlterTable(t *testing.T) {
func TestAlterTableChangeColumnType(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: "ALTER TABLE t1 CHANGE f1 f2 TEXT",
+ Statement: "ALTER TABLE tech_book CHANGE name f2 TEXT",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 CHANGE f1 f2 TEXT\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book CHANGE name f2 TEXT\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 MODIFY f1 TEXT",
+ Statement: "ALTER TABLE tech_book MODIFY name TEXT",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 MODIFY f1 TEXT\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book MODIFY name TEXT\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 MODIFY f1 TEXT NULL",
+ Statement: "ALTER TABLE tech_book MODIFY name TEXT NULL",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 MODIFY f1 TEXT NULL\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book MODIFY name TEXT NULL\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 MODIFY f1 TEXT NOT NULL",
+ Statement: "ALTER TABLE tech_book MODIFY name TEXT NOT NULL",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 MODIFY f1 TEXT NOT NULL\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book MODIFY name TEXT NOT NULL\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
},
{
- Statement: "ALTER TABLE t1 MODIFY f1 TEXT COMMENT 'bla'",
+ Statement: "ALTER TABLE tech_book MODIFY name TEXT COMMENT 'bla'",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
- Content: "\"ALTER TABLE t1 MODIFY f1 TEXT COMMENT 'bla'\" may cause incompatibility with the existing data and code",
+ Content: "\"ALTER TABLE tech_book MODIFY name TEXT COMMENT 'bla'\" may cause incompatibility with the existing data and code",
Line: 1,
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_column_test.go b/plugin/advisor/mysql/advisor_naming_column_test.go
index 5510efa4b1..4cf6bc2953 100644
--- a/plugin/advisor/mysql/advisor_naming_column_test.go
+++ b/plugin/advisor/mysql/advisor_naming_column_test.go
@@ -63,7 +63,7 @@ func TestNamingColumnConvention(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE book RENAME COLUMN creatorId TO creator_id;`,
+ Statement: `ALTER TABLE tech_book RENAME COLUMN id TO creator_id;`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -92,7 +92,7 @@ func TestNamingColumnConvention(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE book CHANGE COLUMN creatorId creator_id int;`,
+ Statement: `ALTER TABLE tech_book CHANGE COLUMN id creator_id int;`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -103,7 +103,7 @@ func TestNamingColumnConvention(t *testing.T) {
},
},
{
- Statement: `ALTER TABLE book DROP COLUMN contentString;`,
+ Statement: `ALTER TABLE tech_book DROP COLUMN id;`,
Want: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
index 131da855f2..77534f2413 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
@@ -111,7 +111,7 @@ func TestNamingIndexConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), INDEX idx_tech_book_name (name))",
+ Statement: "CREATE TABLE tech_book_copy(id INT PRIMARY KEY, name VARCHAR(20), INDEX idx_tech_book_copy_name (name))",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -122,7 +122,7 @@ func TestNamingIndexConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), INDEX (name))",
+ Statement: "CREATE TABLE tech_book_copy(id INT PRIMARY KEY, name VARCHAR(20), INDEX (name))",
Want: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_naming_table_test.go b/plugin/advisor/mysql/advisor_naming_table_test.go
index 623875c3a6..d97f7fe91f 100644
--- a/plugin/advisor/mysql/advisor_naming_table_test.go
+++ b/plugin/advisor/mysql/advisor_naming_table_test.go
@@ -27,7 +27,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id int, name varchar(255))",
+ Statement: "CREATE TABLE tech_book_copy(id int, name varchar(255))",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -50,7 +50,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE techBook RENAME TO TechBook",
+ Statement: "ALTER TABLE tech_book RENAME TO TechBook",
Want: []advisor.Advice{
{
Status: advisor.Error,
@@ -62,7 +62,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE techBook RENAME TO tech_book",
+ Statement: "ALTER TABLE tech_book RENAME TO tech_book_copy",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -73,7 +73,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "RENAME TABLE techBook TO tech_book, literaryBook TO LiteraryBook",
+ Statement: "RENAME TABLE tech_book TO tech_book_copy, tech_book_copy TO LiteraryBook",
Want: []advisor.Advice{
{
Status: advisor.Error,
@@ -85,7 +85,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "RENAME TABLE techBook TO TechBook, literaryBook TO LiteraryBook",
+ Statement: "CREATE TABLE literary_book(a int);RENAME TABLE tech_book TO TechBook, literary_book TO LiteraryBook",
Want: []advisor.Advice{
{
Status: advisor.Error,
@@ -104,7 +104,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
},
{
- Statement: "RENAME TABLE techBook TO tech_book, literaryBook TO literary_book",
+ Statement: "CREATE TABLE literary_book(a int);RENAME TABLE tech_book TO tech_book_copy, literary_book TO literary_book_copy",
Want: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
index 1be026e3c9..e7549562a9 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
@@ -111,7 +111,7 @@ func TestNamingUKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE INDEX uk_tech_book_name (name))",
+ Statement: "CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE INDEX uk_book_name (name))",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -122,7 +122,7 @@ func TestNamingUKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE KEY (name))",
+ Statement: "CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE KEY (name))",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -133,7 +133,7 @@ func TestNamingUKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE INDEX (name))",
+ Statement: "CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE INDEX (name))",
Want: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index 6e7556a52a..9d19c72b0c 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -9,7 +9,7 @@ import (
func TestWhereRequirement(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: "INSERT INTO t values (1)",
+ Statement: "INSERT INTO tech_book(id) values (1)",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -20,31 +20,31 @@ func TestWhereRequirement(t *testing.T) {
},
},
{
- Statement: "DELETE FROM t1",
+ Statement: "DELETE FROM tech_book",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
- Content: "\"DELETE FROM t1\" requires WHERE clause",
+ Content: "\"DELETE FROM tech_book\" requires WHERE clause",
Line: 1,
},
},
},
{
- Statement: "UPDATE t1 SET a = 1",
+ Statement: "UPDATE tech_book SET id = 1",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
- Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
+ Content: "\"UPDATE tech_book SET id = 1\" requires WHERE clause",
Line: 1,
},
},
},
{
- Statement: "DELETE FROM t1 WHERE a > 0",
+ Statement: "DELETE FROM tech_book WHERE id > 0",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -55,7 +55,7 @@ func TestWhereRequirement(t *testing.T) {
},
},
{
- Statement: "UPDATE t1 SET a = 1 WHERE a > 10",
+ Statement: "UPDATE tech_book SET id = 1 WHERE id > 10",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -66,19 +66,19 @@ func TestWhereRequirement(t *testing.T) {
},
},
{
- Statement: "SELECT a FROM t",
+ Statement: "SELECT id FROM tech_book",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
- Content: "\"SELECT a FROM t\" requires WHERE clause",
+ Content: "\"SELECT id FROM tech_book\" requires WHERE clause",
Line: 1,
},
},
},
{
- Statement: "SELECT a FROM t WHERE a > 0",
+ Statement: "SELECT id FROM tech_book WHERE id > 0",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -89,13 +89,13 @@ func TestWhereRequirement(t *testing.T) {
},
},
{
- Statement: "SELECT a FROM t WHERE a > (SELECT max(id) FROM user)",
+ Statement: "SELECT id FROM tech_book WHERE id > (SELECT max(id) FROM tech_book)",
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
- Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
+ Content: "\"SELECT id FROM tech_book WHERE id > (SELECT max(id) FROM tech_book)\" requires WHERE clause",
Line: 1,
},
},
diff --git a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
index a3a5992311..f271249b32 100644
--- a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
+++ b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
@@ -13,7 +13,7 @@ func TestTableDisallowCreateTableAs(t *testing.T) {
{
Statement: `CREATE TABLE t AS
(
- SELECT * FROM tbl
+ SELECT * FROM tech_book
)
`,
Want: []advisor.Advice{
@@ -39,7 +39,7 @@ func TestTableDisallowCreateTableAs(t *testing.T) {
},
},
{
- Statement: `CREATE TABLE t LIKE tbl`,
+ Statement: `CREATE TABLE t LIKE tech_book`,
Want: []advisor.Advice{
{
Status: advisor.Success,
diff --git a/plugin/advisor/mysql/advisor_table_exists.go b/plugin/advisor/mysql/advisor_table_exists.go
deleted file mode 100644
index 55e42a4e31..0000000000
--- a/plugin/advisor/mysql/advisor_table_exists.go
+++ /dev/null
@@ -1,124 +0,0 @@
-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/catalog"
- "github.com/bytebase/bytebase/plugin/advisor/db"
-)
-
-var (
- _ advisor.Advisor = (*TableExistsAdvisor)(nil)
- _ ast.Visitor = (*tableExistsChecker)(nil)
-)
-
-func init() {
- advisor.Register(db.MySQL, advisor.MySQLTableExists, &TableExistsAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLTableExists, &TableExistsAdvisor{})
-}
-
-// TableExistsAdvisor is the advisor checking for table existence..
-type TableExistsAdvisor struct {
-}
-
-// Check checks for table existence..
-func (*TableExistsAdvisor) 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 := &tableExistsChecker{
- level: level,
- title: string(ctx.Rule.Type),
- catalog: ctx.Catalog,
- }
-
- 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 tableExistsChecker struct {
- adviceList []advisor.Advice
- level advisor.Status
- title string
- text string
- line int
- catalog *catalog.Finder
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *tableExistsChecker) Enter(in ast.Node) (ast.Node, bool) {
- var tableList []string
- switch node := in.(type) {
- case *ast.InsertStmt:
- if name, ok := getInsertTableName(node.Table); ok {
- tableList = append(tableList, name)
- }
- case *ast.AlterTableStmt:
- tableList = append(tableList, node.Table.Name.O)
- case *ast.CreateTableStmt:
- if node.ReferTable != nil && sameDatabase(node.ReferTable.Schema.O, checker.catalog.Origin.DatabaseName()) {
- tableList = append(tableList, node.ReferTable.Name.O)
- }
- }
-
- for _, table := range tableList {
- if checker.catalog.Origin.FindTable(&catalog.TableFind{TableName: table}) == nil {
- checker.adviceList = append(checker.adviceList, advisor.Advice{
- Status: checker.level,
- Code: advisor.TableNotExists,
- Title: checker.title,
- Content: fmt.Sprintf("Table `%s` not exists, related statement: \"%s\"", table, checker.text),
- Line: checker.line,
- })
- }
- }
-
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*tableExistsChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
-}
-
-func sameDatabase(a string, b string) bool {
- if a == "" || b == "" {
- return true
- }
- return a == b
-}
-
-func getInsertTableName(table *ast.TableRefsClause) (string, bool) {
- source, isTableSource := table.TableRefs.Left.(*ast.TableSource)
- nilRight := table.TableRefs.Right == nil
- if isTableSource && nilRight {
- if tableName, ok := source.Source.(*ast.TableName); ok {
- return tableName.Name.O, true
- }
- }
- return "", false
-}
diff --git a/plugin/advisor/mysql/advisor_table_exists_test.go b/plugin/advisor/mysql/advisor_table_exists_test.go
deleted file mode 100644
index 9a27af07be..0000000000
--- a/plugin/advisor/mysql/advisor_table_exists_test.go
+++ /dev/null
@@ -1,111 +0,0 @@
-package mysql
-
-// Framework code is generated by the generator.
-
-import (
- "testing"
-
- "github.com/bytebase/bytebase/plugin/advisor"
-)
-
-func TestTableExists(t *testing.T) {
- tests := []advisor.TestCase{
- {
- Statement: `INSERT INTO tech_book values (1)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `INSERT INTO t values (1)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.TableNotExists,
- Title: "table.exists",
- Content: "Table `t` not exists, related statement: \"INSERT INTO t values (1)\"",
- Line: 1,
- },
- },
- },
- {
- Statement: `ALTER TABLE t ADD COLUMN a int`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.TableNotExists,
- Title: "table.exists",
- Content: "Table `t` not exists, related statement: \"ALTER TABLE t ADD COLUMN a int\"",
- Line: 1,
- },
- },
- },
- {
- Statement: `CREATE TABLE t_copy LIKE t`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.TableNotExists,
- Title: "table.exists",
- Content: "Table `t` not exists, related statement: \"CREATE TABLE t_copy LIKE t\"",
- Line: 1,
- },
- },
- },
- {
- Statement: `ALTER TABLE tech_book RENAME TO tech`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `CREATE TABLE tech_book_copy LIKE tech_book`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `CREATE TABLE tech_book_copy AS SELECT * from t`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `CREATE TABLE tech_book_copy (a int)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- }
-
- advisor.RunSQLReviewRuleTests(t, tests, &TableExistsAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleTableExists,
- Level: advisor.SchemaRuleLevelWarning,
- Payload: "",
- }, advisor.MockMySQLDatabase)
-}
diff --git a/plugin/advisor/mysql/advisor_table_not_exists.go b/plugin/advisor/mysql/advisor_table_not_exists.go
deleted file mode 100644
index bd8f3ab83c..0000000000
--- a/plugin/advisor/mysql/advisor_table_not_exists.go
+++ /dev/null
@@ -1,96 +0,0 @@
-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/catalog"
- "github.com/bytebase/bytebase/plugin/advisor/db"
-)
-
-var (
- _ advisor.Advisor = (*TableNotExistsAdvisor)(nil)
- _ ast.Visitor = (*tableNotExistsChecker)(nil)
-)
-
-func init() {
- advisor.Register(db.MySQL, advisor.MySQLTableNotExists, &TableNotExistsAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLTableNotExists, &TableNotExistsAdvisor{})
-}
-
-// TableNotExistsAdvisor is the advisor checking for table name conflict check.
-type TableNotExistsAdvisor struct {
-}
-
-// Check checks for table name conflict check.
-func (*TableNotExistsAdvisor) 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 := &tableNotExistsChecker{
- level: level,
- title: string(ctx.Rule.Type),
- catalog: ctx.Catalog,
- }
-
- 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 tableNotExistsChecker struct {
- adviceList []advisor.Advice
- level advisor.Status
- title string
- text string
- line int
- catalog *catalog.Finder
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *tableNotExistsChecker) Enter(in ast.Node) (ast.Node, bool) {
- if node, ok := in.(*ast.CreateTableStmt); ok {
- if !node.IfNotExists && checker.tableExists(node.Table.Name.O) {
- checker.adviceList = append(checker.adviceList, advisor.Advice{
- Status: checker.level,
- Code: advisor.TableExists,
- Title: checker.title,
- Content: fmt.Sprintf("Table `%s` exists", node.Table.Name.O),
- Line: node.OriginTextPosition(),
- })
- }
- }
-
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*tableNotExistsChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
-}
-
-func (checker *tableNotExistsChecker) tableExists(table string) bool {
- return checker.catalog.Origin.FindTable(&catalog.TableFind{TableName: table}) != nil
-}
diff --git a/plugin/advisor/mysql/advisor_table_not_exists_test.go b/plugin/advisor/mysql/advisor_table_not_exists_test.go
deleted file mode 100644
index 1127777e7e..0000000000
--- a/plugin/advisor/mysql/advisor_table_not_exists_test.go
+++ /dev/null
@@ -1,54 +0,0 @@
-package mysql
-
-// Framework code is generated by the generator.
-
-import (
- "testing"
-
- "github.com/bytebase/bytebase/plugin/advisor"
-)
-
-func TestTableNotExists(t *testing.T) {
- tests := []advisor.TestCase{
- {
- Statement: `CREATE TABLE t(a int)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `CREATE TABLE tech_book(a int)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.TableExists,
- Title: "table.not-exists",
- Content: "Table `tech_book` exists",
- Line: 1,
- },
- },
- },
- {
- Statement: `CREATE TABLE IF NOT EXISTS tech_book(a int)`,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- }
-
- advisor.RunSQLReviewRuleTests(t, tests, &TableNotExistsAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleTableNotExists,
- Level: advisor.SchemaRuleLevelWarning,
- Payload: "",
- }, advisor.MockMySQLDatabase)
-}
diff --git a/plugin/advisor/mysql/advisor_table_require_pk_test.go b/plugin/advisor/mysql/advisor_table_require_pk_test.go
index aafa355cdb..af135c874a 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk_test.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk_test.go
@@ -118,7 +118,7 @@ func TestRequirePK(t *testing.T) {
},
{
// Use MockCatalogService
- Statement: `ALTER TABLE t CHANGE COLUMN id uid INT`,
+ Statement: `ALTER TABLE tech_book CHANGE COLUMN id uid INT`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -142,7 +142,7 @@ func TestRequirePK(t *testing.T) {
},
{
// Use MockCatalogService
- Statement: `ALTER TABLE t MODIFY COLUMN id INT PRIMARY KEY`,
+ Statement: `ALTER TABLE tech_book MODIFY COLUMN id INT`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -179,7 +179,7 @@ func TestRequirePK(t *testing.T) {
},
{
// Use MockCatalogService
- Statement: `ALTER TABLE tech_book DROP COLUMN id, DROP COLUMN name`,
+ Statement: `ALTER TABLE tech_book ADD COLUMN a int, DROP COLUMN id, DROP COLUMN name`,
Want: []advisor.Advice{
{
Status: advisor.Error,
@@ -192,7 +192,7 @@ func TestRequirePK(t *testing.T) {
},
{
// Use MockCatalogService
- Statement: `ALTER TABLE tech_book DROP COLUMN uid, DROP COLUMN name`,
+ Statement: `ALTER TABLE tech_book DROP COLUMN name`,
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -205,7 +205,7 @@ func TestRequirePK(t *testing.T) {
{
// Use MockCatalogService
Statement: `ALTER TABLE tech_book CHANGE COLUMN id uid int;
- ALTER TABLE tech_book DROP COLUMN uid, DROP COLUMN name`,
+ ALTER TABLE tech_book ADD COLUMN a int, DROP COLUMN uid, DROP COLUMN name`,
Want: []advisor.Advice{
{
Status: advisor.Error,
diff --git a/plugin/advisor/mysql/advisor_use_innodb_test.go b/plugin/advisor/mysql/advisor_use_innodb_test.go
index 4f80072047..370844b117 100644
--- a/plugin/advisor/mysql/advisor_use_innodb_test.go
+++ b/plugin/advisor/mysql/advisor_use_innodb_test.go
@@ -43,7 +43,7 @@ func TestUseInnoDB(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE book ENGINE = INNODB",
+ Statement: "ALTER TABLE tech_book ENGINE = INNODB",
Want: []advisor.Advice{
{
Status: advisor.Success,
@@ -54,13 +54,13 @@ func TestUseInnoDB(t *testing.T) {
},
},
{
- Statement: "ALTER TABLE book ENGINE = CSV",
+ Statement: "ALTER TABLE tech_book ENGINE = CSV",
Want: []advisor.Advice{
{
Status: advisor.Error,
Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
- Content: "\"ALTER TABLE book ENGINE = CSV\" doesn't use InnoDB engine",
+ Content: "\"ALTER TABLE tech_book ENGINE = CSV\" doesn't use InnoDB engine",
Line: 1,
},
},
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 75398e5ad7..21d9eee4a8 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -64,8 +64,6 @@ const (
SchemaRuleTableNoFK SQLReviewRuleType = "table.no-foreign-key"
// SchemaRuleTableDropNamingConvention require only the table following the naming convention can be deleted.
SchemaRuleTableDropNamingConvention SQLReviewRuleType = "table.drop-naming-convention"
- // SchemaRuleTableExists require the table existence.
- SchemaRuleTableExists SQLReviewRuleType = "table.exists"
// SchemaRuleTableCommentConvention enforce the table comment convention.
SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"
// SchemaRuleTableNotExists check the table name conflict.
@@ -81,8 +79,6 @@ const (
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"
// SchemaRuleColumnDisallowChangingOrder disallow changing column order.
SchemaRuleColumnDisallowChangingOrder SQLReviewRuleType = "column.disallow-changing-order"
// SchemaRuleColumnCommentConvention enforce the column comment convention.
@@ -106,8 +102,6 @@ const (
SchemaRuleIndexPKType SQLReviewRuleType = "index.pk-type"
// SchemaRuleIndexTypeNoBlob enforce the type restriction of columns in index.
SchemaRuleIndexTypeNoBlob SQLReviewRuleType = "index.type-no-blob"
- // SchemaRuleIndexNotExists check the index name conflict.
- SchemaRuleIndexNotExists SQLReviewRuleType = "index.not-exists"
// SchemaRuleCharsetAllowlist enforce the charset allowlist.
SchemaRuleCharsetAllowlist SQLReviewRuleType = "charset.allowlist"
@@ -675,11 +669,6 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLColumnDisallowChanging, nil
}
- case SchemaRuleColumnExists:
- switch engine {
- case db.MySQL, db.TiDB:
- return MySQLColumnExists, nil
- }
case SchemaRuleColumnDisallowChangingOrder:
switch engine {
case db.MySQL, db.TiDB:
@@ -719,11 +708,6 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLTableDropNamingConvention, nil
}
- case SchemaRuleTableExists:
- switch engine {
- case db.MySQL, db.TiDB:
- return MySQLTableExists, nil
- }
case SchemaRuleTableCommentConvention:
switch engine {
case db.MySQL, db.TiDB:
@@ -773,11 +757,6 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLIndexTypeNoBlob, nil
}
- case SchemaRuleIndexNotExists:
- switch engine {
- case db.MySQL, db.TiDB:
- return MySQLIndexNotExists, nil
- }
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index 3929a5ca65..91a5a8f339 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -125,9 +125,14 @@ func RunSQLReviewRuleTests(
Charset: "",
Collation: "",
Rule: rule,
- Catalog: catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true}),
}
for _, tc := range tests {
+ finder := catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true})
+ if database.DbType == db.MySQL || database.DbType == db.TiDB {
+ err := finder.WalkThrough(tc.Statement)
+ require.NoError(t, err, tc.Statement)
+ }
+ ctx.Catalog = finder
adviceList, err := adv.Check(ctx, tc.Statement)
require.NoError(t, err)
assert.Equal(t, tc.Want, adviceList, tc.Statement)
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index e2f27cef27..df02707ae6 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -517,26 +517,26 @@ func TestSQLReviewForMySQL(t *testing.T) {
},
},
{
- statement: "DELETE FROM t",
+ statement: "DELETE FROM tech_book",
result: []api.TaskCheckResult{
{
Status: api.TaskCheckStatusError,
Namespace: api.AdvisorNamespace,
Code: advisor.StatementNoWhere.Int(),
Title: "statement.where.require",
- Content: "\"DELETE FROM t\" requires WHERE clause",
+ Content: "\"DELETE FROM tech_book\" requires WHERE clause",
},
},
},
{
- statement: "DELETE FROM t WHERE a like `%abc`",
+ statement: "DELETE FROM tech_book WHERE name like `%abc`",
result: []api.TaskCheckResult{
{
Status: api.TaskCheckStatusError,
Namespace: api.AdvisorNamespace,
Code: advisor.StatementLeadingWildcardLike.Int(),
Title: "statement.where.no-leading-wildcard-like",
- Content: "\"DELETE FROM t WHERE a like `%abc`\" uses leading wildcard LIKE",
+ Content: "\"DELETE FROM tech_book WHERE name like `%abc`\" uses leading wildcard LIKE",
},
},
},
@@ -598,7 +598,7 @@ func TestSQLReviewForMySQL(t *testing.T) {
},
},
{
- statement: "DELETE FROM t WHERE a = (SELECT max(id) FROM user WHERE name = 'bytebase')",
+ statement: "DELETE FROM tech_book WHERE a = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')",
result: []api.TaskCheckResult{
{
Status: api.TaskCheckStatusSuccess,