generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
queries.sql.go
1359 lines (1248 loc) · 35.5 KB
/
queries.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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.24.0
// source: queries.sql
package sql
import (
"context"
"encoding/json"
"time"
"github.com/TBD54566975/ftl/backend/common/model"
"github.com/alecthomas/types"
)
const associateArtefactWithDeployment = `-- name: AssociateArtefactWithDeployment :exec
INSERT INTO deployment_artefacts (deployment_id, artefact_id, executable, path)
VALUES ((SELECT id FROM deployments WHERE name = $1), $2, $3, $4)
`
type AssociateArtefactWithDeploymentParams struct {
Name model.DeploymentName
ArtefactID int64
Executable bool
Path string
}
func (q *Queries) AssociateArtefactWithDeployment(ctx context.Context, arg AssociateArtefactWithDeploymentParams) error {
_, err := q.db.Exec(ctx, associateArtefactWithDeployment,
arg.Name,
arg.ArtefactID,
arg.Executable,
arg.Path,
)
return err
}
const createArtefact = `-- name: CreateArtefact :one
INSERT INTO artefacts (digest, content)
VALUES ($1, $2)
RETURNING id
`
// Create a new artefact and return the artefact ID.
func (q *Queries) CreateArtefact(ctx context.Context, digest []byte, content []byte) (int64, error) {
row := q.db.QueryRow(ctx, createArtefact, digest, content)
var id int64
err := row.Scan(&id)
return id, err
}
const createDeployment = `-- name: CreateDeployment :exec
INSERT INTO deployments (module_id, "schema", name)
VALUES ((SELECT id FROM modules WHERE name = $2::TEXT LIMIT 1), $3::BYTEA, $1)
`
func (q *Queries) CreateDeployment(ctx context.Context, name model.DeploymentName, moduleName string, schema []byte) error {
_, err := q.db.Exec(ctx, createDeployment, name, moduleName, schema)
return err
}
const createIngressRequest = `-- name: CreateIngressRequest :exec
INSERT INTO requests (origin, name, source_addr)
VALUES ($1, $2, $3)
`
func (q *Queries) CreateIngressRequest(ctx context.Context, origin Origin, name string, sourceAddr string) error {
_, err := q.db.Exec(ctx, createIngressRequest, origin, name, sourceAddr)
return err
}
const createIngressRoute = `-- name: CreateIngressRoute :exec
INSERT INTO ingress_routes (deployment_id, module, verb, method, path)
VALUES ((SELECT id FROM deployments WHERE name = $1 LIMIT 1), $2, $3, $4, $5)
`
type CreateIngressRouteParams struct {
Name model.DeploymentName
Module string
Verb string
Method string
Path string
}
func (q *Queries) CreateIngressRoute(ctx context.Context, arg CreateIngressRouteParams) error {
_, err := q.db.Exec(ctx, createIngressRoute,
arg.Name,
arg.Module,
arg.Verb,
arg.Method,
arg.Path,
)
return err
}
const deregisterRunner = `-- name: DeregisterRunner :one
WITH matches AS (
UPDATE runners
SET state = 'dead'
WHERE key = $1
RETURNING 1)
SELECT COUNT(*)
FROM matches
`
func (q *Queries) DeregisterRunner(ctx context.Context, key Key) (int64, error) {
row := q.db.QueryRow(ctx, deregisterRunner, key)
var count int64
err := row.Scan(&count)
return count, err
}
const expireRunnerReservations = `-- name: ExpireRunnerReservations :one
WITH rows AS (
UPDATE runners
SET state = 'idle',
deployment_id = NULL,
reservation_timeout = NULL
WHERE state = 'reserved'
AND reservation_timeout < (NOW() AT TIME ZONE 'utc')
RETURNING 1)
SELECT COUNT(*)
FROM rows
`
func (q *Queries) ExpireRunnerReservations(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, expireRunnerReservations)
var count int64
err := row.Scan(&count)
return count, err
}
const getActiveDeploymentSchemas = `-- name: GetActiveDeploymentSchemas :many
SELECT name, schema FROM deployments WHERE min_replicas > 0
`
type GetActiveDeploymentSchemasRow struct {
Name model.DeploymentName
Schema []byte
}
func (q *Queries) GetActiveDeploymentSchemas(ctx context.Context) ([]GetActiveDeploymentSchemasRow, error) {
rows, err := q.db.Query(ctx, getActiveDeploymentSchemas)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetActiveDeploymentSchemasRow
for rows.Next() {
var i GetActiveDeploymentSchemasRow
if err := rows.Scan(&i.Name, &i.Schema); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getActiveDeployments = `-- name: GetActiveDeployments :many
SELECT d.id, d.created_at, d.module_id, d.name, d.schema, d.labels, d.min_replicas, m.name AS module_name, m.language
FROM deployments d
INNER JOIN modules m on d.module_id = m.id
WHERE $1::bool = true
OR min_replicas > 0
ORDER BY d.name
`
type GetActiveDeploymentsRow struct {
Deployment Deployment
ModuleName string
Language string
}
func (q *Queries) GetActiveDeployments(ctx context.Context, all bool) ([]GetActiveDeploymentsRow, error) {
rows, err := q.db.Query(ctx, getActiveDeployments, all)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetActiveDeploymentsRow
for rows.Next() {
var i GetActiveDeploymentsRow
if err := rows.Scan(
&i.Deployment.ID,
&i.Deployment.CreatedAt,
&i.Deployment.ModuleID,
&i.Deployment.Name,
&i.Deployment.Schema,
&i.Deployment.Labels,
&i.Deployment.MinReplicas,
&i.ModuleName,
&i.Language,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getActiveRunners = `-- name: GetActiveRunners :many
SELECT DISTINCT ON (r.key) r.key AS runner_key,
r.endpoint,
r.state,
r.labels,
r.last_seen,
r.module_name,
COALESCE(CASE
WHEN r.deployment_id IS NOT NULL
THEN d.name END, NULL) AS deployment_name
FROM runners r
LEFT JOIN deployments d on d.id = r.deployment_id
WHERE $1::bool = true
OR r.state <> 'dead'
ORDER BY r.key
`
type GetActiveRunnersRow struct {
RunnerKey Key
Endpoint string
State RunnerState
Labels []byte
LastSeen time.Time
ModuleName types.Option[string]
DeploymentName types.Option[string]
}
func (q *Queries) GetActiveRunners(ctx context.Context, all bool) ([]GetActiveRunnersRow, error) {
rows, err := q.db.Query(ctx, getActiveRunners, all)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetActiveRunnersRow
for rows.Next() {
var i GetActiveRunnersRow
if err := rows.Scan(
&i.RunnerKey,
&i.Endpoint,
&i.State,
&i.Labels,
&i.LastSeen,
&i.ModuleName,
&i.DeploymentName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAllIngressRoutes = `-- name: GetAllIngressRoutes :many
SELECT d.name AS deployment_name, ir.module, ir.verb, ir.method, ir.path
FROM ingress_routes ir
INNER JOIN deployments d ON ir.deployment_id = d.id
WHERE $1::bool = true
OR d.min_replicas > 0
`
type GetAllIngressRoutesRow struct {
DeploymentName model.DeploymentName
Module string
Verb string
Method string
Path string
}
func (q *Queries) GetAllIngressRoutes(ctx context.Context, all bool) ([]GetAllIngressRoutesRow, error) {
rows, err := q.db.Query(ctx, getAllIngressRoutes, all)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllIngressRoutesRow
for rows.Next() {
var i GetAllIngressRoutesRow
if err := rows.Scan(
&i.DeploymentName,
&i.Module,
&i.Verb,
&i.Method,
&i.Path,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getArtefactContentRange = `-- name: GetArtefactContentRange :one
SELECT SUBSTRING(a.content FROM $1 FOR $2)::BYTEA AS content
FROM artefacts a
WHERE a.id = $3
`
func (q *Queries) GetArtefactContentRange(ctx context.Context, start int32, count int32, iD int64) ([]byte, error) {
row := q.db.QueryRow(ctx, getArtefactContentRange, start, count, iD)
var content []byte
err := row.Scan(&content)
return content, err
}
const getArtefactDigests = `-- name: GetArtefactDigests :many
SELECT id, digest
FROM artefacts
WHERE digest = ANY ($1::bytea[])
`
type GetArtefactDigestsRow struct {
ID int64
Digest []byte
}
// Return the digests that exist in the database.
func (q *Queries) GetArtefactDigests(ctx context.Context, digests [][]byte) ([]GetArtefactDigestsRow, error) {
rows, err := q.db.Query(ctx, getArtefactDigests, digests)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetArtefactDigestsRow
for rows.Next() {
var i GetArtefactDigestsRow
if err := rows.Scan(&i.ID, &i.Digest); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getControllers = `-- name: GetControllers :many
SELECT id, key, created, last_seen, state, endpoint
FROM controller c
WHERE $1::bool = true
OR c.state <> 'dead'
ORDER BY c.key
`
func (q *Queries) GetControllers(ctx context.Context, all bool) ([]Controller, error) {
rows, err := q.db.Query(ctx, getControllers, all)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Controller
for rows.Next() {
var i Controller
if err := rows.Scan(
&i.ID,
&i.Key,
&i.Created,
&i.LastSeen,
&i.State,
&i.Endpoint,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDeployment = `-- name: GetDeployment :one
SELECT d.id, d.created_at, d.module_id, d.name, d.schema, d.labels, d.min_replicas, m.language, m.name AS module_name, d.min_replicas
FROM deployments d
INNER JOIN modules m ON m.id = d.module_id
WHERE d.name = $1
`
type GetDeploymentRow struct {
Deployment Deployment
Language string
ModuleName string
MinReplicas int32
}
func (q *Queries) GetDeployment(ctx context.Context, name model.DeploymentName) (GetDeploymentRow, error) {
row := q.db.QueryRow(ctx, getDeployment, name)
var i GetDeploymentRow
err := row.Scan(
&i.Deployment.ID,
&i.Deployment.CreatedAt,
&i.Deployment.ModuleID,
&i.Deployment.Name,
&i.Deployment.Schema,
&i.Deployment.Labels,
&i.Deployment.MinReplicas,
&i.Language,
&i.ModuleName,
&i.MinReplicas,
)
return i, err
}
const getDeploymentArtefacts = `-- name: GetDeploymentArtefacts :many
SELECT da.created_at, artefact_id AS id, executable, path, digest, executable
FROM deployment_artefacts da
INNER JOIN artefacts ON artefacts.id = da.artefact_id
WHERE deployment_id = $1
`
type GetDeploymentArtefactsRow struct {
CreatedAt time.Time
ID int64
Executable bool
Path string
Digest []byte
Executable_2 bool
}
// Get all artefacts matching the given digests.
func (q *Queries) GetDeploymentArtefacts(ctx context.Context, deploymentID int64) ([]GetDeploymentArtefactsRow, error) {
rows, err := q.db.Query(ctx, getDeploymentArtefacts, deploymentID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetDeploymentArtefactsRow
for rows.Next() {
var i GetDeploymentArtefactsRow
if err := rows.Scan(
&i.CreatedAt,
&i.ID,
&i.Executable,
&i.Path,
&i.Digest,
&i.Executable_2,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDeploymentsByID = `-- name: GetDeploymentsByID :many
SELECT id, created_at, module_id, name, schema, labels, min_replicas
FROM deployments
WHERE id = ANY ($1::BIGINT[])
`
func (q *Queries) GetDeploymentsByID(ctx context.Context, ids []int64) ([]Deployment, error) {
rows, err := q.db.Query(ctx, getDeploymentsByID, ids)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Deployment
for rows.Next() {
var i Deployment
if err := rows.Scan(
&i.ID,
&i.CreatedAt,
&i.ModuleID,
&i.Name,
&i.Schema,
&i.Labels,
&i.MinReplicas,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDeploymentsNeedingReconciliation = `-- name: GetDeploymentsNeedingReconciliation :many
SELECT d.name AS deployment_name,
m.name AS module_name,
m.language AS language,
COUNT(r.id) AS assigned_runners_count,
d.min_replicas::BIGINT AS required_runners_count
FROM deployments d
LEFT JOIN runners r ON d.id = r.deployment_id AND r.state <> 'dead'
JOIN modules m ON d.module_id = m.id
GROUP BY d.name, d.min_replicas, m.name, m.language
HAVING COUNT(r.id) <> d.min_replicas
`
type GetDeploymentsNeedingReconciliationRow struct {
DeploymentName model.DeploymentName
ModuleName string
Language string
AssignedRunnersCount int64
RequiredRunnersCount int64
}
// Get deployments that have a mismatch between the number of assigned and required replicas.
func (q *Queries) GetDeploymentsNeedingReconciliation(ctx context.Context) ([]GetDeploymentsNeedingReconciliationRow, error) {
rows, err := q.db.Query(ctx, getDeploymentsNeedingReconciliation)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetDeploymentsNeedingReconciliationRow
for rows.Next() {
var i GetDeploymentsNeedingReconciliationRow
if err := rows.Scan(
&i.DeploymentName,
&i.ModuleName,
&i.Language,
&i.AssignedRunnersCount,
&i.RequiredRunnersCount,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDeploymentsWithArtefacts = `-- name: GetDeploymentsWithArtefacts :many
SELECT d.id, d.created_at, d.name as deployment_name, m.name AS module_name
FROM deployments d
INNER JOIN modules m ON d.module_id = m.id
WHERE EXISTS (SELECT 1
FROM deployment_artefacts da
INNER JOIN artefacts a ON da.artefact_id = a.id
WHERE a.digest = ANY ($1::bytea[])
AND da.deployment_id = d.id
HAVING COUNT(*) = $2::BIGINT -- Number of unique digests provided
)
`
type GetDeploymentsWithArtefactsRow struct {
ID int64
CreatedAt time.Time
DeploymentName model.DeploymentName
ModuleName string
}
// Get all deployments that have artefacts matching the given digests.
func (q *Queries) GetDeploymentsWithArtefacts(ctx context.Context, digests [][]byte, count int64) ([]GetDeploymentsWithArtefactsRow, error) {
rows, err := q.db.Query(ctx, getDeploymentsWithArtefacts, digests, count)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetDeploymentsWithArtefactsRow
for rows.Next() {
var i GetDeploymentsWithArtefactsRow
if err := rows.Scan(
&i.ID,
&i.CreatedAt,
&i.DeploymentName,
&i.ModuleName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getExistingDeploymentForModule = `-- name: GetExistingDeploymentForModule :one
SELECT d.id, created_at, module_id, d.name, schema, labels, min_replicas, m.id, language, m.name
FROM deployments d
INNER JOIN modules m on d.module_id = m.id
WHERE m.name = $1
AND min_replicas > 0
LIMIT 1
`
type GetExistingDeploymentForModuleRow struct {
ID int64
CreatedAt time.Time
ModuleID int64
Name model.DeploymentName
Schema []byte
Labels []byte
MinReplicas int32
ID_2 int64
Language string
Name_2 string
}
func (q *Queries) GetExistingDeploymentForModule(ctx context.Context, name string) (GetExistingDeploymentForModuleRow, error) {
row := q.db.QueryRow(ctx, getExistingDeploymentForModule, name)
var i GetExistingDeploymentForModuleRow
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.ModuleID,
&i.Name,
&i.Schema,
&i.Labels,
&i.MinReplicas,
&i.ID_2,
&i.Language,
&i.Name_2,
)
return i, err
}
const getIdleRunners = `-- name: GetIdleRunners :many
SELECT id, key, created, last_seen, reservation_timeout, state, endpoint, module_name, deployment_id, labels
FROM runners
WHERE labels @> $1::jsonb
AND state = 'idle'
LIMIT $2
`
func (q *Queries) GetIdleRunners(ctx context.Context, labels []byte, limit int64) ([]Runner, error) {
rows, err := q.db.Query(ctx, getIdleRunners, labels, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Runner
for rows.Next() {
var i Runner
if err := rows.Scan(
&i.ID,
&i.Key,
&i.Created,
&i.LastSeen,
&i.ReservationTimeout,
&i.State,
&i.Endpoint,
&i.ModuleName,
&i.DeploymentID,
&i.Labels,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getIngressRoutes = `-- name: GetIngressRoutes :many
SELECT r.key AS runner_key, d.name AS deployment_name, endpoint, ir.path, ir.module, ir.verb
FROM ingress_routes ir
INNER JOIN runners r ON ir.deployment_id = r.deployment_id
INNER JOIN deployments d ON ir.deployment_id = d.id
WHERE r.state = 'assigned'
AND ir.method = $1
`
type GetIngressRoutesRow struct {
RunnerKey Key
DeploymentName model.DeploymentName
Endpoint string
Path string
Module string
Verb string
}
// Get the runner endpoints corresponding to the given ingress route.
func (q *Queries) GetIngressRoutes(ctx context.Context, method string) ([]GetIngressRoutesRow, error) {
rows, err := q.db.Query(ctx, getIngressRoutes, method)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetIngressRoutesRow
for rows.Next() {
var i GetIngressRoutesRow
if err := rows.Scan(
&i.RunnerKey,
&i.DeploymentName,
&i.Endpoint,
&i.Path,
&i.Module,
&i.Verb,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getModulesByID = `-- name: GetModulesByID :many
SELECT id, language, name
FROM modules
WHERE id = ANY ($1::BIGINT[])
`
func (q *Queries) GetModulesByID(ctx context.Context, ids []int64) ([]Module, error) {
rows, err := q.db.Query(ctx, getModulesByID, ids)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Module
for rows.Next() {
var i Module
if err := rows.Scan(&i.ID, &i.Language, &i.Name); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getProcessList = `-- name: GetProcessList :many
SELECT d.min_replicas,
d.name AS deployment_name,
d.labels deployment_labels,
r.key AS runner_key,
r.endpoint,
r.labels AS runner_labels
FROM deployments d
LEFT JOIN runners r on d.id = r.deployment_id
WHERE d.min_replicas > 0
ORDER BY d.name
`
type GetProcessListRow struct {
MinReplicas int32
DeploymentName model.DeploymentName
DeploymentLabels []byte
RunnerKey NullKey
Endpoint types.Option[string]
RunnerLabels []byte
}
func (q *Queries) GetProcessList(ctx context.Context) ([]GetProcessListRow, error) {
rows, err := q.db.Query(ctx, getProcessList)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetProcessListRow
for rows.Next() {
var i GetProcessListRow
if err := rows.Scan(
&i.MinReplicas,
&i.DeploymentName,
&i.DeploymentLabels,
&i.RunnerKey,
&i.Endpoint,
&i.RunnerLabels,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRouteForRunner = `-- name: GetRouteForRunner :one
SELECT endpoint, r.key AS runner_key, r.module_name, d.name deployment_name, r.state
FROM runners r
LEFT JOIN deployments d on r.deployment_id = d.id
WHERE r.key = $1
`
type GetRouteForRunnerRow struct {
Endpoint string
RunnerKey Key
ModuleName types.Option[string]
DeploymentName model.DeploymentName
State RunnerState
}
// Retrieve routing information for a runner.
func (q *Queries) GetRouteForRunner(ctx context.Context, key Key) (GetRouteForRunnerRow, error) {
row := q.db.QueryRow(ctx, getRouteForRunner, key)
var i GetRouteForRunnerRow
err := row.Scan(
&i.Endpoint,
&i.RunnerKey,
&i.ModuleName,
&i.DeploymentName,
&i.State,
)
return i, err
}
const getRoutingTable = `-- name: GetRoutingTable :many
SELECT endpoint, r.key AS runner_key, r.module_name, d.name deployment_name
FROM runners r
LEFT JOIN deployments d on r.deployment_id = d.id
WHERE state = 'assigned'
AND (COALESCE(cardinality($1::TEXT[]), 0) = 0
OR module_name = ANY ($1::TEXT[]))
`
type GetRoutingTableRow struct {
Endpoint string
RunnerKey Key
ModuleName types.Option[string]
DeploymentName model.DeploymentName
}
func (q *Queries) GetRoutingTable(ctx context.Context, modules []string) ([]GetRoutingTableRow, error) {
rows, err := q.db.Query(ctx, getRoutingTable, modules)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetRoutingTableRow
for rows.Next() {
var i GetRoutingTableRow
if err := rows.Scan(
&i.Endpoint,
&i.RunnerKey,
&i.ModuleName,
&i.DeploymentName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRunner = `-- name: GetRunner :one
SELECT DISTINCT ON (r.key) r.key AS runner_key,
r.endpoint,
r.state,
r.labels,
r.last_seen,
r.module_name,
COALESCE(CASE
WHEN r.deployment_id IS NOT NULL
THEN d.name END, NULL) AS deployment_name
FROM runners r
LEFT JOIN deployments d on d.id = r.deployment_id OR r.deployment_id IS NULL
WHERE r.key = $1
`
type GetRunnerRow struct {
RunnerKey Key
Endpoint string
State RunnerState
Labels []byte
LastSeen time.Time
ModuleName types.Option[string]
DeploymentName types.Option[string]
}
func (q *Queries) GetRunner(ctx context.Context, key Key) (GetRunnerRow, error) {
row := q.db.QueryRow(ctx, getRunner, key)
var i GetRunnerRow
err := row.Scan(
&i.RunnerKey,
&i.Endpoint,
&i.State,
&i.Labels,
&i.LastSeen,
&i.ModuleName,
&i.DeploymentName,
)
return i, err
}
const getRunnerState = `-- name: GetRunnerState :one
SELECT state
FROM runners
WHERE key = $1
`
func (q *Queries) GetRunnerState(ctx context.Context, key Key) (RunnerState, error) {
row := q.db.QueryRow(ctx, getRunnerState, key)
var state RunnerState
err := row.Scan(&state)
return state, err
}
const getRunnersForDeployment = `-- name: GetRunnersForDeployment :many
SELECT r.id, key, created, last_seen, reservation_timeout, state, endpoint, module_name, deployment_id, r.labels, d.id, created_at, module_id, name, schema, d.labels, min_replicas
FROM runners r
INNER JOIN deployments d on r.deployment_id = d.id
WHERE state = 'assigned'
AND d.name = $1
`
type GetRunnersForDeploymentRow struct {
ID int64
Key Key
Created time.Time
LastSeen time.Time
ReservationTimeout NullTime
State RunnerState
Endpoint string
ModuleName types.Option[string]
DeploymentID types.Option[int64]
Labels []byte
ID_2 int64
CreatedAt time.Time
ModuleID int64
Name model.DeploymentName
Schema []byte
Labels_2 []byte
MinReplicas int32
}
func (q *Queries) GetRunnersForDeployment(ctx context.Context, name model.DeploymentName) ([]GetRunnersForDeploymentRow, error) {
rows, err := q.db.Query(ctx, getRunnersForDeployment, name)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetRunnersForDeploymentRow
for rows.Next() {
var i GetRunnersForDeploymentRow
if err := rows.Scan(
&i.ID,
&i.Key,
&i.Created,
&i.LastSeen,
&i.ReservationTimeout,
&i.State,
&i.Endpoint,
&i.ModuleName,
&i.DeploymentID,
&i.Labels,
&i.ID_2,
&i.CreatedAt,
&i.ModuleID,
&i.Name,
&i.Schema,
&i.Labels_2,
&i.MinReplicas,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertCallEvent = `-- name: InsertCallEvent :exec
INSERT INTO events (deployment_id, request_id, time_stamp, type,
custom_key_1, custom_key_2, custom_key_3, custom_key_4, payload)
VALUES ((SELECT id FROM deployments WHERE deployments.name = $1::TEXT),
(CASE
WHEN $2::TEXT IS NULL THEN NULL
ELSE (SELECT id FROM requests ir WHERE ir.name = $2::TEXT)
END),
$3::TIMESTAMPTZ,
'call',
$4::TEXT,
$5::TEXT,
$6::TEXT,
$7::TEXT,
jsonb_build_object(
'duration_ms', $8::BIGINT,
'request', $9::JSONB,
'response', $10::JSONB,
'error', $11::TEXT,
'stack', $12::TEXT
))
`