This repository has been archived by the owner on Jul 26, 2021. It is now read-only.
/
sql.go
941 lines (809 loc) · 22.1 KB
/
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
package mysql
const initializeUser = `
insert into users (
id,
internal_user_id,
external_user_id
)
values (?, ?, ?)
`
// Index: primary key
const getUser = `
select id, created_at, internal_user_id, external_user_id, name, super_admin from users
where id = ?
`
// Index: internal_user_id_unique
const getUserByInternalID = `
select id, created_at, internal_user_id, external_user_id, name, super_admin from users
where internal_user_id = ?
`
// Index: external_user_id_unique
const getUserByExternalID = `
select id, created_at, internal_user_id, external_user_id, name, super_admin from users
where external_user_id = ?
`
// Index: primary key
const updateUserName = `
update users
set name = ?
where id = ?
`
const createExternalUser = `
insert into external_users (
id,
provider_name,
provider_id,
email,
info
)
values (?, ?, ?, ?, ?)
`
// Index: primary key
const getExternalUser = `
select id, provider_name, provider_id, email, info from external_users
where id = ?
`
// Index: provider_name_provider_id_unique
const getExternalUserByProvider = `
select id, provider_name, provider_id, email, info from external_users
where provider_name = ? and provider_id = ?
`
const createInternalUser = `
insert into internal_users (
id,
email,
password_hash
)
values (?, ?, ?)
`
// Index: primary key
const getInternalUser = `
select id, email from internal_users
where id = ?
`
// Index: email
const lookupInternalUser = `
select id, email from internal_users
where email = ?
`
// Index: id_password_hash
const validateInternalUser = `
select id, email from internal_users
where id = ? and password_hash = ?
`
// Index: email_password_hash
const validateInternalUserWithEmail = `
select id, email from internal_users
where email = ? and password_hash = ?
`
// Index: primary key
const updateInternalUserPasswordHash = `
update internal_users
set password_hash = ?
where id = ?
`
const createRegistrationToken = `
insert into registration_tokens (
id,
internal_user_id,
hash
)
values (?, ?, ?)
`
// Index: primary key
const getRegistrationToken = `
select id, created_at, internal_user_id from registration_tokens
where id = ?
`
// Index: hash
const validateRegistrationToken = `
select id, created_at, internal_user_id from registration_tokens
where hash = ?
`
const createPasswordRecoveryToken = `
insert into password_recovery_tokens (
id,
expires_at,
internal_user_id,
hash
)
values (?, now() + interval 1 hour, ?, ?)
`
// Index: primary key
const getPasswordRecoveryToken = `
select id, created_at, expires_at, internal_user_id from password_recovery_tokens
where id = ?
`
// Index: hash
const validatePasswordRecoveryToken = `
select id, created_at, expires_at, internal_user_id from password_recovery_tokens
where hash = ?
`
const createSession = `
insert into sessions (
id,
user_id,
hash
)
values (?, ?, ?)
`
// Index: primary key
const getSession = `
select id, created_at, user_id from sessions
where id = ?
`
// Index: hash
const validateSession = `
select id, created_at, user_id from sessions
where hash = ?
`
// Index: primary key
const deleteSession = `
delete from sessions
where id = ?
limit 1
`
const createUserAccessKey = `
insert into user_access_keys (
id,
user_id,
hash,
description
)
values (?, ?, ?, ?)
`
// Index: primary key
const getUserAccessKey = `
select id, created_at, user_id, description from user_access_keys
where id = ?
`
// Index: hash
const validateUserAccessKey = `
select id, created_at, user_id, description from user_access_keys
where hash = ?
`
// Index: user_id
const listUserAccessKeys = `
select id, created_at, user_id, description from user_access_keys
where user_id = ?
`
// Index: primary key
const deleteUserAccessKey = `
delete from user_access_keys
where id = ?
limit 1
`
const createProject = `
insert into projects (
id,
name
)
values (?, ?)
`
// Index: primary key
const getProject = `
select id, created_at, name, datadog_api_key from projects
where id = ?
`
// Index: name
const lookupProject = `
select id, created_at, name, datadog_api_key from projects
where name = ?
`
const listProjects = `
select id, created_at, name, datadog_api_key from projects
`
// Index: primary key
const updateProject = `
update projects
set name = ?, datadog_api_key = ?
where id = ?
`
// Index: primary key
const deleteProject = `
delete from projects
where id = ?
limit 1
`
// Index: project_id
const getProjectDeviceCounts = `
select count(*) from devices
where project_id = ?
`
// Index: project_id
const getProjectApplicationCounts = `
select count(*) from applications
where project_id = ?
`
const createRole = `
insert into roles (
id,
project_id,
name,
description,
config
)
values (?, ?, ?, ?, ?)
`
// Index: project_id_id
const getRole = `
select id, created_at, project_id, name, description, config from roles
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupRole = `
select id, created_at, project_id, name, description, config from roles
where name = ? and project_id = ?
`
// Index: project_id_id
const listRoles = `
select id, created_at, project_id, name, description, config from roles
where project_id = ?
`
// Index: project_id_id
const updateRole = `
update roles
set name = ?, description = ?, config = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteRole = `
delete from roles
where id = ? and project_id = ?
limit 1
`
const createMembership = `
insert into memberships (
user_id,
project_id
)
values (?, ?)
`
// Index: primary key
const getMembership = `
select user_id, project_id, created_at from memberships
where user_id = ? and project_id = ?
`
// Index: user_id
const listMembershipsByUser = `
select user_id, project_id, created_at from memberships
where user_id = ?
`
// Index: project_id
const listMembershipsByProject = `
select user_id, project_id, created_at from memberships
where project_id = ?
`
// Index: primary key
const deleteMembership = `
delete from memberships
where user_id = ? and project_id = ?
limit 1
`
const createMembershipRoleBinding = `
insert into membership_role_bindings (
user_id,
role_id,
project_id
)
values (?, ?, ?)
`
// Index: project_id_user_id_role_id
const getMembershipRoleBinding = `
select user_id, role_id, created_at, project_id from membership_role_bindings
where user_id = ? and role_id = ? and project_id = ?
`
// Index: project_id_user_id_role_id
const listMembershipRoleBindings = `
select user_id, role_id, created_at, project_id from membership_role_bindings
where user_id = ? and project_id = ?
`
// Index: project_id_user_id_role_id
const deleteMembershipRoleBinding = `
delete from membership_role_bindings
where user_id = ? and role_id = ? and project_id = ?
limit 1
`
const createServiceAccount = `
insert into service_accounts (
id,
project_id,
name,
description
)
values (?, ?, ?, ?)
`
// Index: project_id_id
const getServiceAccount = `
select id, created_at, project_id, name, description from service_accounts
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupServiceAccount = `
select id, created_at, project_id, name, description from service_accounts
where name = ? and project_id = ?
`
// Index: project_id_id
const listServiceAccounts = `
select id, created_at, project_id, name, description from service_accounts
where project_id = ?
`
// Index: project_id_id
const updateServiceAccount = `
update service_accounts
set name = ?, description = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteServiceAccount = `
delete from service_accounts
where id = ? and project_id = ?
limit 1
`
const createServiceAccountAccessKey = `
insert into service_account_access_keys (
id,
project_id,
service_account_id,
hash,
description
)
values (?, ?, ?, ?, ?)
`
// Index: project_id_id
const getServiceAccountAccessKey = `
select id, created_at, project_id, service_account_id, description from service_account_access_keys
where id = ? and project_id = ?
`
// Index: hash
const validateServiceAccountAccessKey = `
select id, created_at, project_id, service_account_id, description from service_account_access_keys
where hash = ?
`
// Index: project_id_service_account_id_id
const listServiceAccountAccessKeys = `
select id, created_at, project_id, service_account_id, description from service_account_access_keys
where project_id = ? and service_account_id = ?
`
// Index: project_id_id
const deleteServiceAccountAccessKey = `
delete from service_account_access_keys
where id = ? and project_id = ?
limit 1
`
const createServiceAccountRoleBinding = `
insert into service_account_role_bindings (
service_account_id,
role_id,
project_id
)
values (?, ?, ?)
`
// Index: project_id_service_account_id_role_id
const getServiceAccountRoleBinding = `
select service_account_id, role_id, created_at, project_id from service_account_role_bindings
where service_account_id = ? and role_id = ? and project_id = ?
`
// Index: project_id_service_account_id_role_id
const listServiceAccountRoleBindings = `
select service_account_id, role_id, created_at, project_id from service_account_role_bindings
where service_account_id = ? and project_id = ?
`
// Index: project_id_service_account_id_role_id
const deleteServiceAccountRoleBinding = `
delete from service_account_role_bindings
where service_account_id = ? and role_id = ? and project_id = ?
limit 1
`
const createDevice = `
insert into devices (
id,
project_id,
name,
registration_token_id,
labels,
environment_variables
)
values (?, ?, ?, ?, ?, ?)
`
// Index: project_id_id
const getDevice = `
select id, created_at, project_id, name, registration_token_id, desired_agent_version, info, labels, environment_variables, last_seen_at from devices
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupDevice = `
select id, created_at, project_id, name, registration_token_id, desired_agent_version, info, labels, environment_variables, last_seen_at from devices
where name = ? and project_id = ?
`
// Index: project_id_id
const listDevices = `
select id, created_at, project_id, name, registration_token_id, desired_agent_version, info, labels, environment_variables, last_seen_at from devices
where project_id = ?
`
// Index: project_id_id,fulltext
const searchDevices = `
select id, created_at, project_id, name, registration_token_id, desired_agent_version, info, labels, environment_variables, last_seen_at from devices
where project_id = ?
and match (name, labels) against (concat('*', ?, '*') in boolean mode)
`
// Index: project_id_id
const updateDeviceName = `
update devices
set name = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateDeviceLabels = `
update devices
set labels = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateDeviceEnvironmentVariables = `
update devices
set environment_variables = ?
where id = ? and project_id = ?
`
const listAllDeviceLabels = `
select labels from devices
where project_id = ?
`
// Index: project_id_id
const setDeviceInfo = `
update devices
set info = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateDeviceLastSeenAt = `
update devices
set last_seen_at = current_timestamp
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteDevice = `
delete from devices
where id = ? and project_id = ?
`
const createDeviceRegistrationToken = `
insert into device_registration_tokens (
id,
project_id,
name,
description,
max_registrations,
labels,
environment_variables
)
values (?, ?, ?, ?, ?, '{}', '{}')
`
// Index: project_id_id
const getDeviceRegistrationToken = `
select id, created_at, project_id, max_registrations, name, description, labels, environment_variables from device_registration_tokens
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupDeviceRegistrationToken = `
select id, created_at, project_id, max_registrations, name, description, labels, environment_variables from device_registration_tokens
where name = ? and project_id = ?
`
// Index: project_id_id
const listDeviceRegistrationTokens = `
select id, created_at, project_id, max_registrations, name, description, labels, environment_variables from device_registration_tokens
where project_id = ?
`
// Index: project_id_id
const updateDeviceRegistrationToken = `
update device_registration_tokens
set name = ?, description = ?, max_registrations = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateDeviceRegistrationTokenLabels = `
update device_registration_tokens
set labels = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateDeviceRegistrationTokenEnvironmentVariables = `
update device_registration_tokens
set environment_variables = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteDeviceRegistrationToken = `
delete from device_registration_tokens
where id = ? and project_id = ?
limit 1
`
// Index: project_id_registration_token_id
const getDevicesRegisteredWithTokenCount = `
select count(*) from devices
where registration_token_id = ? and project_id = ?
`
const createDeviceAccessKey = `
insert into device_access_keys (
id,
project_id,
device_id,
hash
)
values (?, ?, ?, ?)
`
// Index: project_id_id
const getDeviceAccessKey = `
select id, created_at, project_id, device_id from device_access_keys
where id = ? and project_id = ?
`
// Index: project_id_hash
const validateDeviceAccessKey = `
select id, created_at, project_id, device_id from device_access_keys
where project_id = ? and hash = ?
`
const createConnection = `
insert into connections (
id,
project_id,
name,
protocol,
port
)
values (?, ?, ?, ?, ?)
`
// Index: project_id_id
const getConnection = `
select id, created_at, project_id, name, protocol, port from connections
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupConnection = `
select id, created_at, project_id, name, protocol, port from connections
where name = ? and project_id = ?
`
// Index: project_id_id
const listConnections = `
select id, created_at, project_id, name, protocol, port from connections
where project_id = ?
`
// Index: project_id_id
const updateConnection = `
update connections
set name = ?, protocol = ?, port = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteConnection = `
delete from connections
where id = ? and project_id = ?
limit 1
`
const createApplication = `
insert into applications (
id,
project_id,
name,
description
)
values (?, ?, ?, ?)
`
// Index: project_id_id
const getApplication = `
select id, created_at, project_id, name, description, scheduling_rule, metric_endpoint_configs from applications
where id = ? and project_id = ?
`
// Index: project_id_name
const lookupApplication = `
select id, created_at, project_id, name, description, scheduling_rule, metric_endpoint_configs from applications
where name = ? and project_id = ?
`
// Index: project_id_id
const listApplications = `
select id, created_at, project_id, name, description, scheduling_rule, metric_endpoint_configs from applications
where project_id = ?
`
// Index: project_id_id
const updateApplicationName = `
update applications
set name = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateApplicationDescription = `
update applications
set description = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateApplicationSchedulingRule = `
update applications
set scheduling_rule = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const updateApplicationMetricEndpointConfigs = `
update applications
set metric_endpoint_configs = ?
where id = ? and project_id = ?
`
// Index: project_id_id
const deleteApplication = `
delete from applications
where id = ? and project_id = ?
limit 1
`
// Index: project_id_application_id_current_release_id
const getApplicationDeviceCounts = `
select count(*) from device_application_statuses
where project_id = ? and application_id = ?
`
const createRelease = `
insert into releases (
id,
` + "`number`" + `,
project_id,
application_id,
config,
raw_config,
created_by_user_id,
created_by_service_account_id
)
values (?, ?, ?, ?, ?, ?, ?, ?)
`
// Index: project_id_application_id_id
const getRelease = `
select id, ` + "`number`" + `, created_at, project_id, application_id, config, raw_config, created_by_user_id, created_by_service_account_id from releases
where id = ? and project_id = ? and application_id = ?
`
// Index: project_id_application_id_number
const getReleaseByNumber = `
select id, ` + "`number`" + `, created_at, project_id, application_id, config, raw_config, created_by_user_id, created_by_service_account_id from releases
where ` + "`number`" + ` = ? and project_id = ? and application_id = ?
`
// Index: project_id_application_id_created_at
const getLatestRelease = `
select id, ` + "`number`" + `, created_at, project_id, application_id, config, raw_config, created_by_user_id, created_by_service_account_id from releases
where project_id = ? and application_id = ?
order by created_at desc
limit 1
`
// TODO: real pagination
// Index: project_id_application_id_created_at
const listReleases = `
select id, ` + "`number`" + `, created_at, project_id, application_id, config, raw_config, created_by_user_id, created_by_service_account_id from releases
where project_id = ? and application_id = ?
order by created_at desc
limit 50
`
// Index: project_id_application_id_current_release_id
const getReleaseDeviceCounts = `
select count(*) from device_application_statuses
where project_id = ? and application_id = ? and current_release_id = ?
`
// Index: primary key
const setDeviceApplicationStatus = `
insert into device_application_statuses (
project_id,
device_id,
application_id,
current_release_id
)
values (?, ?, ?, ?)
on duplicate key update
current_release_id = ?
`
// Index: primary key
const getDeviceApplicationStatus = `
select project_id, device_id, application_id, current_release_id from device_application_statuses
where project_id = ? and device_id = ? and application_id = ?
`
// Index: project_id_device_id
const listDeviceApplicationStatuses = `
select project_id, device_id, application_id, current_release_id from device_application_statuses
where project_id = ? and device_id = ?
`
// Index: project_id_device_id
const listAllDeviceApplicationStatuses = `
select project_id, device_id, application_id, current_release_id from device_application_statuses
where project_id = ?
`
// Index: primary key
const deleteDeviceApplicationStatus = `
delete from device_application_statuses
where project_id = ? and device_id = ? and application_id = ?
`
// Index: primary key
const setDeviceServiceStatus = `
insert into device_service_statuses (
project_id,
device_id,
application_id,
service,
current_release_id
)
values (?, ?, ?, ?, ?)
on duplicate key update
current_release_id = ?
`
// Index: primary key
const getDeviceServiceStatus = `
select project_id, device_id, application_id, service, current_release_id from device_service_statuses
where project_id = ? and device_id = ? and application_id = ? and service = ?
`
// Index: project_id_device_id_application_id
const getDeviceServiceStatuses = `
select project_id, device_id, application_id, service, current_release_id from device_service_statuses
where project_id = ? and device_id = ? and application_id = ?
`
// Index: project_id_device_id_application_id
const listDeviceServiceStatuses = `
select project_id, device_id, application_id, service, current_release_id from device_service_statuses
where project_id = ? and device_id = ?
`
// Index: primary key
const deleteDeviceServiceStatus = `
delete from device_service_statuses
where project_id = ? and device_id = ? and application_id = ? and service = ?
`
// Index: primary key
const setDeviceServiceState = `
insert into device_service_states (
project_id,
device_id,
application_id,
service,
state,
error_message
)
values (?, ?, ?, ?, ?, ?)
on duplicate key update
state = ?,
error_message = ?
`
// Index: primary key
const getDeviceServiceState = `
select project_id, device_id, application_id, service, state, error_message from device_service_states
where project_id = ? and device_id = ? and application_id = ? and service = ?
`
// Index: project_id_device_id_application_id
const getDeviceServiceStates = `
select project_id, device_id, application_id, service, state, error_message from device_service_states
where project_id = ? and device_id = ? and application_id = ?
`
// Index: project_id_application_id
const getDeviceServiceStateCountsByApplication = `
select count(state) as count, sum(error_message != "") as count_erroring, state, service, application_id
from device_service_states
where project_id = ? and application_id = ?
group by service, state
order by service, state
`
// Index: project_id_device_id_application_id
const listDeviceServiceStates = `
select project_id, device_id, application_id, service, state, error_message from device_service_states
where project_id = ? and device_id = ?
`
// Index: project_id_device_id_application_id
const listAllDeviceServiceStates = `
select project_id, device_id, application_id, service, state, error_message from device_service_states
where project_id = ?
`
// Index: primary key
const deleteDeviceServiceState = `
delete from device_service_states
where project_id = ? and device_id = ? and application_id = ? and service = ?
`
// Index: primary key
const setProjectConfig = `
replace into project_configs (
project_id,
k,
v
)
values (?, ?, ?)
`
// Index: primary key
const getProjectConfig = `
select project_id, k, v from project_configs
where project_id = ? and k = ?
`