Go · 14716 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: workflow_runs.sql
5
6 package actionsdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const completeWorkflowRun = `-- name: CompleteWorkflowRun :one
15 UPDATE workflow_runs
16 SET status = 'completed',
17 conclusion = $2::check_conclusion,
18 started_at = COALESCE(started_at, now()),
19 completed_at = COALESCE(completed_at, now()),
20 version = version + 1,
21 updated_at = now()
22 WHERE id = $1
23 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
24 head_sha, head_ref, event, event_payload,
25 actor_user_id, parent_run_id, concurrency_group,
26 status, conclusion, pinned, need_approval, approved_by_user_id,
27 started_at, completed_at, version, created_at, updated_at, trigger_event_id
28 `
29
30 type CompleteWorkflowRunParams struct {
31 ID int64
32 Conclusion CheckConclusion
33 }
34
35 func (q *Queries) CompleteWorkflowRun(ctx context.Context, db DBTX, arg CompleteWorkflowRunParams) (WorkflowRun, error) {
36 row := db.QueryRow(ctx, completeWorkflowRun, arg.ID, arg.Conclusion)
37 var i WorkflowRun
38 err := row.Scan(
39 &i.ID,
40 &i.RepoID,
41 &i.RunIndex,
42 &i.WorkflowFile,
43 &i.WorkflowName,
44 &i.HeadSha,
45 &i.HeadRef,
46 &i.Event,
47 &i.EventPayload,
48 &i.ActorUserID,
49 &i.ParentRunID,
50 &i.ConcurrencyGroup,
51 &i.Status,
52 &i.Conclusion,
53 &i.Pinned,
54 &i.NeedApproval,
55 &i.ApprovedByUserID,
56 &i.StartedAt,
57 &i.CompletedAt,
58 &i.Version,
59 &i.CreatedAt,
60 &i.UpdatedAt,
61 &i.TriggerEventID,
62 )
63 return i, err
64 }
65
66 const countWorkflowRunsForRepo = `-- name: CountWorkflowRunsForRepo :one
67 SELECT COUNT(*)::bigint
68 FROM workflow_runs r
69 LEFT JOIN users u ON u.id = r.actor_user_id
70 WHERE r.repo_id = $1::bigint
71 AND ($2::text IS NULL OR r.workflow_file = $2::text)
72 AND ($3::text IS NULL OR r.head_ref = $3::text)
73 AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
74 AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
75 AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
76 AND ($7::text IS NULL OR u.username = $7::citext)
77 `
78
79 type CountWorkflowRunsForRepoParams struct {
80 RepoID int64
81 WorkflowFile pgtype.Text
82 HeadRef pgtype.Text
83 Event NullWorkflowRunEvent
84 Status NullWorkflowRunStatus
85 Conclusion NullCheckConclusion
86 ActorUsername pgtype.Text
87 }
88
89 func (q *Queries) CountWorkflowRunsForRepo(ctx context.Context, db DBTX, arg CountWorkflowRunsForRepoParams) (int64, error) {
90 row := db.QueryRow(ctx, countWorkflowRunsForRepo,
91 arg.RepoID,
92 arg.WorkflowFile,
93 arg.HeadRef,
94 arg.Event,
95 arg.Status,
96 arg.Conclusion,
97 arg.ActorUsername,
98 )
99 var column_1 int64
100 err := row.Scan(&column_1)
101 return column_1, err
102 }
103
104 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
105 INSERT INTO workflow_runs (
106 repo_id, run_index, workflow_file, workflow_name,
107 head_sha, head_ref, event, event_payload,
108 actor_user_id, parent_run_id, concurrency_group, need_approval,
109 trigger_event_id
110 ) VALUES (
111 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
112 )
113 ON CONFLICT (repo_id, workflow_file, trigger_event_id) WHERE trigger_event_id <> ''
114 DO NOTHING
115 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
116 head_sha, head_ref, event, event_payload,
117 actor_user_id, parent_run_id, concurrency_group,
118 status, conclusion, pinned, need_approval, approved_by_user_id,
119 started_at, completed_at, version, created_at, updated_at, trigger_event_id
120 `
121
122 type EnqueueWorkflowRunParams struct {
123 RepoID int64
124 RunIndex int64
125 WorkflowFile string
126 WorkflowName string
127 HeadSha string
128 HeadRef string
129 Event WorkflowRunEvent
130 EventPayload []byte
131 ActorUserID pgtype.Int8
132 ParentRunID pgtype.Int8
133 ConcurrencyGroup string
134 NeedApproval bool
135 TriggerEventID string
136 }
137
138 // Idempotent insert: if a row with the same (repo_id, workflow_file,
139 // trigger_event_id) already exists, returns no rows (pgx.ErrNoRows in
140 // Go). The handler treats that as a successful no-op so worker
141 // retries and admin replays of the same triggering event don't
142 // duplicate runs.
143 //
144 // The ON CONFLICT predicate matches the partial unique index defined
145 // in migration 0051; both must agree for postgres to infer the
146 // target.
147 func (q *Queries) EnqueueWorkflowRun(ctx context.Context, db DBTX, arg EnqueueWorkflowRunParams) (WorkflowRun, error) {
148 row := db.QueryRow(ctx, enqueueWorkflowRun,
149 arg.RepoID,
150 arg.RunIndex,
151 arg.WorkflowFile,
152 arg.WorkflowName,
153 arg.HeadSha,
154 arg.HeadRef,
155 arg.Event,
156 arg.EventPayload,
157 arg.ActorUserID,
158 arg.ParentRunID,
159 arg.ConcurrencyGroup,
160 arg.NeedApproval,
161 arg.TriggerEventID,
162 )
163 var i WorkflowRun
164 err := row.Scan(
165 &i.ID,
166 &i.RepoID,
167 &i.RunIndex,
168 &i.WorkflowFile,
169 &i.WorkflowName,
170 &i.HeadSha,
171 &i.HeadRef,
172 &i.Event,
173 &i.EventPayload,
174 &i.ActorUserID,
175 &i.ParentRunID,
176 &i.ConcurrencyGroup,
177 &i.Status,
178 &i.Conclusion,
179 &i.Pinned,
180 &i.NeedApproval,
181 &i.ApprovedByUserID,
182 &i.StartedAt,
183 &i.CompletedAt,
184 &i.Version,
185 &i.CreatedAt,
186 &i.UpdatedAt,
187 &i.TriggerEventID,
188 )
189 return i, err
190 }
191
192 const getWorkflowRunByID = `-- name: GetWorkflowRunByID :one
193 SELECT id, repo_id, run_index, workflow_file, workflow_name,
194 head_sha, head_ref, event, event_payload,
195 actor_user_id, parent_run_id, concurrency_group,
196 status, conclusion, pinned, need_approval, approved_by_user_id,
197 started_at, completed_at, version, created_at, updated_at, trigger_event_id
198 FROM workflow_runs
199 WHERE id = $1
200 `
201
202 func (q *Queries) GetWorkflowRunByID(ctx context.Context, db DBTX, id int64) (WorkflowRun, error) {
203 row := db.QueryRow(ctx, getWorkflowRunByID, id)
204 var i WorkflowRun
205 err := row.Scan(
206 &i.ID,
207 &i.RepoID,
208 &i.RunIndex,
209 &i.WorkflowFile,
210 &i.WorkflowName,
211 &i.HeadSha,
212 &i.HeadRef,
213 &i.Event,
214 &i.EventPayload,
215 &i.ActorUserID,
216 &i.ParentRunID,
217 &i.ConcurrencyGroup,
218 &i.Status,
219 &i.Conclusion,
220 &i.Pinned,
221 &i.NeedApproval,
222 &i.ApprovedByUserID,
223 &i.StartedAt,
224 &i.CompletedAt,
225 &i.Version,
226 &i.CreatedAt,
227 &i.UpdatedAt,
228 &i.TriggerEventID,
229 )
230 return i, err
231 }
232
233 const insertWorkflowRun = `-- name: InsertWorkflowRun :one
234
235 INSERT INTO workflow_runs (
236 repo_id, run_index, workflow_file, workflow_name,
237 head_sha, head_ref, event, event_payload,
238 actor_user_id, parent_run_id, concurrency_group, need_approval
239 ) VALUES (
240 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
241 )
242 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
243 head_sha, head_ref, event, event_payload,
244 actor_user_id, parent_run_id, concurrency_group,
245 status, conclusion, pinned, need_approval, approved_by_user_id,
246 started_at, completed_at, version, created_at, updated_at, trigger_event_id
247 `
248
249 type InsertWorkflowRunParams struct {
250 RepoID int64
251 RunIndex int64
252 WorkflowFile string
253 WorkflowName string
254 HeadSha string
255 HeadRef string
256 Event WorkflowRunEvent
257 EventPayload []byte
258 ActorUserID pgtype.Int8
259 ParentRunID pgtype.Int8
260 ConcurrencyGroup string
261 NeedApproval bool
262 }
263
264 // SPDX-License-Identifier: AGPL-3.0-or-later
265 func (q *Queries) InsertWorkflowRun(ctx context.Context, db DBTX, arg InsertWorkflowRunParams) (WorkflowRun, error) {
266 row := db.QueryRow(ctx, insertWorkflowRun,
267 arg.RepoID,
268 arg.RunIndex,
269 arg.WorkflowFile,
270 arg.WorkflowName,
271 arg.HeadSha,
272 arg.HeadRef,
273 arg.Event,
274 arg.EventPayload,
275 arg.ActorUserID,
276 arg.ParentRunID,
277 arg.ConcurrencyGroup,
278 arg.NeedApproval,
279 )
280 var i WorkflowRun
281 err := row.Scan(
282 &i.ID,
283 &i.RepoID,
284 &i.RunIndex,
285 &i.WorkflowFile,
286 &i.WorkflowName,
287 &i.HeadSha,
288 &i.HeadRef,
289 &i.Event,
290 &i.EventPayload,
291 &i.ActorUserID,
292 &i.ParentRunID,
293 &i.ConcurrencyGroup,
294 &i.Status,
295 &i.Conclusion,
296 &i.Pinned,
297 &i.NeedApproval,
298 &i.ApprovedByUserID,
299 &i.StartedAt,
300 &i.CompletedAt,
301 &i.Version,
302 &i.CreatedAt,
303 &i.UpdatedAt,
304 &i.TriggerEventID,
305 )
306 return i, err
307 }
308
309 const listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
310 WITH ranked AS (
311 SELECT workflow_file,
312 workflow_name,
313 (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
314 ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
315 FROM workflow_runs
316 WHERE repo_id = $1
317 )
318 SELECT workflow_file,
319 COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
320 run_count
321 FROM ranked
322 WHERE rn = 1
323 ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
324 `
325
326 type ListWorkflowRunWorkflowsForRepoRow struct {
327 WorkflowFile string
328 WorkflowName string
329 RunCount int64
330 }
331
332 func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
333 rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
334 if err != nil {
335 return nil, err
336 }
337 defer rows.Close()
338 items := []ListWorkflowRunWorkflowsForRepoRow{}
339 for rows.Next() {
340 var i ListWorkflowRunWorkflowsForRepoRow
341 if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
342 return nil, err
343 }
344 items = append(items, i)
345 }
346 if err := rows.Err(); err != nil {
347 return nil, err
348 }
349 return items, nil
350 }
351
352 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
353 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
354 r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
355 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
356 r.started_at, r.completed_at, r.created_at, r.updated_at
357 FROM workflow_runs r
358 LEFT JOIN users u ON u.id = r.actor_user_id
359 WHERE r.repo_id = $1::bigint
360 AND ($2::text IS NULL OR r.workflow_file = $2::text)
361 AND ($3::text IS NULL OR r.head_ref = $3::text)
362 AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
363 AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
364 AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
365 AND ($7::text IS NULL OR u.username = $7::citext)
366 ORDER BY r.created_at DESC, r.id DESC
367 LIMIT $9 OFFSET $8
368 `
369
370 type ListWorkflowRunsForRepoParams struct {
371 RepoID int64
372 WorkflowFile pgtype.Text
373 HeadRef pgtype.Text
374 Event NullWorkflowRunEvent
375 Status NullWorkflowRunStatus
376 Conclusion NullCheckConclusion
377 ActorUsername pgtype.Text
378 PageOffset int32
379 PageLimit int32
380 }
381
382 type ListWorkflowRunsForRepoRow struct {
383 ID int64
384 RepoID int64
385 RunIndex int64
386 WorkflowFile string
387 WorkflowName string
388 HeadSha string
389 HeadRef string
390 Event WorkflowRunEvent
391 Status WorkflowRunStatus
392 Conclusion NullCheckConclusion
393 ActorUserID pgtype.Int8
394 ActorUsername string
395 StartedAt pgtype.Timestamptz
396 CompletedAt pgtype.Timestamptz
397 CreatedAt pgtype.Timestamptz
398 UpdatedAt pgtype.Timestamptz
399 }
400
401 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
402 rows, err := db.Query(ctx, listWorkflowRunsForRepo,
403 arg.RepoID,
404 arg.WorkflowFile,
405 arg.HeadRef,
406 arg.Event,
407 arg.Status,
408 arg.Conclusion,
409 arg.ActorUsername,
410 arg.PageOffset,
411 arg.PageLimit,
412 )
413 if err != nil {
414 return nil, err
415 }
416 defer rows.Close()
417 items := []ListWorkflowRunsForRepoRow{}
418 for rows.Next() {
419 var i ListWorkflowRunsForRepoRow
420 if err := rows.Scan(
421 &i.ID,
422 &i.RepoID,
423 &i.RunIndex,
424 &i.WorkflowFile,
425 &i.WorkflowName,
426 &i.HeadSha,
427 &i.HeadRef,
428 &i.Event,
429 &i.Status,
430 &i.Conclusion,
431 &i.ActorUserID,
432 &i.ActorUsername,
433 &i.StartedAt,
434 &i.CompletedAt,
435 &i.CreatedAt,
436 &i.UpdatedAt,
437 ); err != nil {
438 return nil, err
439 }
440 items = append(items, i)
441 }
442 if err := rows.Err(); err != nil {
443 return nil, err
444 }
445 return items, nil
446 }
447
448 const lookupWorkflowRunByTriggerEvent = `-- name: LookupWorkflowRunByTriggerEvent :one
449 SELECT id, repo_id, run_index, workflow_file, workflow_name,
450 head_sha, head_ref, event, event_payload,
451 actor_user_id, parent_run_id, concurrency_group,
452 status, conclusion, pinned, need_approval, approved_by_user_id,
453 started_at, completed_at, version, created_at, updated_at, trigger_event_id
454 FROM workflow_runs
455 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3
456 `
457
458 type LookupWorkflowRunByTriggerEventParams struct {
459 RepoID int64
460 WorkflowFile string
461 TriggerEventID string
462 }
463
464 // Companion to EnqueueWorkflowRun for the conflict path: when an
465 // INSERT ... ON CONFLICT DO NOTHING returns no rows, the trigger
466 // handler uses this to find the existing row so it can surface a
467 // stable RunID. Matches the partial-unique index from migration 0051.
468 func (q *Queries) LookupWorkflowRunByTriggerEvent(ctx context.Context, db DBTX, arg LookupWorkflowRunByTriggerEventParams) (WorkflowRun, error) {
469 row := db.QueryRow(ctx, lookupWorkflowRunByTriggerEvent, arg.RepoID, arg.WorkflowFile, arg.TriggerEventID)
470 var i WorkflowRun
471 err := row.Scan(
472 &i.ID,
473 &i.RepoID,
474 &i.RunIndex,
475 &i.WorkflowFile,
476 &i.WorkflowName,
477 &i.HeadSha,
478 &i.HeadRef,
479 &i.Event,
480 &i.EventPayload,
481 &i.ActorUserID,
482 &i.ParentRunID,
483 &i.ConcurrencyGroup,
484 &i.Status,
485 &i.Conclusion,
486 &i.Pinned,
487 &i.NeedApproval,
488 &i.ApprovedByUserID,
489 &i.StartedAt,
490 &i.CompletedAt,
491 &i.Version,
492 &i.CreatedAt,
493 &i.UpdatedAt,
494 &i.TriggerEventID,
495 )
496 return i, err
497 }
498
499 const markWorkflowRunRunning = `-- name: MarkWorkflowRunRunning :exec
500 UPDATE workflow_runs
501 SET status = 'running',
502 started_at = COALESCE(started_at, now()),
503 version = version + 1,
504 updated_at = now()
505 WHERE id = $1 AND status = 'queued'
506 `
507
508 func (q *Queries) MarkWorkflowRunRunning(ctx context.Context, db DBTX, id int64) error {
509 _, err := db.Exec(ctx, markWorkflowRunRunning, id)
510 return err
511 }
512
513 const nextRunIndexForRepo = `-- name: NextRunIndexForRepo :one
514 SELECT (COALESCE(MAX(run_index), 0) + 1)::bigint AS next_index
515 FROM workflow_runs
516 WHERE repo_id = $1
517 `
518
519 // Atomic next-index emitter: take the max + 1 for this repo. Pairs
520 // with the (repo_id, run_index) UNIQUE so concurrent inserts that
521 // race here will catch a unique-violation and the caller retries.
522 // Cast to bigint so sqlc generates int64 (the column type) rather
523 // than int32 (the type the +1 literal would default to).
524 func (q *Queries) NextRunIndexForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
525 row := db.QueryRow(ctx, nextRunIndexForRepo, repoID)
526 var next_index int64
527 err := row.Scan(&next_index)
528 return next_index, err
529 }
530