Go · 23246 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 deleteOldWorkflowRunsForCleanup = `-- name: DeleteOldWorkflowRunsForCleanup :execrows
105 DELETE FROM workflow_runs
106 WHERE pinned = false
107 AND status IN ('completed', 'cancelled')
108 AND completed_at IS NOT NULL
109 AND completed_at < $1
110 `
111
112 func (q *Queries) DeleteOldWorkflowRunsForCleanup(ctx context.Context, db DBTX, completedAt pgtype.Timestamptz) (int64, error) {
113 result, err := db.Exec(ctx, deleteOldWorkflowRunsForCleanup, completedAt)
114 if err != nil {
115 return 0, err
116 }
117 return result.RowsAffected(), nil
118 }
119
120 const deleteWorkflowRunByID = `-- name: DeleteWorkflowRunByID :execrows
121 DELETE FROM workflow_runs WHERE id = $1
122 `
123
124 // Cascades to workflow_jobs → workflow_steps → workflow_step_log_chunks
125 // and workflow_artifacts via the on-delete-cascade FK chain. The
126 // S3-side artifact blobs are NOT removed here; the handler queries
127 // the artifact object_keys first and deletes them best-effort after
128 // the row is gone.
129 func (q *Queries) DeleteWorkflowRunByID(ctx context.Context, db DBTX, id int64) (int64, error) {
130 result, err := db.Exec(ctx, deleteWorkflowRunByID, id)
131 if err != nil {
132 return 0, err
133 }
134 return result.RowsAffected(), nil
135 }
136
137 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
138 INSERT INTO workflow_runs (
139 repo_id, run_index, workflow_file, workflow_name,
140 head_sha, head_ref, event, event_payload,
141 actor_user_id, parent_run_id, concurrency_group, need_approval,
142 trigger_event_id
143 ) VALUES (
144 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
145 )
146 ON CONFLICT (repo_id, workflow_file, trigger_event_id) WHERE trigger_event_id <> ''
147 DO NOTHING
148 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
149 head_sha, head_ref, event, event_payload,
150 actor_user_id, parent_run_id, concurrency_group,
151 status, conclusion, pinned, need_approval, approved_by_user_id,
152 started_at, completed_at, version, created_at, updated_at, trigger_event_id
153 `
154
155 type EnqueueWorkflowRunParams struct {
156 RepoID int64
157 RunIndex int64
158 WorkflowFile string
159 WorkflowName string
160 HeadSha string
161 HeadRef string
162 Event WorkflowRunEvent
163 EventPayload []byte
164 ActorUserID pgtype.Int8
165 ParentRunID pgtype.Int8
166 ConcurrencyGroup string
167 NeedApproval bool
168 TriggerEventID string
169 }
170
171 // Idempotent insert: if a row with the same (repo_id, workflow_file,
172 // trigger_event_id) already exists, returns no rows (pgx.ErrNoRows in
173 // Go). The handler treats that as a successful no-op so worker
174 // retries and admin replays of the same triggering event don't
175 // duplicate runs.
176 //
177 // The ON CONFLICT predicate matches the partial unique index defined
178 // in migration 0051; both must agree for postgres to infer the
179 // target.
180 func (q *Queries) EnqueueWorkflowRun(ctx context.Context, db DBTX, arg EnqueueWorkflowRunParams) (WorkflowRun, error) {
181 row := db.QueryRow(ctx, enqueueWorkflowRun,
182 arg.RepoID,
183 arg.RunIndex,
184 arg.WorkflowFile,
185 arg.WorkflowName,
186 arg.HeadSha,
187 arg.HeadRef,
188 arg.Event,
189 arg.EventPayload,
190 arg.ActorUserID,
191 arg.ParentRunID,
192 arg.ConcurrencyGroup,
193 arg.NeedApproval,
194 arg.TriggerEventID,
195 )
196 var i WorkflowRun
197 err := row.Scan(
198 &i.ID,
199 &i.RepoID,
200 &i.RunIndex,
201 &i.WorkflowFile,
202 &i.WorkflowName,
203 &i.HeadSha,
204 &i.HeadRef,
205 &i.Event,
206 &i.EventPayload,
207 &i.ActorUserID,
208 &i.ParentRunID,
209 &i.ConcurrencyGroup,
210 &i.Status,
211 &i.Conclusion,
212 &i.Pinned,
213 &i.NeedApproval,
214 &i.ApprovedByUserID,
215 &i.StartedAt,
216 &i.CompletedAt,
217 &i.Version,
218 &i.CreatedAt,
219 &i.UpdatedAt,
220 &i.TriggerEventID,
221 )
222 return i, err
223 }
224
225 const getWorkflowRunByID = `-- name: GetWorkflowRunByID :one
226 SELECT id, repo_id, run_index, workflow_file, workflow_name,
227 head_sha, head_ref, event, event_payload,
228 actor_user_id, parent_run_id, concurrency_group,
229 status, conclusion, pinned, need_approval, approved_by_user_id,
230 started_at, completed_at, version, created_at, updated_at, trigger_event_id
231 FROM workflow_runs
232 WHERE id = $1
233 `
234
235 func (q *Queries) GetWorkflowRunByID(ctx context.Context, db DBTX, id int64) (WorkflowRun, error) {
236 row := db.QueryRow(ctx, getWorkflowRunByID, id)
237 var i WorkflowRun
238 err := row.Scan(
239 &i.ID,
240 &i.RepoID,
241 &i.RunIndex,
242 &i.WorkflowFile,
243 &i.WorkflowName,
244 &i.HeadSha,
245 &i.HeadRef,
246 &i.Event,
247 &i.EventPayload,
248 &i.ActorUserID,
249 &i.ParentRunID,
250 &i.ConcurrencyGroup,
251 &i.Status,
252 &i.Conclusion,
253 &i.Pinned,
254 &i.NeedApproval,
255 &i.ApprovedByUserID,
256 &i.StartedAt,
257 &i.CompletedAt,
258 &i.Version,
259 &i.CreatedAt,
260 &i.UpdatedAt,
261 &i.TriggerEventID,
262 )
263 return i, err
264 }
265
266 const getWorkflowRunForRepoByIndex = `-- name: GetWorkflowRunForRepoByIndex :one
267 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
268 r.head_sha, r.head_ref, r.event, r.event_payload,
269 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
270 r.parent_run_id, r.concurrency_group,
271 r.status, r.conclusion, r.pinned, r.need_approval, r.approved_by_user_id,
272 r.started_at, r.completed_at, r.version, r.created_at, r.updated_at, r.trigger_event_id
273 FROM workflow_runs r
274 LEFT JOIN users u ON u.id = r.actor_user_id
275 WHERE r.repo_id = $1 AND r.run_index = $2
276 `
277
278 type GetWorkflowRunForRepoByIndexParams struct {
279 RepoID int64
280 RunIndex int64
281 }
282
283 type GetWorkflowRunForRepoByIndexRow struct {
284 ID int64
285 RepoID int64
286 RunIndex int64
287 WorkflowFile string
288 WorkflowName string
289 HeadSha string
290 HeadRef string
291 Event WorkflowRunEvent
292 EventPayload []byte
293 ActorUserID pgtype.Int8
294 ActorUsername string
295 ParentRunID pgtype.Int8
296 ConcurrencyGroup string
297 Status WorkflowRunStatus
298 Conclusion NullCheckConclusion
299 Pinned bool
300 NeedApproval bool
301 ApprovedByUserID pgtype.Int8
302 StartedAt pgtype.Timestamptz
303 CompletedAt pgtype.Timestamptz
304 Version int32
305 CreatedAt pgtype.Timestamptz
306 UpdatedAt pgtype.Timestamptz
307 TriggerEventID string
308 }
309
310 func (q *Queries) GetWorkflowRunForRepoByIndex(ctx context.Context, db DBTX, arg GetWorkflowRunForRepoByIndexParams) (GetWorkflowRunForRepoByIndexRow, error) {
311 row := db.QueryRow(ctx, getWorkflowRunForRepoByIndex, arg.RepoID, arg.RunIndex)
312 var i GetWorkflowRunForRepoByIndexRow
313 err := row.Scan(
314 &i.ID,
315 &i.RepoID,
316 &i.RunIndex,
317 &i.WorkflowFile,
318 &i.WorkflowName,
319 &i.HeadSha,
320 &i.HeadRef,
321 &i.Event,
322 &i.EventPayload,
323 &i.ActorUserID,
324 &i.ActorUsername,
325 &i.ParentRunID,
326 &i.ConcurrencyGroup,
327 &i.Status,
328 &i.Conclusion,
329 &i.Pinned,
330 &i.NeedApproval,
331 &i.ApprovedByUserID,
332 &i.StartedAt,
333 &i.CompletedAt,
334 &i.Version,
335 &i.CreatedAt,
336 &i.UpdatedAt,
337 &i.TriggerEventID,
338 )
339 return i, err
340 }
341
342 const insertWorkflowRun = `-- name: InsertWorkflowRun :one
343
344 INSERT INTO workflow_runs (
345 repo_id, run_index, workflow_file, workflow_name,
346 head_sha, head_ref, event, event_payload,
347 actor_user_id, parent_run_id, concurrency_group, need_approval
348 ) VALUES (
349 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
350 )
351 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
352 head_sha, head_ref, event, event_payload,
353 actor_user_id, parent_run_id, concurrency_group,
354 status, conclusion, pinned, need_approval, approved_by_user_id,
355 started_at, completed_at, version, created_at, updated_at, trigger_event_id
356 `
357
358 type InsertWorkflowRunParams struct {
359 RepoID int64
360 RunIndex int64
361 WorkflowFile string
362 WorkflowName string
363 HeadSha string
364 HeadRef string
365 Event WorkflowRunEvent
366 EventPayload []byte
367 ActorUserID pgtype.Int8
368 ParentRunID pgtype.Int8
369 ConcurrencyGroup string
370 NeedApproval bool
371 }
372
373 // SPDX-License-Identifier: AGPL-3.0-or-later
374 func (q *Queries) InsertWorkflowRun(ctx context.Context, db DBTX, arg InsertWorkflowRunParams) (WorkflowRun, error) {
375 row := db.QueryRow(ctx, insertWorkflowRun,
376 arg.RepoID,
377 arg.RunIndex,
378 arg.WorkflowFile,
379 arg.WorkflowName,
380 arg.HeadSha,
381 arg.HeadRef,
382 arg.Event,
383 arg.EventPayload,
384 arg.ActorUserID,
385 arg.ParentRunID,
386 arg.ConcurrencyGroup,
387 arg.NeedApproval,
388 )
389 var i WorkflowRun
390 err := row.Scan(
391 &i.ID,
392 &i.RepoID,
393 &i.RunIndex,
394 &i.WorkflowFile,
395 &i.WorkflowName,
396 &i.HeadSha,
397 &i.HeadRef,
398 &i.Event,
399 &i.EventPayload,
400 &i.ActorUserID,
401 &i.ParentRunID,
402 &i.ConcurrencyGroup,
403 &i.Status,
404 &i.Conclusion,
405 &i.Pinned,
406 &i.NeedApproval,
407 &i.ApprovedByUserID,
408 &i.StartedAt,
409 &i.CompletedAt,
410 &i.Version,
411 &i.CreatedAt,
412 &i.UpdatedAt,
413 &i.TriggerEventID,
414 )
415 return i, err
416 }
417
418 const listActiveWorkflowRunsForAdmin = `-- name: ListActiveWorkflowRunsForAdmin :many
419 SELECT id, repo_id, run_index, workflow_file, workflow_name,
420 head_sha, head_ref, event, event_payload,
421 actor_user_id, parent_run_id, concurrency_group,
422 status, conclusion, pinned, need_approval, approved_by_user_id,
423 started_at, completed_at, version, created_at, updated_at, trigger_event_id
424 FROM workflow_runs
425 WHERE status IN ('queued', 'running')
426 AND ($1::bigint = 0 OR repo_id = $1::bigint)
427 ORDER BY created_at ASC, id ASC
428 LIMIT $2::int
429 `
430
431 type ListActiveWorkflowRunsForAdminParams struct {
432 RepoID int64
433 LimitCount int32
434 }
435
436 func (q *Queries) ListActiveWorkflowRunsForAdmin(ctx context.Context, db DBTX, arg ListActiveWorkflowRunsForAdminParams) ([]WorkflowRun, error) {
437 rows, err := db.Query(ctx, listActiveWorkflowRunsForAdmin, arg.RepoID, arg.LimitCount)
438 if err != nil {
439 return nil, err
440 }
441 defer rows.Close()
442 items := []WorkflowRun{}
443 for rows.Next() {
444 var i WorkflowRun
445 if err := rows.Scan(
446 &i.ID,
447 &i.RepoID,
448 &i.RunIndex,
449 &i.WorkflowFile,
450 &i.WorkflowName,
451 &i.HeadSha,
452 &i.HeadRef,
453 &i.Event,
454 &i.EventPayload,
455 &i.ActorUserID,
456 &i.ParentRunID,
457 &i.ConcurrencyGroup,
458 &i.Status,
459 &i.Conclusion,
460 &i.Pinned,
461 &i.NeedApproval,
462 &i.ApprovedByUserID,
463 &i.StartedAt,
464 &i.CompletedAt,
465 &i.Version,
466 &i.CreatedAt,
467 &i.UpdatedAt,
468 &i.TriggerEventID,
469 ); err != nil {
470 return nil, err
471 }
472 items = append(items, i)
473 }
474 if err := rows.Err(); err != nil {
475 return nil, err
476 }
477 return items, nil
478 }
479
480 const listBlockingConcurrencyRunsForUpdate = `-- name: ListBlockingConcurrencyRunsForUpdate :many
481 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
482 r.head_sha, r.head_ref, r.event, r.event_payload,
483 r.actor_user_id, r.parent_run_id, r.concurrency_group,
484 r.status, r.conclusion, r.pinned, r.need_approval, r.approved_by_user_id,
485 r.started_at, r.completed_at, r.version, r.created_at, r.updated_at, r.trigger_event_id
486 FROM workflow_runs r
487 JOIN workflow_runs current_run ON current_run.id = $1::bigint
488 WHERE r.repo_id = $2::bigint
489 AND r.concurrency_group = $3::text
490 AND r.concurrency_group <> ''
491 AND r.id <> current_run.id
492 AND r.status IN ('queued', 'running')
493 AND (r.created_at, r.id) < (current_run.created_at, current_run.id)
494 AND EXISTS (
495 SELECT 1
496 FROM workflow_jobs j
497 WHERE j.run_id = r.id
498 AND j.status IN ('queued', 'running')
499 AND j.cancel_requested = false
500 )
501 ORDER BY r.created_at ASC, r.id ASC
502 FOR UPDATE OF r
503 `
504
505 type ListBlockingConcurrencyRunsForUpdateParams struct {
506 RunID int64
507 RepoID int64
508 ConcurrencyGroup string
509 }
510
511 // Older queued/running runs with the same group block the new run while they
512 // still have at least one queued/running job that has not already received a
513 // cancel request. cancel-in-progress releases the slot by flipping that job
514 // flag even if the runner is still draining the old container.
515 func (q *Queries) ListBlockingConcurrencyRunsForUpdate(ctx context.Context, db DBTX, arg ListBlockingConcurrencyRunsForUpdateParams) ([]WorkflowRun, error) {
516 rows, err := db.Query(ctx, listBlockingConcurrencyRunsForUpdate, arg.RunID, arg.RepoID, arg.ConcurrencyGroup)
517 if err != nil {
518 return nil, err
519 }
520 defer rows.Close()
521 items := []WorkflowRun{}
522 for rows.Next() {
523 var i WorkflowRun
524 if err := rows.Scan(
525 &i.ID,
526 &i.RepoID,
527 &i.RunIndex,
528 &i.WorkflowFile,
529 &i.WorkflowName,
530 &i.HeadSha,
531 &i.HeadRef,
532 &i.Event,
533 &i.EventPayload,
534 &i.ActorUserID,
535 &i.ParentRunID,
536 &i.ConcurrencyGroup,
537 &i.Status,
538 &i.Conclusion,
539 &i.Pinned,
540 &i.NeedApproval,
541 &i.ApprovedByUserID,
542 &i.StartedAt,
543 &i.CompletedAt,
544 &i.Version,
545 &i.CreatedAt,
546 &i.UpdatedAt,
547 &i.TriggerEventID,
548 ); err != nil {
549 return nil, err
550 }
551 items = append(items, i)
552 }
553 if err := rows.Err(); err != nil {
554 return nil, err
555 }
556 return items, nil
557 }
558
559 const listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
560 WITH ranked AS (
561 SELECT workflow_file,
562 workflow_name,
563 (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
564 ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
565 FROM workflow_runs
566 WHERE repo_id = $1
567 )
568 SELECT workflow_file,
569 COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
570 run_count
571 FROM ranked
572 WHERE rn = 1
573 ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
574 `
575
576 type ListWorkflowRunWorkflowsForRepoRow struct {
577 WorkflowFile string
578 WorkflowName string
579 RunCount int64
580 }
581
582 func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
583 rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
584 if err != nil {
585 return nil, err
586 }
587 defer rows.Close()
588 items := []ListWorkflowRunWorkflowsForRepoRow{}
589 for rows.Next() {
590 var i ListWorkflowRunWorkflowsForRepoRow
591 if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
592 return nil, err
593 }
594 items = append(items, i)
595 }
596 if err := rows.Err(); err != nil {
597 return nil, err
598 }
599 return items, nil
600 }
601
602 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
603 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
604 r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
605 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
606 r.started_at, r.completed_at, r.created_at, r.updated_at
607 FROM workflow_runs r
608 LEFT JOIN users u ON u.id = r.actor_user_id
609 WHERE r.repo_id = $1::bigint
610 AND ($2::text IS NULL OR r.workflow_file = $2::text)
611 AND ($3::text IS NULL OR r.head_ref = $3::text)
612 AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
613 AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
614 AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
615 AND ($7::text IS NULL OR u.username = $7::citext)
616 ORDER BY r.created_at DESC, r.id DESC
617 LIMIT $9 OFFSET $8
618 `
619
620 type ListWorkflowRunsForRepoParams struct {
621 RepoID int64
622 WorkflowFile pgtype.Text
623 HeadRef pgtype.Text
624 Event NullWorkflowRunEvent
625 Status NullWorkflowRunStatus
626 Conclusion NullCheckConclusion
627 ActorUsername pgtype.Text
628 PageOffset int32
629 PageLimit int32
630 }
631
632 type ListWorkflowRunsForRepoRow struct {
633 ID int64
634 RepoID int64
635 RunIndex int64
636 WorkflowFile string
637 WorkflowName string
638 HeadSha string
639 HeadRef string
640 Event WorkflowRunEvent
641 Status WorkflowRunStatus
642 Conclusion NullCheckConclusion
643 ActorUserID pgtype.Int8
644 ActorUsername string
645 StartedAt pgtype.Timestamptz
646 CompletedAt pgtype.Timestamptz
647 CreatedAt pgtype.Timestamptz
648 UpdatedAt pgtype.Timestamptz
649 }
650
651 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
652 rows, err := db.Query(ctx, listWorkflowRunsForRepo,
653 arg.RepoID,
654 arg.WorkflowFile,
655 arg.HeadRef,
656 arg.Event,
657 arg.Status,
658 arg.Conclusion,
659 arg.ActorUsername,
660 arg.PageOffset,
661 arg.PageLimit,
662 )
663 if err != nil {
664 return nil, err
665 }
666 defer rows.Close()
667 items := []ListWorkflowRunsForRepoRow{}
668 for rows.Next() {
669 var i ListWorkflowRunsForRepoRow
670 if err := rows.Scan(
671 &i.ID,
672 &i.RepoID,
673 &i.RunIndex,
674 &i.WorkflowFile,
675 &i.WorkflowName,
676 &i.HeadSha,
677 &i.HeadRef,
678 &i.Event,
679 &i.Status,
680 &i.Conclusion,
681 &i.ActorUserID,
682 &i.ActorUsername,
683 &i.StartedAt,
684 &i.CompletedAt,
685 &i.CreatedAt,
686 &i.UpdatedAt,
687 ); err != nil {
688 return nil, err
689 }
690 items = append(items, i)
691 }
692 if err := rows.Err(); err != nil {
693 return nil, err
694 }
695 return items, nil
696 }
697
698 const lookupWorkflowRunByTriggerEvent = `-- name: LookupWorkflowRunByTriggerEvent :one
699 SELECT id, repo_id, run_index, workflow_file, workflow_name,
700 head_sha, head_ref, event, event_payload,
701 actor_user_id, parent_run_id, concurrency_group,
702 status, conclusion, pinned, need_approval, approved_by_user_id,
703 started_at, completed_at, version, created_at, updated_at, trigger_event_id
704 FROM workflow_runs
705 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3
706 `
707
708 type LookupWorkflowRunByTriggerEventParams struct {
709 RepoID int64
710 WorkflowFile string
711 TriggerEventID string
712 }
713
714 // Companion to EnqueueWorkflowRun for the conflict path: when an
715 // INSERT ... ON CONFLICT DO NOTHING returns no rows, the trigger
716 // handler uses this to find the existing row so it can surface a
717 // stable RunID. Matches the partial-unique index from migration 0051.
718 func (q *Queries) LookupWorkflowRunByTriggerEvent(ctx context.Context, db DBTX, arg LookupWorkflowRunByTriggerEventParams) (WorkflowRun, error) {
719 row := db.QueryRow(ctx, lookupWorkflowRunByTriggerEvent, arg.RepoID, arg.WorkflowFile, arg.TriggerEventID)
720 var i WorkflowRun
721 err := row.Scan(
722 &i.ID,
723 &i.RepoID,
724 &i.RunIndex,
725 &i.WorkflowFile,
726 &i.WorkflowName,
727 &i.HeadSha,
728 &i.HeadRef,
729 &i.Event,
730 &i.EventPayload,
731 &i.ActorUserID,
732 &i.ParentRunID,
733 &i.ConcurrencyGroup,
734 &i.Status,
735 &i.Conclusion,
736 &i.Pinned,
737 &i.NeedApproval,
738 &i.ApprovedByUserID,
739 &i.StartedAt,
740 &i.CompletedAt,
741 &i.Version,
742 &i.CreatedAt,
743 &i.UpdatedAt,
744 &i.TriggerEventID,
745 )
746 return i, err
747 }
748
749 const markWorkflowRunRunning = `-- name: MarkWorkflowRunRunning :exec
750 UPDATE workflow_runs
751 SET status = 'running',
752 started_at = COALESCE(started_at, now()),
753 version = version + 1,
754 updated_at = now()
755 WHERE id = $1 AND status = 'queued'
756 `
757
758 func (q *Queries) MarkWorkflowRunRunning(ctx context.Context, db DBTX, id int64) error {
759 _, err := db.Exec(ctx, markWorkflowRunRunning, id)
760 return err
761 }
762
763 const nextRunIndexForRepo = `-- name: NextRunIndexForRepo :one
764 SELECT (COALESCE(MAX(run_index), 0) + 1)::bigint AS next_index
765 FROM workflow_runs
766 WHERE repo_id = $1
767 `
768
769 // Atomic next-index emitter: take the max + 1 for this repo. Pairs
770 // with the (repo_id, run_index) UNIQUE so concurrent inserts that
771 // race here will catch a unique-violation and the caller retries.
772 // Cast to bigint so sqlc generates int64 (the column type) rather
773 // than int32 (the type the +1 literal would default to).
774 func (q *Queries) NextRunIndexForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
775 row := db.QueryRow(ctx, nextRunIndexForRepo, repoID)
776 var next_index int64
777 err := row.Scan(&next_index)
778 return next_index, err
779 }
780
781 const startWorkflowRun = `-- name: StartWorkflowRun :one
782 UPDATE workflow_runs
783 SET status = 'running',
784 started_at = COALESCE(started_at, now()),
785 version = version + 1,
786 updated_at = now()
787 WHERE id = $1 AND status = 'queued'
788 AND (need_approval = false OR approved_by_user_id IS NOT NULL)
789 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
790 head_sha, head_ref, event, event_payload,
791 actor_user_id, parent_run_id, concurrency_group,
792 status, conclusion, pinned, need_approval, approved_by_user_id,
793 started_at, completed_at, version, created_at, updated_at, trigger_event_id
794 `
795
796 func (q *Queries) StartWorkflowRun(ctx context.Context, db DBTX, id int64) (WorkflowRun, error) {
797 row := db.QueryRow(ctx, startWorkflowRun, id)
798 var i WorkflowRun
799 err := row.Scan(
800 &i.ID,
801 &i.RepoID,
802 &i.RunIndex,
803 &i.WorkflowFile,
804 &i.WorkflowName,
805 &i.HeadSha,
806 &i.HeadRef,
807 &i.Event,
808 &i.EventPayload,
809 &i.ActorUserID,
810 &i.ParentRunID,
811 &i.ConcurrencyGroup,
812 &i.Status,
813 &i.Conclusion,
814 &i.Pinned,
815 &i.NeedApproval,
816 &i.ApprovedByUserID,
817 &i.StartedAt,
818 &i.CompletedAt,
819 &i.Version,
820 &i.CreatedAt,
821 &i.UpdatedAt,
822 &i.TriggerEventID,
823 )
824 return i, err
825 }
826