MySQL · 3957 bytes Raw Blame History
1 -- SPDX-License-Identifier: AGPL-3.0-or-later
2
3 -- name: InsertWorkflowRun :one
4 INSERT INTO workflow_runs (
5 repo_id, run_index, workflow_file, workflow_name,
6 head_sha, head_ref, event, event_payload,
7 actor_user_id, parent_run_id, concurrency_group, need_approval
8 ) VALUES (
9 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
10 )
11 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
12 head_sha, head_ref, event, event_payload,
13 actor_user_id, parent_run_id, concurrency_group,
14 status, conclusion, pinned, need_approval, approved_by_user_id,
15 started_at, completed_at, version, created_at, updated_at, trigger_event_id;
16
17 -- name: EnqueueWorkflowRun :one
18 -- Idempotent insert: if a row with the same (repo_id, workflow_file,
19 -- trigger_event_id) already exists, returns no rows (pgx.ErrNoRows in
20 -- Go). The handler treats that as a successful no-op so worker
21 -- retries and admin replays of the same triggering event don't
22 -- duplicate runs.
23 --
24 -- The ON CONFLICT predicate matches the partial unique index defined
25 -- in migration 0051; both must agree for postgres to infer the
26 -- target.
27 INSERT INTO workflow_runs (
28 repo_id, run_index, workflow_file, workflow_name,
29 head_sha, head_ref, event, event_payload,
30 actor_user_id, parent_run_id, concurrency_group, need_approval,
31 trigger_event_id
32 ) VALUES (
33 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
34 )
35 ON CONFLICT (repo_id, workflow_file, trigger_event_id) WHERE trigger_event_id <> ''
36 DO NOTHING
37 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
38 head_sha, head_ref, event, event_payload,
39 actor_user_id, parent_run_id, concurrency_group,
40 status, conclusion, pinned, need_approval, approved_by_user_id,
41 started_at, completed_at, version, created_at, updated_at, trigger_event_id;
42
43 -- name: LookupWorkflowRunByTriggerEvent :one
44 -- Companion to EnqueueWorkflowRun for the conflict path: when an
45 -- INSERT ... ON CONFLICT DO NOTHING returns no rows, the trigger
46 -- handler uses this to find the existing row so it can surface a
47 -- stable RunID. Matches the partial-unique index from migration 0051.
48 SELECT id, repo_id, run_index, workflow_file, workflow_name,
49 head_sha, head_ref, event, event_payload,
50 actor_user_id, parent_run_id, concurrency_group,
51 status, conclusion, pinned, need_approval, approved_by_user_id,
52 started_at, completed_at, version, created_at, updated_at, trigger_event_id
53 FROM workflow_runs
54 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3;
55
56 -- name: GetWorkflowRunByID :one
57 SELECT id, repo_id, run_index, workflow_file, workflow_name,
58 head_sha, head_ref, event, event_payload,
59 actor_user_id, parent_run_id, concurrency_group,
60 status, conclusion, pinned, need_approval, approved_by_user_id,
61 started_at, completed_at, version, created_at, updated_at, trigger_event_id
62 FROM workflow_runs
63 WHERE id = $1;
64
65 -- name: MarkWorkflowRunRunning :exec
66 UPDATE workflow_runs
67 SET status = 'running',
68 started_at = COALESCE(started_at, now()),
69 version = version + 1,
70 updated_at = now()
71 WHERE id = $1 AND status = 'queued';
72
73 -- name: NextRunIndexForRepo :one
74 -- Atomic next-index emitter: take the max + 1 for this repo. Pairs
75 -- with the (repo_id, run_index) UNIQUE so concurrent inserts that
76 -- race here will catch a unique-violation and the caller retries.
77 -- Cast to bigint so sqlc generates int64 (the column type) rather
78 -- than int32 (the type the +1 literal would default to).
79 SELECT (COALESCE(MAX(run_index), 0) + 1)::bigint AS next_index
80 FROM workflow_runs
81 WHERE repo_id = $1;
82
83 -- name: ListWorkflowRunsForRepo :many
84 SELECT id, repo_id, run_index, workflow_file, workflow_name,
85 head_sha, head_ref, event, status, conclusion,
86 actor_user_id, started_at, completed_at, created_at
87 FROM workflow_runs
88 WHERE repo_id = $1
89 ORDER BY created_at DESC
90 LIMIT $2 OFFSET $3;
91