Skip to content

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

Only add statement syntax task check for MySQL dialect (MySQL and TiDB)

天舟b7a3320a7a83
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 35b5bdae86..1a57c634c9 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -81,10 +81,6 @@ func Register(dbType db.Type, advType AdvisorType, f Advisor) {
}
func Check(dbType db.Type, advType AdvisorType, ctx AdvisorContext, statement string) ([]Advice, error) {
- // TODO(spinningbot): skip Postgres SQL advisor check till it's supported.
- if dbType == db.Postgres {
- return nil, nil
- }
advisorMu.RLock()
dbAdvisors, ok := advisors[dbType]
defer advisorMu.RUnlock()
diff --git a/server/task.go b/server/task.go
index c3ef0a8122..1a6a3b89d7 100644
--- a/server/task.go
+++ b/server/task.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
@@ -86,29 +87,32 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
// If we have updated the statement, then we trigger a syntax check
if task.Type == api.TaskDatabaseSchemaUpdate && taskPatch.Statement != nil {
- payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
- Statement: *taskPatch.Statement,
- DbType: updatedTask.Database.Instance.Engine,
- Charset: updatedTask.Database.CharacterSet,
- Collation: updatedTask.Database.Collation,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err))
- }
- _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(context.Background(), &api.TaskCheckRunCreate{
- CreatorId: api.SYSTEM_BOT_ID,
- TaskId: task.ID,
- Type: api.TaskCheckDatabaseStatementSyntax,
- Payload: string(payload),
- SkipIfAlreadyTerminated: false,
- })
- if err != nil {
- // It's OK if we failed to trigger a check, just emit an error log
- s.l.Error("Failed to trigger syntax check after changing task statement",
- zap.Int("task_id", task.ID),
- zap.String("task_name", task.Name),
- zap.Error(err),
- )
+ // For now we only supported MySQL dialect syntax check
+ if updatedTask.Database.Instance.Engine == db.MySQL || updatedTask.Database.Instance.Engine == db.TiDB {
+ payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
+ Statement: *taskPatch.Statement,
+ DbType: updatedTask.Database.Instance.Engine,
+ Charset: updatedTask.Database.CharacterSet,
+ Collation: updatedTask.Database.Collation,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err))
+ }
+ _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(context.Background(), &api.TaskCheckRunCreate{
+ CreatorId: api.SYSTEM_BOT_ID,
+ TaskId: task.ID,
+ Type: api.TaskCheckDatabaseStatementSyntax,
+ Payload: string(payload),
+ SkipIfAlreadyTerminated: false,
+ })
+ if err != nil {
+ // It's OK if we failed to trigger a check, just emit an error log
+ s.l.Error("Failed to trigger syntax check after changing task statement",
+ zap.Int("task_id", task.ID),
+ zap.String("task_name", task.Name),
+ zap.Error(err),
+ )
+ }
}
}
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 42a21c651c..38536fc5b8 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap"
)
@@ -186,24 +187,27 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
return nil, err
}
- payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
- Statement: taskPayload.Statement,
- DbType: database.Instance.Engine,
- Charset: database.CharacterSet,
- Collation: database.Collation,
- })
- if err != nil {
- return nil, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err)
- }
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
- CreatorId: creatorId,
- TaskId: task.ID,
- Type: api.TaskCheckDatabaseStatementSyntax,
- Payload: string(payload),
- SkipIfAlreadyTerminated: skipIfAlreadyTerminated,
- })
- if err != nil {
- return nil, err
+ // For now we only supported MySQL dialect syntax check
+ if database.Instance.Engine == db.MySQL || database.Instance.Engine == db.TiDB {
+ payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{
+ Statement: taskPayload.Statement,
+ DbType: database.Instance.Engine,
+ Charset: database.CharacterSet,
+ Collation: database.Collation,
+ })
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err)
+ }
+ _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ CreatorId: creatorId,
+ TaskId: task.ID,
+ Type: api.TaskCheckDatabaseStatementSyntax,
+ Payload: string(payload),
+ SkipIfAlreadyTerminated: skipIfAlreadyTerminated,
+ })
+ if err != nil {
+ return nil, err
+ }
}
taskCheckRunFind := &api.TaskCheckRunFind{
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index a6846e2733..a35889c470 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap"
)
@@ -215,12 +216,22 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*
return task, nil
}
- pass, err = passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementSyntax)
+ instanceFind := &api.InstanceFind{
+ ID: &task.InstanceId,
+ }
+ instance, err := s.server.InstanceService.FindInstance(context.Background(), instanceFind)
if err != nil {
return nil, err
}
- if !pass {
- return task, nil
+ // For now we only supported MySQL dialect syntax check
+ if instance.Engine == db.MySQL || instance.Engine == db.TiDB {
+ pass, err = passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementSyntax)
+ if err != nil {
+ return nil, err
+ }
+ if !pass {
+ return task, nil
+ }
}
}
updatedTask, err := s.server.ChangeTaskStatus(ctx, task, api.TaskRunning, api.SYSTEM_BOT_ID)