fix: validate workflows and preserve execution outcomes

This commit is contained in:
Ed1s0nZ committed 2026-10-06 02:32:32 +08:00
1 parent bdefa535b0
commit 8550325dfe
16 files changed
+494 -35

No files matched your search

+28
View File
@@ -3,11 +3,39 @@ package database
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
var ErrWorkflowAlreadyExists = errors.New("工作流 ID 已存在")
// CreateWorkflowDefinition never replaces a definition with the same ID.
func (db *DB) CreateWorkflowDefinition(wf *WorkflowDefinition) error {
if wf == nil || strings.TrimSpace(wf.ID) == "" || strings.TrimSpace(wf.Name) == "" {
return fmt.Errorf("工作流 id 和 name 不能为空")
}
if wf.Version <= 0 {
wf.Version = 1
}
now := time.Now()
res, err := db.Exec(`INSERT INTO workflow_definitions (id, name, description, version, graph_json, enabled, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO NOTHING`,
wf.ID, wf.Name, wf.Description, wf.Version, wf.GraphJSON, boolToInt(wf.Enabled), now, now)
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return ErrWorkflowAlreadyExists
}
return nil
}
// WorkflowDefinition is a persisted user-defined graph/workflow template.
// graph_json intentionally remains opaque so users can define their own fields.
type WorkflowDefinition struct {