Skip to content

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

chore: update advisor plugin (#1650)

ecmadao32d59410ae42
diff --git a/api/api_dependency_gate.go b/api/api_dependency_gate.go
index 17b53df9e7..e938d41ab4 100644
--- a/api/api_dependency_gate.go
+++ b/api/api_dependency_gate.go
@@ -11,7 +11,6 @@ import (
// TODO(rebelice): fix the incorrect dependency and uncomment these
_ "github.com/bytebase/bytebase/plugin/advisor"
- _ "github.com/bytebase/bytebase/plugin/catalog"
_ "github.com/bytebase/bytebase/plugin/db"
_ "github.com/bytebase/bytebase/plugin/metric"
_ "github.com/bytebase/bytebase/plugin/vcs"
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 5ff826da33..55c1ef9c8f 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -2,6 +2,7 @@ package api
import (
"encoding/json"
+ "fmt"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -98,12 +99,23 @@ type TaskCheckDatabaseStatementAdvisePayload struct {
PolicyID int `json:"policyID,omitempty"`
}
+// Namespace is the namespace for task check result.
+type Namespace string
+
+const (
+ // AdvisorNamespace is task check result namespace for advisor.
+ AdvisorNamespace Namespace = "bb.advisor"
+ // BBNamespace is task check result namespace for bytebase.
+ BBNamespace Namespace = "bb.core"
+)
+
// TaskCheckResult is the result of task checks.
type TaskCheckResult struct {
- Status TaskCheckStatus `json:"status,omitempty"`
- Code common.Code `json:"code,omitempty"`
- Title string `json:"title,omitempty"`
- Content string `json:"content,omitempty"`
+ Namespace Namespace `json:"namespace,omitempty"`
+ Code int `json:"code,omitempty"`
+ Status TaskCheckStatus `json:"status,omitempty"`
+ Title string `json:"title,omitempty"`
+ Content string `json:"content,omitempty"`
}
// TaskCheckRunResultPayload is the result payload of a task check run.
@@ -192,63 +204,36 @@ type TaskCheckRunStatusPatch struct {
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
+// ConvertToAdvisorDBType will convert db type into advisor db type
+func ConvertToAdvisorDBType(dbType db.Type) (advisor.DBType, error) {
+ switch dbType {
+ case db.MySQL:
+ return advisor.MySQL, nil
+ case db.Postgres:
+ return advisor.Postgres, nil
+ case db.TiDB:
+ return advisor.TiDB, nil
}
- return common.Internal
+
+ return "", fmt.Errorf("unsupported db type %s for advisor", dbType)
+}
+
+// IsSyntaxCheckSupported checks the engine type if syntax check supports it.
+func IsSyntaxCheckSupported(dbType db.Type) bool {
+ advisorDB, err := ConvertToAdvisorDBType(dbType)
+ if err != nil {
+ return false
+ }
+
+ return advisor.IsSyntaxCheckSupported(advisorDB)
+}
+
+// IsSchemaReviewSupported checks the engine type if schema review supports it.
+func IsSchemaReviewSupported(dbType db.Type) bool {
+ advisorDB, err := ConvertToAdvisorDBType(dbType)
+ if err != nil {
+ return false
+ }
+
+ return advisor.IsSchemaReviewSupported(advisorDB)
}
diff --git a/common/error.go b/common/error.go
index 70346412ae..c2436ce4ae 100644
--- a/common/error.go
+++ b/common/error.go
@@ -19,9 +19,8 @@ const (
NotImplemented Code = 6
// 101 ~ 199 db error
- DbConnectionFailure Code = 101
- DbStatementSyntaxError Code = 102
- DbExecutionError Code = 103
+ DbConnectionFailure Code = 101
+ DbExecutionError Code = 102
// 201 db migration error
// Db migration is a core feature, so we separate it from the db error
@@ -34,48 +33,13 @@ const (
// 301 task error
TaskTimingNotAllowed Code = 301
-
- // 401 task check error
- TaskCheckEmptySchemaReviewPolicy Code = 401
-
- // 10001 advisor 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
-
- 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
)
+// Int returns the int type of code.
+func (c Code) Int() int {
+ return int(c)
+}
+
// Error represents an application-specific error. Application errors can be
// unwrapped by the caller to extract out the code & message.
//
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 26e32b2d2d..6f1dbbad24 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -6,8 +6,7 @@ import (
"fmt"
"sync"
- "github.com/bytebase/bytebase/plugin/catalog"
- "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"go.uber.org/zap/zapcore"
)
@@ -159,13 +158,13 @@ type Advisor interface {
var (
advisorMu sync.RWMutex
- advisors = make(map[db.Type]map[Type]Advisor)
+ advisors = make(map[DBType]map[Type]Advisor)
)
// Register makes a advisor available by the provided id.
// If Register is called twice with the same name or if advisor is nil,
// it panics.
-func Register(dbType db.Type, advType Type, f Advisor) {
+func Register(dbType DBType, advType Type, f Advisor) {
advisorMu.Lock()
defer advisorMu.Unlock()
if f == nil {
@@ -185,7 +184,7 @@ func Register(dbType db.Type, advType Type, f Advisor) {
}
// Check runs the advisor and returns the advices.
-func Check(dbType db.Type, advType Type, ctx Context, statement string) ([]Advice, error) {
+func Check(dbType DBType, advType Type, ctx Context, statement string) ([]Advice, error) {
advisorMu.RLock()
dbAdvisors, ok := advisors[dbType]
defer advisorMu.RUnlock()
@@ -202,16 +201,16 @@ func Check(dbType db.Type, advType Type, ctx Context, statement string) ([]Advic
}
// IsSyntaxCheckSupported checks the engine type if syntax check supports it.
-func IsSyntaxCheckSupported(engine db.Type) bool {
- if engine == db.MySQL || engine == db.TiDB {
+func IsSyntaxCheckSupported(dbType DBType) bool {
+ if dbType == MySQL || dbType == TiDB {
return true
}
return false
}
// IsSchemaReviewSupported checks the engine type if schema review supports it.
-func IsSchemaReviewSupported(engine db.Type) bool {
- if engine == db.MySQL || engine == db.TiDB || engine == db.Postgres {
+func IsSchemaReviewSupported(dbType DBType) bool {
+ if dbType == MySQL || dbType == TiDB || dbType == Postgres {
return true
}
return false
diff --git a/plugin/catalog/catalog.go b/plugin/advisor/catalog/catalog.go
similarity index 100%
rename from plugin/catalog/catalog.go
rename to plugin/advisor/catalog/catalog.go
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 7019f72d33..540d2e34ca 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -7,48 +7,51 @@ type Code int
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
+ NotFound Code = 2
+
+ // 101 ~ 199 compatibility error code
+ CompatibilityDropDatabase Code = 101
+ CompatibilityRenameTable Code = 102
+ CompatibilityDropTable Code = 103
+ CompatibilityRenameColumn Code = 104
+ CompatibilityDropColumn Code = 105
+ CompatibilityAddPrimaryKey Code = 106
+ CompatibilityAddUniqueKey Code = 107
+ CompatibilityAddForeignKey Code = 108
+ CompatibilityAddCheck Code = 109
+ CompatibilityAlterCheck Code = 110
+ CompatibilityAlterColumn Code = 111
+
+ // 201 ~ 299 statement error code
+ StatementSyntaxError Code = 201
+ StatementNoWhere Code = 202
+ StatementSelectAll Code = 203
+ StatementLeadingWildcardLike Code = 204
+
+ // 301 ~ 399 naming error code
+ // 301 table naming advisor error code
+ NamingTableConventionMismatch Code = 301
+ // 302 column naming advisor error code
+ NamingColumnConventionMismatch Code = 302
+ // 303 index naming advisor error code
+ NamingIndexConventionMismatch Code = 303
+ // 304 unique key naming advisor error code
+ NamingUKConventionMismatch Code = 304
+ // 305 foreign key naming advisor error code
+ NamingFKConventionMismatch Code = 305
+
+ // 401 ~ 499 column error code
+ NoRequiredColumn Code = 401
+ ColumnCanNotNull Code = 402
+
+ // 501 engine error code
+ NotInnoDBEngine Code = 501
+
+ // 601 table rule advisor error code
+ TableNoPK Code = 601
)
+
+// Int returns the int type of code.
+func (c Code) Int() int {
+ return int(c)
+}
diff --git a/plugin/advisor/db.go b/plugin/advisor/db.go
new file mode 100644
index 0000000000..82ed84b2d4
--- /dev/null
+++ b/plugin/advisor/db.go
@@ -0,0 +1,13 @@
+package advisor
+
+// DBType is the type of a database.
+type DBType string
+
+const (
+ // MySQL is the database type for MYSQL.
+ MySQL DBType = "MYSQL"
+ // Postgres is the database type for POSTGRES.
+ Postgres DBType = "POSTGRES"
+ // TiDB is the database type for TiDB.
+ TiDB DBType = "TIDB"
+)
diff --git a/plugin/advisor/fake/fake_advisor.go b/plugin/advisor/fake/fake_advisor.go
index dfa79ddc4a..986b094932 100644
--- a/plugin/advisor/fake/fake_advisor.go
+++ b/plugin/advisor/fake/fake_advisor.go
@@ -2,7 +2,6 @@ package fake
import (
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
)
var (
@@ -10,9 +9,9 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.Fake, &Advisor{})
- advisor.Register(db.Postgres, advisor.Fake, &Advisor{})
- advisor.Register(db.TiDB, advisor.Fake, &Advisor{})
+ advisor.Register(advisor.MySQL, advisor.Fake, &Advisor{})
+ advisor.Register(advisor.Postgres, advisor.Fake, &Advisor{})
+ advisor.Register(advisor.TiDB, advisor.Fake, &Advisor{})
}
// Advisor is the fake sql advisor.
diff --git a/plugin/advisor/mysql/advisor_column_no_null.go b/plugin/advisor/mysql/advisor_column_no_null.go
index e5b6ee6290..fe2f3b2ac4 100644
--- a/plugin/advisor/mysql/advisor_column_no_null.go
+++ b/plugin/advisor/mysql/advisor_column_no_null.go
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -13,8 +12,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLColumnNoNull, &ColumnNoNullAdvisor{})
}
// ColumnNoNullAdvisor is the advisor checking for column no NULL value.
diff --git a/plugin/advisor/mysql/advisor_column_required.go b/plugin/advisor/mysql/advisor_column_required.go
index 9ba7bc13ab..a6dfcc0cbe 100644
--- a/plugin/advisor/mysql/advisor_column_required.go
+++ b/plugin/advisor/mysql/advisor_column_required.go
@@ -6,7 +6,6 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -15,8 +14,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLColumnRequirement, &ColumnRequirementAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLColumnRequirement, &ColumnRequirementAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLColumnRequirement, &ColumnRequirementAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLColumnRequirement, &ColumnRequirementAdvisor{})
}
// ColumnRequirementAdvisor is the advisor checking for column requirement.
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility.go b/plugin/advisor/mysql/advisor_migration_compatibility.go
index 91de7f83b9..bf0d0bcb7d 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility.go
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -14,8 +13,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
}
// CompatibilityAdvisor is the advisor checking for schema backward compatibility.
diff --git a/plugin/advisor/mysql/advisor_naming_column.go b/plugin/advisor/mysql/advisor_naming_column.go
index bd8c4bda94..ccb27d3d10 100644
--- a/plugin/advisor/mysql/advisor_naming_column.go
+++ b/plugin/advisor/mysql/advisor_naming_column.go
@@ -5,7 +5,6 @@ import (
"regexp"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -14,8 +13,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
}
// NamingColumnConventionAdvisor is the advisor checking for column naming convention.
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
index 740e4af112..658bd63608 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
@@ -5,7 +5,6 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -14,8 +13,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNamingFKConvention, &NamingFKConventionAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNamingFKConvention, &NamingFKConventionAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNamingFKConvention, &NamingFKConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNamingFKConvention, &NamingFKConventionAdvisor{})
}
// NamingFKConventionAdvisor is the advisor checking for foreign key naming convention.
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.go
index eaa184a6c2..978076c205 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -8,8 +8,7 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/catalog"
- "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/pingcap/tidb/parser/ast"
)
@@ -18,8 +17,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNamingIndexConvention, &NamingIndexConventionAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNamingIndexConvention, &NamingIndexConventionAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNamingIndexConvention, &NamingIndexConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNamingIndexConvention, &NamingIndexConventionAdvisor{})
}
// NamingIndexConventionAdvisor is the advisor checking for index naming convention.
diff --git a/plugin/advisor/mysql/advisor_naming_table.go b/plugin/advisor/mysql/advisor_naming_table.go
index 762895a82b..b1e513957c 100644
--- a/plugin/advisor/mysql/advisor_naming_table.go
+++ b/plugin/advisor/mysql/advisor_naming_table.go
@@ -5,7 +5,6 @@ import (
"regexp"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -14,8 +13,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
}
// NamingTableConventionAdvisor is the advisor checking for table naming convention.
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
index 9dc0abc310..9120e16219 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
@@ -7,8 +7,7 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/catalog"
- "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/pingcap/tidb/parser/ast"
)
@@ -17,8 +16,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNamingUKConvention, &NamingUKConventionAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNamingUKConvention, &NamingUKConventionAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNamingUKConvention, &NamingUKConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNamingUKConvention, &NamingUKConventionAdvisor{})
}
// NamingUKConventionAdvisor is the advisor checking for unique key naming convention.
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 ea9ddeea09..81f8e19f45 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
)
@@ -18,8 +17,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})
}
// NoLeadingWildcardLikeAdvisor is the advisor checking for no 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 f131a68e88..fbcd1c152f 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -13,8 +12,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})
}
// NoSelectAllAdvisor is the advisor checking for no "select *".
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required.go b/plugin/advisor/mysql/advisor_stmt_where_required.go
index 4aef999c69..71a1289838 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required.go
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
)
@@ -14,8 +13,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
}
// WhereRequirementAdvisor is the advisor checking for the WHERE clause requirement.
diff --git a/plugin/advisor/mysql/advisor_syntax.go b/plugin/advisor/mysql/advisor_syntax.go
index 4aaba6add8..2dbe396579 100644
--- a/plugin/advisor/mysql/advisor_syntax.go
+++ b/plugin/advisor/mysql/advisor_syntax.go
@@ -2,7 +2,6 @@ package mysql
import (
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
)
var (
@@ -10,8 +9,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLSyntax, &SyntaxAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLSyntax, &SyntaxAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLSyntax, &SyntaxAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLSyntax, &SyntaxAdvisor{})
}
// SyntaxAdvisor is the advisor for checking syntax.
@@ -27,7 +26,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
return []advisor.Advice{
{
Status: advisor.Error,
- Code: advisor.DbStatementSyntaxError,
+ Code: advisor.StatementSyntaxError,
Title: "Syntax error",
Content: err.Error(),
},
@@ -38,7 +37,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]adviso
for _, warn := range warns {
adviceList = append(adviceList, advisor.Advice{
Status: advisor.Warn,
- Code: advisor.DbStatementSyntaxError,
+ Code: advisor.StatementSyntaxError,
Title: "Syntax Warning",
Content: warn.Error(),
})
diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.go
index cd4a17cb89..9fc208dc86 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk.go
@@ -7,8 +7,7 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/catalog"
- "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/pingcap/tidb/parser/ast"
)
@@ -21,8 +20,8 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLTableRequirePK, &TableRequirePKAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLTableRequirePK, &TableRequirePKAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLTableRequirePK, &TableRequirePKAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLTableRequirePK, &TableRequirePKAdvisor{})
}
// TableRequirePKAdvisor is the advisor checking table requires PK.
diff --git a/plugin/advisor/mysql/advisor_use_innodb.go b/plugin/advisor/mysql/advisor_use_innodb.go
index ce79aa00ec..498a8ab657 100644
--- a/plugin/advisor/mysql/advisor_use_innodb.go
+++ b/plugin/advisor/mysql/advisor_use_innodb.go
@@ -5,7 +5,6 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
)
@@ -20,7 +19,7 @@ var (
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLUseInnoDB, &UseInnoDBAdvisor{})
+ advisor.Register(advisor.MySQL, advisor.MySQLUseInnoDB, &UseInnoDBAdvisor{})
}
// UseInnoDBAdvisor is the advisor checking for using InnoDB engine.
diff --git a/plugin/advisor/mysql/parser.go b/plugin/advisor/mysql/parser.go
index 0d7416e153..0a17ca6a9a 100644
--- a/plugin/advisor/mysql/parser.go
+++ b/plugin/advisor/mysql/parser.go
@@ -25,7 +25,7 @@ func parseStatement(statement string, charset string, collation string) ([]ast.S
return nil, []advisor.Advice{
{
Status: advisor.Error,
- Code: advisor.DbStatementSyntaxError,
+ Code: advisor.StatementSyntaxError,
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 43e3089cc7..356e6bcfbc 100644
--- a/plugin/advisor/pg/advisor_naming_column.go
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -8,7 +8,6 @@ import (
"github.com/auxten/postgresql-parser/pkg/sql/sem/tree"
"github.com/auxten/postgresql-parser/pkg/walk"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
)
var (
@@ -16,7 +15,7 @@ var (
)
func init() {
- advisor.Register(db.Postgres, advisor.PostgreSQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
}
// NamingColumnConventionAdvisor is the advisor checking for column convention.
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index f4720fff52..06555d278d 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -8,7 +8,6 @@ import (
"github.com/auxten/postgresql-parser/pkg/sql/sem/tree"
"github.com/auxten/postgresql-parser/pkg/walk"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
)
var (
@@ -16,7 +15,7 @@ var (
)
func init() {
- advisor.Register(db.Postgres, advisor.PostgreSQLNamingTableConvention, &NamingTableConventionAdvisor{})
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLNamingTableConvention, &NamingTableConventionAdvisor{})
}
// NamingTableConventionAdvisor is the advisor checking for table naming convention.
diff --git a/plugin/advisor/pg/advisor_syntax.go b/plugin/advisor/pg/advisor_syntax.go
index b56d3f5866..2aa7a77883 100644
--- a/plugin/advisor/pg/advisor_syntax.go
+++ b/plugin/advisor/pg/advisor_syntax.go
@@ -2,7 +2,6 @@ package pg
import (
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
)
var (
@@ -10,7 +9,7 @@ var (
)
func init() {
- advisor.Register(db.Postgres, advisor.PostgreSQLSyntax, &SyntaxAdvisor{})
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLSyntax, &SyntaxAdvisor{})
}
// SyntaxAdvisor is the advisor for checking syntax.
diff --git a/plugin/advisor/pg/advisor_syntax_test.go b/plugin/advisor/pg/advisor_syntax_test.go
index c5d1f02be3..6eec98ff7a 100644
--- a/plugin/advisor/pg/advisor_syntax_test.go
+++ b/plugin/advisor/pg/advisor_syntax_test.go
@@ -26,7 +26,7 @@ func TestPostgreSQLSyntax(t *testing.T) {
Want: []advisor.Advice{
{
Status: advisor.Error,
- Code: advisor.DbStatementSyntaxError,
+ Code: advisor.StatementSyntaxError,
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 801e7ea053..b8471a1530 100644
--- a/plugin/advisor/pg/parser.go
+++ b/plugin/advisor/pg/parser.go
@@ -11,7 +11,7 @@ func parseStatement(statement string) (parser.Statements, []advisor.Advice) {
return nil, []advisor.Advice{
{
Status: advisor.Error,
- Code: advisor.DbStatementSyntaxError,
+ Code: advisor.StatementSyntaxError,
Title: advisor.SyntaxErrorTitle,
Content: err.Error(),
},
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index f6a983a5ab..27123d62c9 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -7,14 +7,13 @@ import (
"log"
"regexp"
- "github.com/bytebase/bytebase/plugin/catalog"
- "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
)
// 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. Add advisor error code if needed. After new code added, we also need to update the ConvertToErrorCode in api/task_check_run.go
+// 2. Register this advisor in map[DBType][AdvisorType].(plugin/advisor.go)
+// 3. Add advisor error code if needed(plugin/advisor/code.go).
// 4. Map SchemaReviewRuleType to advisor.Type in getAdvisorTypeByRule(current file).
// SchemaReviewRuleLevel is the error level for schema review rule.
@@ -226,7 +225,7 @@ func UnmarshalRequiredColumnRulePayload(payload string) (*RequiredColumnRulePayl
type SchemaReviewCheckContext struct {
Charset string
Collation string
- DbType db.Type
+ DbType DBType
Catalog catalog.Catalog
}
@@ -277,74 +276,74 @@ func SchemaReviewCheck(ctx context.Context, statements string, policy *SchemaRev
return result, nil
}
-func getAdvisorTypeByRule(ruleType SchemaReviewRuleType, engine db.Type) (Type, error) {
+func getAdvisorTypeByRule(ruleType SchemaReviewRuleType, engine DBType) (Type, error) {
switch ruleType {
case SchemaRuleStatementRequireWhere:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLWhereRequirement, nil
}
case SchemaRuleStatementNoLeadingWildcardLike:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNoLeadingWildcardLike, nil
}
case SchemaRuleStatementNoSelectAll:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNoSelectAll, nil
}
case SchemaRuleSchemaBackwardCompatibility:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLMigrationCompatibility, nil
}
case SchemaRuleTableNaming:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNamingTableConvention, nil
- case db.Postgres:
+ case Postgres:
return PostgreSQLNamingTableConvention, nil
}
case SchemaRuleIDXNaming:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNamingIndexConvention, nil
}
case SchemaRuleUKNaming:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNamingUKConvention, nil
}
case SchemaRuleFKNaming:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNamingFKConvention, nil
}
case SchemaRuleColumnNaming:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLNamingColumnConvention, nil
- case db.Postgres:
+ case Postgres:
return PostgreSQLNamingColumnConvention, nil
}
case SchemaRuleRequiredColumn:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLColumnRequirement, nil
}
case SchemaRuleColumnNotNull:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLColumnNoNull, nil
}
case SchemaRuleTableRequirePK:
switch engine {
- case db.MySQL, db.TiDB:
+ case MySQL, TiDB:
return MySQLTableRequirePK, nil
}
case SchemaRuleMySQLEngine:
- if engine == db.MySQL {
+ if engine == MySQL {
return MySQLUseInnoDB, nil
}
}
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index dc135733d4..f52c5ab71e 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -6,7 +6,7 @@ import (
"testing"
"github.com/bmizerany/assert"
- "github.com/bytebase/bytebase/plugin/catalog"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/stretchr/testify/require"
)
diff --git a/server/sql.go b/server/sql.go
index 4554eacd6c..854b7ae1e9 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -158,7 +158,7 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
adviceLevel := advisor.Success
adviceList := []advisor.Advice{}
- if s.feature(api.FeatureSchemaReviewPolicy) && advisor.IsSchemaReviewSupported(instance.Engine) {
+ if s.feature(api.FeatureSchemaReviewPolicy) && api.IsSchemaReviewSupported(instance.Engine) {
adviceLevel, adviceList, err = s.sqlCheck(ctx, instance, exec)
if err != nil {
return err
@@ -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: advisor.EmptySchemaReviewPolicy,
+ Code: advisor.NotFound,
Title: "Empty schema review policy or disabled",
Content: "",
})
@@ -794,10 +794,15 @@ func (s *Server) sqlCheck(ctx context.Context, instance *api.Instance, exec *api
return advisor.Error, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("There are multiple database `%s` for instance ID: %d", exec.DatabaseName, instance.ID))
}
+ dbType, err := api.ConvertToAdvisorDBType(instance.Engine)
+ if err != nil {
+ return advisor.Error, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to convert db type %v into advisor db type", instance.Engine))
+ }
+
res, err := advisor.SchemaReviewCheck(ctx, exec.Statement, policy, advisor.SchemaReviewCheckContext{
Charset: db[0].CharacterSet,
Collation: db[0].Collation,
- DbType: instance.Engine,
+ DbType: dbType,
Catalog: store.NewCatalog(&db[0].ID, s.store),
})
if err != nil {
diff --git a/server/task.go b/server/task.go
index 4d59e68d8a..88802e7d62 100644
--- a/server/task.go
+++ b/server/task.go
@@ -14,7 +14,6 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
- "github.com/bytebase/bytebase/plugin/advisor"
)
var (
@@ -178,10 +177,10 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating task statement: %v", taskPatched.Name)).SetInternal(err)
}
- if advisor.IsSyntaxCheckSupported(taskPatched.Database.Instance.Engine) {
+ if api.IsSyntaxCheckSupported(task.Database.Instance.Engine) {
payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
Statement: *taskPatch.Statement,
- DbType: taskPatched.Database.Instance.Engine,
+ DbType: task.Database.Instance.Engine,
Charset: taskPatched.Database.CharacterSet,
Collation: taskPatched.Database.Collation,
})
@@ -205,7 +204,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
}
}
- if s.feature(api.FeatureSchemaReviewPolicy) && advisor.IsSchemaReviewSupported(taskPatched.Database.Instance.Engine) {
+ if s.feature(api.FeatureSchemaReviewPolicy) && api.IsSchemaReviewSupported(task.Database.Instance.Engine) {
if err := s.triggerDatabaseStatementAdviseTask(ctx, *taskPatch.Statement, taskPatched); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to trigger database statement advise task, err: %w", err)).SetInternal(err)
}
diff --git a/server/task_check_executor_database_connect.go b/server/task_check_executor_database_connect.go
index 3d0471c643..0c69816aa2 100644
--- a/server/task_check_executor_database_connect.go
+++ b/server/task_check_executor_database_connect.go
@@ -26,10 +26,11 @@ func (exec *TaskCheckDatabaseConnectExecutor) Run(ctx context.Context, server *S
if task == nil {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.Internal,
- Title: fmt.Sprintf("Failed to find task %v", taskCheckRun.TaskID),
- Content: err.Error(),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.Internal.Int(),
+ Title: fmt.Sprintf("Failed to find task %v", taskCheckRun.TaskID),
+ Content: err.Error(),
},
}, nil
}
@@ -46,10 +47,11 @@ func (exec *TaskCheckDatabaseConnectExecutor) Run(ctx context.Context, server *S
if err != nil {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.DbConnectionFailure,
- Title: fmt.Sprintf("Failed to connect %q", database.Name),
- Content: err.Error(),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.DbConnectionFailure.Int(),
+ Title: fmt.Sprintf("Failed to connect %q", database.Name),
+ Content: err.Error(),
},
}, nil
}
@@ -57,10 +59,11 @@ func (exec *TaskCheckDatabaseConnectExecutor) Run(ctx context.Context, server *S
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: fmt.Sprintf("Successfully connected %q", database.Name),
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: fmt.Sprintf("Successfully connected %q", database.Name),
},
}, nil
}
diff --git a/server/task_check_executor_migration_schema.go b/server/task_check_executor_migration_schema.go
index 0e865364fa..0704465a5a 100644
--- a/server/task_check_executor_migration_schema.go
+++ b/server/task_check_executor_migration_schema.go
@@ -26,10 +26,11 @@ func (exec *TaskCheckMigrationSchemaExecutor) Run(ctx context.Context, server *S
if task == nil {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.Internal,
- Title: "Error",
- Content: fmt.Sprintf("task not found for ID %v", taskCheckRun.TaskID),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.Internal.Int(),
+ Title: "Error",
+ Content: fmt.Sprintf("task not found for ID %v", taskCheckRun.TaskID),
},
}, nil
}
@@ -53,20 +54,22 @@ func (exec *TaskCheckMigrationSchemaExecutor) Run(ctx context.Context, server *S
if setup {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.MigrationSchemaMissing,
- Title: "Error",
- Content: fmt.Sprintf("Missing migration schema for instance %q", instance.Name),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.MigrationSchemaMissing.Int(),
+ Title: "Error",
+ Content: fmt.Sprintf("Missing migration schema for instance %q", instance.Name),
},
}, nil
}
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: fmt.Sprintf("Instance %q has setup migration schema", instance.Name),
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: fmt.Sprintf("Instance %q has setup migration schema", instance.Name),
},
}, nil
}
diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.go
index b80bad8486..30336b9702 100644
--- a/server/task_check_executor_statement_advisor_composite.go
+++ b/server/task_check_executor_statement_advisor_composite.go
@@ -45,10 +45,11 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
if e, ok := err.(*common.Error); ok && e.Code == common.NotFound {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusWarn,
- Code: common.TaskCheckEmptySchemaReviewPolicy,
- Title: "Empty schema review policy or disabled",
- Content: "",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NotFound.Int(),
+ Title: "Empty schema review policy or disabled",
+ Content: "",
},
}, nil
}
@@ -62,10 +63,15 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
catalog := store.NewCatalog(task.DatabaseID, server.store)
+ dbType, err := api.ConvertToAdvisorDBType(payload.DbType)
+ if err != nil {
+ return nil, err
+ }
+
adviceList, err := advisor.SchemaReviewCheck(ctx, payload.Statement, policy, advisor.SchemaReviewCheckContext{
Charset: payload.Charset,
Collation: payload.Collation,
- DbType: payload.DbType,
+ DbType: dbType,
Catalog: catalog,
})
if err != nil {
@@ -85,19 +91,21 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
}
result = append(result, api.TaskCheckResult{
- Status: status,
- Code: api.ConvertToErrorCode(advice.Code),
- Title: advice.Title,
- Content: advice.Content,
+ Status: status,
+ Namespace: api.AdvisorNamespace,
+ Code: advice.Code.Int(),
+ Title: advice.Title,
+ Content: advice.Content,
})
}
if len(result) == 0 {
result = append(result, api.TaskCheckResult{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: "",
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: "",
})
}
diff --git a/server/task_check_executor_statement_advisor_simple.go b/server/task_check_executor_statement_advisor_simple.go
index 7f6f62a12a..5947e37338 100644
--- a/server/task_check_executor_statement_advisor_simple.go
+++ b/server/task_check_executor_statement_advisor_simple.go
@@ -42,8 +42,13 @@ func (exec *TaskCheckStatementAdvisorSimpleExecutor) Run(ctx context.Context, se
}
}
+ dbType, err := api.ConvertToAdvisorDBType(payload.DbType)
+ if err != nil {
+ return nil, err
+ }
+
adviceList, err := advisor.Check(
- payload.DbType,
+ dbType,
advisorType,
advisor.Context{
Charset: payload.Charset,
@@ -68,10 +73,11 @@ func (exec *TaskCheckStatementAdvisorSimpleExecutor) Run(ctx context.Context, se
}
result = append(result, api.TaskCheckResult{
- Status: status,
- Code: api.ConvertToErrorCode(advice.Code),
- Title: advice.Title,
- Content: advice.Content,
+ Status: status,
+ Namespace: api.AdvisorNamespace,
+ Code: advice.Code.Int(),
+ Title: advice.Title,
+ Content: advice.Content,
})
}
diff --git a/server/task_check_executor_timing.go b/server/task_check_executor_timing.go
index 178af9d393..bbd3a1c4b4 100644
--- a/server/task_check_executor_timing.go
+++ b/server/task_check_executor_timing.go
@@ -31,10 +31,11 @@ func (exec *TaskCheckTimingExecutor) Run(ctx context.Context, server *Server, ta
if payload.EarliestAllowedTs == 0 {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: "Earliest allowed time unset",
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: "Earliest allowed time unset",
},
}, nil
}
@@ -43,20 +44,22 @@ func (exec *TaskCheckTimingExecutor) Run(ctx context.Context, server *Server, ta
if time.Now().UTC().Before(time.Unix(payload.EarliestAllowedTs, 0).UTC()) {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.TaskTimingNotAllowed,
- Title: "Not ready to run",
- Content: fmt.Sprintf("Need to wait until the configured earliest running time: %s (UTC+0000)", time.Unix(payload.EarliestAllowedTs, 0).UTC().Format(dataFormat)),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.TaskTimingNotAllowed.Int(),
+ Title: "Not ready to run",
+ Content: fmt.Sprintf("Need to wait until the configured earliest running time: %s (UTC+0000)", time.Unix(payload.EarliestAllowedTs, 0).UTC().Format(dataFormat)),
},
}, nil
}
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: fmt.Sprintf("Passed the configured earliest running time: %s (UTC+0000)", time.Unix(payload.EarliestAllowedTs, 0).UTC().Format(dataFormat)),
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: fmt.Sprintf("Passed the configured earliest running time: %s (UTC+0000)", time.Unix(payload.EarliestAllowedTs, 0).UTC().Format(dataFormat)),
},
}, nil
}
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index f40daae010..e540a24ca6 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -10,7 +10,6 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
- "github.com/bytebase/bytebase/plugin/advisor"
"go.uber.org/zap"
)
@@ -293,7 +292,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
return nil, err
}
- if advisor.IsSyntaxCheckSupported(database.Instance.Engine) {
+ if api.IsSyntaxCheckSupported(database.Instance.Engine) {
payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
Statement: statement,
DbType: database.Instance.Engine,
@@ -315,7 +314,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
}
}
- if s.server.feature(api.FeatureSchemaReviewPolicy) && advisor.IsSchemaReviewSupported(database.Instance.Engine) {
+ if s.server.feature(api.FeatureSchemaReviewPolicy) && api.IsSchemaReviewSupported(database.Instance.Engine) {
policyID, err := s.server.store.GetSchemaReviewPolicyIDByEnvID(ctx, task.Instance.EnvironmentID)
if err != nil {
return nil, fmt.Errorf("failed to get schema review policy ID for task: %v, in environment: %v, err: %w", task.Name, task.Instance.EnvironmentID, err)
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index e65012ca79..7df21658d4 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -11,7 +11,6 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
- "github.com/bytebase/bytebase/plugin/advisor"
"go.uber.org/zap"
)
@@ -277,7 +276,8 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*
if instance == nil {
return nil, fmt.Errorf("instance ID not found %v", task.InstanceID)
}
- if advisor.IsSyntaxCheckSupported(instance.Engine) {
+
+ if api.IsSyntaxCheckSupported(instance.Engine) {
pass, err = s.server.passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementSyntax)
if err != nil {
return nil, err
@@ -287,7 +287,7 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*
}
}
- if s.server.feature(api.FeatureSchemaReviewPolicy) && advisor.IsSchemaReviewSupported(instance.Engine) {
+ if s.server.feature(api.FeatureSchemaReviewPolicy) && api.IsSchemaReviewSupported(instance.Engine) {
pass, err = s.server.passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementAdvise)
if err != nil {
return nil, err
diff --git a/store/catalog.go b/store/catalog.go
index 083a9016c5..d60cfd8a97 100644
--- a/store/catalog.go
+++ b/store/catalog.go
@@ -5,7 +5,7 @@ import (
"sort"
"github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/plugin/catalog"
+ "github.com/bytebase/bytebase/plugin/advisor/catalog"
)
var (
diff --git a/tests/schema_system_test.go b/tests/schema_system_test.go
index a8acd475da..4ede6c7cf4 100644
--- a/tests/schema_system_test.go
+++ b/tests/schema_system_test.go
@@ -53,10 +53,11 @@ func TestSchemaSystem(t *testing.T) {
}
noSchemaReviewPolicy = []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusWarn,
- Code: common.TaskCheckEmptySchemaReviewPolicy,
- Title: "Empty schema review policy or disabled",
- Content: "",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NotFound.Int(),
+ Title: "Empty schema review policy or disabled",
+ Content: "",
},
}
tests = []test{
@@ -64,10 +65,11 @@ func TestSchemaSystem(t *testing.T) {
statement: statements[0],
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: "",
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: "",
},
},
},
@@ -75,10 +77,11 @@ func TestSchemaSystem(t *testing.T) {
statement: "CREATE TABLE user(id);",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.DbStatementSyntaxError,
- Title: advisor.SyntaxErrorTitle,
- Content: "line 1 column 21 near \");\" ",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.StatementSyntaxError.Int(),
+ Title: advisor.SyntaxErrorTitle,
+ Content: "line 1 column 21 near \");\" ",
},
},
},
@@ -86,70 +89,81 @@ func TestSchemaSystem(t *testing.T) {
statement: statements[1],
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.NotInnoDBEngine,
- Title: "engine.mysql.use-innodb",
- Content: fmt.Sprintf("%q doesn't use InnoDB engine", statements[1]),
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NotInnoDBEngine.Int(),
+ Title: "engine.mysql.use-innodb",
+ Content: fmt.Sprintf("%q doesn't use InnoDB engine", statements[1]),
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NamingTableConventionMismatch,
- Title: "naming.table",
- Content: "`userTable` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NamingTableConventionMismatch.Int(),
+ Title: "naming.table",
+ Content: "`userTable` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NamingColumnConventionMismatch,
- Title: "naming.column",
- Content: "`userTable`.`roomId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NamingColumnConventionMismatch.Int(),
+ Title: "naming.column",
+ Content: "`userTable`.`roomId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NamingIndexConventionMismatch,
- Title: "naming.index.idx",
- Content: "Index in table `userTable` mismatches the naming convention, expect \"^idx_userTable_name$\" but found `idx1`",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NamingIndexConventionMismatch.Int(),
+ Title: "naming.index.idx",
+ Content: "Index in table `userTable` mismatches the naming convention, expect \"^idx_userTable_name$\" but found `idx1`",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NamingUKConventionMismatch,
- Title: "naming.index.uk",
- Content: "Unique key in table `userTable` mismatches the naming convention, expect \"^uk_userTable_id_name$\" but found `uk1`",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NamingUKConventionMismatch.Int(),
+ Title: "naming.index.uk",
+ Content: "Unique key in table `userTable` mismatches the naming convention, expect \"^uk_userTable_id_name$\" but found `uk1`",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NamingFKConventionMismatch,
- Title: "naming.index.fk",
- Content: "Foreign key in table `userTable` mismatches the naming convention, expect \"^fk_userTable_roomId_room_id$\" but found `fk1`",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NamingFKConventionMismatch.Int(),
+ Title: "naming.index.fk",
+ Content: "Foreign key in table `userTable` mismatches the naming convention, expect \"^fk_userTable_roomId_room_id$\" but found `fk1`",
},
{
- Status: api.TaskCheckStatusError,
- Code: common.TableNoPK,
- Title: "table.require-pk",
- Content: "Table `userTable` requires PRIMARY KEY",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.TableNoPK.Int(),
+ Title: "table.require-pk",
+ Content: "Table `userTable` requires PRIMARY KEY",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.NoRequiredColumn,
- Title: "column.required",
- Content: "Table `userTable` requires columns: created_ts, creator_id, updated_ts, updater_id",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.NoRequiredColumn.Int(),
+ Title: "column.required",
+ Content: "Table `userTable` requires columns: created_ts, creator_id, updated_ts, updater_id",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.ColumnCanNotNull,
- Title: "column.no-null",
- Content: "`userTable`.`id` can not have NULL value",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.ColumnCanNotNull.Int(),
+ Title: "column.no-null",
+ Content: "`userTable`.`id` can not have NULL value",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.ColumnCanNotNull,
- Title: "column.no-null",
- Content: "`userTable`.`name` can not have NULL value",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.ColumnCanNotNull.Int(),
+ Title: "column.no-null",
+ Content: "`userTable`.`name` can not have NULL value",
},
{
- Status: api.TaskCheckStatusWarn,
- Code: common.ColumnCanNotNull,
- Title: "column.no-null",
- Content: "`userTable`.`roomId` can not have NULL value",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.ColumnCanNotNull.Int(),
+ Title: "column.no-null",
+ Content: "`userTable`.`roomId` can not have NULL value",
},
},
},
@@ -157,10 +171,11 @@ func TestSchemaSystem(t *testing.T) {
statement: "DELETE FROM t",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.StatementNoWhere,
- Title: "statement.where.require",
- Content: "\"DELETE FROM t\" requires WHERE clause",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.StatementNoWhere.Int(),
+ Title: "statement.where.require",
+ Content: "\"DELETE FROM t\" requires WHERE clause",
},
},
},
@@ -168,10 +183,11 @@ func TestSchemaSystem(t *testing.T) {
statement: "DELETE FROM t WHERE a like `%abc`",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.StatementLeadingWildcardLike,
- Title: "statement.where.no-leading-wildcard-like",
- Content: "\"DELETE FROM t WHERE a like `%abc`\" uses leading wildcard LIKE",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.StatementLeadingWildcardLike.Int(),
+ Title: "statement.where.no-leading-wildcard-like",
+ Content: "\"DELETE FROM t WHERE a like `%abc`\" uses leading wildcard LIKE",
},
},
},
@@ -179,16 +195,18 @@ func TestSchemaSystem(t *testing.T) {
statement: "INSERT INTO t_copy SELECT * FROM t",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
- Code: common.StatementSelectAll,
- Title: "statement.select.no-select-all",
- Content: "\"INSERT INTO t_copy SELECT * FROM t\" uses SELECT all",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.StatementSelectAll.Int(),
+ Title: "statement.select.no-select-all",
+ Content: "\"INSERT INTO t_copy SELECT * FROM t\" uses SELECT all",
},
{
- Status: api.TaskCheckStatusError,
- Code: common.StatementNoWhere,
- Title: "statement.where.require",
- Content: "\"INSERT INTO t_copy SELECT * FROM t\" requires WHERE clause",
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.StatementNoWhere.Int(),
+ Title: "statement.where.require",
+ Content: "\"INSERT INTO t_copy SELECT * FROM t\" requires WHERE clause",
},
},
},
@@ -196,10 +214,11 @@ func TestSchemaSystem(t *testing.T) {
statement: "DELETE FROM t WHERE a = (SELECT max(id) FROM user WHERE name = 'bytebase')",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusSuccess,
- Code: common.Ok,
- Title: "OK",
- Content: "",
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: "",
},
},
},
@@ -207,10 +226,11 @@ func TestSchemaSystem(t *testing.T) {
statement: "DROP TABLE user",
result: []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusWarn,
- Code: common.CompatibilityDropTable,
- Title: "schema.backward-compatibility",
- Content: "\"DROP TABLE user\" may cause incompatibility with the existing data and code",
+ Status: api.TaskCheckStatusWarn,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.CompatibilityDropTable.Int(),
+ Title: "schema.backward-compatibility",
+ Content: "\"DROP TABLE user\" may cause incompatibility with the existing data and code",
},
},
},