forked from r3-team/r3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
641 lines (573 loc) · 14.5 KB
/
schema.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
package schema
import (
"database/sql"
"fmt"
"r3/db"
"github.com/gofrs/uuid"
"github.com/jackc/pgx/v5"
)
// checks the given ID
// if nil, it is overwritten with a new one
// if not nil, it is checked whether the ID is known already
// returns whether the ID was already known
func CheckCreateId_tx(tx pgx.Tx, id *uuid.UUID, relName string, pkName string) (bool, error) {
var err error
if *id == uuid.Nil {
*id, err = uuid.NewV4()
return false, err
}
var known bool
err = tx.QueryRow(db.Ctx, fmt.Sprintf(`
SELECT EXISTS(SELECT * FROM app.%s WHERE "%s" = $1)
`, relName, pkName), id).Scan(&known)
return known, err
}
// attribute checks
func IsContentFiles(content string) bool {
return content == "files"
}
func IsContentNumeric(content string) bool {
return content == "numeric"
}
func IsContentRelationship(content string) bool {
return content == "1:1" || content == "n:1"
}
func IsContentRelationship11(content string) bool {
return content == "1:1"
}
func IsContentText(content string) bool {
return content == "varchar" || content == "text"
}
// fully validates module dependencies
func ValidateDependency_tx(tx pgx.Tx, moduleId uuid.UUID) error {
var cnt int
var name1, name2 sql.NullString
// check parent module without dependency
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(name, ', ')
FROM app.module
WHERE id = (
SELECT parent_id
FROM app.module
WHERE id = $1
)
AND id NOT IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $2
)
`, moduleId, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, is assigned to independent module '%s'",
name1.String)
}
// check attribute relationships with external relations
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(CONCAT(r.name, '.', a.name), ', ')
FROM app.attribute AS a
INNER JOIN app.relation AS r
ON r.id = a.relation_id
INNER JOIN app.module AS m
ON m.id = r.module_id
AND m.id = $1
-- dependency
WHERE a.relationship_id NOT IN (
SELECT id
FROM app.relation
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, attribute(s) '%s' with relationships to independent module(s)",
name1.String)
}
// check query relation access
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(COALESCE(f.name,lf.name), ', ')
FROM app.query AS q
LEFT JOIN app.form AS f ON f.id = q.form_id -- query for form
LEFT JOIN app.field AS l ON l.id = q.field_id -- query for list/data field
LEFT JOIN app.form AS lf ON lf.id = l.form_id -- form of list/data field
INNER JOIN app.module AS m
ON m.id = $1
AND (
f.module_id = m.id
OR lf.module_id = m.id
)
-- dependency
AND (
q.relation_id IS NOT NULL
AND q.relation_id NOT IN (
SELECT id
FROM app.relation
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, form(s) '%s' with relations to independent module(s)",
name1.String)
}
// check field access to external forms
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(f3.name, ', '), STRING_AGG(f1.name, ', ')
FROM app.open_form AS of
INNER JOIN app.form AS f1 ON f1.id = of.form_id_open -- opened form
INNER JOIN app.field AS f2 ON f2.id = of.field_id -- field that opens
INNER JOIN app.form AS f3 ON f3.id = f2.form_id -- form of field that opens
INNER JOIN app.module AS m
ON m.id = f3.module_id
AND m.id = $1
-- dependency
WHERE f1.id NOT IN (
SELECT id
FROM app.form
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
`, moduleId).Scan(&cnt, &name1, &name2); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, fields(s) on form(s) '%s' opening form(s) '%s' from independent module(s)",
name1.String, name2.String)
}
// check collection access to external forms
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(f.name, ', ')
FROM app.open_form AS of
INNER JOIN app.form AS f ON f.id = of.form_id_open
INNER JOIN app.collection_consumer AS cc ON cc.id = of.collection_consumer_id
INNER JOIN app.collection AS c ON c.id = cc.collection_id
INNER JOIN app.module AS m
ON m.id = c.module_id
AND m.id = $1
-- dependency
WHERE f.id NOT IN (
SELECT id
FROM app.form
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, collection(s) accessing form(s) '%s' from independent module(s)",
name1.String)
}
// check relation policy access to external roles
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(r.name, ', ')
FROM app.relation_policy AS rp
INNER JOIN app.role AS r ON r.id = rp.role_id
INNER JOIN app.relation AS t ON t.id = rp.relation_id
INNER JOIN app.module AS m ON m.id = t.module_id
AND m.id = $1
-- dependency
WHERE r.id NOT IN (
SELECT id
FROM app.role
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, relation policies accessing role(s) '%s' from independent module(s)",
name1.String)
}
// check menu access to external forms
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(f.name, ', ')
FROM app.menu AS h
INNER JOIN app.form AS f ON f.id = h.form_id
INNER JOIN app.module AS m
ON m.id = h.module_id
AND m.id = $1
-- dependency
WHERE f.id NOT IN (
SELECT id
FROM app.form
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, menu(s) accessing form(s) '%s' from independent module(s)",
name1.String)
}
// check access to external icons
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*)
FROM app.icon
WHERE id IN (
-- module icons
SELECT icon_id
FROM app.module
WHERE id = $1
UNION
-- form icons
SELECT icon_id
FROM app.form
WHERE module_id = $2
UNION
-- field icons
SELECT icon_id
FROM app.field
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $3
)
UNION
-- menu icons
SELECT icon_id
FROM app.menu
WHERE module_id = $4
)
-- dependency
AND id NOT IN (
SELECT id
FROM app.icon
WHERE module_id = $5
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $6
)
)
`, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId).Scan(&cnt); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, accessing %d icons(s) from independent module(s), check module, menu & button field icons", cnt)
}
// check PG function access to external pgFunctions/modules/relations/attributes
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*)
FROM app.module
WHERE id IN (
-- dependent on PG functions
SELECT module_id
FROM app.pg_function
WHERE id IN (
SELECT d.pg_function_id_on
FROM app.pg_function_depends AS d
INNER JOIN app.pg_function AS f ON f.id = d.pg_function_id
WHERE f.module_id = $1
)
UNION
-- dependent on modules
SELECT id
FROM app.module
WHERE id IN (
SELECT d.module_id_on
FROM app.pg_function_depends AS d
INNER JOIN app.pg_function AS f ON f.id = d.pg_function_id
WHERE f.module_id = $2
)
UNION
-- dependent on relations
SELECT module_id
FROM app.relation
WHERE id IN (
SELECT d.relation_id_on
FROM app.pg_function_depends AS d
INNER JOIN app.pg_function AS f ON f.id = d.pg_function_id
WHERE f.module_id = $3
)
OR id IN (
-- dependent on attributes
SELECT relation_id
FROM app.attribute
WHERE id IN (
SELECT d.attribute_id_on
FROM app.pg_function_depends AS d
INNER JOIN app.pg_function AS f ON f.id = d.pg_function_id
WHERE f.module_id = $4
)
)
)
-- dependency
AND id <> $5
AND id NOT IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $6
)
`, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId).Scan(&cnt); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, backend functions accessing entities from independent module(s)")
}
// check JS function access to external pgFunctions/jsFunctions/forms/fields/roles
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*)
FROM app.module
WHERE id IN (
-- dependent on PG functions
SELECT module_id
FROM app.pg_function
WHERE id IN (
SELECT d.pg_function_id_on
FROM app.js_function_depends AS d
INNER JOIN app.js_function AS f ON f.id = d.js_function_id
WHERE f.module_id = $1
)
UNION
-- dependent on JS functions
SELECT module_id
FROM app.js_function
WHERE id IN (
SELECT d.js_function_id_on
FROM app.js_function_depends AS d
INNER JOIN app.js_function AS f ON f.id = d.js_function_id
WHERE f.module_id = $2
)
UNION
-- dependent on forms
SELECT module_id
FROM app.form
WHERE id IN (
SELECT d.form_id_on
FROM app.js_function_depends AS d
INNER JOIN app.js_function AS f ON f.id = d.js_function_id
WHERE f.module_id = $3
)
OR id IN (
-- dependent on fields
SELECT form_id
FROM app.field
WHERE id IN (
SELECT d.field_id_on
FROM app.js_function_depends AS d
INNER JOIN app.js_function AS f ON f.id = d.js_function_id
WHERE f.module_id = $4
)
)
UNION
-- dependent on roles
SELECT module_id
FROM app.role
WHERE id IN (
SELECT d.role_id_on
FROM app.js_function_depends AS d
INNER JOIN app.js_function AS f ON f.id = d.js_function_id
WHERE f.module_id = $5
)
)
-- dependency
AND id <> $6
AND id NOT IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $7
)
`, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId).Scan(&cnt); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, frontend functions accessing entities from independent module(s)")
}
// check field (button/data) & form function access to external JS functions
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*)
FROM app.module
WHERE id IN (
-- dependent on JS functions
SELECT module_id
FROM app.js_function
WHERE id IN (
SELECT fb.js_function_id
FROM app.field_button AS fb
JOIN app.field AS f ON f.id = fb.field_id
WHERE f.form_id IN (
SELECT id
FROM app.form
WHERE module_id = $1
)
UNION
SELECT fd.js_function_id
FROM app.field_data AS fd
JOIN app.field AS f ON f.id = fd.field_id
WHERE f.form_id IN (
SELECT id
FROM app.form
WHERE module_id = $2
)
UNION
SELECT js_function_id
FROM app.form_function
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $3
)
)
)
-- dependency
AND id <> $4
AND id NOT IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $5
)
`, moduleId, moduleId, moduleId, moduleId, moduleId).Scan(&cnt); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, fields/forms accessing frontend functions from independent module(s)")
}
// check role membership inside external parent roles
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*), STRING_AGG(r.name, ', ')
FROM app.role AS r
INNER JOIN app.module AS m
ON m.id = r.module_id
AND m.id = $1
WHERE r.id IN (
SELECT role_id
FROM app.role_child
WHERE role_id_child NOT IN (
SELECT id
FROM app.role
WHERE module_id = m.id
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = m.id
)
)
)
`, moduleId).Scan(&cnt, &name1); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, role(s) '%s' is/are member(s) of role(s) from independent module(s)",
name1.String)
}
// check data presets without dependency
if err := tx.QueryRow(db.Ctx, `
SELECT COUNT(*)
FROM app.preset
WHERE id IN (
-- presets from query filters (and their sub query filters)
SELECT id
FROM app.preset
WHERE id = ANY(
app.get_preset_ids_inside_queries((
SELECT ARRAY_AGG(id)
FROM app.query
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $1
)
OR field_id IN (
SELECT id
FROM app.field
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $2
)
)
OR column_id IN (
SELECT id
FROM app.column
WHERE field_id IN (
SELECT id
FROM app.field
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $3
)
)
)
OR api_id IN (
SELECT id
FROM app.api
WHERE module_id = $4
)
OR collection_id IN (
SELECT id
FROM app.collection
WHERE module_id = $5
)
))
)
UNION
-- presets from field default values
SELECT preset_id
FROM app.field_data_relationship_preset
WHERE field_id IN (
SELECT id
FROM app.field
WHERE form_id IN (
SELECT id
FROM app.form
WHERE module_id = $6
)
)
)
AND id NOT IN (
SELECT id
FROM app.preset
WHERE relation_id IN (
SELECT id
FROM app.relation
WHERE module_id = $7
OR module_id IN (
SELECT module_id_on
FROM app.module_depends
WHERE module_id = $8
)
)
)
`, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId, moduleId).Scan(&cnt); err != nil {
return err
}
if cnt != 0 {
return fmt.Errorf("dependency check failed, %d presets (either as field default or inside filters) referenced from independent module(s)",
cnt)
}
return nil
}