Skip to content

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

fix: trigger statement type check after modifing task statement (#2434)

rebelice9f4dab74a7eb
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 88f5f557ec..098871673a 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -237,3 +237,13 @@ func IsSQLReviewSupported(dbType db.Type, _ common.ReleaseMode) bool {
return false
}
+
+// IsStatementTypeCheckSupported checks the engine type if statement type check supports it.
+func IsStatementTypeCheckSupported(dbType db.Type) bool {
+ switch dbType {
+ case db.Postgres, db.TiDB, db.MySQL:
+ return true
+ default:
+ return false
+ }
+}
diff --git a/server/task.go b/server/task.go
index 6fd6ab1f1a..9d1e0236a0 100644
--- a/server/task.go
+++ b/server/task.go
@@ -424,6 +424,32 @@ func (s *Server) patchTask(ctx context.Context, task *api.Task, taskPatch *api.T
return nil, echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "failed to trigger database statement advise task")).SetInternal(err)
}
}
+
+ if api.IsStatementTypeCheckSupported(task.Instance.Engine) {
+ payload, err := json.Marshal(api.TaskCheckDatabaseStatementTypePayload{
+ Statement: *taskPatch.Statement,
+ DbType: task.Instance.Engine,
+ Charset: task.Database.CharacterSet,
+ Collation: task.Database.Collation,
+ })
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, errors.Wrapf(err, "failed to marshal check statement type payload: %v", task.Name))
+ }
+ if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ CreatorID: api.SystemBotID,
+ TaskID: task.ID,
+ Type: api.TaskCheckDatabaseStatementType,
+ Payload: string(payload),
+ SkipIfAlreadyTerminated: false,
+ }); err != nil {
+ // It's OK if we failed to trigger a check, just emit an error log
+ log.Error("Failed to trigger statement type check after changing the 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 728c03020c..a4234d8156 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -13,7 +13,6 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
- "github.com/bytebase/bytebase/plugin/db"
)
// NewTaskCheckScheduler creates a task check scheduler.
@@ -300,10 +299,7 @@ func (*TaskCheckScheduler) getStatement(task *api.Task) (string, error) {
}
}
func (s *TaskCheckScheduler) scheduleStmtTypeTaskCheck(ctx context.Context, task *api.Task, creatorID int, skipIfAlreadyTerminated bool, database *api.Database, statement string) error {
- switch database.Instance.Engine {
- case db.Postgres:
- case db.MySQL, db.TiDB:
- default:
+ if !api.IsStatementTypeCheckSupported(database.Instance.Engine) {
return nil
}
payload, err := json.Marshal(api.TaskCheckDatabaseStatementTypePayload{