Go · 16876 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 getWorkflowRunForRepoByIndex = `-- name: GetWorkflowRunForRepoByIndex :one
234 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
235 r.head_sha, r.head_ref, r.event, r.event_payload,
236 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
237 r.parent_run_id, r.concurrency_group,
238 r.status, r.conclusion, r.pinned, r.need_approval, r.approved_by_user_id,
239 r.started_at, r.completed_at, r.version, r.created_at, r.updated_at, r.trigger_event_id
240 FROM workflow_runs r
241 LEFT JOIN users u ON u.id = r.actor_user_id
242 WHERE r.repo_id = $1 AND r.run_index = $2
243 `
244
245 type GetWorkflowRunForRepoByIndexParams struct {
246 RepoID int64
247 RunIndex int64
248 }
249
250 type GetWorkflowRunForRepoByIndexRow struct {
251 ID int64
252 RepoID int64
253 RunIndex int64
254 WorkflowFile string
255 WorkflowName string
256 HeadSha string
257 HeadRef string
258 Event WorkflowRunEvent
259 EventPayload []byte
260 ActorUserID pgtype.Int8
261 ActorUsername string
262 ParentRunID pgtype.Int8
263 ConcurrencyGroup string
264 Status WorkflowRunStatus
265 Conclusion NullCheckConclusion
266 Pinned bool
267 NeedApproval bool
268 ApprovedByUserID pgtype.Int8
269 StartedAt pgtype.Timestamptz
270 CompletedAt pgtype.Timestamptz
271 Version int32
272 CreatedAt pgtype.Timestamptz
273 UpdatedAt pgtype.Timestamptz
274 TriggerEventID string
275 }
276
277 func (q *Queries) GetWorkflowRunForRepoByIndex(ctx context.Context, db DBTX, arg GetWorkflowRunForRepoByIndexParams) (GetWorkflowRunForRepoByIndexRow, error) {
278 row := db.QueryRow(ctx, getWorkflowRunForRepoByIndex, arg.RepoID, arg.RunIndex)
279 var i GetWorkflowRunForRepoByIndexRow
280 err := row.Scan(
281 &i.ID,
282 &i.RepoID,
283 &i.RunIndex,
284 &i.WorkflowFile,
285 &i.WorkflowName,
286 &i.HeadSha,
287 &i.HeadRef,
288 &i.Event,
289 &i.EventPayload,
290 &i.ActorUserID,
291 &i.ActorUsername,
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 insertWorkflowRun = `-- name: InsertWorkflowRun :one
310
311 INSERT INTO workflow_runs (
312 repo_id, run_index, workflow_file, workflow_name,
313 head_sha, head_ref, event, event_payload,
314 actor_user_id, parent_run_id, concurrency_group, need_approval
315 ) VALUES (
316 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
317 )
318 RETURNING id, repo_id, run_index, workflow_file, workflow_name,
319 head_sha, head_ref, event, event_payload,
320 actor_user_id, parent_run_id, concurrency_group,
321 status, conclusion, pinned, need_approval, approved_by_user_id,
322 started_at, completed_at, version, created_at, updated_at, trigger_event_id
323 `
324
325 type InsertWorkflowRunParams struct {
326 RepoID int64
327 RunIndex int64
328 WorkflowFile string
329 WorkflowName string
330 HeadSha string
331 HeadRef string
332 Event WorkflowRunEvent
333 EventPayload []byte
334 ActorUserID pgtype.Int8
335 ParentRunID pgtype.Int8
336 ConcurrencyGroup string
337 NeedApproval bool
338 }
339
340 // SPDX-License-Identifier: AGPL-3.0-or-later
341 func (q *Queries) InsertWorkflowRun(ctx context.Context, db DBTX, arg InsertWorkflowRunParams) (WorkflowRun, error) {
342 row := db.QueryRow(ctx, insertWorkflowRun,
343 arg.RepoID,
344 arg.RunIndex,
345 arg.WorkflowFile,
346 arg.WorkflowName,
347 arg.HeadSha,
348 arg.HeadRef,
349 arg.Event,
350 arg.EventPayload,
351 arg.ActorUserID,
352 arg.ParentRunID,
353 arg.ConcurrencyGroup,
354 arg.NeedApproval,
355 )
356 var i WorkflowRun
357 err := row.Scan(
358 &i.ID,
359 &i.RepoID,
360 &i.RunIndex,
361 &i.WorkflowFile,
362 &i.WorkflowName,
363 &i.HeadSha,
364 &i.HeadRef,
365 &i.Event,
366 &i.EventPayload,
367 &i.ActorUserID,
368 &i.ParentRunID,
369 &i.ConcurrencyGroup,
370 &i.Status,
371 &i.Conclusion,
372 &i.Pinned,
373 &i.NeedApproval,
374 &i.ApprovedByUserID,
375 &i.StartedAt,
376 &i.CompletedAt,
377 &i.Version,
378 &i.CreatedAt,
379 &i.UpdatedAt,
380 &i.TriggerEventID,
381 )
382 return i, err
383 }
384
385 const listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
386 WITH ranked AS (
387 SELECT workflow_file,
388 workflow_name,
389 (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
390 ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
391 FROM workflow_runs
392 WHERE repo_id = $1
393 )
394 SELECT workflow_file,
395 COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
396 run_count
397 FROM ranked
398 WHERE rn = 1
399 ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
400 `
401
402 type ListWorkflowRunWorkflowsForRepoRow struct {
403 WorkflowFile string
404 WorkflowName string
405 RunCount int64
406 }
407
408 func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
409 rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
410 if err != nil {
411 return nil, err
412 }
413 defer rows.Close()
414 items := []ListWorkflowRunWorkflowsForRepoRow{}
415 for rows.Next() {
416 var i ListWorkflowRunWorkflowsForRepoRow
417 if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
418 return nil, err
419 }
420 items = append(items, i)
421 }
422 if err := rows.Err(); err != nil {
423 return nil, err
424 }
425 return items, nil
426 }
427
428 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
429 SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
430 r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
431 r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
432 r.started_at, r.completed_at, r.created_at, r.updated_at
433 FROM workflow_runs r
434 LEFT JOIN users u ON u.id = r.actor_user_id
435 WHERE r.repo_id = $1::bigint
436 AND ($2::text IS NULL OR r.workflow_file = $2::text)
437 AND ($3::text IS NULL OR r.head_ref = $3::text)
438 AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
439 AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
440 AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
441 AND ($7::text IS NULL OR u.username = $7::citext)
442 ORDER BY r.created_at DESC, r.id DESC
443 LIMIT $9 OFFSET $8
444 `
445
446 type ListWorkflowRunsForRepoParams struct {
447 RepoID int64
448 WorkflowFile pgtype.Text
449 HeadRef pgtype.Text
450 Event NullWorkflowRunEvent
451 Status NullWorkflowRunStatus
452 Conclusion NullCheckConclusion
453 ActorUsername pgtype.Text
454 PageOffset int32
455 PageLimit int32
456 }
457
458 type ListWorkflowRunsForRepoRow struct {
459 ID int64
460 RepoID int64
461 RunIndex int64
462 WorkflowFile string
463 WorkflowName string
464 HeadSha string
465 HeadRef string
466 Event WorkflowRunEvent
467 Status WorkflowRunStatus
468 Conclusion NullCheckConclusion
469 ActorUserID pgtype.Int8
470 ActorUsername string
471 StartedAt pgtype.Timestamptz
472 CompletedAt pgtype.Timestamptz
473 CreatedAt pgtype.Timestamptz
474 UpdatedAt pgtype.Timestamptz
475 }
476
477 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
478 rows, err := db.Query(ctx, listWorkflowRunsForRepo,
479 arg.RepoID,
480 arg.WorkflowFile,
481 arg.HeadRef,
482 arg.Event,
483 arg.Status,
484 arg.Conclusion,
485 arg.ActorUsername,
486 arg.PageOffset,
487 arg.PageLimit,
488 )
489 if err != nil {
490 return nil, err
491 }
492 defer rows.Close()
493 items := []ListWorkflowRunsForRepoRow{}
494 for rows.Next() {
495 var i ListWorkflowRunsForRepoRow
496 if err := rows.Scan(
497 &i.ID,
498 &i.RepoID,
499 &i.RunIndex,
500 &i.WorkflowFile,
501 &i.WorkflowName,
502 &i.HeadSha,
503 &i.HeadRef,
504 &i.Event,
505 &i.Status,
506 &i.Conclusion,
507 &i.ActorUserID,
508 &i.ActorUsername,
509 &i.StartedAt,
510 &i.CompletedAt,
511 &i.CreatedAt,
512 &i.UpdatedAt,
513 ); err != nil {
514 return nil, err
515 }
516 items = append(items, i)
517 }
518 if err := rows.Err(); err != nil {
519 return nil, err
520 }
521 return items, nil
522 }
523
524 const lookupWorkflowRunByTriggerEvent = `-- name: LookupWorkflowRunByTriggerEvent :one
525 SELECT id, repo_id, run_index, workflow_file, workflow_name,
526 head_sha, head_ref, event, event_payload,
527 actor_user_id, parent_run_id, concurrency_group,
528 status, conclusion, pinned, need_approval, approved_by_user_id,
529 started_at, completed_at, version, created_at, updated_at, trigger_event_id
530 FROM workflow_runs
531 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3
532 `
533
534 type LookupWorkflowRunByTriggerEventParams struct {
535 RepoID int64
536 WorkflowFile string
537 TriggerEventID string
538 }
539
540 // Companion to EnqueueWorkflowRun for the conflict path: when an
541 // INSERT ... ON CONFLICT DO NOTHING returns no rows, the trigger
542 // handler uses this to find the existing row so it can surface a
543 // stable RunID. Matches the partial-unique index from migration 0051.
544 func (q *Queries) LookupWorkflowRunByTriggerEvent(ctx context.Context, db DBTX, arg LookupWorkflowRunByTriggerEventParams) (WorkflowRun, error) {
545 row := db.QueryRow(ctx, lookupWorkflowRunByTriggerEvent, arg.RepoID, arg.WorkflowFile, arg.TriggerEventID)
546 var i WorkflowRun
547 err := row.Scan(
548 &i.ID,
549 &i.RepoID,
550 &i.RunIndex,
551 &i.WorkflowFile,
552 &i.WorkflowName,
553 &i.HeadSha,
554 &i.HeadRef,
555 &i.Event,
556 &i.EventPayload,
557 &i.ActorUserID,
558 &i.ParentRunID,
559 &i.ConcurrencyGroup,
560 &i.Status,
561 &i.Conclusion,
562 &i.Pinned,
563 &i.NeedApproval,
564 &i.ApprovedByUserID,
565 &i.StartedAt,
566 &i.CompletedAt,
567 &i.Version,
568 &i.CreatedAt,
569 &i.UpdatedAt,
570 &i.TriggerEventID,
571 )
572 return i, err
573 }
574
575 const markWorkflowRunRunning = `-- name: MarkWorkflowRunRunning :exec
576 UPDATE workflow_runs
577 SET status = 'running',
578 started_at = COALESCE(started_at, now()),
579 version = version + 1,
580 updated_at = now()
581 WHERE id = $1 AND status = 'queued'
582 `
583
584 func (q *Queries) MarkWorkflowRunRunning(ctx context.Context, db DBTX, id int64) error {
585 _, err := db.Exec(ctx, markWorkflowRunRunning, id)
586 return err
587 }
588
589 const nextRunIndexForRepo = `-- name: NextRunIndexForRepo :one
590 SELECT (COALESCE(MAX(run_index), 0) + 1)::bigint AS next_index
591 FROM workflow_runs
592 WHERE repo_id = $1
593 `
594
595 // Atomic next-index emitter: take the max + 1 for this repo. Pairs
596 // with the (repo_id, run_index) UNIQUE so concurrent inserts that
597 // race here will catch a unique-violation and the caller retries.
598 // Cast to bigint so sqlc generates int64 (the column type) rather
599 // than int32 (the type the +1 literal would default to).
600 func (q *Queries) NextRunIndexForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
601 row := db.QueryRow(ctx, nextRunIndexForRepo, repoID)
602 var next_index int64
603 err := row.Scan(&next_index)
604 return next_index, err
605 }
606