Skip to content

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

chore: set line for MySQL SQL review rules (#2378)

rebelicee7ed695c1404
diff --git a/plugin/advisor/mysql/advisor_column_no_null.go b/plugin/advisor/mysql/advisor_column_no_null.go
index d27bc43748..9c2adc089e 100644
--- a/plugin/advisor/mysql/advisor_column_no_null.go
+++ b/plugin/advisor/mysql/advisor_column_no_null.go
@@ -62,6 +62,7 @@ type columnNoNullChecker struct {
type columnName struct {
tableName string
columnName string
+ line int
}
// Enter implements the ast.Visitor interface.
@@ -75,6 +76,7 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
columns = append(columns, columnName{
tableName: node.Table.Name.String(),
columnName: column.Name.Name.String(),
+ line: column.OriginTextPosition(),
})
}
}
@@ -89,6 +91,7 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
columns = append(columns, columnName{
tableName: node.Table.Name.String(),
columnName: column.Name.Name.String(),
+ line: node.OriginTextPosition(),
})
}
}
@@ -98,6 +101,7 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
columns = append(columns, columnName{
tableName: node.Table.Name.String(),
columnName: spec.NewColumns[0].Name.Name.String(),
+ line: node.OriginTextPosition(),
})
}
}
@@ -110,6 +114,7 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.ColumnCanNotNull,
Title: v.title,
Content: fmt.Sprintf("`%s`.`%s` can not have NULL value", column.tableName, column.columnName),
+ Line: column.line,
})
}
diff --git a/plugin/advisor/mysql/advisor_column_no_null_test.go b/plugin/advisor/mysql/advisor_column_no_null_test.go
index b4ff7af3cb..29d320ac9e 100644
--- a/plugin/advisor/mysql/advisor_column_no_null_test.go
+++ b/plugin/advisor/mysql/advisor_column_no_null_test.go
@@ -9,19 +9,23 @@ import (
func TestColumnNoNull(t *testing.T) {
tests := []advisor.TestCase{
{
- Statement: "CREATE TABLE book(id int, name varchar(255))",
+ Statement: `CREATE TABLE book(
+ id int,
+ name varchar(255))`,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`id` can not have NULL value",
+ Line: 2,
},
{
Status: advisor.Warn,
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
+ Line: 3,
},
},
},
@@ -33,6 +37,7 @@ func TestColumnNoNull(t *testing.T) {
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
+ Line: 1,
},
},
},
@@ -44,6 +49,7 @@ func TestColumnNoNull(t *testing.T) {
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
+ Line: 1,
},
},
},
@@ -66,6 +72,7 @@ func TestColumnNoNull(t *testing.T) {
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`id` can not have NULL value",
+ Line: 1,
},
},
},
@@ -88,6 +95,7 @@ func TestColumnNoNull(t *testing.T) {
Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_column_required.go b/plugin/advisor/mysql/advisor_column_required.go
index 2986cbafad..2e31eadf39 100644
--- a/plugin/advisor/mysql/advisor_column_required.go
+++ b/plugin/advisor/mysql/advisor_column_required.go
@@ -48,6 +48,7 @@ func (*ColumnRequirementAdvisor) Check(ctx advisor.Context, statement string) ([
title: string(ctx.Rule.Type),
requiredColumns: requiredColumns,
tables: make(tableState),
+ line: make(map[string]int),
}
for _, stmtNode := range root {
@@ -63,6 +64,7 @@ type columnRequirementChecker struct {
title string
requiredColumns columnSet
tables tableState
+ line map[string]int
}
// Enter implements the ast.Visitor interface.
@@ -84,6 +86,7 @@ func (v *columnRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
// RENAME COLUMN
case ast.AlterTableRenameColumn:
v.renameColumn(table, spec.OldColumnName.Name.O, spec.NewColumnName.Name.O)
+ v.line[table] = node.OriginTextPosition()
// ADD COLUMNS
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
@@ -91,10 +94,14 @@ func (v *columnRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
}
// DROP COLUMN
case ast.AlterTableDropColumn:
- v.dropColumn(table, spec.OldColumnName.Name.O)
+ if v.dropColumn(table, spec.OldColumnName.Name.O) {
+ v.line[table] = node.OriginTextPosition()
+ }
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- v.renameColumn(table, spec.OldColumnName.Name.O, spec.NewColumns[0].Name.Name.O)
+ if v.renameColumn(table, spec.OldColumnName.Name.O, spec.NewColumns[0].Name.Name.O) {
+ v.line[table] = node.OriginTextPosition()
+ }
}
}
}
@@ -125,6 +132,7 @@ func (v *columnRequirementChecker) generateAdviceList() []advisor.Advice {
Code: advisor.NoRequiredColumn,
Title: v.title,
Content: fmt.Sprintf("Table `%s` requires columns: %s", tableName, strings.Join(missingColumns, ", ")),
+ Line: v.line[tableName],
})
}
}
@@ -155,11 +163,11 @@ func (v *columnRequirementChecker) initFullTable(name string) columnSet {
return table
}
-func (v *columnRequirementChecker) renameColumn(table string, oldColumn string, newColumn string) {
+func (v *columnRequirementChecker) renameColumn(table string, oldColumn string, newColumn string) bool {
_, oldNeed := v.requiredColumns[oldColumn]
_, newNeed := v.requiredColumns[newColumn]
if !oldNeed && !newNeed {
- return
+ return false
}
t, ok := v.tables[table]
if !ok {
@@ -173,11 +181,12 @@ func (v *columnRequirementChecker) renameColumn(table string, oldColumn string,
if newNeed {
t[newColumn] = true
}
+ return oldNeed
}
-func (v *columnRequirementChecker) dropColumn(table string, column string) {
+func (v *columnRequirementChecker) dropColumn(table string, column string) bool {
if _, ok := v.requiredColumns[column]; !ok {
- return
+ return false
}
t, ok := v.tables[table]
if !ok {
@@ -186,6 +195,7 @@ func (v *columnRequirementChecker) dropColumn(table string, column string) {
t = v.initFullTable(table)
}
t[column] = false
+ return true
}
func (v *columnRequirementChecker) addColumn(table string, column string) {
@@ -202,6 +212,7 @@ func (v *columnRequirementChecker) addColumn(table string, column string) {
}
func (v *columnRequirementChecker) createTable(node *ast.CreateTableStmt) {
+ v.line[node.Table.Name.O] = node.OriginTextPosition()
v.initEmptyTable(node.Table.Name.O)
for _, column := range node.Cols {
v.addColumn(node.Table.Name.O, column.Name.Name.O)
diff --git a/plugin/advisor/mysql/advisor_column_required_test.go b/plugin/advisor/mysql/advisor_column_required_test.go
index ba1e2d5be3..fe8dbea93a 100644
--- a/plugin/advisor/mysql/advisor_column_required_test.go
+++ b/plugin/advisor/mysql/advisor_column_required_test.go
@@ -18,6 +18,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: created_ts, creator_id, updated_ts, updater_id",
+ Line: 1,
},
},
},
@@ -51,6 +52,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
+ Line: 7,
},
},
},
@@ -85,6 +87,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
+ Line: 7,
},
},
},
@@ -119,6 +122,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
+ Line: 7,
},
},
},
@@ -153,6 +157,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: updater_id",
+ Line: 1,
},
},
},
@@ -189,12 +194,14 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
+ Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `student` requires columns: creator_id, updater_id",
+ Line: 6,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_database_drop_empty_db.go b/plugin/advisor/mysql/advisor_database_drop_empty_db.go
index 88a8674df2..ff01d4a919 100644
--- a/plugin/advisor/mysql/advisor_database_drop_empty_db.go
+++ b/plugin/advisor/mysql/advisor_database_drop_empty_db.go
@@ -71,6 +71,7 @@ func (v *allowDropEmptyDBChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NotCurrentDatabase,
Title: v.title,
Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.database.Name),
+ Line: node.OriginTextPosition(),
})
} else if !v.database.HasNoTable() {
v.adviceList = append(v.adviceList, advisor.Advice{
@@ -78,6 +79,7 @@ func (v *allowDropEmptyDBChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.DatabaseNotEmpty,
Title: v.title,
Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", node.Name),
+ Line: node.OriginTextPosition(),
})
}
}
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 e154a93c27..362d6c118b 100644
--- a/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go
+++ b/plugin/advisor/mysql/advisor_database_drop_empty_db_test.go
@@ -16,6 +16,7 @@ func TestMySQLDatabaseAllowDropIfEmpty(t *testing.T) {
Code: advisor.DatabaseNotEmpty,
Title: "database.drop-empty-database",
Content: "Database `test` is not allowed to drop if not empty",
+ Line: 1,
},
},
},
@@ -27,6 +28,7 @@ func TestMySQLDatabaseAllowDropIfEmpty(t *testing.T) {
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,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility.go b/plugin/advisor/mysql/advisor_migration_compatibility.go
index c6691b4477..f0274fa6bf 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility.go
@@ -161,6 +161,7 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: code,
Title: v.title,
Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", in.Text()),
+ Line: in.OriginTextPosition(),
})
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility_test.go b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
index e681116b30..d035334e35 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
@@ -18,6 +18,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropDatabase,
Title: "schema.backward-compatibility",
Content: "\"DROP DATABASE d1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -29,6 +30,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -40,6 +42,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"RENAME TABLE t1 to t2\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -51,6 +54,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP VIEW v1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -62,6 +66,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"CREATE UNIQUE INDEX idx1 ON t1 (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -73,12 +78,14 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1;\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -140,6 +147,7 @@ func TestAlterTable(t *testing.T) {
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",
+ Line: 1,
},
},
},
@@ -151,6 +159,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityDropColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 DROP COLUMN f1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -162,6 +171,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddPrimaryKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD PRIMARY KEY (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -173,6 +183,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -184,6 +195,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE KEY (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -195,6 +207,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE INDEX (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -206,6 +219,7 @@ func TestAlterTable(t *testing.T) {
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",
+ Line: 1,
},
},
},
@@ -217,6 +231,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -238,6 +253,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAlterCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ALTER CHECK chk1 ENFORCED\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -259,6 +275,7 @@ func TestAlterTable(t *testing.T) {
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",
+ Line: 1,
},
},
},
@@ -281,6 +298,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 RENAME TO t2\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -303,6 +321,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 CHANGE f1 f2 TEXT\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -314,6 +333,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 MODIFY f1 TEXT\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -325,6 +345,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 MODIFY f1 TEXT NULL\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -336,6 +357,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
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",
+ Line: 1,
},
},
},
@@ -347,6 +369,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
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",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_column.go b/plugin/advisor/mysql/advisor_naming_column.go
index 6bd7c725e6..f514a9b554 100644
--- a/plugin/advisor/mysql/advisor_naming_column.go
+++ b/plugin/advisor/mysql/advisor_naming_column.go
@@ -73,14 +73,21 @@ type namingColumnConventionChecker struct {
// Enter implements the ast.Visitor interface.
func (v *namingColumnConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
- var columnList []string
+ type columnData struct {
+ name string
+ line int
+ }
+ var columnList []columnData
var tableName string
switch node := in.(type) {
// CREATE TABLE
case *ast.CreateTableStmt:
tableName = node.Table.Name.O
for _, column := range node.Cols {
- columnList = append(columnList, column.Name.Name.O)
+ columnList = append(columnList, columnData{
+ name: column.Name.Name.O,
+ line: column.OriginTextPosition(),
+ })
}
// ALTER TABLE
case *ast.AlterTableStmt:
@@ -89,34 +96,45 @@ func (v *namingColumnConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
switch spec.Tp {
// RENAME COLUMN
case ast.AlterTableRenameColumn:
- columnList = append(columnList, spec.NewColumnName.Name.O)
+ columnList = append(columnList, columnData{
+ name: spec.NewColumnName.Name.O,
+ line: in.OriginTextPosition(),
+ })
// ADD COLUMNS
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
- columnList = append(columnList, column.Name.Name.O)
+ columnList = append(columnList, columnData{
+ name: column.Name.Name.O,
+ line: in.OriginTextPosition(),
+ })
}
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- columnList = append(columnList, spec.NewColumns[0].Name.Name.O)
+ columnList = append(columnList, columnData{
+ name: spec.NewColumns[0].Name.Name.O,
+ line: in.OriginTextPosition(),
+ })
}
}
}
for _, column := range columnList {
- if !v.format.MatchString(column) {
+ if !v.format.MatchString(column.name) {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: advisor.NamingColumnConventionMismatch,
Title: v.title,
- Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column, v.format),
+ Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column.name, v.format),
+ Line: column.line,
})
}
- if v.maxLength > 0 && len(column) > v.maxLength {
+ if v.maxLength > 0 && len(column.name) > v.maxLength {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: advisor.NamingColumnConventionMismatch,
Title: v.title,
- Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should be within %d characters", tableName, column, v.maxLength),
+ Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should be within %d characters", tableName, column.name, v.maxLength),
+ Line: column.line,
})
}
}
diff --git a/plugin/advisor/mysql/advisor_naming_column_test.go b/plugin/advisor/mysql/advisor_naming_column_test.go
index 7110c58427..43516032b0 100644
--- a/plugin/advisor/mysql/advisor_naming_column_test.go
+++ b/plugin/advisor/mysql/advisor_naming_column_test.go
@@ -21,6 +21,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -32,6 +33,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: fmt.Sprintf("`book`.`%s` mismatches column naming convention, its length should be within 64 characters", invalidColumnName),
+ Line: 1,
},
},
},
@@ -55,6 +57,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 2,
},
},
},
@@ -83,6 +86,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 7,
},
},
},
@@ -121,6 +125,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`contentString` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 6,
},
},
},
@@ -140,24 +145,28 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`createdTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`updaterId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 4,
},
{
Status: advisor.Warn,
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`student`.`createdTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 8,
},
{
Status: advisor.Warn,
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`student`.`updatedTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 9,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
index c86c8f7704..3eac6275e1 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
@@ -92,6 +92,7 @@ func (checker *namingFKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingFKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Foreign key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -100,6 +101,7 @@ func (checker *namingFKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingFKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Foreign key `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -140,6 +142,7 @@ func (*namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData
indexName: constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: constraint.OriginTextPosition(),
})
}
}
@@ -165,6 +168,7 @@ func (*namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData
indexName: spec.Constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
index 2ca0fec97e..3fb2220286 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
@@ -32,6 +32,7 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table `tech_book` mismatches the naming convention, expect \"^$|^fk_tech_book_author_id_author_id$\" but found `fk_author_id`",
+ Line: 1,
},
},
},
@@ -43,12 +44,14 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: fmt.Sprintf("Foreign key in table `tech_book` mismatches the naming convention, expect \"^$|^fk_tech_book_author_id_author_id$\" but found `%s`", invalidFKName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: fmt.Sprintf("Foreign key `%s` in table `tech_book` mismatches the naming convention, its length should be within 64 characters", invalidFKName),
+ Line: 1,
},
},
},
@@ -82,6 +85,7 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table `book` mismatches the naming convention, expect \"^$|^fk_book_author_id_author_id$\" but found `fk_book_author_id`",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.go
index a52e9b8855..47eb96d1ff 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -96,6 +96,7 @@ func (checker *namingIndexConventionChecker) Enter(in ast.Node) (ast.Node, bool)
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -104,6 +105,7 @@ func (checker *namingIndexConventionChecker) Enter(in ast.Node) (ast.Node, bool)
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -120,6 +122,7 @@ type indexMetaData struct {
indexName string
tableName string
metaData map[string]string
+ line int
}
// getMetaDataList returns the list of index with meta data.
@@ -142,6 +145,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
indexName: constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: constraint.OriginTextPosition(),
})
}
}
@@ -168,6 +172,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
indexName: spec.ToKey.String(),
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
case ast.AlterTableAddConstraint:
if spec.Constraint.Tp == ast.ConstraintIndex {
@@ -184,6 +189,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
indexName: spec.Constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
}
}
@@ -202,6 +208,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
indexName: node.IndexName,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
index c699e15712..0af5088c31 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
@@ -33,6 +33,7 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `tech_book_id_name`",
+ Line: 1,
},
},
},
@@ -44,12 +45,14 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: fmt.Sprintf("Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `%s`", invalidIndexName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: fmt.Sprintf("Index `%s` in table `tech_book` mismatches the naming convention, its length should be within 64 characters", invalidIndexName),
+ Line: 1,
},
},
},
@@ -79,6 +82,7 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `idx_tech_book`",
+ Line: 1,
},
},
},
@@ -101,6 +105,7 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `tech_book_id_name`",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_table.go b/plugin/advisor/mysql/advisor_naming_table.go
index 21ac5f5120..99330d5476 100644
--- a/plugin/advisor/mysql/advisor_naming_table.go
+++ b/plugin/advisor/mysql/advisor_naming_table.go
@@ -97,6 +97,7 @@ func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingTableConventionMismatch,
Title: v.title,
Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, v.format),
+ Line: in.OriginTextPosition(),
})
}
if v.maxLength > 0 && len(tableName) > v.maxLength {
@@ -105,6 +106,7 @@ func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingTableConventionMismatch,
Title: v.title,
Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should be within %d characters", tableName, v.maxLength),
+ Line: in.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_naming_table_test.go b/plugin/advisor/mysql/advisor_naming_table_test.go
index c808ff99ed..d6d81c9bd8 100644
--- a/plugin/advisor/mysql/advisor_naming_table_test.go
+++ b/plugin/advisor/mysql/advisor_naming_table_test.go
@@ -21,6 +21,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`techBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -43,6 +44,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should be within 64 characters", invalidTableName),
+ Line: 1,
},
},
},
@@ -54,6 +56,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`TechBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -76,6 +79,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`LiteraryBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -87,12 +91,14 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`TechBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`LiteraryBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
index c524340fb2..c06f9afe8b 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
@@ -94,6 +94,7 @@ func (checker *namingUKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingUKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Unique key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -102,6 +103,7 @@ func (checker *namingUKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.NamingUKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Unique key `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -135,6 +137,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
indexName: constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: constraint.OriginTextPosition(),
})
}
}
@@ -161,6 +164,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
indexName: spec.ToKey.String(),
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
case ast.AlterTableAddConstraint:
switch spec.Constraint.Tp {
@@ -178,6 +182,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
indexName: spec.Constraint.Name,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
}
}
@@ -196,6 +201,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
indexName: node.IndexName,
tableName: node.Table.Name.String(),
metaData: metaData,
+ line: in.OriginTextPosition(),
})
}
}
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 32dd03f0d6..c01f786d99 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
@@ -33,6 +33,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `tech_book_id_name`",
+ Line: 1,
},
},
},
@@ -44,12 +45,14 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: fmt.Sprintf("Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `%s`", invalidUKName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: fmt.Sprintf("Unique key `%s` in table `tech_book` mismatches the naming convention, its length should be within 64 characters", invalidUKName),
+ Line: 1,
},
},
},
@@ -72,6 +75,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `tech_book_id_name`",
+ Line: 1,
},
},
},
@@ -101,6 +105,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `uk_tech_book`",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
index 00c651c99a..741721a2f1 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
@@ -51,6 +51,7 @@ func (*NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement string
Code: advisor.StatementLeadingWildcardLike,
Title: string(ctx.Rule.Type),
Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", checker.text),
+ Line: stmtNode.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
index 84ad758673..5e5ed2c38f 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
@@ -27,6 +27,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -38,6 +39,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -49,6 +51,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -60,6 +63,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all.go b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
index 5bf3013d3e..4c45715a40 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
@@ -39,6 +39,7 @@ func (*NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]advis
}
for _, stmtNode := range root {
checker.text = stmtNode.Text()
+ checker.line = stmtNode.OriginTextPosition()
(stmtNode).Accept(checker)
}
@@ -58,6 +59,7 @@ type noSelectAllChecker struct {
level advisor.Status
title string
text string
+ line int
}
// Enter implements the ast.Visitor interface.
@@ -70,6 +72,7 @@ func (v *noSelectAllChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.StatementSelectAll,
Title: v.title,
Content: fmt.Sprintf("\"%s\" uses SELECT all", v.text),
+ Line: v.line,
})
break
}
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
index 1a8b206559..ea0b210ac3 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
@@ -16,6 +16,7 @@ func TestNoSelectAll(t *testing.T) {
Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT * FROM t\" uses SELECT all",
+ Line: 1,
},
},
},
@@ -38,6 +39,7 @@ func TestNoSelectAll(t *testing.T) {
Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT a, b FROM (SELECT * from t1 JOIN t2) t\" uses SELECT all",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required.go b/plugin/advisor/mysql/advisor_stmt_where_required.go
index e41647df86..fb0039e155 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required.go
@@ -40,6 +40,7 @@ func (*WhereRequirementAdvisor) Check(ctx advisor.Context, statement string) ([]
}
for _, stmtNode := range root {
checker.text = stmtNode.Text()
+ checker.line = stmtNode.OriginTextPosition()
(stmtNode).Accept(checker)
}
@@ -59,6 +60,7 @@ type whereRequirementChecker struct {
level advisor.Status
title string
text string
+ line int
}
// Enter implements the ast.Visitor interface.
@@ -88,6 +90,7 @@ func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: code,
Title: v.title,
Content: fmt.Sprintf("\"%s\" requires WHERE clause", v.text),
+ Line: v.line,
})
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index 2241bcb616..6e7556a52a 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -27,6 +27,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"DELETE FROM t1\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -38,6 +39,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -71,6 +73,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -93,6 +96,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_table_drop_naming_convention.go b/plugin/advisor/mysql/advisor_table_drop_naming_convention.go
index c54e6a0aaf..bcbf97ee07 100644
--- a/plugin/advisor/mysql/advisor_table_drop_naming_convention.go
+++ b/plugin/advisor/mysql/advisor_table_drop_naming_convention.go
@@ -76,6 +76,7 @@ func (v *namingDropTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.TableDropNamingConventionMismatch,
Title: v.title,
Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", table.Name.O, v.format),
+ Line: node.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go b/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go
index 7ce752f11a..438908916c 100644
--- a/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go
+++ b/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go
@@ -29,6 +29,7 @@ func TestMySQLTableDropNamingConvention(t *testing.T) {
Code: advisor.TableDropNamingConventionMismatch,
Title: "table.drop-naming-convention",
Content: "`foo` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ Line: 1,
},
},
},
@@ -40,6 +41,7 @@ func TestMySQLTableDropNamingConvention(t *testing.T) {
Code: advisor.TableDropNamingConventionMismatch,
Title: "table.drop-naming-convention",
Content: "`bar` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_table_no_fk.go b/plugin/advisor/mysql/advisor_table_no_fk.go
index a99069927d..df77c3f5f8 100644
--- a/plugin/advisor/mysql/advisor_table_no_fk.go
+++ b/plugin/advisor/mysql/advisor_table_no_fk.go
@@ -71,6 +71,7 @@ func (checker *tableNoFKChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.TableHasFK,
Title: checker.title,
Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", node.Table.Name),
+ Line: constraint.OriginTextPosition(),
})
}
}
@@ -82,6 +83,7 @@ func (checker *tableNoFKChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: advisor.TableHasFK,
Title: checker.title,
Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", node.Table.Name),
+ Line: in.OriginTextPosition(),
})
}
}
diff --git a/plugin/advisor/mysql/advisor_table_no_fk_test.go b/plugin/advisor/mysql/advisor_table_no_fk_test.go
index 1cb87a87ad..c0e678c9bb 100644
--- a/plugin/advisor/mysql/advisor_table_no_fk_test.go
+++ b/plugin/advisor/mysql/advisor_table_no_fk_test.go
@@ -16,6 +16,7 @@ func TestTableNoFK(t *testing.T) {
Code: advisor.TableHasFK,
Title: "table.no-foreign-key",
Content: "Foreign key is not allowed in the table `tech_book`",
+ Line: 1,
},
},
},
@@ -27,6 +28,7 @@ func TestTableNoFK(t *testing.T) {
Code: advisor.TableHasFK,
Title: "table.no-foreign-key",
Content: "Foreign key is not allowed in the table `book`",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.go
index e20e4c8de9..a9eea2bb00 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk.go
@@ -43,6 +43,7 @@ func (*TableRequirePKAdvisor) Check(ctx advisor.Context, statement string) ([]ad
level: level,
title: string(ctx.Rule.Type),
tables: make(tablePK),
+ line: make(map[string]int),
database: ctx.Database,
}
@@ -58,6 +59,7 @@ type tableRequirePKChecker struct {
level advisor.Status
title string
tables tablePK
+ line map[string]int
database *catalog.Database
}
@@ -67,6 +69,7 @@ func (v *tableRequirePKChecker) Enter(in ast.Node) (ast.Node, bool) {
// CREATE TABLE
case *ast.CreateTableStmt:
v.createTable(node)
+ v.line[node.Table.Name.O] = node.OriginTextPosition()
// DROP TABLE
case *ast.DropTableStmt:
for _, table := range node.Tables {
@@ -85,24 +88,30 @@ func (v *tableRequirePKChecker) Enter(in ast.Node) (ast.Node, bool) {
// DROP PRIMARY KEY
case ast.AlterTableDropPrimaryKey:
v.initEmptyTable(tableName)
+ v.line[tableName] = node.OriginTextPosition()
// DROP INDEX
case ast.AlterTableDropIndex:
if strings.ToUpper(spec.Name) == primaryKeyName {
v.initEmptyTable(tableName)
+ v.line[tableName] = node.OriginTextPosition()
}
// ADD COLUMNS
case ast.AlterTableAddColumns:
v.addPKIfExistByCols(tableName, spec.NewColumns)
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- v.changeColumn(tableName, spec.OldColumnName.Name.String(), spec.NewColumns[0].Name.Name.String())
+ if v.changeColumn(tableName, spec.OldColumnName.Name.String(), spec.NewColumns[0].Name.Name.String()) {
+ v.line[tableName] = node.OriginTextPosition()
+ }
v.addPKIfExistByCols(tableName, spec.NewColumns[:1])
// MODIFY COLUMN
case ast.AlterTableModifyColumn:
v.addPKIfExistByCols(tableName, spec.NewColumns[:1])
// DROP COLUMN
case ast.AlterTableDropColumn:
- v.dropColumn(tableName, spec.OldColumnName.Name.String())
+ if v.dropColumn(tableName, spec.OldColumnName.Name.String()) {
+ v.line[tableName] = node.OriginTextPosition()
+ }
}
}
}
@@ -124,6 +133,7 @@ func (v *tableRequirePKChecker) generateAdviceList() []advisor.Advice {
Code: advisor.TableNoPK,
Title: v.title,
Content: fmt.Sprintf("Table `%s` requires PRIMARY KEY", tableName),
+ Line: v.line[tableName],
})
}
}
@@ -156,26 +166,31 @@ func (v *tableRequirePKChecker) createTable(node *ast.CreateTableStmt) {
}
}
-func (v *tableRequirePKChecker) dropColumn(table string, column string) {
+func (v *tableRequirePKChecker) dropColumn(table string, column string) bool {
if _, ok := v.tables[table]; !ok {
- v.tables[table] = make(columnSet)
_, pk := v.database.FindIndex(&catalog.IndexFind{
TableName: table,
IndexName: primaryKeyName,
})
if pk == nil {
- return
+ return false
}
v.tables[table] = newColumnSet(pk.ExpressionList)
}
+ pk := v.tables[table]
+ _, columnInPK := pk[column]
delete(v.tables[table], column)
+ return columnInPK
}
-func (v *tableRequirePKChecker) changeColumn(table string, oldColumn string, newColumn string) {
- v.dropColumn(table, oldColumn)
- pk := v.tables[table]
- pk[newColumn] = true
+func (v *tableRequirePKChecker) changeColumn(table string, oldColumn string, newColumn string) bool {
+ if v.dropColumn(table, oldColumn) {
+ pk := v.tables[table]
+ pk[newColumn] = true
+ return true
+ }
+ return false
}
func (v *tableRequirePKChecker) addPKIfExistByCols(table string, columns []*ast.ColumnDef) {
diff --git a/plugin/advisor/mysql/advisor_table_require_pk_test.go b/plugin/advisor/mysql/advisor_table_require_pk_test.go
index a0c568c333..aafa355cdb 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk_test.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk_test.go
@@ -38,6 +38,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
+ Line: 1,
},
},
},
@@ -74,11 +75,12 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
+ Line: 2,
},
},
},
{
- Statement: "CREATE TABLE t(id INT PRIMARY KEY);" +
+ Statement: "CREATE TABLE t(id INT PRIMARY KEY);\n" +
"ALTER TABLE t DROP INDEX `PRIMARY`",
Want: []advisor.Advice{
{
@@ -86,6 +88,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
+ Line: 2,
},
},
},
@@ -170,18 +173,20 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
+ Line: 2,
},
},
},
{
// Use MockCatalogService
- Statement: `ALTER TABLE t DROP COLUMN id, DROP COLUMN name`,
+ Statement: `ALTER TABLE tech_book DROP COLUMN id, DROP COLUMN name`,
Want: []advisor.Advice{
{
Status: advisor.Error,
Code: advisor.TableNoPK,
Title: "table.require-pk",
- Content: "Table `t` requires PRIMARY KEY",
+ Content: "Table `tech_book` requires PRIMARY KEY",
+ Line: 1,
},
},
},
@@ -207,6 +212,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `tech_book` requires PRIMARY KEY",
+ Line: 2,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_use_innodb.go b/plugin/advisor/mysql/advisor_use_innodb.go
index be4884f071..86928173ff 100644
--- a/plugin/advisor/mysql/advisor_use_innodb.go
+++ b/plugin/advisor/mysql/advisor_use_innodb.go
@@ -120,6 +120,7 @@ func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {
Code: code,
Title: v.title,
Content: fmt.Sprintf("\"%s\" doesn't use InnoDB engine", in.Text()),
+ Line: in.OriginTextPosition(),
})
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_use_innodb_test.go b/plugin/advisor/mysql/advisor_use_innodb_test.go
index 12958b1514..4f80072047 100644
--- a/plugin/advisor/mysql/advisor_use_innodb_test.go
+++ b/plugin/advisor/mysql/advisor_use_innodb_test.go
@@ -38,6 +38,7 @@ func TestUseInnoDB(t *testing.T) {
Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"CREATE TABLE book(id int) ENGINE = CSV\" doesn't use InnoDB engine",
+ Line: 1,
},
},
},
@@ -60,6 +61,7 @@ func TestUseInnoDB(t *testing.T) {
Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"ALTER TABLE book ENGINE = CSV\" doesn't use InnoDB engine",
+ Line: 1,
},
},
},
@@ -82,6 +84,7 @@ func TestUseInnoDB(t *testing.T) {
Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"SET default_storage_engine=CSV\" doesn't use InnoDB engine",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/mysql/parser.go b/plugin/advisor/mysql/parser.go
index 0a17ca6a9a..15c81035c0 100644
--- a/plugin/advisor/mysql/parser.go
+++ b/plugin/advisor/mysql/parser.go
@@ -1,14 +1,17 @@
package mysql
import (
+ "fmt"
+
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/pingcap/tidb/parser"
+ "github.com/bytebase/bytebase/plugin/parser"
+ tidbparser "github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
)
// Wrapper for parser.New().
-func newParser() *parser.Parser {
- p := parser.New()
+func newParser() *tidbparser.Parser {
+ p := tidbparser.New()
// To support MySQL8 window function syntax.
// See https://github.com/bytebase/bytebase/issues/175.
@@ -31,5 +34,42 @@ func parseStatement(statement string, charset string, collation string) ([]ast.S
},
}
}
+ sqlList, err := parser.SplitMultiSQL(parser.MySQL, statement)
+ if err != nil {
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.Internal,
+ Title: "Split multi-SQL error",
+ Content: err.Error(),
+ },
+ }
+ }
+ if len(sqlList) != len(root) {
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.Internal,
+ Title: "Split multi-SQL error",
+ Content: fmt.Sprintf("split multi-SQL failed: the length should be %d, but get %d. stmt: \"%s\"", len(root), len(sqlList), statement),
+ },
+ }
+ }
+
+ for i, node := range root {
+ node.SetOriginTextPosition(sqlList[i].Line)
+ if n, ok := node.(*ast.CreateTableStmt); ok {
+ if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.Internal,
+ Title: "Set line error",
+ Content: err.Error(),
+ },
+ }
+ }
+ }
+ }
return root, nil
}