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