-
Notifications
You must be signed in to change notification settings - Fork 126
/
workflow_runs.sql.go
932 lines (890 loc) · 26.6 KB
/
workflow_runs.sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.24.0
// source: workflow_runs.sql
package dbsqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countWorkflowRuns = `-- name: CountWorkflowRuns :one
SELECT
count(runs) OVER() AS total
FROM
"WorkflowRun" as runs
LEFT JOIN
"WorkflowRunTriggeredBy" as runTriggers ON runTriggers."parentId" = runs."id"
LEFT JOIN
"Event" as events ON runTriggers."eventId" = events."id"
LEFT JOIN
"WorkflowVersion" as workflowVersion ON runs."workflowVersionId" = workflowVersion."id"
LEFT JOIN
"Workflow" as workflow ON workflowVersion."workflowId" = workflow."id"
WHERE
runs."tenantId" = $1 AND
(
$2::uuid IS NULL OR
workflowVersion."id" = $2::uuid
) AND
(
$3::uuid IS NULL OR
workflow."id" = $3::uuid
) AND
(
$4::uuid IS NULL OR
events."id" = $4::uuid
) AND
(
$5::text IS NULL OR
runs."concurrencyGroupId" = $5::text
) AND
(
$6::"WorkflowRunStatus" IS NULL OR
runs."status" = $6::"WorkflowRunStatus"
)
`
type CountWorkflowRunsParams struct {
TenantId pgtype.UUID `json:"tenantId"`
WorkflowVersionId pgtype.UUID `json:"workflowVersionId"`
WorkflowId pgtype.UUID `json:"workflowId"`
EventId pgtype.UUID `json:"eventId"`
GroupKey pgtype.Text `json:"groupKey"`
Status NullWorkflowRunStatus `json:"status"`
}
func (q *Queries) CountWorkflowRuns(ctx context.Context, db DBTX, arg CountWorkflowRunsParams) (int64, error) {
row := db.QueryRow(ctx, countWorkflowRuns,
arg.TenantId,
arg.WorkflowVersionId,
arg.WorkflowId,
arg.EventId,
arg.GroupKey,
arg.Status,
)
var total int64
err := row.Scan(&total)
return total, err
}
const createGetGroupKeyRun = `-- name: CreateGetGroupKeyRun :one
INSERT INTO "GetGroupKeyRun" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"tenantId",
"workflowRunId",
"workerId",
"tickerId",
"status",
"input",
"output",
"requeueAfter",
"error",
"startedAt",
"finishedAt",
"timeoutAt",
"cancelledAt",
"cancelledReason",
"cancelledError"
) VALUES (
COALESCE($1::uuid, gen_random_uuid()),
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL,
$2::uuid,
$3::uuid,
NULL,
NULL,
'PENDING', -- default status
$4::jsonb,
NULL,
$5::timestamp,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
) RETURNING id, "createdAt", "updatedAt", "deletedAt", "tenantId", "workerId", "tickerId", status, input, output, "requeueAfter", error, "startedAt", "finishedAt", "timeoutAt", "cancelledAt", "cancelledReason", "cancelledError", "workflowRunId"
`
type CreateGetGroupKeyRunParams struct {
ID pgtype.UUID `json:"id"`
Tenantid pgtype.UUID `json:"tenantid"`
Workflowrunid pgtype.UUID `json:"workflowrunid"`
Input []byte `json:"input"`
Requeueafter pgtype.Timestamp `json:"requeueafter"`
}
func (q *Queries) CreateGetGroupKeyRun(ctx context.Context, db DBTX, arg CreateGetGroupKeyRunParams) (*GetGroupKeyRun, error) {
row := db.QueryRow(ctx, createGetGroupKeyRun,
arg.ID,
arg.Tenantid,
arg.Workflowrunid,
arg.Input,
arg.Requeueafter,
)
var i GetGroupKeyRun
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.WorkerId,
&i.TickerId,
&i.Status,
&i.Input,
&i.Output,
&i.RequeueAfter,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.TimeoutAt,
&i.CancelledAt,
&i.CancelledReason,
&i.CancelledError,
&i.WorkflowRunId,
)
return &i, err
}
const createJobRun = `-- name: CreateJobRun :one
INSERT INTO "JobRun" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"tenantId",
"workflowRunId",
"jobId",
"tickerId",
"status",
"result",
"startedAt",
"finishedAt",
"timeoutAt",
"cancelledAt",
"cancelledReason",
"cancelledError"
) VALUES (
COALESCE($1::uuid, gen_random_uuid()),
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL,
$2::uuid,
$3::uuid,
$4::uuid,
NULL,
'PENDING', -- default status
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
) RETURNING id, "createdAt", "updatedAt", "deletedAt", "tenantId", "jobId", "tickerId", status, result, "startedAt", "finishedAt", "timeoutAt", "cancelledAt", "cancelledReason", "cancelledError", "workflowRunId"
`
type CreateJobRunParams struct {
ID pgtype.UUID `json:"id"`
Tenantid pgtype.UUID `json:"tenantid"`
Workflowrunid pgtype.UUID `json:"workflowrunid"`
Jobid pgtype.UUID `json:"jobid"`
}
func (q *Queries) CreateJobRun(ctx context.Context, db DBTX, arg CreateJobRunParams) (*JobRun, error) {
row := db.QueryRow(ctx, createJobRun,
arg.ID,
arg.Tenantid,
arg.Workflowrunid,
arg.Jobid,
)
var i JobRun
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.JobId,
&i.TickerId,
&i.Status,
&i.Result,
&i.StartedAt,
&i.FinishedAt,
&i.TimeoutAt,
&i.CancelledAt,
&i.CancelledReason,
&i.CancelledError,
&i.WorkflowRunId,
)
return &i, err
}
const createJobRunLookupData = `-- name: CreateJobRunLookupData :one
INSERT INTO "JobRunLookupData" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"jobRunId",
"tenantId",
"data"
) VALUES (
COALESCE($1::uuid, gen_random_uuid()),
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL,
$2::uuid,
$3::uuid,
jsonb_build_object(
'input', COALESCE($4::jsonb, '{}'::jsonb),
'triggered_by', $5::text,
'steps', '{}'::jsonb
)
) RETURNING id, "createdAt", "updatedAt", "deletedAt", "jobRunId", "tenantId", data
`
type CreateJobRunLookupDataParams struct {
ID pgtype.UUID `json:"id"`
Jobrunid pgtype.UUID `json:"jobrunid"`
Tenantid pgtype.UUID `json:"tenantid"`
Input []byte `json:"input"`
Triggeredby string `json:"triggeredby"`
}
func (q *Queries) CreateJobRunLookupData(ctx context.Context, db DBTX, arg CreateJobRunLookupDataParams) (*JobRunLookupData, error) {
row := db.QueryRow(ctx, createJobRunLookupData,
arg.ID,
arg.Jobrunid,
arg.Tenantid,
arg.Input,
arg.Triggeredby,
)
var i JobRunLookupData
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.JobRunId,
&i.TenantId,
&i.Data,
)
return &i, err
}
const createStepRun = `-- name: CreateStepRun :one
INSERT INTO "StepRun" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"tenantId",
"jobRunId",
"stepId",
"workerId",
"tickerId",
"status",
"input",
"output",
"requeueAfter",
"scheduleTimeoutAt",
"error",
"startedAt",
"finishedAt",
"timeoutAt",
"cancelledAt",
"cancelledReason",
"cancelledError",
"callerFiles"
) VALUES (
COALESCE($1::uuid, gen_random_uuid()),
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL,
$2::uuid,
$3::uuid,
$4::uuid,
NULL,
NULL,
'PENDING', -- default status
NULL,
NULL,
$5::timestamp,
$6::timestamp,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
'{}'
) RETURNING id, "createdAt", "updatedAt", "deletedAt", "tenantId", "jobRunId", "stepId", "order", "workerId", "tickerId", status, input, output, "requeueAfter", "scheduleTimeoutAt", error, "startedAt", "finishedAt", "timeoutAt", "cancelledAt", "cancelledReason", "cancelledError", "inputSchema", "callerFiles", "gitRepoBranch", "retryCount"
`
type CreateStepRunParams struct {
ID pgtype.UUID `json:"id"`
Tenantid pgtype.UUID `json:"tenantid"`
Jobrunid pgtype.UUID `json:"jobrunid"`
Stepid pgtype.UUID `json:"stepid"`
Requeueafter pgtype.Timestamp `json:"requeueafter"`
Scheduletimeoutat pgtype.Timestamp `json:"scheduletimeoutat"`
}
func (q *Queries) CreateStepRun(ctx context.Context, db DBTX, arg CreateStepRunParams) (*StepRun, error) {
row := db.QueryRow(ctx, createStepRun,
arg.ID,
arg.Tenantid,
arg.Jobrunid,
arg.Stepid,
arg.Requeueafter,
arg.Scheduletimeoutat,
)
var i StepRun
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.JobRunId,
&i.StepId,
&i.Order,
&i.WorkerId,
&i.TickerId,
&i.Status,
&i.Input,
&i.Output,
&i.RequeueAfter,
&i.ScheduleTimeoutAt,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.TimeoutAt,
&i.CancelledAt,
&i.CancelledReason,
&i.CancelledError,
&i.InputSchema,
&i.CallerFiles,
&i.GitRepoBranch,
&i.RetryCount,
)
return &i, err
}
const createWorkflowRun = `-- name: CreateWorkflowRun :one
INSERT INTO "WorkflowRun" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"displayName",
"tenantId",
"workflowVersionId",
"status",
"error",
"startedAt",
"finishedAt"
) VALUES (
COALESCE($1::uuid, gen_random_uuid()),
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL, -- assuming deletedAt is not set on creation
$2::text,
$3::uuid,
$4::uuid,
'PENDING', -- default status
NULL, -- assuming error is not set on creation
NULL, -- assuming startedAt is not set on creation
NULL -- assuming finishedAt is not set on creation
) RETURNING "createdAt", "updatedAt", "deletedAt", "tenantId", "workflowVersionId", status, error, "startedAt", "finishedAt", "concurrencyGroupId", "displayName", id, "gitRepoBranch"
`
type CreateWorkflowRunParams struct {
ID pgtype.UUID `json:"id"`
DisplayName pgtype.Text `json:"displayName"`
Tenantid pgtype.UUID `json:"tenantid"`
Workflowversionid pgtype.UUID `json:"workflowversionid"`
}
func (q *Queries) CreateWorkflowRun(ctx context.Context, db DBTX, arg CreateWorkflowRunParams) (*WorkflowRun, error) {
row := db.QueryRow(ctx, createWorkflowRun,
arg.ID,
arg.DisplayName,
arg.Tenantid,
arg.Workflowversionid,
)
var i WorkflowRun
err := row.Scan(
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.WorkflowVersionId,
&i.Status,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.ConcurrencyGroupId,
&i.DisplayName,
&i.ID,
&i.GitRepoBranch,
)
return &i, err
}
const createWorkflowRunTriggeredBy = `-- name: CreateWorkflowRunTriggeredBy :one
INSERT INTO "WorkflowRunTriggeredBy" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"tenantId",
"parentId",
"eventId",
"cronParentId",
"cronSchedule",
"scheduledId"
) VALUES (
gen_random_uuid(), -- Generates a new UUID for id
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
NULL, -- assuming deletedAt is not set on creation
$1::uuid,
$2::uuid, -- assuming parentId is the workflowRunId
$3::uuid, -- NULL if not provided
$4::uuid, -- NULL if not provided
$5::text, -- NULL if not provided
$6::uuid -- NULL if not provided
) RETURNING id, "createdAt", "updatedAt", "deletedAt", "tenantId", "eventId", "cronParentId", "cronSchedule", "scheduledId", input, "parentId"
`
type CreateWorkflowRunTriggeredByParams struct {
Tenantid pgtype.UUID `json:"tenantid"`
Workflowrunid pgtype.UUID `json:"workflowrunid"`
EventId pgtype.UUID `json:"eventId"`
CronParentId pgtype.UUID `json:"cronParentId"`
Cron pgtype.Text `json:"cron"`
ScheduledId pgtype.UUID `json:"scheduledId"`
}
func (q *Queries) CreateWorkflowRunTriggeredBy(ctx context.Context, db DBTX, arg CreateWorkflowRunTriggeredByParams) (*WorkflowRunTriggeredBy, error) {
row := db.QueryRow(ctx, createWorkflowRunTriggeredBy,
arg.Tenantid,
arg.Workflowrunid,
arg.EventId,
arg.CronParentId,
arg.Cron,
arg.ScheduledId,
)
var i WorkflowRunTriggeredBy
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.EventId,
&i.CronParentId,
&i.CronSchedule,
&i.ScheduledId,
&i.Input,
&i.ParentId,
)
return &i, err
}
const linkStepRunParents = `-- name: LinkStepRunParents :exec
INSERT INTO "_StepRunOrder" ("A", "B")
SELECT
parent_run."id" AS "A",
child_run."id" AS "B"
FROM
"_StepOrder" AS step_order
JOIN
"StepRun" AS parent_run ON parent_run."stepId" = step_order."A" AND parent_run."jobRunId" = $1::uuid
JOIN
"StepRun" AS child_run ON child_run."stepId" = step_order."B" AND child_run."jobRunId" = $1::uuid
`
func (q *Queries) LinkStepRunParents(ctx context.Context, db DBTX, jobrunid pgtype.UUID) error {
_, err := db.Exec(ctx, linkStepRunParents, jobrunid)
return err
}
const listStartableStepRuns = `-- name: ListStartableStepRuns :many
SELECT
child_run.id, child_run."createdAt", child_run."updatedAt", child_run."deletedAt", child_run."tenantId", child_run."jobRunId", child_run."stepId", child_run."order", child_run."workerId", child_run."tickerId", child_run.status, child_run.input, child_run.output, child_run."requeueAfter", child_run."scheduleTimeoutAt", child_run.error, child_run."startedAt", child_run."finishedAt", child_run."timeoutAt", child_run."cancelledAt", child_run."cancelledReason", child_run."cancelledError", child_run."inputSchema", child_run."callerFiles", child_run."gitRepoBranch", child_run."retryCount"
FROM
"StepRun" AS child_run
JOIN
"_StepRunOrder" AS step_run_order ON step_run_order."B" = child_run."id"
WHERE
child_run."tenantId" = $1::uuid
AND child_run."jobRunId" = $2::uuid
AND child_run."status" = 'PENDING'
AND step_run_order."A" = $3::uuid
AND NOT EXISTS (
SELECT 1
FROM "_StepRunOrder" AS parent_order
JOIN "StepRun" AS parent_run ON parent_order."A" = parent_run."id"
WHERE
parent_order."B" = child_run."id"
AND parent_run."status" != 'SUCCEEDED'
)
`
type ListStartableStepRunsParams struct {
Tenantid pgtype.UUID `json:"tenantid"`
Jobrunid pgtype.UUID `json:"jobrunid"`
Parentsteprunid pgtype.UUID `json:"parentsteprunid"`
}
func (q *Queries) ListStartableStepRuns(ctx context.Context, db DBTX, arg ListStartableStepRunsParams) ([]*StepRun, error) {
rows, err := db.Query(ctx, listStartableStepRuns, arg.Tenantid, arg.Jobrunid, arg.Parentsteprunid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*StepRun
for rows.Next() {
var i StepRun
if err := rows.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.JobRunId,
&i.StepId,
&i.Order,
&i.WorkerId,
&i.TickerId,
&i.Status,
&i.Input,
&i.Output,
&i.RequeueAfter,
&i.ScheduleTimeoutAt,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.TimeoutAt,
&i.CancelledAt,
&i.CancelledReason,
&i.CancelledError,
&i.InputSchema,
&i.CallerFiles,
&i.GitRepoBranch,
&i.RetryCount,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listWorkflowRuns = `-- name: ListWorkflowRuns :many
SELECT
runs."createdAt", runs."updatedAt", runs."deletedAt", runs."tenantId", runs."workflowVersionId", runs.status, runs.error, runs."startedAt", runs."finishedAt", runs."concurrencyGroupId", runs."displayName", runs.id, runs."gitRepoBranch",
workflow.id, workflow."createdAt", workflow."updatedAt", workflow."deletedAt", workflow."tenantId", workflow.name, workflow.description,
runtriggers.id, runtriggers."createdAt", runtriggers."updatedAt", runtriggers."deletedAt", runtriggers."tenantId", runtriggers."eventId", runtriggers."cronParentId", runtriggers."cronSchedule", runtriggers."scheduledId", runtriggers.input, runtriggers."parentId",
workflowversion.id, workflowversion."createdAt", workflowversion."updatedAt", workflowversion."deletedAt", workflowversion.version, workflowversion."order", workflowversion."workflowId", workflowversion.checksum,
-- waiting on https://github.com/sqlc-dev/sqlc/pull/2858 for nullable events field
events.id, events.key, events."createdAt", events."updatedAt"
FROM
"WorkflowRun" as runs
LEFT JOIN
"WorkflowRunTriggeredBy" as runTriggers ON runTriggers."parentId" = runs."id"
LEFT JOIN
"Event" as events ON runTriggers."eventId" = events."id"
LEFT JOIN
"WorkflowVersion" as workflowVersion ON runs."workflowVersionId" = workflowVersion."id"
LEFT JOIN
"Workflow" as workflow ON workflowVersion."workflowId" = workflow."id"
WHERE
runs."tenantId" = $1 AND
(
$2::uuid IS NULL OR
workflowVersion."id" = $2::uuid
) AND
(
$3::uuid IS NULL OR
workflow."id" = $3::uuid
) AND
(
$4::uuid IS NULL OR
events."id" = $4::uuid
) AND
(
$5::text IS NULL OR
runs."concurrencyGroupId" = $5::text
) AND
(
$6::"WorkflowRunStatus" IS NULL OR
runs."status" = $6::"WorkflowRunStatus"
)
ORDER BY
case when $7 = 'createdAt ASC' THEN runs."createdAt" END ASC ,
case when $7 = 'createdAt DESC' then runs."createdAt" END DESC
OFFSET
COALESCE($8, 0)
LIMIT
COALESCE($9, 50)
`
type ListWorkflowRunsParams struct {
TenantId pgtype.UUID `json:"tenantId"`
WorkflowVersionId pgtype.UUID `json:"workflowVersionId"`
WorkflowId pgtype.UUID `json:"workflowId"`
EventId pgtype.UUID `json:"eventId"`
GroupKey pgtype.Text `json:"groupKey"`
Status NullWorkflowRunStatus `json:"status"`
Orderby interface{} `json:"orderby"`
Offset interface{} `json:"offset"`
Limit interface{} `json:"limit"`
}
type ListWorkflowRunsRow struct {
WorkflowRun WorkflowRun `json:"workflow_run"`
Workflow Workflow `json:"workflow"`
WorkflowRunTriggeredBy WorkflowRunTriggeredBy `json:"workflow_run_triggered_by"`
WorkflowVersion WorkflowVersion `json:"workflow_version"`
ID pgtype.UUID `json:"id"`
Key pgtype.Text `json:"key"`
CreatedAt pgtype.Timestamp `json:"createdAt"`
UpdatedAt pgtype.Timestamp `json:"updatedAt"`
}
func (q *Queries) ListWorkflowRuns(ctx context.Context, db DBTX, arg ListWorkflowRunsParams) ([]*ListWorkflowRunsRow, error) {
rows, err := db.Query(ctx, listWorkflowRuns,
arg.TenantId,
arg.WorkflowVersionId,
arg.WorkflowId,
arg.EventId,
arg.GroupKey,
arg.Status,
arg.Orderby,
arg.Offset,
arg.Limit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*ListWorkflowRunsRow
for rows.Next() {
var i ListWorkflowRunsRow
if err := rows.Scan(
&i.WorkflowRun.CreatedAt,
&i.WorkflowRun.UpdatedAt,
&i.WorkflowRun.DeletedAt,
&i.WorkflowRun.TenantId,
&i.WorkflowRun.WorkflowVersionId,
&i.WorkflowRun.Status,
&i.WorkflowRun.Error,
&i.WorkflowRun.StartedAt,
&i.WorkflowRun.FinishedAt,
&i.WorkflowRun.ConcurrencyGroupId,
&i.WorkflowRun.DisplayName,
&i.WorkflowRun.ID,
&i.WorkflowRun.GitRepoBranch,
&i.Workflow.ID,
&i.Workflow.CreatedAt,
&i.Workflow.UpdatedAt,
&i.Workflow.DeletedAt,
&i.Workflow.TenantId,
&i.Workflow.Name,
&i.Workflow.Description,
&i.WorkflowRunTriggeredBy.ID,
&i.WorkflowRunTriggeredBy.CreatedAt,
&i.WorkflowRunTriggeredBy.UpdatedAt,
&i.WorkflowRunTriggeredBy.DeletedAt,
&i.WorkflowRunTriggeredBy.TenantId,
&i.WorkflowRunTriggeredBy.EventId,
&i.WorkflowRunTriggeredBy.CronParentId,
&i.WorkflowRunTriggeredBy.CronSchedule,
&i.WorkflowRunTriggeredBy.ScheduledId,
&i.WorkflowRunTriggeredBy.Input,
&i.WorkflowRunTriggeredBy.ParentId,
&i.WorkflowVersion.ID,
&i.WorkflowVersion.CreatedAt,
&i.WorkflowVersion.UpdatedAt,
&i.WorkflowVersion.DeletedAt,
&i.WorkflowVersion.Version,
&i.WorkflowVersion.Order,
&i.WorkflowVersion.WorkflowId,
&i.WorkflowVersion.Checksum,
&i.ID,
&i.Key,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const resolveWorkflowRunStatus = `-- name: ResolveWorkflowRunStatus :one
WITH jobRuns AS (
SELECT sum(case when runs."status" = 'PENDING' then 1 else 0 end) AS pendingRuns,
sum(case when runs."status" = 'RUNNING' then 1 else 0 end) AS runningRuns,
sum(case when runs."status" = 'SUCCEEDED' then 1 else 0 end) AS succeededRuns,
sum(case when runs."status" = 'FAILED' then 1 else 0 end) AS failedRuns,
sum(case when runs."status" = 'CANCELLED' then 1 else 0 end) AS cancelledRuns
FROM "JobRun" as runs
WHERE
"workflowRunId" = (
SELECT "workflowRunId"
FROM "JobRun"
WHERE "id" = $1::uuid
) AND
"tenantId" = $2::uuid
)
UPDATE "WorkflowRun"
SET "status" = CASE
-- Final states are final, cannot be updated
WHEN "status" IN ('SUCCEEDED', 'FAILED') THEN "status"
-- We check for running first, because if a job run is running, then the workflow is running
WHEN j.runningRuns > 0 THEN 'RUNNING'
-- When at least one job run has failed or been cancelled, then the workflow is failed
WHEN j.failedRuns > 0 OR j.cancelledRuns > 0 THEN 'FAILED'
-- When all job runs have succeeded, then the workflow is succeeded
WHEN j.succeededRuns > 0 AND j.pendingRuns = 0 AND j.runningRuns = 0 AND j.failedRuns = 0 AND j.cancelledRuns = 0 THEN 'SUCCEEDED'
ELSE "status"
END, "finishedAt" = CASE
-- Final states are final, cannot be updated
WHEN "finishedAt" IS NOT NULL THEN "finishedAt"
-- We check for running first, because if a job run is running, then the workflow is not finished
WHEN j.runningRuns > 0 THEN NULL
-- When one job run has failed or been cancelled, then the workflow is failed
WHEN j.failedRuns > 0 OR j.cancelledRuns > 0 OR j.succeededRuns > 0 THEN NOW()
ELSE "finishedAt"
END, "startedAt" = CASE
-- Started at is final, cannot be changed
WHEN "startedAt" IS NOT NULL THEN "startedAt"
-- If a job is running or in a final state, then the workflow has started
WHEN j.runningRuns > 0 OR j.succeededRuns > 0 OR j.failedRuns > 0 OR j.cancelledRuns > 0 THEN NOW()
ELSE "startedAt"
END
FROM
jobRuns j
WHERE "id" = (
SELECT "workflowRunId"
FROM "JobRun"
WHERE "id" = $1::uuid
) AND "tenantId" = $2::uuid
RETURNING "WorkflowRun"."createdAt", "WorkflowRun"."updatedAt", "WorkflowRun"."deletedAt", "WorkflowRun"."tenantId", "WorkflowRun"."workflowVersionId", "WorkflowRun".status, "WorkflowRun".error, "WorkflowRun"."startedAt", "WorkflowRun"."finishedAt", "WorkflowRun"."concurrencyGroupId", "WorkflowRun"."displayName", "WorkflowRun".id, "WorkflowRun"."gitRepoBranch"
`
type ResolveWorkflowRunStatusParams struct {
Jobrunid pgtype.UUID `json:"jobrunid"`
Tenantid pgtype.UUID `json:"tenantid"`
}
func (q *Queries) ResolveWorkflowRunStatus(ctx context.Context, db DBTX, arg ResolveWorkflowRunStatusParams) (*WorkflowRun, error) {
row := db.QueryRow(ctx, resolveWorkflowRunStatus, arg.Jobrunid, arg.Tenantid)
var i WorkflowRun
err := row.Scan(
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.WorkflowVersionId,
&i.Status,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.ConcurrencyGroupId,
&i.DisplayName,
&i.ID,
&i.GitRepoBranch,
)
return &i, err
}
const updateWorkflowRun = `-- name: UpdateWorkflowRun :one
UPDATE
"WorkflowRun"
SET
"status" = COALESCE($1::"WorkflowRunStatus", "status"),
"error" = COALESCE($2::text, "error"),
"startedAt" = COALESCE($3::timestamp, "startedAt"),
"finishedAt" = COALESCE($4::timestamp, "finishedAt")
WHERE
"id" = $5::uuid AND
"tenantId" = $6::uuid
RETURNING "WorkflowRun"."createdAt", "WorkflowRun"."updatedAt", "WorkflowRun"."deletedAt", "WorkflowRun"."tenantId", "WorkflowRun"."workflowVersionId", "WorkflowRun".status, "WorkflowRun".error, "WorkflowRun"."startedAt", "WorkflowRun"."finishedAt", "WorkflowRun"."concurrencyGroupId", "WorkflowRun"."displayName", "WorkflowRun".id, "WorkflowRun"."gitRepoBranch"
`
type UpdateWorkflowRunParams struct {
Status NullWorkflowRunStatus `json:"status"`
Error pgtype.Text `json:"error"`
StartedAt pgtype.Timestamp `json:"startedAt"`
FinishedAt pgtype.Timestamp `json:"finishedAt"`
ID pgtype.UUID `json:"id"`
Tenantid pgtype.UUID `json:"tenantid"`
}
func (q *Queries) UpdateWorkflowRun(ctx context.Context, db DBTX, arg UpdateWorkflowRunParams) (*WorkflowRun, error) {
row := db.QueryRow(ctx, updateWorkflowRun,
arg.Status,
arg.Error,
arg.StartedAt,
arg.FinishedAt,
arg.ID,
arg.Tenantid,
)
var i WorkflowRun
err := row.Scan(
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.WorkflowVersionId,
&i.Status,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.ConcurrencyGroupId,
&i.DisplayName,
&i.ID,
&i.GitRepoBranch,
)
return &i, err
}
const updateWorkflowRunGroupKey = `-- name: UpdateWorkflowRunGroupKey :one
WITH groupKeyRun AS (
SELECT "id", "status" as groupKeyRunStatus, "output", "workflowRunId"
FROM "GetGroupKeyRun" as groupKeyRun
WHERE
"id" = $2::uuid AND
"tenantId" = $1::uuid
)
UPDATE "WorkflowRun" workflowRun
SET "status" = CASE
-- Final states are final, cannot be updated. We also can't move out of a queued state
WHEN "status" IN ('SUCCEEDED', 'FAILED', 'QUEUED') THEN "status"
-- When the GetGroupKeyRun failed or been cancelled, then the workflow is failed
WHEN groupKeyRun.groupKeyRunStatus IN ('FAILED', 'CANCELLED') THEN 'FAILED'
WHEN groupKeyRun.output IS NOT NULL THEN 'QUEUED'
ELSE "status"
END, "finishedAt" = CASE
-- Final states are final, cannot be updated
WHEN "finishedAt" IS NOT NULL THEN "finishedAt"
-- When one job run has failed or been cancelled, then the workflow is failed
WHEN groupKeyRun.groupKeyRunStatus IN ('FAILED', 'CANCELLED') THEN NOW()
ELSE "finishedAt"
END,
"concurrencyGroupId" = groupKeyRun."output"
FROM
groupKeyRun
WHERE
workflowRun."id" = groupKeyRun."workflowRunId" AND
workflowRun."tenantId" = $1::uuid
RETURNING workflowrun."createdAt", workflowrun."updatedAt", workflowrun."deletedAt", workflowrun."tenantId", workflowrun."workflowVersionId", workflowrun.status, workflowrun.error, workflowrun."startedAt", workflowrun."finishedAt", workflowrun."concurrencyGroupId", workflowrun."displayName", workflowrun.id, workflowrun."gitRepoBranch"
`
type UpdateWorkflowRunGroupKeyParams struct {
Tenantid pgtype.UUID `json:"tenantid"`
Groupkeyrunid pgtype.UUID `json:"groupkeyrunid"`
}
func (q *Queries) UpdateWorkflowRunGroupKey(ctx context.Context, db DBTX, arg UpdateWorkflowRunGroupKeyParams) (*WorkflowRun, error) {
row := db.QueryRow(ctx, updateWorkflowRunGroupKey, arg.Tenantid, arg.Groupkeyrunid)
var i WorkflowRun
err := row.Scan(
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.TenantId,
&i.WorkflowVersionId,
&i.Status,
&i.Error,
&i.StartedAt,
&i.FinishedAt,
&i.ConcurrencyGroupId,
&i.DisplayName,
&i.ID,
&i.GitRepoBranch,
)
return &i, err
}