-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_insert.sql
More file actions
699 lines (627 loc) · 21 KB
/
Copy pathdb_insert.sql
File metadata and controls
699 lines (627 loc) · 21 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
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
-- heroku psql -a closer-temp < db_insert.sql;
-- populate database from temp tables;
-- 1. instruments;
\qecho '1. instruments';
INSERT INTO instruments (agency, version, prefix, Label, study, created_at, updated_at, slug)
(select 'www.closer.ac.uk',
'1.0',
a.Label,
a.Label,
'Study',
current_timestamp,
current_timestamp,
a.Label
from temp_sequence a
where a.Parent_Name is Null);
-- 2. cc_loops;
\qecho '11. cc_loops';
INSERT INTO cc_loops (Label, start_val, end_val, loop_while, loop_var, parent_type, branch, position, created_at, updated_at, instrument_id)
(select a.Label,
a.Start_Value,
a.End_Value,
a.Loop_While,
a.Variable,
a.Parent_type,
a.Branch,
a.Position,
current_timestamp,
current_timestamp,
b.id
from temp_loop a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
-- 3. cc_sequences;
\qecho '2. cc_sequences';
INSERT INTO cc_sequences (instrument_id, created_at, updated_at)
(select id,
current_timestamp,
current_timestamp
from instruments i
cross join temp_sequence temp
where i.prefix = temp.Label
and temp.Parent_name is Null);
INSERT INTO cc_sequences (instrument_id, created_at, updated_at, Label, parent_id, parent_type, position, branch)
(select b.id,
current_timestamp,
current_timestamp,
b.prefix,
a.id,
'CcSequence',
1,
0
from cc_sequences a
left join instruments b on b.id = a.instrument_id
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.Label is null);
INSERT INTO cc_sequences (instrument_id, created_at, updated_at, Label, parent_id, parent_type, position, branch)
(select b.instrument_id,
current_timestamp,
current_timestamp,
a.Label,
b.id,
a.Parent_type,
a.position,
0
from temp_sequence a
cross join cc_sequences b
cross join temp_sequence temp
where b.Label= temp.Label
and temp.Parent_name is Null
and a.Parent_Name = temp.Label
);
INSERT INTO cc_sequences (instrument_id, created_at, updated_at, Label, parent_id, parent_type, position, branch)
(select b.instrument_id,
current_timestamp,
current_timestamp,
a.Label,
b.id,
a.Parent_type,
a.position,
0
from temp_sequence a
cross join cc_sequences b
cross join temp_sequence temp
where b.Label= a.Parent_name
and temp.Parent_name is Null
and a.Parent_Name != temp.Label );
INSERT INTO cc_sequences (instrument_id, created_at, updated_at, Label, parent_id, parent_type, position, branch)
(select b.instrument_id,
current_timestamp,
current_timestamp,
a.Label,
b.id,
a.Parent_type,
a.position,
0
from temp_sequence a
cross join cc_loops b
cross join temp_sequence temp
where b.Label= a.Parent_name
and temp.Parent_name is Null
and a.Parent_Name != temp.Label );
SELECT pg_sleep(2);
-- 4. response_domain_numerics;
\qecho '3. response_domain_numerics';
INSERT INTO response_domain_numerics (numeric_type, Label, min, max, created_at, updated_at, instrument_id, response_domain_type)
(select a.Type2,
a.Label,
a.Min,
a.Max,
current_timestamp,
current_timestamp,
b.id,
'ResponseDomainNumeric'
from temp_response a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.type = 'Numeric');
SELECT pg_sleep(2);
-- 5. response_domain_texts;
\qecho '4. response_domain_texts';
INSERT INTO response_domain_texts (Label, maxlen, created_at, updated_at, instrument_id, response_domain_type)
(select a.Label,
a.max,
current_timestamp,
current_timestamp,
b.id,
'ResponseDomainText'
from temp_response a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.type = 'Text');
SELECT pg_sleep(2);
-- 6. response_domain_datetimes;
\qecho '5. response_domain_datetimes';
INSERT INTO response_domain_datetimes (datetime_type, Label, Format, created_at, updated_at, instrument_id, response_domain_type)
(select a.type2,
a.Label,
a.Format,
current_timestamp,
current_timestamp,
b.id,
'ResponseDomainDatetime'
from temp_response a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.type = 'Date');
SELECT pg_sleep(2);
-- 7. code_lists;
\qecho '6. code_lists';
INSERT INTO code_lists (Label, created_at, updated_at, instrument_id)
(select distinct a.Label,
current_timestamp,
current_timestamp,
b.id
from temp_codelist a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
-- 8. response_domain_codes;
\qecho '7. response_domain_codes';
INSERT INTO response_domain_codes (code_list_id, created_at, updated_at, response_domain_type, instrument_id, min_responses, max_responses)
(select a.id,
current_timestamp,
current_timestamp,
'ResponseDomainCode',
b.id,
c.min_responses,
c.max_responses
from code_lists a
join temp_question_item c on a.Label = c.Response
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
union
select a.id,
current_timestamp,
current_timestamp,
'ResponseDomainCode',
b.id,
c.Horizontal_min_responses,
c.Horizontal_max_responses
from code_lists a
join temp_question_grid c on a.Label = c.Response_domain
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
union
select a.id,
current_timestamp,
current_timestamp,
'ResponseDomainCode',
b.id,
c.Vertical_min_responses,
c.Vertical_max_responses
from code_lists a
join temp_question_grid c on a.Label = c.Vertical_Codelist_Name
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
union
select a.id,
current_timestamp,
current_timestamp,
'ResponseDomainCode',
b.id,
1,
1
from code_lists a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.Label = 'cs_dash');
SELECT pg_sleep(2);
-- 8. categories;
\qecho '8. categories';
INSERT INTO categories (Label, created_at, updated_at, instrument_id)
(select distinct a.category,
current_timestamp,
current_timestamp,
b.id
from temp_codelist a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.category is not null);
SELECT pg_sleep(2);
-- 10. codes;
\qecho '9. codes';
INSERT INTO codes (value, "order", code_list_id, category_id, created_at, updated_at, instrument_id)
(select a.Code_Value as value,
a.Code_Order as "order",
b.id as code_lists_id,
c.id as category_id,
current_timestamp,
current_timestamp,
c.instrument_id
from temp_codelist a
left join code_lists b on a.Label = b.Label and b.instrument_id = (select id from instruments cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null)
left join categories c on a.category = c.Label and c.instrument_id = (select id from instruments cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null)
where c.id is not null);
SELECT pg_sleep(2);
-- 11. cc_conditions;
\qecho '10. cc_conditions';
INSERT INTO cc_conditions (instrument_id, Label, literal, logic, parent_id, parent_type, position, branch, created_at, updated_at)
(select b.id,
a.Label,
a.Literal,
a.Logic,
d.id,
a.Parent_type,
a.Position,
a.Branch,
current_timestamp,
current_timestamp
from temp_condition a
cross join instruments b
left join cc_sequences d on a.Parent_name = d.Label and d.instrument_id = b.id
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
-- 12. instructions;
\qecho '12. instructions';
INSERT INTO instructions (text, created_at, updated_at, instrument_id)
(select distinct a.instructions,
current_timestamp,
current_timestamp,
b.id
from temp_question_item a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.instructions is not null);
INSERT INTO instructions (text, created_at, updated_at, instrument_id)
(select distinct a.instructions,
current_timestamp,
current_timestamp,
b.id
from temp_question_grid a
cross join instruments b
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
and a.instructions is not null);
SELECT pg_sleep(2);
-- 13. question_items;
\qecho '13. question_items';
INSERT INTO question_items (Label, literal, instruction_id, created_at, updated_at, instrument_id, question_type)
(select a.Label,
a.Literal,
b.id,
current_timestamp,
current_timestamp,
c.id,
'QuestionItem'
from temp_question_item a
cross join instruments c
left join instructions b on a.instructions = b.text and b.instrument_id = c.id
cross join temp_sequence temp
where a.rd_order = 1
and c.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
-- 14. question_grids;
\qecho '14. question_grids';
INSERT INTO question_grids (label, literal, instruction_id, horizontal_code_list_id, vertical_code_list_id, created_at, updated_at, instrument_id, question_type)
(select a.Label,
a.Literal,
b.id,
h.id,
v.id,
current_timestamp,
current_timestamp,
c.id,
'QuestionGrid'
from temp_question_grid a
cross join instruments c
left join instructions b on a.instructions = b.text and b.instrument_id = c.id
left join code_lists h on h.label = 'cs_dash'
left join code_lists v on a.vertical_codelist_name = v.label
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
and h.id is not null
and v.id is not null);
SELECT pg_sleep(2);
-- 15. cc_questions;
\qecho '15. cc_questions';
INSERT INTO response_units (Label, created_at, updated_at, instrument_id)
(select distinct
a.Interviewee,
current_timestamp,
current_timestamp,
c.id
from (select distinct Interviewee from temp_question_item
UNION
select distinct Interviewee from temp_question_grid) a
cross join instruments c
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
INSERT INTO cc_questions (instrument_id, question_id, question_type, response_unit_id, created_at, updated_at, Label, parent_id, parent_type, position, branch)
(select c.id,
b.id as question_id,
'QuestionItem',
d.id,
current_timestamp,
current_timestamp,
replace(a.Label, 'qi_', 'qc_'),
f.id,
a.Parent_Type,
a.Position,
a.Branch
from temp_question_item a
cross join instruments c
left join question_items b on a.Label = b.Label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_sequences f on a.Parent_Name = f.Label and f.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
UNION
select c.id,
b.id as question_id,
'QuestionItem',
d.id,
current_timestamp,
current_timestamp,
replace(a.Label, 'qi_', 'qc_'),
g.id,
a.Parent_Type,
a.Position,
a.Branch
from temp_question_item a
cross join instruments c
left join question_items b on a.Label = b.Label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_conditions g on a.Parent_Name = g.Label and g.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
UNION
select c.id,
b.id as question_id,
'QuestionItem',
d.id,
current_timestamp,
current_timestamp,
replace(a.Label, 'qi_', 'qc_'),
h.id,
a.Parent_Type,
a.Position,
a.Branch
from temp_question_item a
cross join instruments c
left join question_items b on a.Label = b.Label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_loops h on a.Parent_Name = h.Label and h.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
INSERT INTO cc_questions (instrument_id, question_id, question_type, response_unit_id, created_at, updated_at, label, parent_id, parent_type, position, branch)
(select c.id,
b.id as question_id,
'QuestionGrid',
d.id,
current_timestamp,
current_timestamp,
replace(a.label, 'qg_', 'qc_'),
f.id,
a.parent_type,
a.position,
a.branch
from temp_question_grid a
cross join instruments c
left join question_grids b on a.label = b.label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_sequences f on a.Parent_Name = f.label and f.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
and b.id is not null
UNION
select c.id,
b.id as question_id,
'QuestionGrid',
d.id,
current_timestamp,
current_timestamp,
replace(a.label, 'qg_', 'qc_'),
g.id,
a.parent_type,
a.position,
a.branch
from temp_question_grid a
cross join instruments c
left join question_grids b on a.label = b.label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_conditions g on a.Parent_Name = g.label and g.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
and b.id is not null
UNION
select c.id,
b.id as question_id,
'QuestionGrid',
d.id,
current_timestamp,
current_timestamp,
replace(a.label, 'qg_', 'qc_'),
h.id,
a.parent_type,
a.position,
a.branch
from temp_question_grid a
cross join instruments c
left join question_grids b on a.label = b.label and b.instrument_id = c.id
left join response_units d on c.id = d.instrument_id and d.Label = a.Interviewee
join cc_loops h on a.Parent_Name = h.label and h.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
and b.id is not null
);
SELECT pg_sleep(2);
-- 16. rds_qs
\qecho '16. rds_qs';
INSERT INTO rds_qs (instrument_id, question_id, question_type, code_id, response_domain_id, response_domain_type, created_at, updated_at, rd_order)
(
select ccq.instrument_id, ccq.question_id, ccq.question_type, f.id, b.id, b.response_domain_type, current_timestamp, current_timestamp, a.rd_order
from temp_question_item a
cross join instruments
join cc_questions ccq on replace(a.Label, 'qi_', 'qc_') = ccq.Label and ccq.instrument_id = instruments.id
join code_lists f on a.Response = f.Label and f.instrument_id = instruments.id
join response_domain_codes b on f.id = b.code_list_id and b.instrument_id = instruments.id
cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null
union
select ccq.instrument_id, ccq.question_id, ccq.question_type, null, c.id, c.response_domain_type, current_timestamp, current_timestamp, a.rd_order
from temp_question_item a
cross join instruments
join cc_questions ccq on replace(a.Label, 'qi_', 'qc_') = ccq.Label and ccq.instrument_id = instruments.id
join response_domain_datetimes c on a.response = c.Label and c.instrument_id = instruments.id
cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null
union
select ccq.instrument_id, ccq.question_id, ccq.question_type, null, d.id, d.response_domain_type, current_timestamp, current_timestamp, a.rd_order
from temp_question_item a
cross join instruments
join cc_questions ccq on replace(a.Label, 'qi_', 'qc_') = ccq.Label and ccq.instrument_id = instruments.id
join response_domain_numerics d on a.response = d.Label and d.instrument_id = instruments.id
cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null
union
select ccq.instrument_id, ccq.question_id, ccq.question_type, null, e.id, e.response_domain_type, current_timestamp, current_timestamp, a.rd_order
from temp_question_item a
cross join instruments
join cc_questions ccq on replace(a.Label, 'qi_', 'qc_') = ccq.Label and ccq.instrument_id = instruments.id
join response_domain_texts e on a.response = e.Label and e.instrument_id = instruments.id
cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null
);
SELECT pg_sleep(2);
INSERT INTO rds_qs (instrument_id, question_id, question_type, code_id, response_domain_id, response_domain_type, created_at, updated_at, rd_order)
(
select ccq.instrument_id, ccq.question_id, ccq.question_type, h.id, b.id, b.response_domain_type, current_timestamp, current_timestamp, 1
from temp_question_grid a
cross join instruments
join cc_questions ccq on replace(a.label, 'qg_', 'qc_') = ccq.label and ccq.instrument_id = instruments.id
join code_lists h on a.response_domain = h.label and h.instrument_id = instruments.id
join response_domain_codes b on h.id = b.code_list_id and b.instrument_id = instruments.id
cross join temp_sequence temp
where instruments.prefix = temp.Label
and temp.Parent_name is Null);
-- 17. cc_statements;
\qecho '17. cc_statements';
INSERT INTO cc_statements (label, literal, position, branch, parent_id, parent_type, created_at, updated_at, instrument_id)
(select distinct a.Label,
a.Literal,
a.Position,
a.Branch,
d.id,
a.Parent_Type,
current_timestamp,
current_timestamp,
c.id
from temp_statement a
cross join instruments c
join cc_sequences d on a.Parent_Name = d.Label and d.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null
union
select distinct a.Label,
a.Literal,
a.Position,
a.Branch,
g.id,
a.Parent_Type,
current_timestamp,
current_timestamp,
c.id
from temp_statement a
cross join instruments c
join cc_conditions g on a.Parent_Name = g.label and g.instrument_id = c.id
cross join temp_sequence temp
where c.prefix = temp.Label
and temp.Parent_name is Null);
SELECT pg_sleep(2);
-- 18. update parent_id for cc_loops, cc_conditions;
\qecho '18. update parent_id for cc_loops, cc_conditions';
with t as (
select old.id as row_id,
COALESCE(s.id, c.id, l.id) as parent_id,
a.Parent_Type as parent_type,
a.Position as position,
current_timestamp as updated_at
from cc_loops old
join temp_loop a on a.Label = old.Label
cross join instruments b
left join cc_sequences s on a.Parent_Name = s.Label and s.instrument_id = b.id
left join cc_conditions c on a.Parent_Name = c.Label and c.instrument_id = b.id
left join cc_loops l on a.Parent_Name = l.Label and l.instrument_id = b.id
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
)
update cc_loops
set parent_id = t.parent_id,
parent_type = t.parent_type,
position = t.position,
updated_at = t.updated_at
from t
where id = t.row_id;
with t as (
select old.id as row_id,
COALESCE(s.id, c.id, l.id) as parent_id,
a.parent_type as parent_type,
a.position as position,
current_timestamp as updated_at
from cc_conditions old
join temp_condition a on a.Label = old.Label
cross join instruments b
left join cc_sequences s on a.Parent_Name = s.Label and s.instrument_id = b.id
left join cc_conditions c on a.Parent_Name = c.Label and c.instrument_id = b.id
left join cc_loops l on a.Parent_Name = l.Label and l.instrument_id = b.id
cross join temp_sequence temp
where b.prefix = temp.Label
and temp.Parent_name is Null
)
update cc_conditions
set parent_id = t.parent_id,
parent_type = t.parent_type,
position = t.position,
updated_at = t.updated_at
from t
where id = t.row_id;