Skip to content

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

chore: remove dependency for common in advisor (#1614)

ecmadao179532b1b755
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 93cfe43847..5ff826da33 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -190,3 +191,64 @@ type TaskCheckRunStatusPatch struct {
Code common.Code
Result string
}
+
+// ConvertToErrorCode will convert advisor code into common code
+func ConvertToErrorCode(code advisor.Code) common.Code {
+ switch code {
+ case advisor.Ok:
+ return common.Ok
+ case advisor.Internal:
+ return common.Internal
+ case advisor.DbStatementSyntaxError:
+ return common.DbStatementSyntaxError
+ case advisor.EmptySchemaReviewPolicy:
+ return common.TaskCheckEmptySchemaReviewPolicy
+ case advisor.CompatibilityDropDatabase:
+ return common.CompatibilityDropDatabase
+ case advisor.CompatibilityRenameTable:
+ return common.CompatibilityRenameTable
+ case advisor.CompatibilityDropTable:
+ return common.CompatibilityDropTable
+ case advisor.CompatibilityRenameColumn:
+ return common.CompatibilityRenameColumn
+ case advisor.CompatibilityDropColumn:
+ return common.CompatibilityDropColumn
+ case advisor.CompatibilityAddPrimaryKey:
+ return common.CompatibilityAddPrimaryKey
+ case advisor.CompatibilityAddUniqueKey:
+ return common.CompatibilityAddUniqueKey
+ case advisor.CompatibilityAddForeignKey:
+ return common.CompatibilityAddForeignKey
+ case advisor.CompatibilityAddCheck:
+ return common.CompatibilityAddCheck
+ case advisor.CompatibilityAlterCheck:
+ return common.CompatibilityAlterCheck
+ case advisor.CompatibilityAlterColumn:
+ return common.CompatibilityAlterColumn
+ case advisor.StatementNoWhere:
+ return common.StatementNoWhere
+ case advisor.StatementSelectAll:
+ return common.StatementSelectAll
+ case advisor.StatementLeadingWildcardLike:
+ return common.StatementLeadingWildcardLike
+ case advisor.NamingTableConventionMismatch:
+ return common.NamingTableConventionMismatch
+ case advisor.NamingColumnConventionMismatch:
+ return common.NamingColumnConventionMismatch
+ case advisor.NamingIndexConventionMismatch:
+ return common.NamingIndexConventionMismatch
+ case advisor.NamingUKConventionMismatch:
+ return common.NamingUKConventionMismatch
+ case advisor.NamingFKConventionMismatch:
+ return common.NamingFKConventionMismatch
+ case advisor.NoRequiredColumn:
+ return common.NoRequiredColumn
+ case advisor.ColumnCanNotNull:
+ return common.ColumnCanNotNull
+ case advisor.NotInnoDBEngine:
+ return common.NotInnoDBEngine
+ case advisor.TableNoPK:
+ return common.TableNoPK
+ }
+ return common.Internal
+}
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 6c1cd10793..59f096d516 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -6,7 +6,6 @@ import (
"fmt"
"sync"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap/zapcore"
@@ -115,10 +114,10 @@ const (
// Advice is the result of an advisor.
type Advice struct {
- Status Status `json:"status"`
- Code common.Code `json:"code"`
- Title string `json:"title"`
- Content string `json:"content"`
+ Status Status `json:"status"`
+ Code Code `json:"code"`
+ Title string `json:"title"`
+ Content string `json:"content"`
}
// MarshalLogObject constructs a field that carries Advice.
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
new file mode 100644
index 0000000000..7019f72d33
--- /dev/null
+++ b/plugin/advisor/code.go
@@ -0,0 +1,54 @@
+package advisor
+
+// Code is the error code.
+type Code int
+
+// Application error codes.
+const (
+ Ok Code = 0
+ Internal Code = 1
+
+ // db error
+ DbStatementSyntaxError Code = 102
+
+ // task check error
+ EmptySchemaReviewPolicy Code = 401
+
+ // 10001 ~ 10100 compatibility error code
+ CompatibilityDropDatabase Code = 10001
+ CompatibilityRenameTable Code = 10002
+ CompatibilityDropTable Code = 10003
+ CompatibilityRenameColumn Code = 10004
+ CompatibilityDropColumn Code = 10005
+ CompatibilityAddPrimaryKey Code = 10006
+ CompatibilityAddUniqueKey Code = 10007
+ CompatibilityAddForeignKey Code = 10008
+ CompatibilityAddCheck Code = 10009
+ CompatibilityAlterCheck Code = 10010
+ CompatibilityAlterColumn Code = 10011
+
+ // 10101 ~ 10200 statement error code
+ StatementNoWhere Code = 10101
+ StatementSelectAll Code = 10102
+ StatementLeadingWildcardLike Code = 10103
+
+ // 10201 table naming advisor error code
+ NamingTableConventionMismatch Code = 10201
+ // 10202 column naming advisor error code
+ NamingColumnConventionMismatch Code = 10202
+ // 10203 index naming advisor error code
+ NamingIndexConventionMismatch Code = 10203
+ // 10204 unique key naming advisor error code
+ NamingUKConventionMismatch Code = 10204
+ // 10205 foreign key naming advisor error code
+ NamingFKConventionMismatch Code = 10205
+
+ // 10301 column rule advisor error code
+ NoRequiredColumn Code = 10301
+ ColumnCanNotNull Code = 10302
+
+ NotInnoDBEngine Code = 10401
+
+ // 10501 table rule advisor error code
+ TableNoPK Code = 10501
+)
diff --git a/plugin/advisor/fake/fake_advisor.go b/plugin/advisor/fake/fake_advisor.go
index 9a57a73e98..dfa79ddc4a 100644
--- a/plugin/advisor/fake/fake_advisor.go
+++ b/plugin/advisor/fake/fake_advisor.go
@@ -1,7 +1,6 @@
package fake
import (
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -25,19 +24,19 @@ func (adv *Advisor) Check(ctx advisor.Context, statement string) ([]advisor.Advi
return []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "INFO check",
Content: statement,
},
{
Status: advisor.Warn,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "WARN check",
Content: statement,
},
{
Status: advisor.Error,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "ERROR check",
Content: statement,
},
diff --git a/plugin/advisor/mysql/advisor_column_no_null.go b/plugin/advisor/mysql/advisor_column_no_null.go
index 9d4c995c07..e5b6ee6290 100644
--- a/plugin/advisor/mysql/advisor_column_no_null.go
+++ b/plugin/advisor/mysql/advisor_column_no_null.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -45,7 +44,7 @@ func (adv *ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -107,7 +106,7 @@ func (v *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
for _, column := range columns {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: v.title,
Content: fmt.Sprintf("`%s`.`%s` can not have NULL value", column.tableName, column.columnName),
})
diff --git a/plugin/advisor/mysql/advisor_column_no_null_test.go b/plugin/advisor/mysql/advisor_column_no_null_test.go
index 71d60d4f50..134e028e90 100644
--- a/plugin/advisor/mysql/advisor_column_no_null_test.go
+++ b/plugin/advisor/mysql/advisor_column_no_null_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,13 +13,13 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`id` can not have NULL value",
},
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
},
@@ -31,7 +30,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
},
@@ -42,7 +41,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
},
@@ -53,7 +52,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -64,7 +63,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`id` can not have NULL value",
},
@@ -75,7 +74,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -86,7 +85,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.ColumnCanNotNull,
+ Code: advisor.ColumnCanNotNull,
Title: "column.no-null",
Content: "`book`.`name` can not have NULL value",
},
@@ -97,7 +96,7 @@ func TestColumnNoNull(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
diff --git a/plugin/advisor/mysql/advisor_column_required.go b/plugin/advisor/mysql/advisor_column_required.go
index d4b2fa3d54..9ba7bc13ab 100644
--- a/plugin/advisor/mysql/advisor_column_required.go
+++ b/plugin/advisor/mysql/advisor_column_required.go
@@ -5,7 +5,6 @@ import (
"sort"
"strings"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -122,7 +121,7 @@ func (v *columnRequirementChecker) generateAdviceList() []advisor.Advice {
sort.Strings(missingColumns)
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: v.title,
Content: fmt.Sprintf("Table `%s` requires columns: %s", tableName, strings.Join(missingColumns, ", ")),
})
@@ -132,7 +131,7 @@ func (v *columnRequirementChecker) generateAdviceList() []advisor.Advice {
if len(v.adviceList) == 0 {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
diff --git a/plugin/advisor/mysql/advisor_column_required_test.go b/plugin/advisor/mysql/advisor_column_required_test.go
index eaacafdad3..5ffef3cee3 100644
--- a/plugin/advisor/mysql/advisor_column_required_test.go
+++ b/plugin/advisor/mysql/advisor_column_required_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: created_ts, creator_id, updated_ts, updater_id",
},
@@ -32,7 +31,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -49,7 +48,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
},
@@ -66,7 +65,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -83,7 +82,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
},
@@ -100,7 +99,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -117,7 +116,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
},
@@ -135,7 +134,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -151,7 +150,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: updater_id",
},
@@ -168,7 +167,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -187,13 +186,13 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
},
{
Status: advisor.Warn,
- Code: common.NoRequiredColumn,
+ Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `student` requires columns: creator_id, updater_id",
},
@@ -210,7 +209,7 @@ func TestColumnRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility.go b/plugin/advisor/mysql/advisor_migration_compatibility.go
index 1093a380f6..91de7f83b9 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -46,7 +45,7 @@ func (adv *CompatibilityAdvisor) Check(ctx advisor.Context, statement string) ([
if len(c.adviceList) == 0 {
c.adviceList = append(c.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -62,58 +61,58 @@ type compatibilityChecker struct {
// Enter implements the ast.Visitor interface
func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := common.Ok
+ code := advisor.Ok
switch node := in.(type) {
// DROP DATABASE
case *ast.DropDatabaseStmt:
- code = common.CompatibilityDropDatabase
+ code = advisor.CompatibilityDropDatabase
// RENAME TABLE
case *ast.RenameTableStmt:
- code = common.CompatibilityRenameTable
+ code = advisor.CompatibilityRenameTable
// DROP TABLE/VIEW
case *ast.DropTableStmt:
- code = common.CompatibilityDropTable
+ code = advisor.CompatibilityDropTable
// ALTER TABLE
case *ast.AlterTableStmt:
for _, spec := range node.Specs {
// RENAME COLUMN
if spec.Tp == ast.AlterTableRenameColumn {
- code = common.CompatibilityRenameColumn
+ code = advisor.CompatibilityRenameColumn
break
}
// DROP COLUMN
if spec.Tp == ast.AlterTableDropColumn {
- code = common.CompatibilityDropColumn
+ code = advisor.CompatibilityDropColumn
break
}
// RENAME TABLE
if spec.Tp == ast.AlterTableRenameTable {
- code = common.CompatibilityRenameTable
+ code = advisor.CompatibilityRenameTable
break
}
if spec.Tp == ast.AlterTableAddConstraint {
// ADD PRIMARY KEY
if spec.Constraint.Tp == ast.ConstraintPrimaryKey {
- code = common.CompatibilityAddPrimaryKey
+ code = advisor.CompatibilityAddPrimaryKey
break
}
// ADD UNIQUE/UNIQUE KEY/UNIQUE INDEX
if spec.Constraint.Tp == ast.ConstraintPrimaryKey ||
spec.Constraint.Tp == ast.ConstraintUniq ||
spec.Constraint.Tp == ast.ConstraintUniqKey {
- code = common.CompatibilityAddUniqueKey
+ code = advisor.CompatibilityAddUniqueKey
break
}
// ADD FOREIGN KEY
if spec.Constraint.Tp == ast.ConstraintForeignKey {
- code = common.CompatibilityAddForeignKey
+ code = advisor.CompatibilityAddForeignKey
break
}
// Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
// ADD CHECK ENFORCED
if spec.Constraint.Tp == ast.ConstraintCheck && spec.Constraint.Enforced {
- code = common.CompatibilityAddCheck
+ code = advisor.CompatibilityAddCheck
break
}
}
@@ -122,7 +121,7 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
// ALTER CHECK ENFORCED
if spec.Tp == ast.AlterTableAlterCheck {
if spec.Constraint.Enforced {
- code = common.CompatibilityAlterCheck
+ code = advisor.CompatibilityAlterCheck
break
}
}
@@ -133,7 +132,7 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
// 1. Change to a compatible data type such as INT to BIGINT
// 2. Change property like comment, change it to NULL
if spec.Tp == ast.AlterTableModifyColumn || spec.Tp == ast.AlterTableChangeColumn {
- code = common.CompatibilityAlterColumn
+ code = advisor.CompatibilityAlterColumn
break
}
}
@@ -144,11 +143,11 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
// CREATE UNIQUE INDEX
case *ast.CreateIndexStmt:
if node.KeyType == ast.IndexKeyTypeUnique {
- code = common.CompatibilityAddUniqueKey
+ code = advisor.CompatibilityAddUniqueKey
}
}
- if code != common.Ok {
+ if code != advisor.Ok {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: code,
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility_test.go b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
index c8e3f1db89..4585bf492e 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
@@ -5,7 +5,6 @@ import (
_ "github.com/pingcap/tidb/types/parser_driver"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -16,7 +15,7 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityDropDatabase,
+ Code: advisor.CompatibilityDropDatabase,
Title: "schema.backward-compatibility",
Content: "\"DROP DATABASE d1\" may cause incompatibility with the existing data and code",
},
@@ -27,7 +26,7 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityDropTable,
+ Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1\" may cause incompatibility with the existing data and code",
},
@@ -38,7 +37,7 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityRenameTable,
+ Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"RENAME TABLE t1 to t2\" may cause incompatibility with the existing data and code",
},
@@ -49,7 +48,7 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityDropTable,
+ Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP VIEW v1\" may cause incompatibility with the existing data and code",
},
@@ -60,7 +59,7 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddUniqueKey,
+ Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"CREATE UNIQUE INDEX idx1 ON t1 (f1)\" may cause incompatibility with the existing data and code",
},
@@ -71,13 +70,13 @@ func TestBasic(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityDropTable,
+ Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1;\" may cause incompatibility with the existing data and code",
},
{
Status: advisor.Warn,
- Code: common.CompatibilityDropTable,
+ Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",
},
@@ -99,7 +98,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -110,7 +109,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityRenameColumn,
+ 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",
},
@@ -121,7 +120,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityDropColumn,
+ Code: advisor.CompatibilityDropColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 DROP COLUMN f1\" may cause incompatibility with the existing data and code",
},
@@ -132,7 +131,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddPrimaryKey,
+ Code: advisor.CompatibilityAddPrimaryKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD PRIMARY KEY (f1)\" may cause incompatibility with the existing data and code",
},
@@ -143,7 +142,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddUniqueKey,
+ Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE (f1)\" may cause incompatibility with the existing data and code",
},
@@ -154,7 +153,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddUniqueKey,
+ Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE KEY (f1)\" may cause incompatibility with the existing data and code",
},
@@ -165,7 +164,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddUniqueKey,
+ Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE INDEX (f1)\" may cause incompatibility with the existing data and code",
},
@@ -176,7 +175,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddForeignKey,
+ 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",
},
@@ -187,7 +186,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddCheck,
+ Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
},
@@ -208,7 +207,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterCheck,
+ Code: advisor.CompatibilityAlterCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ALTER CHECK chk1 ENFORCED\" may cause incompatibility with the existing data and code",
},
@@ -229,7 +228,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAddCheck,
+ 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",
},
@@ -240,7 +239,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -251,7 +250,7 @@ func TestAlterTable(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityRenameTable,
+ Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 RENAME TO t2\" may cause incompatibility with the existing data and code",
},
@@ -273,7 +272,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterColumn,
+ Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 CHANGE f1 f2 TEXT\" may cause incompatibility with the existing data and code",
},
@@ -284,7 +283,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterColumn,
+ Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 MODIFY f1 TEXT\" may cause incompatibility with the existing data and code",
},
@@ -295,7 +294,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterColumn,
+ Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 MODIFY f1 TEXT NULL\" may cause incompatibility with the existing data and code",
},
@@ -306,7 +305,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterColumn,
+ 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",
},
@@ -317,7 +316,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.CompatibilityAlterColumn,
+ 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",
},
diff --git a/plugin/advisor/mysql/advisor_naming_column.go b/plugin/advisor/mysql/advisor_naming_column.go
index 3ee018140a..bd8c4bda94 100644
--- a/plugin/advisor/mysql/advisor_naming_column.go
+++ b/plugin/advisor/mysql/advisor_naming_column.go
@@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -52,7 +51,7 @@ func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement s
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -104,7 +103,7 @@ func (v *namingColumnConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if !v.format.MatchString(column) {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: v.title,
Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column, v.format),
})
diff --git a/plugin/advisor/mysql/advisor_naming_column_test.go b/plugin/advisor/mysql/advisor_naming_column_test.go
index db25c1d60b..20578757c8 100644
--- a/plugin/advisor/mysql/advisor_naming_column_test.go
+++ b/plugin/advisor/mysql/advisor_naming_column_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -27,7 +26,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -39,7 +38,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -50,7 +49,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -67,7 +66,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`creatorId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -78,7 +77,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -89,7 +88,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -105,7 +104,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`contentString` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -124,25 +123,25 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`createdTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`book`.`updaterId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`student`.`createdTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "`student`.`updatedTs` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
index 5e403e12c0..740e4af112 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
@@ -4,7 +4,6 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -52,7 +51,7 @@ func (check *NamingFKConventionAdvisor) Check(ctx advisor.Context, statement str
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -78,7 +77,7 @@ func (checker *namingFKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if err != nil {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "Internal error for foreign key naming convention rule",
Content: fmt.Sprintf("%q meet internal error %q", in.Text(), err.Error()),
})
@@ -87,7 +86,7 @@ func (checker *namingFKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if !regex.MatchString(indexData.indexName) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.NamingFKConventionMismatch,
+ 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),
})
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 b729c87ea1..0011181acb 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestNamingFKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -27,7 +26,7 @@ func TestNamingFKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingFKConventionMismatch,
+ 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`",
},
@@ -38,7 +37,7 @@ func TestNamingFKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -49,7 +48,7 @@ func TestNamingFKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingFKConventionMismatch,
+ 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`",
},
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.go
index a1be8c0999..eaa184a6c2 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -3,16 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "log"
"regexp"
"strings"
- "github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
- "go.uber.org/zap"
)
var (
@@ -58,7 +56,7 @@ func (check *NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -85,7 +83,7 @@ func (checker *namingIndexConventionChecker) Enter(in ast.Node) (ast.Node, bool)
if err != nil {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "Internal error for index naming convention rule",
Content: fmt.Sprintf("%q meet internal error %q", in.Text(), err.Error()),
})
@@ -94,7 +92,7 @@ func (checker *namingIndexConventionChecker) Enter(in ast.Node) (ast.Node, bool)
if !regex.MatchString(indexData.indexName) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.NamingIndexConventionMismatch,
+ 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),
})
@@ -148,11 +146,11 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
IndexName: spec.FromKey.String(),
})
if err != nil {
- log.Error(
- "Cannot find index in table",
- zap.String("table_name", node.Table.Name.String()),
- zap.String("index_name", spec.FromKey.String()),
- zap.Error(err),
+ log.Printf(
+ "Cannot find index %s in table %s with error %v\n",
+ node.Table.Name.String(),
+ spec.FromKey.String(),
+ err,
)
continue
}
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
index 6112d0d8ed..b3f0abca71 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
@@ -6,7 +6,6 @@ import (
"strings"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -18,7 +17,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -29,7 +28,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingIndexConventionMismatch,
+ 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`",
},
@@ -44,7 +43,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -58,7 +57,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingIndexConventionMismatch,
+ 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`",
},
@@ -69,7 +68,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -80,7 +79,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingIndexConventionMismatch,
+ 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`",
},
@@ -91,7 +90,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -102,7 +101,7 @@ func TestNamingIndexConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingIndexConventionMismatch,
+ Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_name$\" but found ``",
},
diff --git a/plugin/advisor/mysql/advisor_naming_table.go b/plugin/advisor/mysql/advisor_naming_table.go
index d6c9f02190..762895a82b 100644
--- a/plugin/advisor/mysql/advisor_naming_table.go
+++ b/plugin/advisor/mysql/advisor_naming_table.go
@@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -50,7 +49,7 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -92,7 +91,7 @@ func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if !v.format.MatchString(tableName) {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: v.title,
Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, v.format),
})
diff --git a/plugin/advisor/mysql/advisor_naming_table_test.go b/plugin/advisor/mysql/advisor_naming_table_test.go
index 32b0bea5a0..0ade956178 100644
--- a/plugin/advisor/mysql/advisor_naming_table_test.go
+++ b/plugin/advisor/mysql/advisor_naming_table_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`techBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -27,7 +26,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -38,7 +37,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`TechBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -49,7 +48,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -60,7 +59,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`LiteraryBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -71,13 +70,13 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`TechBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "`LiteraryBook` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -88,7 +87,7 @@ func TestMySQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
index 1825518394..9dc0abc310 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
@@ -3,15 +3,13 @@ package mysql
import (
"context"
"fmt"
+ "log"
"strings"
- "github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
- "go.uber.org/zap"
)
var (
@@ -57,7 +55,7 @@ func (check *NamingUKConventionAdvisor) Check(ctx advisor.Context, statement str
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -83,7 +81,7 @@ func (checker *namingUKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if err != nil {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "Internal error for unique key naming convention rule",
Content: fmt.Sprintf("%q meet internal error %q", in.Text(), err.Error()),
})
@@ -92,7 +90,7 @@ func (checker *namingUKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
if !regex.MatchString(indexData.indexName) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.NamingUKConventionMismatch,
+ 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),
})
@@ -141,11 +139,11 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
IndexName: spec.FromKey.String(),
})
if err != nil {
- log.Error(
- "Cannot find index in table",
- zap.String("table_name", node.Table.Name.String()),
- zap.String("index_name", spec.FromKey.String()),
- zap.Error(err),
+ log.Printf(
+ "Cannot find index %s in table %s with error %v\n",
+ node.Table.Name.String(),
+ spec.FromKey.String(),
+ err,
)
continue
}
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 01162d88ab..76bd1b1665 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
@@ -6,7 +6,6 @@ import (
"strings"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -18,7 +17,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -29,7 +28,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ 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`",
},
@@ -40,7 +39,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -51,7 +50,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ 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`",
},
@@ -66,7 +65,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -80,7 +79,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ 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`",
},
@@ -91,7 +90,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -102,7 +101,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",
},
@@ -113,7 +112,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",
},
@@ -124,7 +123,7 @@ func TestNamingUKConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingUKConventionMismatch,
+ Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",
},
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 d32ac007de..ea9ddeea09 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -48,7 +47,7 @@ func (adv *NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement st
if checker.leadingWildcardLike {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.StatementLeadingWildcardLike,
+ Code: advisor.StatementLeadingWildcardLike,
Title: string(ctx.Rule.Type),
Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", checker.text),
})
@@ -58,7 +57,7 @@ func (adv *NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement st
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -81,7 +80,7 @@ func (v *noLeadingWildcardLikeChecker) Enter(in ast.Node) (ast.Node, bool) {
if err != nil {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "Internal error for no leading wildcard LIKE rule",
Content: fmt.Sprintf("\"%s\" meet internal error %q", v.text, err.Error()),
})
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 339293f486..5d809f35f5 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
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,7 +13,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -25,7 +24,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementLeadingWildcardLike,
+ Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",
},
@@ -36,7 +35,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementLeadingWildcardLike,
+ 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",
},
@@ -47,7 +46,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementLeadingWildcardLike,
+ 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",
},
@@ -58,7 +57,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementLeadingWildcardLike,
+ 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",
},
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all.go b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
index 7f703117f0..f131a68e88 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -45,7 +44,7 @@ func (adv *NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]a
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -67,7 +66,7 @@ func (v *noSelectAllChecker) Enter(in ast.Node) (ast.Node, bool) {
if field.WildCard != nil {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.StatementSelectAll,
+ Code: advisor.StatementSelectAll,
Title: v.title,
Content: fmt.Sprintf("\"%s\" uses SELECT all", v.text),
})
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 8637937833..48f7472ff6 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,7 +13,7 @@ func TestNoSelectAll(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementSelectAll,
+ Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT * FROM t\" uses SELECT all",
},
@@ -25,7 +24,7 @@ func TestNoSelectAll(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -36,7 +35,7 @@ func TestNoSelectAll(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.StatementSelectAll,
+ Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT a, b FROM (SELECT * from t1 JOIN t2) t\" uses SELECT all",
},
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required.go b/plugin/advisor/mysql/advisor_stmt_where_required.go
index c2f9ffc84c..4aef999c69 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required.go
@@ -3,7 +3,6 @@ package mysql
import (
"fmt"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -46,7 +45,7 @@ func (adv *WhereRequirementAdvisor) Check(ctx advisor.Context, statement string)
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -63,26 +62,26 @@ type whereRequirementChecker struct {
// Enter implements the ast.Visitor interface
func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := common.Ok
+ code := advisor.Ok
switch node := in.(type) {
// DELETE
case *ast.DeleteStmt:
if node.Where == nil {
- code = common.StatementNoWhere
+ code = advisor.StatementNoWhere
}
// UPDATE
case *ast.UpdateStmt:
if node.Where == nil {
- code = common.StatementNoWhere
+ code = advisor.StatementNoWhere
}
// SELECT
case *ast.SelectStmt:
if node.Where == nil {
- code = common.StatementNoWhere
+ code = advisor.StatementNoWhere
}
}
- if code != common.Ok {
+ if code != advisor.Ok {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: code,
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index b86875276c..01a5c7f1d3 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,7 +13,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.StatementNoWhere,
+ Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"DELETE FROM t1\" requires WHERE clause",
},
@@ -25,7 +24,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.StatementNoWhere,
+ Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
},
@@ -36,7 +35,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -47,7 +46,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -58,7 +57,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.StatementNoWhere,
+ Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t\" requires WHERE clause",
},
@@ -69,7 +68,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -80,7 +79,7 @@ func TestWhereRequirement(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.StatementNoWhere,
+ Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
},
diff --git a/plugin/advisor/mysql/advisor_syntax.go b/plugin/advisor/mysql/advisor_syntax.go
index b049b5e50e..4aaba6add8 100644
--- a/plugin/advisor/mysql/advisor_syntax.go
+++ b/plugin/advisor/mysql/advisor_syntax.go
@@ -1,7 +1,6 @@
package mysql
import (
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -28,7 +27,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
return []advisor.Advice{
{
Status: advisor.Error,
- Code: common.DbStatementSyntaxError,
+ Code: advisor.DbStatementSyntaxError,
Title: "Syntax error",
Content: err.Error(),
},
@@ -39,7 +38,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
for _, warn := range warns {
adviceList = append(adviceList, advisor.Advice{
Status: advisor.Warn,
- Code: common.DbStatementSyntaxError,
+ Code: advisor.DbStatementSyntaxError,
Title: "Syntax Warning",
Content: warn.Error(),
})
@@ -47,7 +46,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
adviceList = append(adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "Syntax OK",
Content: "OK"})
return adviceList, nil
diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.go
index cd6e8b6381..cd4a17cb89 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk.go
@@ -3,15 +3,13 @@ package mysql
import (
"context"
"fmt"
+ "log"
"strings"
- "github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
- "go.uber.org/zap"
)
const (
@@ -124,7 +122,7 @@ func (v *tableRequirePKChecker) generateAdviceList() []advisor.Advice {
if len(v.tables[tableName]) == 0 {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: v.title,
Content: fmt.Sprintf("Table `%s` requires PRIMARY KEY", tableName),
})
@@ -134,7 +132,7 @@ func (v *tableRequirePKChecker) generateAdviceList() []advisor.Advice {
if len(v.adviceList) == 0 {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -167,10 +165,10 @@ func (v *tableRequirePKChecker) dropColumn(table string, column string) {
IndexName: primaryKeyName,
})
if err != nil {
- log.Error(
- "Cannot find primary key in table",
- zap.String("table_name", table),
- zap.Error(err),
+ log.Printf(
+ "Cannot find primary key in table %s with error %v\n",
+ table,
+ err,
)
return
}
diff --git a/plugin/advisor/mysql/advisor_table_require_pk_test.go b/plugin/advisor/mysql/advisor_table_require_pk_test.go
index 770d37b2ff..1dfe126e61 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk_test.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,7 +13,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -25,7 +24,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -36,7 +35,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
@@ -48,7 +47,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -60,7 +59,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -72,7 +71,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
@@ -84,7 +83,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
@@ -96,7 +95,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -108,7 +107,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -120,7 +119,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -132,7 +131,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -144,7 +143,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -156,7 +155,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -168,7 +167,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
@@ -180,7 +179,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
@@ -192,7 +191,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -205,7 +204,7 @@ func TestRequirePK(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.TableNoPK,
+ Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table `t` requires PRIMARY KEY",
},
diff --git a/plugin/advisor/mysql/advisor_use_innodb.go b/plugin/advisor/mysql/advisor_use_innodb.go
index 463e63a927..ce79aa00ec 100644
--- a/plugin/advisor/mysql/advisor_use_innodb.go
+++ b/plugin/advisor/mysql/advisor_use_innodb.go
@@ -4,7 +4,6 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
@@ -52,7 +51,7 @@ func (adv *UseInnoDBAdvisor) Check(ctx advisor.Context, statement string) ([]adv
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -68,13 +67,13 @@ type useInnoDBChecker struct {
// Enter implements the ast.Visitor interface
func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := common.Ok
+ code := advisor.Ok
switch node := in.(type) {
// CREATE TABLE
case *ast.CreateTableStmt:
for _, option := range node.Options {
if option.Tp == ast.TableOptionEngine && strings.ToLower(option.StrValue) != innoDB {
- code = common.NotInnoDBEngine
+ code = advisor.NotInnoDBEngine
break
}
}
@@ -85,7 +84,7 @@ func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {
if spec.Tp == ast.AlterTableOption {
for _, option := range spec.Options {
if option.Tp == ast.TableOptionEngine && strings.ToLower(option.StrValue) != innoDB {
- code = common.NotInnoDBEngine
+ code = advisor.NotInnoDBEngine
break
}
}
@@ -100,21 +99,21 @@ func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {
if err != nil {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
- Code: common.Internal,
+ Code: advisor.Internal,
Title: "Internal error for use InnoDB rule",
Content: fmt.Sprintf("\"%s\" meet internal error %q", in.Text(), err.Error()),
})
continue
}
if text != innoDB {
- code = common.NotInnoDBEngine
+ code = advisor.NotInnoDBEngine
break
}
}
}
}
- if code != common.Ok {
+ if code != advisor.Ok {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: code,
diff --git a/plugin/advisor/mysql/advisor_use_innodb_test.go b/plugin/advisor/mysql/advisor_use_innodb_test.go
index 9a937f105a..2de234813c 100644
--- a/plugin/advisor/mysql/advisor_use_innodb_test.go
+++ b/plugin/advisor/mysql/advisor_use_innodb_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -14,7 +13,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -25,7 +24,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -36,7 +35,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NotInnoDBEngine,
+ Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"CREATE TABLE book(id int) ENGINE = CSV\" doesn't use InnoDB engine",
},
@@ -47,7 +46,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -58,7 +57,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NotInnoDBEngine,
+ Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"ALTER TABLE book ENGINE = CSV\" doesn't use InnoDB engine",
},
@@ -69,7 +68,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -80,7 +79,7 @@ func TestUseInnoDB(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NotInnoDBEngine,
+ Code: advisor.NotInnoDBEngine,
Title: "engine.mysql.use-innodb",
Content: "\"SET default_storage_engine=CSV\" doesn't use InnoDB engine",
},
diff --git a/plugin/advisor/mysql/parser.go b/plugin/advisor/mysql/parser.go
index a6ff6b21fd..0d7416e153 100644
--- a/plugin/advisor/mysql/parser.go
+++ b/plugin/advisor/mysql/parser.go
@@ -1,7 +1,6 @@
package mysql
import (
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
@@ -26,7 +25,7 @@ func parseStatement(statement string, charset string, collation string) ([]ast.S
return nil, []advisor.Advice{
{
Status: advisor.Error,
- Code: common.DbStatementSyntaxError,
+ Code: advisor.DbStatementSyntaxError,
Title: advisor.SyntaxErrorTitle,
Content: err.Error(),
},
diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.go
index 7d6f43e458..43e3089cc7 100644
--- a/plugin/advisor/pg/advisor_naming_column.go
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -7,7 +7,6 @@ import (
"github.com/auxten/postgresql-parser/pkg/sql/parser"
"github.com/auxten/postgresql-parser/pkg/sql/sem/tree"
"github.com/auxten/postgresql-parser/pkg/walk"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -58,7 +57,7 @@ func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement s
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -105,7 +104,7 @@ func (checker *namingColumnConventionChecker) check(ctx interface{}, node interf
if !checker.format.MatchString(column) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, naming format should be %q", tableName, column, checker.format),
})
diff --git a/plugin/advisor/pg/advisor_naming_column_test.go b/plugin/advisor/pg/advisor_naming_column_test.go
index d0c38432ca..cb02ac6546 100644
--- a/plugin/advisor/pg/advisor_naming_column_test.go
+++ b/plugin/advisor/pg/advisor_naming_column_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -28,7 +27,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -39,7 +38,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -51,7 +50,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -63,7 +62,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -75,7 +74,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Warn,
- Code: common.NamingColumnConventionMismatch,
+ Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -87,7 +86,7 @@ func TestNamingColumnConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index 6743d063c1..f4720fff52 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -7,7 +7,6 @@ import (
"github.com/auxten/postgresql-parser/pkg/sql/parser"
"github.com/auxten/postgresql-parser/pkg/sql/sem/tree"
"github.com/auxten/postgresql-parser/pkg/walk"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -58,7 +57,7 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
@@ -98,7 +97,7 @@ func (checker *namingTableConventionChecker) check(ctx interface{}, node interfa
if !checker.format.MatchString(tableName) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, checker.format),
})
diff --git a/plugin/advisor/pg/advisor_naming_table_test.go b/plugin/advisor/pg/advisor_naming_table_test.go
index ff1f7ededc..a4a1ebe761 100644
--- a/plugin/advisor/pg/advisor_naming_table_test.go
+++ b/plugin/advisor/pg/advisor_naming_table_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -27,7 +26,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"_techbook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -39,7 +38,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -50,7 +49,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
@@ -61,7 +60,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.NamingTableConventionMismatch,
+ Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"TechBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
@@ -72,7 +71,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
},
diff --git a/plugin/advisor/pg/advisor_syntax.go b/plugin/advisor/pg/advisor_syntax.go
index dca4c3e006..b56d3f5866 100644
--- a/plugin/advisor/pg/advisor_syntax.go
+++ b/plugin/advisor/pg/advisor_syntax.go
@@ -1,7 +1,6 @@
package pg
import (
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -27,7 +26,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
return []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "Syntax OK",
Content: "OK",
},
diff --git a/plugin/advisor/pg/advisor_syntax_test.go b/plugin/advisor/pg/advisor_syntax_test.go
index df511fe3ac..c5d1f02be3 100644
--- a/plugin/advisor/pg/advisor_syntax_test.go
+++ b/plugin/advisor/pg/advisor_syntax_test.go
@@ -4,7 +4,6 @@ import (
"testing"
"github.com/bmizerany/assert"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
)
@@ -16,7 +15,7 @@ func TestPostgreSQLSyntax(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "Syntax OK",
Content: "OK",
},
@@ -27,7 +26,7 @@ func TestPostgreSQLSyntax(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: common.DbStatementSyntaxError,
+ Code: advisor.DbStatementSyntaxError,
Title: "Syntax error",
Content: "at or near \"engine\": syntax error",
},
diff --git a/plugin/advisor/pg/parser.go b/plugin/advisor/pg/parser.go
index 9611bdcfb3..801e7ea053 100644
--- a/plugin/advisor/pg/parser.go
+++ b/plugin/advisor/pg/parser.go
@@ -2,7 +2,6 @@ package pg
import (
"github.com/auxten/postgresql-parser/pkg/sql/parser"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -12,7 +11,7 @@ func parseStatement(statement string) (parser.Statements, []advisor.Advice) {
return nil, []advisor.Advice{
{
Status: advisor.Error,
- Code: common.DbStatementSyntaxError,
+ Code: advisor.DbStatementSyntaxError,
Title: advisor.SyntaxErrorTitle,
Content: err.Error(),
},
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index a77aac4dcd..f6a983a5ab 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -4,19 +4,18 @@ import (
"context"
"encoding/json"
"fmt"
+ "log"
"regexp"
- "github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
- "go.uber.org/zap"
)
// How to add a schema review rule:
// 1. Implement an advisor.(plugin/advisor/mysql or plugin/advisor/pg)
// 2. Register this advisor in map[db.Type][AdvisorType].(plugin/advisor.go)
-// 3. Map SchemaReviewRuleType to advisor.Type in getAdvisorTypeByRule(current file).
+// 3. Add advisor error code if needed. After new code added, we also need to update the ConvertToErrorCode in api/task_check_run.go
+// 4. Map SchemaReviewRuleType to advisor.Type in getAdvisorTypeByRule(current file).
// SchemaReviewRuleLevel is the error level for schema review rule.
type SchemaReviewRuleLevel string
@@ -179,7 +178,7 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
}
template := nr.Format
- keys, _ := common.ParseTemplateTokens(template)
+ keys, _ := parseTemplateTokens(template)
for _, key := range keys {
if _, ok := TemplateNamingTokens[ruleType][key]; !ok {
@@ -190,6 +189,27 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
return template, keys, nil
}
+// parseTemplateTokens parses the template and returns template tokens and their delimiters.
+// For example, if the template is "{{DB_NAME}}_hello_{{LOCATION}}", then the tokens will be ["{{DB_NAME}}", "{{LOCATION}}"],
+// and the delimiters will be ["_hello_"].
+// The caller will usually replace the tokens with a normal string, or a regexp. In the latter case, it will be a problem
+// if there are special regexp characters like "$" in the delimiters. The caller should escape the delimiters in such cases.
+func parseTemplateTokens(template string) ([]string, []string) {
+ r := regexp.MustCompile(`{{[^{}]+}}`)
+ tokens := r.FindAllString(template, -1)
+ if len(tokens) > 0 {
+ split := r.Split(template, -1)
+ var delimiters []string
+ for _, s := range split {
+ if s != "" {
+ delimiters = append(delimiters, s)
+ }
+ }
+ return tokens, delimiters
+ }
+ return nil, nil
+}
+
// UnmarshalRequiredColumnRulePayload will unmarshal payload to RequiredColumnRulePayload.
func UnmarshalRequiredColumnRulePayload(payload string) (*RequiredColumnRulePayload, error) {
var rcr RequiredColumnRulePayload
@@ -220,7 +240,7 @@ func SchemaReviewCheck(ctx context.Context, statements string, policy *SchemaRev
advisorType, err := getAdvisorTypeByRule(rule.Type, context.DbType)
if err != nil {
- log.Debug("not supported rule", zap.Error(err))
+ log.Printf("not supported rule: %v. error: %v\n", rule.Type, err)
continue
}
@@ -236,7 +256,7 @@ func SchemaReviewCheck(ctx context.Context, statements string, policy *SchemaRev
statements,
)
if err != nil {
- return nil, common.Errorf(common.Internal, fmt.Errorf("failed to check statement: %w", err))
+ return nil, fmt.Errorf("failed to check statement: %w", err)
}
result = append(result, adviceList...)
@@ -249,7 +269,7 @@ func SchemaReviewCheck(ctx context.Context, statements string, policy *SchemaRev
if len(result) == 0 {
result = append(result, Advice{
Status: Success,
- Code: common.Ok,
+ Code: Ok,
Title: "OK",
Content: "",
})
diff --git a/server/sql.go b/server/sql.go
index c601515649..4554eacd6c 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -770,7 +770,7 @@ func (s *Server) sqlCheck(ctx context.Context, instance *api.Instance, exec *api
adviceLevel = advisor.Warn
adviceList = append(adviceList, advisor.Advice{
Status: advisor.Warn,
- Code: common.TaskCheckEmptySchemaReviewPolicy,
+ Code: advisor.EmptySchemaReviewPolicy,
Title: "Empty schema review policy or disabled",
Content: "",
})
@@ -822,7 +822,7 @@ func (s *Server) sqlCheck(ctx context.Context, instance *api.Instance, exec *api
if len(adviceList) == 0 {
adviceList = append(adviceList, advisor.Advice{
Status: advisor.Success,
- Code: common.Ok,
+ Code: advisor.Ok,
Title: "OK",
Content: "",
})
diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.go
index 253e6a0fc7..b80bad8486 100644
--- a/server/task_check_executor_statement_advisor_composite.go
+++ b/server/task_check_executor_statement_advisor_composite.go
@@ -86,7 +86,7 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
result = append(result, api.TaskCheckResult{
Status: status,
- Code: advice.Code,
+ Code: api.ConvertToErrorCode(advice.Code),
Title: advice.Title,
Content: advice.Content,
})
diff --git a/server/task_check_executor_statement_advisor_simple.go b/server/task_check_executor_statement_advisor_simple.go
index 56cd75cc23..7f6f62a12a 100644
--- a/server/task_check_executor_statement_advisor_simple.go
+++ b/server/task_check_executor_statement_advisor_simple.go
@@ -69,7 +69,7 @@ func (exec *TaskCheckStatementAdvisorSimpleExecutor) Run(ctx context.Context, se
result = append(result, api.TaskCheckResult{
Status: status,
- Code: advice.Code,
+ Code: api.ConvertToErrorCode(advice.Code),
Title: advice.Title,
Content: advice.Content,
})