-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathentities.sql.go
More file actions
629 lines (565 loc) · 16.4 KB
/
Copy pathentities.sql.go
File metadata and controls
629 lines (565 loc) · 16.4 KB
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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: entities.sql
package db
import (
"context"
"encoding/json"
"github.com/google/uuid"
"github.com/lib/pq"
)
const countEntitiesByType = `-- name: CountEntitiesByType :one
SELECT COUNT(*) FROM entity_instances
WHERE entity_instances.entity_type = $1
`
// CountEntitiesByType counts all entities of a given type (across all projects/providers).
func (q *Queries) CountEntitiesByType(ctx context.Context, entityType Entities) (int64, error) {
row := q.db.QueryRowContext(ctx, countEntitiesByType, entityType)
var count int64
err := row.Scan(&count)
return count, err
}
const countEntitiesByTypeAndProject = `-- name: CountEntitiesByTypeAndProject :one
SELECT COUNT(*) FROM entity_instances
WHERE entity_instances.entity_type = $1 AND entity_instances.project_id = $2
`
type CountEntitiesByTypeAndProjectParams struct {
EntityType Entities `json:"entity_type"`
ProjectID uuid.UUID `json:"project_id"`
}
// CountEntitiesByTypeAndProject counts entities of a given type for a specific project.
func (q *Queries) CountEntitiesByTypeAndProject(ctx context.Context, arg CountEntitiesByTypeAndProjectParams) (int64, error) {
row := q.db.QueryRowContext(ctx, countEntitiesByTypeAndProject, arg.EntityType, arg.ProjectID)
var count int64
err := row.Scan(&count)
return count, err
}
const createEntity = `-- name: CreateEntity :one
INSERT INTO entity_instances (
entity_type,
name,
project_id,
provider_id,
originated_from
) VALUES ($1, $2, $3, $4, $5)
RETURNING id, entity_type, name, project_id, provider_id, created_at, originated_from
`
type CreateEntityParams struct {
EntityType Entities `json:"entity_type"`
Name string `json:"name"`
ProjectID uuid.UUID `json:"project_id"`
ProviderID uuid.UUID `json:"provider_id"`
OriginatedFrom uuid.NullUUID `json:"originated_from"`
}
// CreateEntity adds an entry to the entity_instances table so it can be tracked by Minder.
func (q *Queries) CreateEntity(ctx context.Context, arg CreateEntityParams) (EntityInstance, error) {
row := q.db.QueryRowContext(ctx, createEntity,
arg.EntityType,
arg.Name,
arg.ProjectID,
arg.ProviderID,
arg.OriginatedFrom,
)
var i EntityInstance
err := row.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
)
return i, err
}
const createEntityWithID = `-- name: CreateEntityWithID :one
INSERT INTO entity_instances (
id,
entity_type,
name,
project_id,
provider_id,
originated_from
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, entity_type, name, project_id, provider_id, created_at, originated_from
`
type CreateEntityWithIDParams struct {
ID uuid.UUID `json:"id"`
EntityType Entities `json:"entity_type"`
Name string `json:"name"`
ProjectID uuid.UUID `json:"project_id"`
ProviderID uuid.UUID `json:"provider_id"`
OriginatedFrom uuid.NullUUID `json:"originated_from"`
}
// CreateEntityWithID adds an entry to the entities table with a specific ID so it can be tracked by Minder.
func (q *Queries) CreateEntityWithID(ctx context.Context, arg CreateEntityWithIDParams) (EntityInstance, error) {
row := q.db.QueryRowContext(ctx, createEntityWithID,
arg.ID,
arg.EntityType,
arg.Name,
arg.ProjectID,
arg.ProviderID,
arg.OriginatedFrom,
)
var i EntityInstance
err := row.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
)
return i, err
}
const createOrEnsureEntityByID = `-- name: CreateOrEnsureEntityByID :one
INSERT INTO entity_instances (
id,
entity_type,
name,
project_id,
provider_id,
originated_from
) VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id) DO UPDATE
SET
id = entity_instances.id -- This is a "noop" update to ensure the RETURNING clause works
RETURNING id, entity_type, name, project_id, provider_id, created_at, originated_from
`
type CreateOrEnsureEntityByIDParams struct {
ID uuid.UUID `json:"id"`
EntityType Entities `json:"entity_type"`
Name string `json:"name"`
ProjectID uuid.UUID `json:"project_id"`
ProviderID uuid.UUID `json:"provider_id"`
OriginatedFrom uuid.NullUUID `json:"originated_from"`
}
// CreateOrEnsureEntityByID adds an entry to the entity_instances table if it does not exist, or returns the existing entry.
func (q *Queries) CreateOrEnsureEntityByID(ctx context.Context, arg CreateOrEnsureEntityByIDParams) (EntityInstance, error) {
row := q.db.QueryRowContext(ctx, createOrEnsureEntityByID,
arg.ID,
arg.EntityType,
arg.Name,
arg.ProjectID,
arg.ProviderID,
arg.OriginatedFrom,
)
var i EntityInstance
err := row.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
)
return i, err
}
const deleteAllPropertiesForEntity = `-- name: DeleteAllPropertiesForEntity :exec
DELETE FROM properties
WHERE entity_id = $1
`
func (q *Queries) DeleteAllPropertiesForEntity(ctx context.Context, entityID uuid.UUID) error {
_, err := q.db.ExecContext(ctx, deleteAllPropertiesForEntity, entityID)
return err
}
const deleteEntity = `-- name: DeleteEntity :exec
DELETE FROM entity_instances
WHERE id = $1 AND project_id = $2
`
type DeleteEntityParams struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"project_id"`
}
// DeleteEntity removes an entity from the entity_instances table for a project.
func (q *Queries) DeleteEntity(ctx context.Context, arg DeleteEntityParams) error {
_, err := q.db.ExecContext(ctx, deleteEntity, arg.ID, arg.ProjectID)
return err
}
const deleteProperty = `-- name: DeleteProperty :exec
DELETE FROM properties
WHERE entity_id = $1 AND key = $2
`
type DeletePropertyParams struct {
EntityID uuid.UUID `json:"entity_id"`
Key string `json:"key"`
}
func (q *Queries) DeleteProperty(ctx context.Context, arg DeletePropertyParams) error {
_, err := q.db.ExecContext(ctx, deleteProperty, arg.EntityID, arg.Key)
return err
}
const entityExistsAfterID = `-- name: EntityExistsAfterID :one
SELECT EXISTS (
SELECT 1
FROM entity_instances
WHERE entity_instances.entity_type = $1
AND entity_instances.id > $2
) AS exists
`
type EntityExistsAfterIDParams struct {
EntityType Entities `json:"entity_type"`
ID uuid.UUID `json:"id"`
}
// EntityExistsAfterID checks if any entity of a given type exists after a cursor ID.
func (q *Queries) EntityExistsAfterID(ctx context.Context, arg EntityExistsAfterIDParams) (bool, error) {
row := q.db.QueryRowContext(ctx, entityExistsAfterID, arg.EntityType, arg.ID)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const getAllPropertiesForEntity = `-- name: GetAllPropertiesForEntity :many
SELECT id, entity_id, key, value, updated_at FROM properties
WHERE entity_id = $1
`
func (q *Queries) GetAllPropertiesForEntity(ctx context.Context, entityID uuid.UUID) ([]Property, error) {
rows, err := q.db.QueryContext(ctx, getAllPropertiesForEntity, entityID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Property{}
for rows.Next() {
var i Property
if err := rows.Scan(
&i.ID,
&i.EntityID,
&i.Key,
&i.Value,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEntitiesByProjectHierarchy = `-- name: GetEntitiesByProjectHierarchy :many
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE entity_instances.project_id = ANY($1::uuid[])
`
// GetEntitiesByProjectHierarchy retrieves all entities for a project or hierarchy of projects.
func (q *Queries) GetEntitiesByProjectHierarchy(ctx context.Context, projects []uuid.UUID) ([]EntityInstance, error) {
rows, err := q.db.QueryContext(ctx, getEntitiesByProjectHierarchy, pq.Array(projects))
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntityInstance{}
for rows.Next() {
var i EntityInstance
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEntitiesByProvider = `-- name: GetEntitiesByProvider :many
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE entity_instances.provider_id = $1
`
// GetEntitiesByProvider retrieves all entities of a given provider.
// this is how one would get all repositories, artifacts, etc. for a given provider.
func (q *Queries) GetEntitiesByProvider(ctx context.Context, providerID uuid.UUID) ([]EntityInstance, error) {
rows, err := q.db.QueryContext(ctx, getEntitiesByProvider, providerID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntityInstance{}
for rows.Next() {
var i EntityInstance
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEntitiesByType = `-- name: GetEntitiesByType :many
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE entity_instances.entity_type = $1
AND entity_instances.provider_id = $2
AND entity_instances.project_id = ANY($3::uuid[])
`
type GetEntitiesByTypeParams struct {
EntityType Entities `json:"entity_type"`
ProviderID uuid.UUID `json:"provider_id"`
Projects []uuid.UUID `json:"projects"`
}
// GetEntitiesByType retrieves all entities of a given type for a project or hierarchy of projects.
// this is how one would get all repositories, artifacts, etc.
func (q *Queries) GetEntitiesByType(ctx context.Context, arg GetEntitiesByTypeParams) ([]EntityInstance, error) {
rows, err := q.db.QueryContext(ctx, getEntitiesByType, arg.EntityType, arg.ProviderID, pq.Array(arg.Projects))
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntityInstance{}
for rows.Next() {
var i EntityInstance
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEntityByID = `-- name: GetEntityByID :one
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE entity_instances.id = $1
LIMIT 1
`
// GetEntityByID retrieves an entity by its ID for a project or hierarchy of projects.
func (q *Queries) GetEntityByID(ctx context.Context, id uuid.UUID) (EntityInstance, error) {
row := q.db.QueryRowContext(ctx, getEntityByID, id)
var i EntityInstance
err := row.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
)
return i, err
}
const getEntityByName = `-- name: GetEntityByName :one
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE
entity_instances.name = $3
AND entity_instances.project_id = $1
AND entity_instances.entity_type = $2
AND entity_instances.provider_id = $4
LIMIT 1
`
type GetEntityByNameParams struct {
ProjectID uuid.UUID `json:"project_id"`
EntityType Entities `json:"entity_type"`
Name string `json:"name"`
ProviderID uuid.UUID `json:"provider_id"`
}
// GetEntityByName retrieves an entity by its name for a project or hierarchy of projects.
func (q *Queries) GetEntityByName(ctx context.Context, arg GetEntityByNameParams) (EntityInstance, error) {
row := q.db.QueryRowContext(ctx, getEntityByName,
arg.ProjectID,
arg.EntityType,
arg.Name,
arg.ProviderID,
)
var i EntityInstance
err := row.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
)
return i, err
}
const getProperty = `-- name: GetProperty :one
SELECT id, entity_id, key, value, updated_at FROM properties
WHERE entity_id = $1 AND key = $2
`
type GetPropertyParams struct {
EntityID uuid.UUID `json:"entity_id"`
Key string `json:"key"`
}
func (q *Queries) GetProperty(ctx context.Context, arg GetPropertyParams) (Property, error) {
row := q.db.QueryRowContext(ctx, getProperty, arg.EntityID, arg.Key)
var i Property
err := row.Scan(
&i.ID,
&i.EntityID,
&i.Key,
&i.Value,
&i.UpdatedAt,
)
return i, err
}
const getTypedEntitiesByProperty = `-- name: GetTypedEntitiesByProperty :many
SELECT ei.id, ei.entity_type, ei.name, ei.project_id, ei.provider_id, ei.created_at, ei.originated_from
FROM entity_instances ei
JOIN properties p ON ei.id = p.entity_id
WHERE ei.entity_type = $1
AND ($2::uuid = '00000000-0000-0000-0000-000000000000'::uuid OR ei.project_id = $2)
AND ($3::uuid = '00000000-0000-0000-0000-000000000000'::uuid OR ei.provider_id = $3)
AND p.key = $4
AND p.value @> $5::jsonb
`
type GetTypedEntitiesByPropertyParams struct {
EntityType Entities `json:"entity_type"`
ProjectID uuid.UUID `json:"project_id"`
ProviderID uuid.UUID `json:"provider_id"`
Key string `json:"key"`
Value json.RawMessage `json:"value"`
}
func (q *Queries) GetTypedEntitiesByProperty(ctx context.Context, arg GetTypedEntitiesByPropertyParams) ([]EntityInstance, error) {
rows, err := q.db.QueryContext(ctx, getTypedEntitiesByProperty,
arg.EntityType,
arg.ProjectID,
arg.ProviderID,
arg.Key,
arg.Value,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntityInstance{}
for rows.Next() {
var i EntityInstance
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listEntitiesAfterID = `-- name: ListEntitiesAfterID :many
SELECT id, entity_type, name, project_id, provider_id, created_at, originated_from FROM entity_instances
WHERE entity_instances.entity_type = $1
AND entity_instances.id > $2
ORDER BY entity_instances.id
LIMIT $3::bigint
`
type ListEntitiesAfterIDParams struct {
EntityType Entities `json:"entity_type"`
ID uuid.UUID `json:"id"`
Limit int64 `json:"limit"`
}
// ListEntitiesAfterID retrieves entities of a given type after a cursor ID, for pagination.
// This is used for cursor-based iteration over all entities (e.g., in the reminder service).
func (q *Queries) ListEntitiesAfterID(ctx context.Context, arg ListEntitiesAfterIDParams) ([]EntityInstance, error) {
rows, err := q.db.QueryContext(ctx, listEntitiesAfterID, arg.EntityType, arg.ID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntityInstance{}
for rows.Next() {
var i EntityInstance
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.Name,
&i.ProjectID,
&i.ProviderID,
&i.CreatedAt,
&i.OriginatedFrom,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const upsertProperty = `-- name: UpsertProperty :one
INSERT INTO properties (
entity_id,
key,
value,
updated_at
) VALUES ($1, $2, $3, NOW())
ON CONFLICT (entity_id, key) DO UPDATE
SET
value = $4,
updated_at = NOW()
RETURNING id, entity_id, key, value, updated_at
`
type UpsertPropertyParams struct {
EntityID uuid.UUID `json:"entity_id"`
Key string `json:"key"`
Value json.RawMessage `json:"value"`
}
func (q *Queries) UpsertProperty(ctx context.Context, arg UpsertPropertyParams) (Property, error) {
row := q.db.QueryRowContext(ctx, upsertProperty,
arg.EntityID,
arg.Key,
arg.Value,
arg.Value,
)
var i Property
err := row.Scan(
&i.ID,
&i.EntityID,
&i.Key,
&i.Value,
&i.UpdatedAt,
)
return i, err
}