generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
queries.sql.go
2412 lines (2215 loc) · 62.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.26.0
// source: queries.sql
package sql
import (
"context"
"encoding/json"
"time"
"github.com/TBD54566975/ftl/backend/controller/leases"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/model"
"github.com/alecthomas/types/optional"
"github.com/google/uuid"
)
const acquireAsyncCall = `-- name: AcquireAsyncCall :one
WITH async_call AS (
SELECT id
FROM async_calls
WHERE state = 'pending' AND scheduled_at <= (NOW() AT TIME ZONE 'utc')
LIMIT 1
FOR UPDATE SKIP LOCKED
), lease AS (
INSERT INTO leases (idempotency_key, key, expires_at)
VALUES (gen_random_uuid(), '/system/async_call/' || (SELECT id FROM async_call), (NOW() AT TIME ZONE 'utc') + $1::interval)
RETURNING id, idempotency_key, key, created_at, expires_at, metadata
)
UPDATE async_calls
SET state = 'executing', lease_id = (SELECT id FROM lease)
WHERE id = (SELECT id FROM async_call)
RETURNING
id AS async_call_id,
(SELECT idempotency_key FROM lease) AS lease_idempotency_key,
(SELECT key FROM lease) AS lease_key,
origin,
verb,
request,
scheduled_at,
remaining_attempts,
backoff,
max_backoff
`
type AcquireAsyncCallRow struct {
AsyncCallID int64
LeaseIdempotencyKey uuid.UUID
LeaseKey leases.Key
Origin string
Verb schema.RefKey
Request []byte
ScheduledAt time.Time
RemainingAttempts int32
Backoff time.Duration
MaxBackoff time.Duration
}
// Reserve a pending async call for execution, returning the associated lease
// reservation key.
func (q *Queries) AcquireAsyncCall(ctx context.Context, ttl time.Duration) (AcquireAsyncCallRow, error) {
row := q.db.QueryRow(ctx, acquireAsyncCall, ttl)
var i AcquireAsyncCallRow
err := row.Scan(
&i.AsyncCallID,
&i.LeaseIdempotencyKey,
&i.LeaseKey,
&i.Origin,
&i.Verb,
&i.Request,
&i.ScheduledAt,
&i.RemainingAttempts,
&i.Backoff,
&i.MaxBackoff,
)
return i, err
}
const associateArtefactWithDeployment = `-- name: AssociateArtefactWithDeployment :exec
INSERT INTO deployment_artefacts (deployment_id, artefact_id, executable, path)
VALUES ((SELECT id FROM deployments WHERE key = $1::deployment_key), $2, $3, $4)
`
type AssociateArtefactWithDeploymentParams struct {
Key model.DeploymentKey
ArtefactID int64
Executable bool
Path string
}
func (q *Queries) AssociateArtefactWithDeployment(ctx context.Context, arg AssociateArtefactWithDeploymentParams) error {
_, err := q.db.Exec(ctx, associateArtefactWithDeployment,
arg.Key,
arg.ArtefactID,
arg.Executable,
arg.Path,
)
return err
}
const beginConsumingTopicEvent = `-- name: BeginConsumingTopicEvent :exec
WITH event AS (
SELECT id, created_at, key, topic_id, payload
FROM topic_events
WHERE "key" = $2::topic_event_key
)
UPDATE topic_subscriptions
SET state = 'executing',
cursor = (SELECT id FROM event)
WHERE key = $1::subscription_key
`
func (q *Queries) BeginConsumingTopicEvent(ctx context.Context, subscription model.SubscriptionKey, event model.TopicEventKey) error {
_, err := q.db.Exec(ctx, beginConsumingTopicEvent, subscription, event)
return err
}
const completeEventForSubscription = `-- name: CompleteEventForSubscription :exec
WITH module AS (
SELECT id
FROM modules
WHERE name = $2::TEXT
)
UPDATE topic_subscriptions
SET state = 'idle'
WHERE name = $1::TEXT
AND module_id = (SELECT id FROM module)
`
func (q *Queries) CompleteEventForSubscription(ctx context.Context, name string, module string) error {
_, err := q.db.Exec(ctx, completeEventForSubscription, name, module)
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 createAsyncCall = `-- name: CreateAsyncCall :one
INSERT INTO async_calls (verb, origin, request, remaining_attempts, backoff, max_backoff)
VALUES ($1, $2, $3, $4, $5::interval, $6::interval)
RETURNING id
`
type CreateAsyncCallParams struct {
Verb schema.RefKey
Origin string
Request []byte
RemainingAttempts int32
Backoff time.Duration
MaxBackoff time.Duration
}
func (q *Queries) CreateAsyncCall(ctx context.Context, arg CreateAsyncCallParams) (int64, error) {
row := q.db.QueryRow(ctx, createAsyncCall,
arg.Verb,
arg.Origin,
arg.Request,
arg.RemainingAttempts,
arg.Backoff,
arg.MaxBackoff,
)
var id int64
err := row.Scan(&id)
return id, err
}
const createCronJob = `-- name: CreateCronJob :exec
INSERT INTO cron_jobs (key, deployment_id, module_name, verb, schedule, start_time, next_execution)
VALUES (
$1::cron_job_key,
(SELECT id FROM deployments WHERE key = $2::deployment_key LIMIT 1),
$3::TEXT,
$4::TEXT,
$5::TEXT,
$6::TIMESTAMPTZ,
$7::TIMESTAMPTZ)
`
type CreateCronJobParams struct {
Key model.CronJobKey
DeploymentKey model.DeploymentKey
ModuleName string
Verb string
Schedule string
StartTime time.Time
NextExecution time.Time
}
func (q *Queries) CreateCronJob(ctx context.Context, arg CreateCronJobParams) error {
_, err := q.db.Exec(ctx, createCronJob,
arg.Key,
arg.DeploymentKey,
arg.ModuleName,
arg.Verb,
arg.Schedule,
arg.StartTime,
arg.NextExecution,
)
return err
}
const createDeployment = `-- name: CreateDeployment :exec
INSERT INTO deployments (module_id, "schema", "key")
VALUES ((SELECT id FROM modules WHERE name = $1::TEXT LIMIT 1), $2::BYTEA, $3::deployment_key)
`
func (q *Queries) CreateDeployment(ctx context.Context, moduleName string, schema []byte, key model.DeploymentKey) error {
_, err := q.db.Exec(ctx, createDeployment, moduleName, schema, key)
return err
}
const createIngressRoute = `-- name: CreateIngressRoute :exec
INSERT INTO ingress_routes (deployment_id, module, verb, method, path)
VALUES ((SELECT id FROM deployments WHERE key = $1::deployment_key LIMIT 1), $2, $3, $4, $5)
`
type CreateIngressRouteParams struct {
Key model.DeploymentKey
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.Key,
arg.Module,
arg.Verb,
arg.Method,
arg.Path,
)
return err
}
const createRequest = `-- name: CreateRequest :exec
INSERT INTO requests (origin, "key", source_addr)
VALUES ($1, $2, $3)
`
func (q *Queries) CreateRequest(ctx context.Context, origin Origin, key model.RequestKey, sourceAddr string) error {
_, err := q.db.Exec(ctx, createRequest, origin, key, sourceAddr)
return err
}
const deleteSubscribers = `-- name: DeleteSubscribers :exec
DELETE FROM topic_subscribers
WHERE deployment_id IN (
SELECT deployments.id
FROM deployments
WHERE deployments.key = $1::deployment_key
)
`
func (q *Queries) DeleteSubscribers(ctx context.Context, deployment model.DeploymentKey) error {
_, err := q.db.Exec(ctx, deleteSubscribers, deployment)
return err
}
const deleteSubscriptions = `-- name: DeleteSubscriptions :exec
DELETE FROM topic_subscriptions
WHERE deployment_id IN (
SELECT deployments.id
FROM deployments
WHERE deployments.key = $1::deployment_key
)
`
func (q *Queries) DeleteSubscriptions(ctx context.Context, deployment model.DeploymentKey) error {
_, err := q.db.Exec(ctx, deleteSubscriptions, deployment)
return err
}
const deregisterRunner = `-- name: DeregisterRunner :one
WITH matches AS (
UPDATE runners
SET state = 'dead',
deployment_id = NULL
WHERE key = $1::runner_key
RETURNING 1)
SELECT COUNT(*)
FROM matches
`
func (q *Queries) DeregisterRunner(ctx context.Context, key model.RunnerKey) (int64, error) {
row := q.db.QueryRow(ctx, deregisterRunner, key)
var count int64
err := row.Scan(&count)
return count, err
}
const endCronJob = `-- name: EndCronJob :one
WITH j AS (
UPDATE cron_jobs
SET state = 'idle',
next_execution = $1::TIMESTAMPTZ
WHERE key = $2::cron_job_key
AND state = 'executing'
AND start_time = $3::TIMESTAMPTZ
RETURNING id, key, deployment_id, verb, schedule, start_time, next_execution, state, module_name
)
SELECT j.key as key, d.key as deployment_key, j.module_name as module, j.verb, j.schedule, j.start_time, j.next_execution, j.state
FROM j
INNER JOIN deployments d on j.deployment_id = d.id
LIMIT 1
`
type EndCronJobRow struct {
Key model.CronJobKey
DeploymentKey model.DeploymentKey
Module string
Verb string
Schedule string
StartTime time.Time
NextExecution time.Time
State model.CronJobState
}
func (q *Queries) EndCronJob(ctx context.Context, nextExecution time.Time, key model.CronJobKey, startTime time.Time) (EndCronJobRow, error) {
row := q.db.QueryRow(ctx, endCronJob, nextExecution, key, startTime)
var i EndCronJobRow
err := row.Scan(
&i.Key,
&i.DeploymentKey,
&i.Module,
&i.Verb,
&i.Schedule,
&i.StartTime,
&i.NextExecution,
&i.State,
)
return i, err
}
const expireLeases = `-- name: ExpireLeases :one
WITH expired AS (
DELETE FROM leases
WHERE expires_at < NOW() AT TIME ZONE 'utc'
RETURNING 1
)
SELECT COUNT(*)
FROM expired
`
func (q *Queries) ExpireLeases(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, expireLeases)
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 failAsyncCall = `-- name: FailAsyncCall :one
UPDATE async_calls
SET
state = 'error'::async_call_state,
error = $1::TEXT
WHERE id = $2
RETURNING true
`
func (q *Queries) FailAsyncCall(ctx context.Context, error string, iD int64) (bool, error) {
row := q.db.QueryRow(ctx, failAsyncCall, error, iD)
var column_1 bool
err := row.Scan(&column_1)
return column_1, err
}
const failAsyncCallWithRetry = `-- name: FailAsyncCallWithRetry :one
WITH updated AS (
UPDATE async_calls
SET state = 'error'::async_call_state,
error = $5::TEXT
WHERE id = $6::BIGINT
RETURNING id, created_at, lease_id, verb, state, origin, scheduled_at, request, response, error, remaining_attempts, backoff, max_backoff
)
INSERT INTO async_calls (verb, origin, request, remaining_attempts, backoff, max_backoff, scheduled_at)
SELECT updated.verb, updated.origin, updated.request, $1, $2::interval, $3::interval, $4::TIMESTAMPTZ
FROM updated
RETURNING true
`
type FailAsyncCallWithRetryParams struct {
RemainingAttempts int32
Backoff time.Duration
MaxBackoff time.Duration
ScheduledAt time.Time
Error string
ID int64
}
func (q *Queries) FailAsyncCallWithRetry(ctx context.Context, arg FailAsyncCallWithRetryParams) (bool, error) {
row := q.db.QueryRow(ctx, failAsyncCallWithRetry,
arg.RemainingAttempts,
arg.Backoff,
arg.MaxBackoff,
arg.ScheduledAt,
arg.Error,
arg.ID,
)
var column_1 bool
err := row.Scan(&column_1)
return column_1, err
}
const failFSMInstance = `-- name: FailFSMInstance :one
UPDATE fsm_instances
SET
current_state = NULL,
async_call_id = NULL,
status = 'failed'::fsm_status
WHERE
fsm = $1::schema_ref AND key = $2::TEXT
RETURNING true
`
func (q *Queries) FailFSMInstance(ctx context.Context, fsm schema.RefKey, key string) (bool, error) {
row := q.db.QueryRow(ctx, failFSMInstance, fsm, key)
var column_1 bool
err := row.Scan(&column_1)
return column_1, err
}
const finishFSMTransition = `-- name: FinishFSMTransition :one
UPDATE fsm_instances
SET
current_state = destination_state,
destination_state = NULL,
async_call_id = NULL
WHERE
fsm = $1::schema_ref AND key = $2::TEXT
RETURNING true
`
// Mark an FSM transition as completed, updating the current state and clearing the async call ID.
func (q *Queries) FinishFSMTransition(ctx context.Context, fsm schema.RefKey, key string) (bool, error) {
row := q.db.QueryRow(ctx, finishFSMTransition, fsm, key)
var column_1 bool
err := row.Scan(&column_1)
return column_1, err
}
const getActiveControllers = `-- name: GetActiveControllers :many
SELECT id, key, created, last_seen, state, endpoint
FROM controller c
WHERE c.state <> 'dead'
ORDER BY c.key
`
func (q *Queries) GetActiveControllers(ctx context.Context) ([]Controller, error) {
rows, err := q.db.Query(ctx, getActiveControllers)
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 getActiveDeploymentSchemas = `-- name: GetActiveDeploymentSchemas :many
SELECT key, schema FROM deployments WHERE min_replicas > 0
`
type GetActiveDeploymentSchemasRow struct {
Key model.DeploymentKey
Schema *schema.Module
}
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.Key, &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.key, d.schema, d.labels, d.min_replicas, m.name AS module_name, m.language, COUNT(r.id) AS replicas
FROM deployments d
JOIN modules m ON d.module_id = m.id
JOIN runners r ON d.id = r.deployment_id
WHERE min_replicas > 0 AND r.state = 'assigned'
GROUP BY d.id, m.name, m.language
HAVING COUNT(r.id) > 0
`
type GetActiveDeploymentsRow struct {
Deployment Deployment
ModuleName string
Language string
Replicas int64
}
func (q *Queries) GetActiveDeployments(ctx context.Context) ([]GetActiveDeploymentsRow, error) {
rows, err := q.db.Query(ctx, getActiveDeployments)
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.Key,
&i.Deployment.Schema,
&i.Deployment.Labels,
&i.Deployment.MinReplicas,
&i.ModuleName,
&i.Language,
&i.Replicas,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getActiveIngressRoutes = `-- name: GetActiveIngressRoutes :many
SELECT d.key AS deployment_key, ir.module, ir.verb, ir.method, ir.path
FROM ingress_routes ir
INNER JOIN deployments d ON ir.deployment_id = d.id
WHERE d.min_replicas > 0
`
type GetActiveIngressRoutesRow struct {
DeploymentKey model.DeploymentKey
Module string
Verb string
Method string
Path string
}
func (q *Queries) GetActiveIngressRoutes(ctx context.Context) ([]GetActiveIngressRoutesRow, error) {
rows, err := q.db.Query(ctx, getActiveIngressRoutes)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetActiveIngressRoutesRow
for rows.Next() {
var i GetActiveIngressRoutesRow
if err := rows.Scan(
&i.DeploymentKey,
&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 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.key END, NULL) AS deployment_key
FROM runners r
LEFT JOIN deployments d on d.id = r.deployment_id
WHERE r.state <> 'dead'
ORDER BY r.key
`
type GetActiveRunnersRow struct {
RunnerKey model.RunnerKey
Endpoint string
State RunnerState
Labels []byte
LastSeen time.Time
ModuleName optional.Option[string]
DeploymentKey optional.Option[string]
}
func (q *Queries) GetActiveRunners(ctx context.Context) ([]GetActiveRunnersRow, error) {
rows, err := q.db.Query(ctx, getActiveRunners)
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.DeploymentKey,
); 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 getCronJobs = `-- name: GetCronJobs :many
SELECT j.key as key, d.key as deployment_key, j.module_name as module, j.verb, j.schedule, j.start_time, j.next_execution, j.state
FROM cron_jobs j
INNER JOIN deployments d on j.deployment_id = d.id
WHERE d.min_replicas > 0
`
type GetCronJobsRow struct {
Key model.CronJobKey
DeploymentKey model.DeploymentKey
Module string
Verb string
Schedule string
StartTime time.Time
NextExecution time.Time
State model.CronJobState
}
func (q *Queries) GetCronJobs(ctx context.Context) ([]GetCronJobsRow, error) {
rows, err := q.db.Query(ctx, getCronJobs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetCronJobsRow
for rows.Next() {
var i GetCronJobsRow
if err := rows.Scan(
&i.Key,
&i.DeploymentKey,
&i.Module,
&i.Verb,
&i.Schedule,
&i.StartTime,
&i.NextExecution,
&i.State,
); 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.key, 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.key = $1::deployment_key
`
type GetDeploymentRow struct {
Deployment Deployment
Language string
ModuleName string
MinReplicas int32
}
func (q *Queries) GetDeployment(ctx context.Context, key model.DeploymentKey) (GetDeploymentRow, error) {
row := q.db.QueryRow(ctx, getDeployment, key)
var i GetDeploymentRow
err := row.Scan(
&i.Deployment.ID,
&i.Deployment.CreatedAt,
&i.Deployment.ModuleID,
&i.Deployment.Key,
&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, key, 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.Key,
&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.key AS deployment_key,
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.key, d.min_replicas, m.name, m.language
HAVING COUNT(r.id) <> d.min_replicas
`
type GetDeploymentsNeedingReconciliationRow struct {
DeploymentKey model.DeploymentKey
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.DeploymentKey,
&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.key as deployment_key, d.schema, 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
AND d.schema = $2::BYTEA
HAVING COUNT(*) = $3::BIGINT -- Number of unique digests provided
)
`
type GetDeploymentsWithArtefactsRow struct {
ID int64
CreatedAt time.Time
DeploymentKey model.DeploymentKey
Schema *schema.Module
ModuleName string
}
// Get all deployments that have artefacts matching the given digests.
func (q *Queries) GetDeploymentsWithArtefacts(ctx context.Context, digests [][]byte, schema []byte, count int64) ([]GetDeploymentsWithArtefactsRow, error) {
rows, err := q.db.Query(ctx, getDeploymentsWithArtefacts, digests, schema, 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.DeploymentKey,
&i.Schema,
&i.ModuleName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDeploymentsWithMinReplicas = `-- name: GetDeploymentsWithMinReplicas :many
SELECT d.id, d.created_at, d.module_id, d.key, 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 min_replicas > 0
ORDER BY d.key
`
type GetDeploymentsWithMinReplicasRow struct {
Deployment Deployment
ModuleName string
Language string
}
func (q *Queries) GetDeploymentsWithMinReplicas(ctx context.Context) ([]GetDeploymentsWithMinReplicasRow, error) {
rows, err := q.db.Query(ctx, getDeploymentsWithMinReplicas)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetDeploymentsWithMinReplicasRow