Go · 17394 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 listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
402 WITH ranked AS (
403 SELECT workflow_file,
404 workflow_name,
405 (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
406 ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
407 FROM workflow_runs
408 WHERE repo_id = $1
409 )
410 SELECT workflow_file,
411 COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
412 run_count
413 FROM ranked
414 WHERE rn = 1
415 ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
416 `
417
418 type ListWorkflowRunWorkflowsForRepoRow struct {
419 WorkflowFile string
420 WorkflowName string
421 RunCount int64
422 }
423
424 func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
425 rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
426 if err != nil {
427 return nil, err
428 }
429 defer rows.Close()
430 items := []ListWorkflowRunWorkflowsForRepoRow{}
431 for rows.Next() {
432 var i ListWorkflowRunWorkflowsForRepoRow
433 if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
434 return nil, err
435 }
436 items = append(items, i)
437 }
438 if err := rows.Err(); err != nil {
439 return nil, err
440 }
441 return items, nil
442 }
443
444 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
445 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
446 r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
447 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
448 r.started_at, r.completed_at, r.created_at, r.updated_at
449 FROM workflow_runs r
450 LEFT JOIN users u ON u.id = r.actor_user_id
451 WHERE r.repo_id = $1::bigint
452 AND ($2::text IS NULL OR r.workflow_file = $2::text)
453 AND ($3::text IS NULL OR r.head_ref = $3::text)
454 AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
455 AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
456 AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
457 AND ($7::text IS NULL OR u.username = $7::citext)
458 ORDER BY r.created_at DESC, r.id DESC
459 LIMIT $9 OFFSET $8
460 `
461
462 type ListWorkflowRunsForRepoParams struct {
463 RepoID int64
464 WorkflowFile pgtype.Text
465 HeadRef pgtype.Text
466 Event NullWorkflowRunEvent
467 Status NullWorkflowRunStatus
468 Conclusion NullCheckConclusion
469 ActorUsername pgtype.Text
470 PageOffset int32
471 PageLimit int32
472 }
473
474 type ListWorkflowRunsForRepoRow struct {
475 ID int64
476 RepoID int64
477 RunIndex int64
478 WorkflowFile string
479 WorkflowName string
480 HeadSha string
481 HeadRef string
482 Event WorkflowRunEvent
483 Status WorkflowRunStatus
484 Conclusion NullCheckConclusion
485 ActorUserID pgtype.Int8
486 ActorUsername string
487 StartedAt pgtype.Timestamptz
488 CompletedAt pgtype.Timestamptz
489 CreatedAt pgtype.Timestamptz
490 UpdatedAt pgtype.Timestamptz
491 }
492
493 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
494 rows, err := db.Query(ctx, listWorkflowRunsForRepo,
495 arg.RepoID,
496 arg.WorkflowFile,
497 arg.HeadRef,
498 arg.Event,
499 arg.Status,
500 arg.Conclusion,
501 arg.ActorUsername,
502 arg.PageOffset,
503 arg.PageLimit,
504 )
505 if err != nil {
506 return nil, err
507 }
508 defer rows.Close()
509 items := []ListWorkflowRunsForRepoRow{}
510 for rows.Next() {
511 var i ListWorkflowRunsForRepoRow
512 if err := rows.Scan(
513 &i.ID,
514 &i.RepoID,
515 &i.RunIndex,
516 &i.WorkflowFile,
517 &i.WorkflowName,
518 &i.HeadSha,
519 &i.HeadRef,
520 &i.Event,
521 &i.Status,
522 &i.Conclusion,
523 &i.ActorUserID,
524 &i.ActorUsername,
525 &i.StartedAt,
526 &i.CompletedAt,
527 &i.CreatedAt,
528 &i.UpdatedAt,
529 ); err != nil {
530 return nil, err
531 }
532 items = append(items, i)
533 }
534 if err := rows.Err(); err != nil {
535 return nil, err
536 }
537 return items, nil
538 }
539
540 const lookupWorkflowRunByTriggerEvent = `-- name: LookupWorkflowRunByTriggerEvent :one
541 SELECT id, repo_id, run_index, workflow_file, workflow_name,
542 head_sha, head_ref, event, event_payload,
543 actor_user_id, parent_run_id, concurrency_group,
544 status, conclusion, pinned, need_approval, approved_by_user_id,
545 started_at, completed_at, version, created_at, updated_at, trigger_event_id
546 FROM workflow_runs
547 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3
548 `
549
550 type LookupWorkflowRunByTriggerEventParams struct {
551 RepoID int64
552 WorkflowFile string
553 TriggerEventID string
554 }
555
556 // Companion to EnqueueWorkflowRun for the conflict path: when an
557 // INSERT ... ON CONFLICT DO NOTHING returns no rows, the trigger
558 // handler uses this to find the existing row so it can surface a
559 // stable RunID. Matches the partial-unique index from migration 0051.
560 func (q *Queries) LookupWorkflowRunByTriggerEvent(ctx context.Context, db DBTX, arg LookupWorkflowRunByTriggerEventParams) (WorkflowRun, error) {
561 row := db.QueryRow(ctx, lookupWorkflowRunByTriggerEvent, arg.RepoID, arg.WorkflowFile, arg.TriggerEventID)
562 var i WorkflowRun
563 err := row.Scan(
564 &i.ID,
565 &i.RepoID,
566 &i.RunIndex,
567 &i.WorkflowFile,
568 &i.WorkflowName,
569 &i.HeadSha,
570 &i.HeadRef,
571 &i.Event,
572 &i.EventPayload,
573 &i.ActorUserID,
574 &i.ParentRunID,
575 &i.ConcurrencyGroup,
576 &i.Status,
577 &i.Conclusion,
578 &i.Pinned,
579 &i.NeedApproval,
580 &i.ApprovedByUserID,
581 &i.StartedAt,
582 &i.CompletedAt,
583 &i.Version,
584 &i.CreatedAt,
585 &i.UpdatedAt,
586 &i.TriggerEventID,
587 )
588 return i, err
589 }
590
591 const markWorkflowRunRunning = `-- name: MarkWorkflowRunRunning :exec
592 UPDATE workflow_runs
593 SET status = 'running',
594 started_at = COALESCE(started_at, now()),
595 version = version + 1,
596 updated_at = now()
597 WHERE id = $1 AND status = 'queued'
598 `
599
600 func (q *Queries) MarkWorkflowRunRunning(ctx context.Context, db DBTX, id int64) error {
601 _, err := db.Exec(ctx, markWorkflowRunRunning, id)
602 return err
603 }
604
605 const nextRunIndexForRepo = `-- name: NextRunIndexForRepo :one
606 SELECT (COALESCE(MAX(run_index), 0) + 1)::bigint AS next_index
607 FROM workflow_runs
608 WHERE repo_id = $1
609 `
610
611 // Atomic next-index emitter: take the max + 1 for this repo. Pairs
612 // with the (repo_id, run_index) UNIQUE so concurrent inserts that
613 // race here will catch a unique-violation and the caller retries.
614 // Cast to bigint so sqlc generates int64 (the column type) rather
615 // than int32 (the type the +1 literal would default to).
616 func (q *Queries) NextRunIndexForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
617 row := db.QueryRow(ctx, nextRunIndexForRepo, repoID)
618 var next_index int64
619 err := row.Scan(&next_index)
620 return next_index, err
621 }
622