Skip to content

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

feat: use statement report in approval finder (#7746)

p0ny783b35fda267
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index 425c97deff..4fd461189f 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -348,6 +348,35 @@ func getDatabaseGeneralIssueRisk(ctx context.Context, s *store.Store, licenseSer
return 0, store.RiskSourceUnknown, false, errors.Errorf("plan %v not found", *issue.PlanUID)
}
+ planCheckRuns, err := s.ListPlanCheckRuns(ctx, &store.FindPlanCheckRunMessage{
+ PlanUID: &plan.UID,
+ Type: &[]store.PlanCheckRunType{store.PlanCheckDatabaseStatementSummaryReport},
+ })
+ if err != nil {
+ return 0, store.RiskSourceUnknown, false, errors.Wrapf(err, "failed to list plan check runs for plan %v", plan.UID)
+ }
+ type Key struct {
+ InstanceUID int
+ DatabaseName string
+ }
+ latestPlanCheckRun := map[Key]*store.PlanCheckRunMessage{}
+ for _, run := range planCheckRuns {
+ key := Key{
+ InstanceUID: int(run.Config.InstanceUid),
+ DatabaseName: run.Config.DatabaseName,
+ }
+ oldValue, ok := latestPlanCheckRun[key]
+ if !ok || oldValue.UID < run.UID {
+ latestPlanCheckRun[key] = run
+ }
+ }
+ for _, run := range latestPlanCheckRun {
+ // the latest plan check run is not done yet, return done=false
+ if run.Status != store.PlanCheckRunStatusDone {
+ return 0, store.RiskSourceUnknown, false, nil
+ }
+ }
+
pipelineCreate, err := v1.GetPipelineCreate(ctx, s, licenseService, dbFactory, plan.Config.Steps, issue.Project)
if err != nil {
return 0, store.RiskSourceUnknown, false, errors.Wrap(err, "failed to get pipeline create")
@@ -457,6 +486,33 @@ func getDatabaseGeneralIssueRisk(ctx context.Context, s *store.Store, licenseSer
if boolVal, ok := val.(bool); ok && boolVal {
return risk.Level, nil
}
+
+ if run, ok := latestPlanCheckRun[Key{
+ InstanceUID: instance.UID,
+ DatabaseName: databaseName,
+ }]; ok {
+ for _, result := range run.Result.Results {
+ report := result.GetSqlSummaryReport()
+ if report == nil {
+ continue
+ }
+ args["affected_rows"] = report.AffectedRows
+ for _, statementType := range report.StatementTypes {
+ args["sql_type"] = statementType
+ res, _, err := prg.Eval(args)
+ if err != nil {
+ return 0, err
+ }
+ val, err := res.ConvertToNative(reflect.TypeOf(false))
+ if err != nil {
+ return 0, errors.Wrap(err, "expect bool result")
+ }
+ if boolVal, ok := val.(bool); ok && boolVal {
+ return risk.Level, nil
+ }
+ }
+ }
+ }
}
return 0, nil
}()
diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.go
index 3a8c86fdad..9c9ba84ccd 100644
--- a/backend/store/plan_check_run.go
+++ b/backend/store/plan_check_run.go
@@ -68,6 +68,7 @@ type FindPlanCheckRunMessage struct {
PlanUID *int64
Status *[]PlanCheckRunStatus
+ Type *[]PlanCheckRunType
}
// CreatePlanCheckRuns creates new plan check runs.
@@ -132,12 +133,12 @@ func (s *Store) ListPlanCheckRuns(ctx context.Context, find *FindPlanCheckRunMes
args = append(args, *v)
}
if v := find.Status; v != nil {
- var list []string
- for _, status := range *v {
- list = append(list, fmt.Sprintf("$%d", len(args)+1))
- args = append(args, status)
- }
- where = append(where, fmt.Sprintf("plan_check_run.status in (%s)", strings.Join(list, ",")))
+ where = append(where, fmt.Sprintf("plan_check_run.status = ANY($%d)", len(args)+1))
+ args = append(args, *v)
+ }
+ if v := find.Type; v != nil {
+ where = append(where, fmt.Sprintf("plan_check_run.type = ANY($%d)", len(args)+1))
+ args = append(args, *v)
}
query := fmt.Sprintf(`
SELECT