Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 repo 中 7 处重复的 execution_context JSON 取值表达式为 sqlExecutionContextJSONText (#667) (#668)

fx-kurumic5a62aa581be
diff --git a/internal/repo/issues_delete.go b/internal/repo/issues_delete.go
index 4099058c..25b02742 100644
--- a/internal/repo/issues_delete.go
+++ b/internal/repo/issues_delete.go
@@ -152,10 +152,7 @@ func (r *IssuesRepo) issueDeletionStates(db *gorm.DB, issues []domain.Issue) (ma
}
}
- expr := "json_extract(execution_context, '$.issueId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.issueId'))"
- }
+ expr := r.sqlExecutionContextJSONText("issueId")
q := db.Table("runs").Select("id, status, "+expr+" AS issue_id, "+
"EXISTS (SELECT 1 FROM run_nodes WHERE run_id = runs.id AND status IN ('running', 'pending_input')) AS active_nodes").
Where(expr+" IN ?", ids)
diff --git a/internal/repo/payload_json_text_test.go b/internal/repo/payload_json_text_test.go
index 0c9124c9..1b388ac1 100644
--- a/internal/repo/payload_json_text_test.go
+++ b/internal/repo/payload_json_text_test.go
@@ -49,3 +49,35 @@ func TestSQLPayloadJSONTextMySQL(t *testing.T) {
}
}
}
+
+func TestSQLExecutionContextJSONText(t *testing.T) {
+ s, err := OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(s.Close)
+ sqlite := &deps{DB: s.DB}
+ mysqlDB, err := gorm.Open(mysql.New(mysql.Config{
+ DSN: "user:pass@tcp(127.0.0.1:1)/agentflow",
+ SkipInitializeWithVersion: true,
+ }), &gorm.Config{DisableAutomaticPing: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ my := &deps{DB: mysqlDB}
+ cases := []struct {
+ d *deps
+ path string
+ want string
+ }{
+ {sqlite, "issueId", "json_extract(execution_context, '$.issueId')"},
+ {sqlite, "projectId", "json_extract(execution_context, '$.projectId')"},
+ {my, "issueId", "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.issueId'))"},
+ {my, "projectId", "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.projectId'))"},
+ }
+ for _, c := range cases {
+ if got := c.d.sqlExecutionContextJSONText(c.path); got != c.want {
+ t.Fatalf("sqlExecutionContextJSONText(%q) = %q, want %q", c.path, got, c.want)
+ }
+ }
+}
diff --git a/internal/repo/repo.go b/internal/repo/repo.go
index 37892422..e7367b12 100644
--- a/internal/repo/repo.go
+++ b/internal/repo/repo.go
@@ -57,6 +57,16 @@ func (d *deps) sqlPayloadJSONText(path string) string {
return "json_extract(payload, '$." + path + "')"
}
+// sqlExecutionContextJSONText is the execution_context-column counterpart of
+// sqlPayloadJSONText. path is the JSON path below "$." and must be a code
+// constant, never user input.
+func (d *deps) sqlExecutionContextJSONText(path string) string {
+ if d.isMySQL() {
+ return "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$." + path + "'))"
+ }
+ return "json_extract(execution_context, '$." + path + "')"
+}
+
// locking returns the query with a row-lock clause on MySQL. sqlite serializes
// writers already (single connection in tests), so no clause is needed there.
func (d *deps) locking(q *gorm.DB) *gorm.DB {
diff --git a/internal/repo/run_issue_latest.go b/internal/repo/run_issue_latest.go
index eec44141..3a9f8489 100644
--- a/internal/repo/run_issue_latest.go
+++ b/internal/repo/run_issue_latest.go
@@ -5,10 +5,7 @@ import "agentflow/internal/domain"
// LatestIssueSOPRun returns the newest top-level SOP run of an issue; role
// group records and conversation workspaces are not SOP runs.
func (r *RunsRepo) LatestIssueSOPRun(issueID string) (domain.Run, bool) {
- expr := "json_extract(execution_context, '$.issueId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.issueId'))"
- }
+ expr := r.sqlExecutionContextJSONText("issueId")
ids := []string{}
if err := r.DB.Model(&runRow{}).
Where(expr+" = ? AND (kind IS NULL OR kind <> ?)", issueID, domain.RunKindWorkspace).
diff --git a/internal/repo/runs.go b/internal/repo/runs.go
index 5d6a2a9a..b3ed1534 100644
--- a/internal/repo/runs.go
+++ b/internal/repo/runs.go
@@ -140,20 +140,14 @@ func (r *RunsRepo) runSummariesFor(filter domain.AnalyticsFilter, issueID string
}
q := r.DB
if filter.ProjectIDs != nil {
- expr := "json_extract(execution_context, '$.projectId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.projectId'))"
- }
+ expr := r.sqlExecutionContextJSONText("projectId")
q = q.Where(expr+" IN ? OR "+expr+" IS NULL OR "+expr+" = ''", filter.ProjectIDs)
}
if filter.WorkflowID != "" {
q = q.Where("workflow_id = ?", filter.WorkflowID)
}
if projectID := strings.TrimSpace(filter.ProjectID); projectID != "" {
- expr := "json_extract(execution_context, '$.projectId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.projectId'))"
- }
+ expr := r.sqlExecutionContextJSONText("projectId")
// Keep legacy rows with an empty project stamp; FilterRunsByProject
// still resolves those through the linked issue.
q = q.Where(expr+" = ? OR "+expr+" IS NULL OR "+expr+" = ''", projectID)
@@ -165,10 +159,7 @@ func (r *RunsRepo) runSummariesFor(filter domain.AnalyticsFilter, issueID string
q = q.Where("created_at <= ? OR created_at IS NULL", filter.To)
}
if issueID != "" {
- expr := "json_extract(execution_context, '$.issueId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.issueId'))"
- }
+ expr := r.sqlExecutionContextJSONText("issueId")
q = q.Where(expr+" = ?", issueID)
}
var rows []runRow
@@ -446,10 +437,7 @@ func (r *RunsRepo) SupersededIssueRunIDs(issueID, keepRunID string) []string {
if issueID == "" {
return nil
}
- expr := "json_extract(execution_context, '$.issueId')"
- if r.isMySQL() {
- expr = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.issueId'))"
- }
+ expr := r.sqlExecutionContextJSONText("issueId")
var ids []string
q := r.DB.Model(&runRow{}).
Where(expr+" = ?", issueID).
@@ -756,10 +744,7 @@ func (r *RunsRepo) runTimingEvents(ids []string) (map[string][]domain.RunEvent,
// nodes, events or artifact content. Missing runs are absent from the map.
func (r *RunsRepo) RunProjectIDs(ids []string) (map[string]string, error) {
out := make(map[string]string, len(ids))
- project := "json_extract(execution_context, '$.projectId')"
- if r.isMySQL() {
- project = "JSON_UNQUOTE(JSON_EXTRACT(execution_context, '$.projectId'))"
- }
+ project := r.sqlExecutionContextJSONText("projectId")
for start := 0; start < len(ids); start += 500 {
var rows []struct {
ID string