-
Notifications
You must be signed in to change notification settings - Fork 8
/
mock_scans.go
860 lines (798 loc) · 26.2 KB
/
mock_scans.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
package testhelpers
import (
"fmt"
"strings"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/lib/pq"
)
var defaultPrivilege = pq.StringArray{"s1=arwd/s1", "u1=UC/u18", "u8=arw/s1"}
func mockQueryBuilder(query, predicate, order string) string {
q := strings.Builder{}
q.WriteString(query)
if predicate != "" {
q.WriteString(fmt.Sprintf(" %s", predicate))
}
if order != "" {
q.WriteString(fmt.Sprintf(" %s", order))
}
q.WriteString(`;`)
return q.String()
}
func MockClusterReplicaScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_cluster_replicas.id,
mz_cluster_replicas.name AS replica_name,
mz_clusters.name AS cluster_name,
mz_cluster_replicas.size,
mz_cluster_replicas.availability_zone,
mz_cluster_replicas.disk,
comments.comment AS comment
FROM mz_cluster_replicas
JOIN mz_clusters
ON mz_cluster_replicas.cluster_id = mz_clusters.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'cluster-replica'
\) comments
ON mz_cluster_replicas.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "replica_name", "cluster_name", "size", "availability_zone", "disk", "comment"}).
AddRow("u1", "replica", "cluster", "small", "use1-az2", false, "comment")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockClusterScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_clusters.id,
mz_clusters.name,
mz_clusters.managed,
mz_clusters.size,
mz_clusters.replication_factor,
mz_clusters.disk,
mz_clusters.availability_zones,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_clusters.privileges
FROM mz_clusters
JOIN mz_roles
ON mz_clusters.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'cluster'
\) comments
ON mz_clusters.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
az := pq.StringArray{"use1-az1", "use1-az2", "use1-az3"}
ir := mock.NewRows([]string{"id", "name", "managed", "size", "replication_factor", "disk", "availability_zones", "comment", "owner_name", "privileges"}).
AddRow("u1", "cluster", true, "small", 2, true, az, "comment", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockConnectionScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_connections.id,
mz_connections.name AS connection_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_connections.type AS connection_type,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_connections.privileges
FROM mz_connections
JOIN mz_schemas
ON mz_connections.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_connections.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'connection'
\) comments
ON mz_connections.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "connection_name", "schema_name", "database_name", "connection_type", "owner_name", "privileges"}).
AddRow("u1", "connection", "schema", "database", "kafka", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockConnectionAwsScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_connections.id,
mz_connections.name AS connection_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_aws_connections.endpoint,
mz_aws_connections.region AS aws_region,
mz_aws_connections.access_key_id,
mz_aws_connections.access_key_id_secret_id,
mz_aws_connections.secret_access_key_secret_id,
mz_aws_connections.session_token,
mz_aws_connections.session_token_secret_id,
mz_aws_connections.assume_role_arn,
mz_aws_connections.assume_role_session_name,
comments.comment AS comment,
mz_aws_connections.principal,
mz_roles.name AS owner_name
FROM mz_connections
JOIN mz_schemas
ON mz_connections.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_internal.mz_aws_connections AS mz_aws_connections
ON mz_connections.id = mz_aws_connections.id
JOIN mz_roles
ON mz_connections.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'connection'
\) comments
ON mz_connections.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{
"id",
"connection_name",
"schema_name",
"database_name",
"endpoint",
"aws_region",
"access_key_id",
"access_key_id_secret_id",
"secret_access_key_secret_id",
"session_token",
"session_token_secret_id",
"assume_role_arn",
"assume_role_session_name",
"comment",
"owner_name",
}).AddRow(
"u1",
"connection",
"schema",
"database",
"localhost",
"us-east-1",
"foo",
"u1",
"u1",
"bar",
"u1",
"arn:aws:iam::123456789012:user/JohnDoe",
"s3-access-example",
"comment",
"owner_name",
)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockConnectionAwsPrivatelinkScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_connections.id,
mz_connections.name AS connection_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_aws_privatelink_connections.principal,
comments.comment AS comment,
mz_roles.name AS owner_name
FROM mz_connections
JOIN mz_schemas
ON mz_connections.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_aws_privatelink_connections
ON mz_connections.id = mz_aws_privatelink_connections.id
JOIN mz_roles
ON mz_connections.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'connection'
\) comments
ON mz_connections.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "connection_name", "schema_name", "database_name", "principal"}).
AddRow("u1", "connection", "schema", "database", "principal")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockConnectionSshTunnelScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_connections.id,
mz_connections.name AS connection_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_ssh_tunnel_connections.public_key_1,
mz_ssh_tunnel_connections.public_key_2,
comments.comment AS comment,
mz_roles.name AS owner_name
FROM mz_connections
JOIN mz_schemas
ON mz_connections.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_ssh_tunnel_connections
ON mz_connections.id = mz_ssh_tunnel_connections.id
JOIN mz_roles
ON mz_connections.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'connection'
\) comments
ON mz_connections.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "connection_name", "schema_name", "database_name", "public_key_1", "public_key_2"}).
AddRow("u1", "connection", "schema", "database", "key_1", "key_2")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockDefaultPrivilegeScan(mock sqlmock.Sqlmock, predicate, objectType string) {
b := `
SELECT
mz_default_privileges.object_type,
mz_default_privileges.grantee AS grantee_id,
\(CASE WHEN mz_default_privileges.grantee = 'p' THEN 'PUBLIC' ELSE grantee.name END\) AS grantee_name,
mz_default_privileges.role_id AS target_id,
\(CASE WHEN mz_default_privileges.role_id = 'p' THEN 'PUBLIC' ELSE target.name END\) AS target_name,
mz_default_privileges.database_id AS database_id,
mz_default_privileges.schema_id AS schema_id,
mz_default_privileges.privileges
FROM mz_default_privileges
LEFT JOIN mz_roles AS grantee
ON mz_default_privileges.grantee = grantee.id
LEFT JOIN mz_roles AS target
ON mz_default_privileges.role_id = target.id
LEFT JOIN mz_schemas
ON mz_default_privileges.schema_id = mz_schemas.id
LEFT JOIN mz_databases
ON mz_default_privileges.database_id = mz_databases.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"object_type", "grantee_id", "grantee_name", "target_id", "target_name", "database_id", "schema_id", "privileges"}).
AddRow(objectType, "u2", "grantee", "u4", "target", "u1", "u3", "U").
AddRow(objectType, "u1", "grantee", "u1", "target", nil, nil, "Ur").
AddRow(objectType, "u3", "grantee", "u6", "target", "u2", nil, "rw")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockDatabaseScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_databases.id,
mz_databases.name AS database_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_databases.privileges
FROM mz_databases
JOIN mz_roles
ON mz_databases.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'database'
\) comments
ON mz_databases.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "database_name", "owner_name", "privileges"}).
AddRow("u1", "database", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockIndexColumnScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_columns.id,
mz_columns.name,
mz_columns.position,
mz_columns.nullable,
mz_columns.type,
mz_columns.default,
CASE WHEN mz_index_columns.index_id IS NOT NULL THEN true ELSE false END AS indexed_column,
mz_indexes.name AS index_name,
mz_indexes.id AS index_id
FROM mz_columns
LEFT JOIN mz_indexes
ON mz_columns.id = mz_indexes.on_id
LEFT JOIN mz_index_columns
ON mz_index_columns.index_id = mz_indexes.id
AND mz_index_columns.index_position = mz_columns.position`
q := mockQueryBuilder(b, predicate, "ORDER BY mz_columns.position")
ir := mock.NewRows([]string{"id", "name", "position", "nullable", "type", "default", "indexed_column", "index_name", "index_id"}).
AddRow("u1", "column", "1", "true", "integer", "", "true", "index", "u1")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockIndexScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_indexes.id,
mz_indexes.name AS index_name,
mz_objects.name AS obj_name,
mz_schemas.name AS obj_schema_name,
mz_databases.name AS obj_database_name,
comments.comment AS comment
FROM mz_indexes
JOIN mz_objects
ON mz_indexes.on_id = mz_objects.id
LEFT JOIN mz_schemas
ON mz_objects.schema_id = mz_schemas.id
LEFT JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'index'
\) comments
ON mz_indexes.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "index_name", "obj_name", "obj_schema_name", "obj_database_name"}).
AddRow("u1", "index", "obj", "schema", "database")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockMaterializeViewScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_materialized_views.id,
mz_materialized_views.name AS materialized_view_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_clusters.name AS cluster_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_materialized_views.create_sql,
mz_materialized_views.privileges
FROM mz_materialized_views
JOIN mz_schemas
ON mz_materialized_views.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_clusters
ON mz_materialized_views.cluster_id = mz_clusters.id
JOIN mz_roles
ON mz_materialized_views.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'materialized-view'
AND object_sub_id IS NULL
\) comments
ON mz_materialized_views.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "materialized_view_name", "schema_name", "database_name", "cluster_name", "owner_name", "privileges"}).
AddRow("u1", "view", "schema", "database", "cluster", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSystemPrivilege(mock sqlmock.Sqlmock) {
b := "SELECT privileges FROM mz_system_privileges"
ir := mock.NewRows([]string{"privileges"}).AddRow("s1=RBN/s1")
mock.ExpectQuery(b).WillReturnRows(ir)
}
func MockRoleScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_roles.id,
mz_roles.name AS role_name,
mz_roles.inherit,
comments.comment AS comment
FROM mz_roles
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'role'
\) comments
ON mz_roles.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "role_name", "inherit"}).
AddRow("u1", "joe", true)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockRoleGrantScan(mock sqlmock.Sqlmock) {
q := `
SELECT
mz_role_members.role_id,
mz_role_members.member,
mz_role_members.grantor
FROM mz_role_members`
ir := mock.NewRows([]string{"role_id", "member", "grantor"}).
AddRow("u2", "u3", "u3").
AddRow("u1", "u1", "s1")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSchemaScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_schemas.id,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_schemas.privileges
FROM mz_schemas JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_schemas.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'schema'
\) comments
ON mz_schemas.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "schema_name", "database_name", "owner_name", "privileges"}).
AddRow("u1", "schema", "database", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSecretScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_secrets.id,
mz_secrets.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_secrets.privileges
FROM mz_secrets
JOIN mz_schemas
ON mz_secrets.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_secrets.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'secret'
\) comments
ON mz_secrets.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "name", "schema_name", "database_name", "owner_name", "privileges"}).
AddRow("u1", "secret", "schema", "database", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSinkScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_sinks.id,
mz_sinks.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_sinks.type AS sink_type,
COALESCE\(mz_sinks.size, mz_clusters.size\) AS size,
mz_sinks.envelope_type,
mz_connections.name as connection_name,
mz_clusters.name as cluster_name,
comments.comment AS comment,
mz_roles.name AS owner_name
FROM mz_sinks
JOIN mz_schemas
ON mz_sinks.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_connections
ON mz_sinks.connection_id = mz_connections.id
LEFT JOIN mz_clusters
ON mz_sinks.cluster_id = mz_clusters.id
JOIN mz_roles
ON mz_sinks.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'sink'
\) comments
ON mz_sinks.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "name", "schema_name", "database_name", "sink_type", "size", "envelope_type", "connection_name", "cluster_name", "owner_name"}).
AddRow("u1", "sink", "schema", "database", "kafka", "small", "JSON", "conn", "cluster", "joe")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSourceScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_sources.id,
mz_sources.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_sources.type AS source_type,
COALESCE\(mz_sources.size, mz_clusters.size\) AS size,
mz_sources.envelope_type,
mz_connections.name as connection_name,
mz_clusters.name as cluster_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_sources.privileges
FROM mz_sources
JOIN mz_schemas
ON mz_sources.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_connections
ON mz_sources.connection_id = mz_connections.id
LEFT JOIN mz_clusters
ON mz_sources.cluster_id = mz_clusters.id
JOIN mz_roles
ON mz_sources.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'source'
\) comments
ON mz_sources.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "name", "schema_name", "database_name", "source_type", "size", "envelope_type", "connection_name", "cluster_name", "owner_name", "privileges"}).
AddRow("u1", "source", "schema", "database", "kafka", "small", "BYTES", "conn", "cluster", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSubsourceScan(mock sqlmock.Sqlmock, predicate string) {
b := `
WITH dependencies AS \(
SELECT
mz_object_dependencies.object_id,
mz_object_dependencies.referenced_object_id,
mz_objects.name AS object_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_objects.type,
mz_object_dependencies.object_id AS filter_id,
'object' AS source_type
FROM mz_internal.mz_object_dependencies
JOIN mz_objects
ON mz_object_dependencies.referenced_object_id = mz_objects.id
JOIN mz_schemas
ON mz_objects.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
UNION
SELECT
mz_object_dependencies.object_id,
mz_object_dependencies.referenced_object_id,
mz_objects.name AS object_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_objects.type,
mz_object_dependencies.referenced_object_id AS filter_id,
'reference' AS source_type
FROM mz_internal.mz_object_dependencies
JOIN mz_objects
ON mz_object_dependencies.object_id = mz_objects.id
JOIN mz_schemas
ON mz_objects.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id\)
SELECT \* FROM dependencies`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"object_id", "referenced_object_id", "object_name", "schema_name", "database_name", "type", "filter_id", "source_type"}).
AddRow("u1", "u2", "object", "schema", "database", "source", "u1", "object")
mock.ExpectQuery(q).WillReturnRows(ir)
}
// MockPosgresSubsourceScan mocks the scan of a postgres source
func MockPosgresSubsourceScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT DISTINCT
mz_sources.id AS object_id,
subsources.id AS referenced_object_id,
mz_sources.name AS object_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_sources.type,
mz_postgres_source_tables.table_name AS upstream_table_name,
mz_postgres_source_tables.schema_name AS upstream_table_schema
FROM mz_sources AS subsources
JOIN mz_internal.mz_object_dependencies
ON subsources.id = mz_object_dependencies.referenced_object_id
JOIN mz_sources
ON mz_sources.id = mz_object_dependencies.object_id
JOIN mz_schemas
ON mz_sources.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_internal.mz_postgres_source_tables
ON mz_sources.id = mz_postgres_source_tables.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"object_id", "referenced_object_id", "object_name", "schema_name", "database_name", "type", "upstream_table_name", "upstream_table_schema"}).
AddRow("u1", "u2", "object", "schema", "database", "source", "table", "table_schema")
mock.ExpectQuery(q).WillReturnRows(ir)
}
// MockMysqlSubsourceScan mocks the scan of a postgres source
func MockMysqlSubsourceScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT DISTINCT
mz_sources.id AS object_id,
subsources.id AS referenced_object_id,
mz_sources.name AS object_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_sources.type,
mz_mysql_source_tables.table_name AS upstream_table_name,
mz_mysql_source_tables.schema_name AS upstream_table_schema
FROM mz_sources AS subsources
JOIN mz_internal.mz_object_dependencies
ON subsources.id = mz_object_dependencies.referenced_object_id
JOIN mz_sources
ON mz_sources.id = mz_object_dependencies.object_id
JOIN mz_schemas
ON mz_sources.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
LEFT JOIN mz_internal.mz_mysql_source_tables
ON mz_sources.id = mz_mysql_source_tables.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"object_id", "referenced_object_id", "object_name", "schema_name", "database_name", "type", "upstream_table_name", "upstream_table_schema"}).
AddRow("u1", "u2", "object", "schema", "database", "source", "table", "table_schema")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockTableColumnScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_columns.id,
mz_columns.name,
mz_columns.position,
mz_columns.nullable,
comments.comment,
mz_columns.type,
mz_columns.default
FROM mz_columns
LEFT JOIN \(
SELECT id, object_sub_id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'table'
\) comments
ON mz_columns.id = comments.id
AND mz_columns.position = comments.object_sub_id`
q := mockQueryBuilder(b, predicate, "ORDER BY mz_columns.position")
ir := mock.NewRows([]string{"id", "name", "position", "nullable", "type", "default"}).
AddRow("u1", "column", "1", "true", "integer", "")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockSystemGrantScan(mock sqlmock.Sqlmock) {
q := `SELECT privileges FROM mz_system_privileges`
ir := mock.NewRows([]string{"privileges"}).
AddRow("u1=B/s1").AddRow("u9=RBN/s1").AddRow("u5=B/s1")
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockTableScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_tables.id,
mz_tables.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_tables.privileges
FROM mz_tables
JOIN mz_schemas
ON mz_tables.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_tables.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'table'
AND object_sub_id IS NULL
\) comments
ON mz_tables.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "name", "schema_name", "database_name", "comment", "owner_name", "privileges"}).
AddRow("u1", "table", "schema", "database", "comment", "materialize", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockTypeScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_types.id,
mz_types.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_types.category,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_types.privileges
FROM mz_types
JOIN mz_schemas
ON mz_types.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_types.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'type'
\) comments
ON mz_types.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{"id", "name", "schema_name", "database_name", "category", "owner_name", "privileges"}).
AddRow("u1", "type", "schema", "database", "category", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockViewScan(mock sqlmock.Sqlmock, predicate string) {
b := `
SELECT
mz_views.id,
mz_views.name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_views.create_sql,
mz_views.privileges
FROM mz_views
JOIN mz_schemas
ON mz_views.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
JOIN mz_roles
ON mz_views.owner_id = mz_roles.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'view'
AND object_sub_id IS NULL
\) comments
ON mz_views.id = comments.id`
q := mockQueryBuilder(b, predicate, "")
ir := sqlmock.NewRows([]string{"id", "name", "schema_name", "database_name", "owner_name", "privileges"}).
AddRow("u1", "view", "schema", "database", "joe", defaultPrivilege)
mock.ExpectQuery(q).WillReturnRows(ir)
}
func MockNetworkPolicyScan(mock sqlmock.Sqlmock, predicate string) {
b := `
WITH policy AS \(
SELECT
mz_network_policies\.id,
mz_network_policies\.name AS policy_name,
comments\.comment AS comment,
mz_roles\.name AS owner_name,
mz_network_policies\.privileges
FROM mz_internal\.mz_network_policies
JOIN mz_roles
ON mz_network_policies\.owner_id = mz_roles\.id
LEFT JOIN \(
SELECT id, comment
FROM mz_internal\.mz_comments
WHERE object_type = 'network-policy'
\) comments
ON mz_network_policies\.id = comments\.id
\),
rules AS \(
SELECT
policy_id,
jsonb_agg\(
jsonb_build_object\(
'rule_name', name,
'rule_action', action,
'rule_direction', direction,
'rule_address', address
\)
\) as rules
FROM mz_internal\.mz_network_policy_rules
GROUP BY policy_id
\)
SELECT
policy\.\*,
COALESCE\(rules\.rules, '\[\]'::json\) as rules
FROM policy
LEFT JOIN rules
ON policy\.id = rules\.policy_id`
q := mockQueryBuilder(b, predicate, "")
ir := mock.NewRows([]string{
"id",
"policy_name",
"comment",
"owner_name",
"privileges",
"rules",
}).AddRow(
"u1",
"office_policy",
"Network policy for office locations",
"mz_system",
pq.StringArray{"s1=U/s1"},
`[{"rule_name":"minnesota","rule_action":"allow","rule_direction":"ingress","rule_address":"2.3.4.5/32"},
{"rule_name":"new_york","rule_action":"allow","rule_direction":"ingress","rule_address":"1.2.3.4/28"}]`,
)
mock.ExpectQuery(q).WillReturnRows(ir)
}