Skip to content

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

Add compatibility error code and pass it back in the TaskCheckResult

天舟1acbf1cb2492
diff --git a/common/error.go b/common/error.go
index b6fb50624e..13bcb4462b 100644
--- a/common/error.go
+++ b/common/error.go
@@ -28,6 +28,19 @@ const (
MigrationAlreadyApplied Code = 202
MigrationOutOfOrder Code = 203
MigrationBaselineMissing Code = 204
+
+ // 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
)
// Error represents an application-specific error. Application errors can be
diff --git a/frontend/src/types/error.ts b/frontend/src/types/error.ts
index 3e908e9b62..e590ce3534 100644
--- a/frontend/src/types/error.ts
+++ b/frontend/src/types/error.ts
@@ -21,4 +21,16 @@ export enum MigrationErrorCode {
MIGRATION_BASELINE_MISSING = 204,
}
-export type ErrorCode = GeneralErrorCode | DBErrorCode | MigrationErrorCode;
+export enum CompatibilityErrorCode {
+ COMPATIBILITY_DROP_DATABASE = 10001,
+ COMPATIBILITY_RENAME_TABLE = 10002,
+ COMPATIBILITY_DROP_TABLE = 10003,
+ COMPATIBILITY_RENAME_COLUMN = 10004,
+ COMPATIBILITY_DROP_COLUMN = 10005,
+ COMPATIBILITY_ADD_PRIMARY_KEY = 10006,
+ COMPATIBILITY_ADD_UNIQUE_KEY = 10007,
+ COMPATIBILITY_ADD_FOREIGN_KEY = 10008,
+ COMPATIBILITY_ADD_CHECK = 10009,
+ COMPATIBILITY_ALTER_CHECK = 10010,
+ COMPATIBILITY_ALTER_COLUMN = 10011,
+}
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index b925785325..2a31e2ec74 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -6,6 +6,7 @@ import (
"fmt"
"sync"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap"
)
@@ -40,6 +41,7 @@ const (
type Advice struct {
Status Status
+ Code common.Code
Title string
Content string
}
diff --git a/plugin/advisor/fake/fake_advisor.go b/plugin/advisor/fake/fake_advisor.go
index d23a436d8f..faf32e5f23 100644
--- a/plugin/advisor/fake/fake_advisor.go
+++ b/plugin/advisor/fake/fake_advisor.go
@@ -1,6 +1,7 @@
package fake
import (
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
)
@@ -23,16 +24,19 @@ func (adv *Advisor) Check(ctx advisor.AdvisorContext, statement string) ([]advis
return []advisor.Advice{
{
Status: advisor.Success,
+ Code: common.Ok,
Title: "INFO check",
Content: statement,
},
{
Status: advisor.Warn,
+ Code: common.Internal,
Title: "WARN check",
Content: statement,
},
{
Status: advisor.Error,
+ Code: common.Internal,
Title: "ERROR check",
Content: statement,
},
diff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go
index 71b23aa7b2..695188cce4 100644
--- a/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go
+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go
@@ -3,6 +3,7 @@ package mysql
import (
"fmt"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -46,6 +47,7 @@ func (adv *CompatibilityAdvisor) Check(ctx advisor.AdvisorContext, statement str
if len(c.advisorList) == 0 {
c.advisorList = append(c.advisorList, advisor.Advice{
Status: advisor.Success,
+ Code: common.Ok,
Title: "OK",
Content: "Migration is backward compatible"})
}
@@ -57,20 +59,20 @@ type compatibilityChecker struct {
}
func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
- compatible := true
+ code := common.Ok
// DROP DATABASE
if _, ok := in.(*ast.DropDatabaseStmt); ok {
- compatible = false
+ code = common.CompatibilityDropDatabase
goto END
}
// RENAME TABLE
if _, ok := in.(*ast.RenameTableStmt); ok {
- compatible = false
+ code = common.CompatibilityRenameTable
goto END
}
// DROP TABLE/VIEW
if _, ok := in.(*ast.DropTableStmt); ok {
- compatible = false
+ code = common.CompatibilityDropTable
goto END
}
// ALTER TABLE
@@ -78,29 +80,38 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
for _, spec := range node.Specs {
fmt.Printf("spec %d: %+v\n\n", spec.Tp, spec)
// RENAME COLUMN
+ if spec.Tp == ast.AlterTableRenameColumn {
+ code = common.CompatibilityRenameColumn
+ goto END
+ }
// DROP COLUMN
- if spec.Tp == ast.AlterTableRenameColumn ||
- spec.Tp == ast.AlterTableDropColumn {
- compatible = false
+ if spec.Tp == ast.AlterTableDropColumn {
+ code = common.CompatibilityDropColumn
goto END
}
if spec.Tp == ast.AlterTableAddConstraint {
// ADD PRIMARY KEY
+ if spec.Constraint.Tp == ast.ConstraintPrimaryKey {
+ code = common.CompatibilityAddPrimaryKey
+ goto END
+ }
// ADD UNIQUE/UNIQUE KEY/UNIQUE INDEX
- // ADD FOREIGN KEY
if spec.Constraint.Tp == ast.ConstraintPrimaryKey ||
spec.Constraint.Tp == ast.ConstraintUniq ||
- spec.Constraint.Tp == ast.ConstraintUniqKey ||
- spec.Constraint.Tp == ast.ConstraintUniqIndex ||
- spec.Constraint.Tp == ast.ConstraintForeignKey {
- compatible = false
+ spec.Constraint.Tp == ast.ConstraintUniqKey {
+ code = common.CompatibilityAddUniqueKey
+ goto END
+ }
+ // ADD FOREIGN KEY
+ if spec.Constraint.Tp == ast.ConstraintForeignKey {
+ code = common.CompatibilityAddForeignKey
goto END
}
// 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 {
- compatible = false
+ code = common.CompatibilityAddCheck
goto END
}
}
@@ -109,7 +120,7 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
// ALTER CHECK ENFORCED
if spec.Tp == ast.AlterTableAlterCheck {
if spec.Constraint.Enforced {
- compatible = false
+ code = common.CompatibilityAlterCheck
goto END
}
}
@@ -120,7 +131,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 {
- compatible = false
+ code = common.CompatibilityAlterColumn
goto END
}
}
@@ -132,15 +143,16 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
// CREATE UNIQUE INDEX
if node, ok := in.(*ast.CreateIndexStmt); ok {
if node.KeyType == ast.IndexKeyTypeUnique {
- compatible = false
+ code = common.CompatibilityAddUniqueKey
goto END
}
}
END:
- if !compatible {
+ if code != common.Ok {
v.advisorList = append(v.advisorList, advisor.Advice{
Status: advisor.Warn,
+ Code: code,
Title: "Potential incompatible migration",
Content: fmt.Sprintf("%q may cause incompatibility with the existing data and code", in.Text()),
})
diff --git a/plugin/advisor/mysql/mysql_syntax_advisor.go b/plugin/advisor/mysql/mysql_syntax_advisor.go
index b1a103f150..443578a473 100644
--- a/plugin/advisor/mysql/mysql_syntax_advisor.go
+++ b/plugin/advisor/mysql/mysql_syntax_advisor.go
@@ -1,6 +1,7 @@
package mysql
import (
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -30,6 +31,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.AdvisorContext, statement string) ([
return []advisor.Advice{
{
Status: advisor.Error,
+ Code: common.DbStatementSyntaxError,
Title: "Syntax error",
Content: err.Error(),
},
@@ -40,6 +42,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.AdvisorContext, statement string) ([
for _, warn := range warns {
advisorList = append(advisorList, advisor.Advice{
Status: advisor.Warn,
+ Code: common.DbStatementSyntaxError,
Title: "Syntax Warning",
Content: warn.Error(),
})
@@ -47,6 +50,7 @@ func (adv *SyntaxAdvisor) Check(ctx advisor.AdvisorContext, statement string) ([
advisorList = append(advisorList, advisor.Advice{
Status: advisor.Success,
+ Code: common.Ok,
Title: "Syntax OK",
Content: "OK"})
return advisorList, nil
diff --git a/server/task_check_executor_statement_advisor.go b/server/task_check_executor_statement_advisor.go
index bd8328b9cf..78115cf04f 100644
--- a/server/task_check_executor_statement_advisor.go
+++ b/server/task_check_executor_statement_advisor.go
@@ -54,21 +54,18 @@ func (exec *TaskCheckStatementAdvisorExecutor) Run(ctx context.Context, server *
result = []api.TaskCheckResult{}
for _, advice := range adviceList {
status := api.TaskCheckStatusSuccess
- code := common.Ok
switch advice.Status {
case advisor.Success:
status = api.TaskCheckStatusSuccess
case advisor.Warn:
status = api.TaskCheckStatusWarn
- code = common.DbStatementSyntaxError
case advisor.Error:
status = api.TaskCheckStatusError
- code = common.DbStatementSyntaxError
}
result = append(result, api.TaskCheckResult{
Status: status,
- Code: code,
+ Code: advice.Code,
Title: advice.Title,
Content: advice.Content,
})