-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueries.sql
1367 lines (1262 loc) · 49.2 KB
/
queries.sql
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Nice view of collection -> unit -> file
SELECT c.id AS "c.id",
c.properties -> 'film_date' AS "film_date",
c.properties -> 'kmedia_id' AS "kmedia",
c.type_id,
ccu.name AS "part",
cu.id AS "cu.id",
cu.type_id,
cui.name,
cu.properties -> 'duration' AS "duration",
cu.properties -> 'kmedia_id' AS "kmedia",
f.id AS "f.id",
f.name
FROM collections c
INNER JOIN collections_content_units ccu ON c.id = ccu.collection_id
INNER JOIN content_units cu ON ccu.content_unit_id = cu.id
INNER JOIN content_unit_i18n cui ON cu.id = cui.content_unit_id AND cui.language = 'en'
LEFT JOIN files f ON cu.id = f.content_unit_id AND f.language = 'en'
ORDER BY c.properties -> 'film_date' DESC, ccu.name :: INT;
-- Sources: top level collections
SELECT a.id, a.code, s.id, s.name
FROM authors a
INNER JOIN authors_sources AS "as" ON a.id = "as".author_id
INNER JOIN sources s ON "as".source_id = s.id AND s.parent_id IS NULL
ORDER BY a.id;
-- Sources with their i18n
SELECT s.id, s.uid, s.pattern, s.name, si.language, si.name
FROM sources s
INNER JOIN source_i18n si ON s.id = si.source_id
ORDER BY s.id;
-- Delete all sources
-- DELETE FROM authors_sources;
-- DELETE FROM author_i18n;
-- DELETE FROM authors;
-- DELETE FROM source_i18n;
-- DELETE FROM sources;
WITH RECURSIVE rec_sources AS (SELECT s.*
FROM sources s
WHERE s.id = 3715
UNION
SELECT s.*
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id)
SELECT string_agg(name, '/')
FROM rec_sources
LIMIT 3;
WITH RECURSIVE rec_sources AS (SELECT s.id, concat(a.code, '/', s.name) path
FROM sources s
INNER JOIN authors_sources x ON s.id = x.source_id
INNER JOIN authors a ON x.author_id = a.id
WHERE s.parent_id IS NULL
UNION
SELECT s.id, concat(rs.path, '/', s.name)
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id)
SELECT *
FROM rec_sources;
-- sources with named path
COPY (
WITH RECURSIVE rec_sources AS (
SELECT s.id, s.pattern, concat(a.code, '/', s.name) path
FROM sources s
INNER JOIN authors_sources x ON s.id = x.source_id
INNER JOIN authors a ON x.author_id = a.id
WHERE s.parent_id IS NULL
UNION
SELECT s.id, s.pattern, concat(rs.path, '/', s.name)
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id
)
SELECT *
FROM rec_sources
WHERE pattern IS NOT NULL
ORDER BY pattern
) TO '/var/lib/postgres/data/mdb_sources_patterns.csv' (
FORMAT CSV);
-- kmedia patterns -> catalogs
COPY (
SELECT p.id, p.pattern, p.lang, c.id, c.name
FROM catalogs c
INNER JOIN catalogs_container_description_patterns cp ON c.id = cp.catalog_id
INNER JOIN container_description_patterns p ON cp.container_description_pattern_id = p.id
ORDER BY p.pattern
) TO '/var/lib/postgres/data/kmedia_patterns_catalogs.csv' (
FORMAT CSV);
-- kmedia catalogs
WITH RECURSIVE rec_catalogs AS (SELECT c.id,
c.name :: TEXT path,
(SELECT DISTINCT cdp.pattern
FROM catalogs_container_description_patterns ccdp
INNER JOIN container_creation_patterns cdp
ON ccdp.container_description_pattern_id = cdp.id
WHERE ccdp.catalog_id = c.id
LIMIT 1) "pattern",
-- (select count(distinct container_id) from catalogs_containers where catalog_id = c.id) as containers
1 depth
FROM catalogs c
WHERE c.id = 3672
UNION
SELECT c.id,
concat(rc.path, '/', c.name),
(SELECT DISTINCT cdp.pattern
FROM catalogs_container_description_patterns ccdp
INNER JOIN container_creation_patterns cdp ON ccdp.container_description_pattern_id = cdp.id
WHERE ccdp.catalog_id = c.id
LIMIT 1) "pattern",
-- (select count(distinct container_id) from catalogs_containers where catalog_id = c.id) as containers
rc.depth + 1
FROM catalogs c
INNER JOIN rec_catalogs rc ON c.parent_id = rc.id
WHERE rc.depth < 2)
SELECT *
FROM rec_catalogs
ORDER BY path;
-- kmedia catalogs with i18n
COPY (
SELECT c.id,
c.name,
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'ENG') "en.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'HEB') "he.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'RUS') "ru.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'SPA') "es.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'GER') "de.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'UKR') "ua.name",
(SELECT name
FROM catalog_descriptions
WHERE catalog_id = c.id
AND lang_id = 'CHN') "zh.name"
FROM catalogs c
WHERE c.parent_id = 3672
ORDER BY c.name
) TO '/var/lib/postgres/data/kmedia_tvshows.csv' (
FORMAT CSV);
WITH RECURSIVE rec_sources AS (SELECT s.id,
s.uid,
s.pattern,
s.parent_id,
s.position,
s.type_id,
coalesce((SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'en'),
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) "name",
coalesce((SELECT description
FROM source_i18n
WHERE source_id = s.id
AND language = 'en'),
(SELECT description
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) "description",
1 "depth"
FROM sources s
WHERE s.parent_id IS NULL
UNION
SELECT s.id,
s.uid,
s.pattern,
s.parent_id,
s.position,
s.type_id,
coalesce((SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'en'),
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) "name",
coalesce((SELECT description
FROM source_i18n
WHERE source_id = s.id
AND language = 'en'),
(SELECT description
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) "description",
depth + 1
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id
WHERE rs.depth < 2)
SELECT *
FROM rec_sources
ORDER BY depth, parent_id, position;
WITH RECURSIVE rec_tags AS (SELECT t.id,
t.uid,
t.pattern,
t.parent_id,
coalesce((SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'en'),
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'he')) "label",
1 "depth"
FROM tags t
WHERE t.parent_id IS NULL
UNION
SELECT t.id,
t.uid,
t.pattern,
t.parent_id,
coalesce((SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'en'),
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'he')) "label",
depth + 1
FROM tags t
INNER JOIN rec_tags rt ON t.parent_id = rt.id
WHERE rt.depth < 2)
SELECT *
FROM rec_tags
ORDER BY depth, parent_id, label;
WITH RECURSIVE ffo AS (SELECT file_id, min(operation_id) "o_id" FROM files_operations GROUP BY file_id),
rec_files AS (SELECT f.created_at, f.id, f.parent_id, f.name, f.size,
-- f.properties "fprops",
o.id "o_id", ot.name "op"
-- o.properties "oprops"
FROM files f
INNER JOIN ffo ON f.id = ffo.file_id
INNER JOIN operations o ON ffo.o_id = o.id
INNER JOIN operation_types ot ON o.type_id = ot.id
WHERE f.id = 353547
UNION
SELECT f.created_at, f.id, f.parent_id, f.name, f.size,
-- f.properties "fprops",
o.id "o_id", ot.name "op"
-- o.properties "oprops"
FROM files f
INNER JOIN rec_files rf ON f.parent_id = rf.id
INNER JOIN ffo ON f.id = ffo.file_id
INNER JOIN operations o ON ffo.o_id = o.id
INNER JOIN operation_types ot ON o.type_id = ot.id)
SELECT *
FROM rec_files;
-- find all ancestors of a file
WITH RECURSIVE rf AS (SELECT f.*
FROM files f
WHERE f.id = 353590
UNION
SELECT f.*
FROM files f
INNER JOIN rf ON f.id = rf.parent_id)
SELECT *
FROM rf
WHERE id != 353590;
-- find all descendants of a file
WITH RECURSIVE rf AS (SELECT f.*
FROM files f
WHERE f.id = 380600
UNION
SELECT f.*
FROM files f
INNER JOIN rf ON f.parent_id = rf.id)
SELECT id, parent_id, name, size, sha1,
-- created_at,
content_unit_id, properties ->> 'duration'
FROM rf;
-- update files set content_unit_id=26031 where parent_id=380610;
-- update files set content_unit_id=26032 where parent_id=380662;
-- update files set content_unit_id=26033 where parent_id=380667;
-- update files set content_unit_id=26034 where parent_id=380739;
-- update files set content_unit_id=26035 where parent_id=380791;
update content_units
set published = true
where id in (26031, 26032, 26033, 26034, 26035);
-- WITH RECURSIVE rf AS (
-- SELECT f.*
-- FROM files f
-- WHERE f.id = 360841
-- UNION
-- SELECT f.*
-- FROM files f INNER JOIN rf ON f.parent_id = rf.id
-- ) SELECT id, parent_id, content_unit_id, name
-- FROM rf
-- WHERE id != 360841 order by parent_id, id;
-- UPDATE files SET content_unit_id = 25378 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 360841 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25382 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 360896 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25383 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 360961 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25384 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 360975 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25385 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361036 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25386 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361100 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25387 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361155 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25388 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361265 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25389 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361282 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25390 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361340 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25391 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361405 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25392 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361483 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25393 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361638 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
--
-- UPDATE files SET content_unit_id = 25396 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361693 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25397 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361695 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
-- UPDATE files SET content_unit_id = 25398 WHERE id IN
-- (WITH RECURSIVE rf AS ( SELECT f.* FROM files f WHERE f.id = 361696 UNION SELECT f.* FROM files f INNER JOIN rf ON f.parent_id = rf.id ) SELECT id FROM rf);
SELECT cu.id, cu.properties ->> 'artifact_type'
FROM files f
INNER JOIN content_units cu ON f.content_unit_id = cu.id AND cu.properties ? 'artifact_type'
WHERE f.parent_id = 1;
UPDATE content_units
SET properties = properties - 'artifact_type'
WHERE id = 1;
select properties - 'artifact_type'
from content_units
limit 1;
copy (
WITH RECURSIVE rec_sources AS (
SELECT s.id, s.pattern, si.name :: text path
FROM sources s
INNER JOIN source_i18n si on s.id = si.source_id and si.language = 'en'
WHERE s.parent_id IS NULL
UNION
SELECT s.id, s.pattern, concat(rs.path, ' ', si.name)
FROM sources s
INNER JOIN source_i18n si on s.id = si.source_id and si.language = 'en'
INNER JOIN rec_sources rs ON s.parent_id = rs.id
)
SELECT *
FROM rec_sources
WHERE pattern IS NOT NULL
ORDER BY pattern)
to '/var/lib/postgres/data/titles.csv' (format CSV);
-- insert into collections (uid, type_id, properties) values
-- ('TYbA7WoZ', 4, '{"active": true, "pattern": "italy", "country": "Italy", "city": "Rome", "start_date": "2017-07-28", "end_date": "2017-07-30", "full_address": "SHG Hotel Antonella, Via Pontina, 00040 Pomezia RM, Italy" }');
--
-- insert into collection_i18n (collection_id, language, name) VALUES
-- (10796, 'ru', 'Переход');
DROP TABLE IF EXISTS file_mappings;
CREATE TABLE file_mappings (
sha1 CHAR(40),
k_id INT NULL,
k_cid INT NULL,
m_id BIGINT NULL,
m_cuid BIGINT NULL,
m_exists BOOLEAN
);
-- update content_units duration property
UPDATE content_units
SET properties = properties || jsonb_build_object('duration', b.duration)
FROM (SELECT content_unit_id, round(a) AS duration
FROM (SELECT content_unit_id,
avg((properties ->> 'duration') :: REAL) AS a,
stddev((properties ->> 'duration') :: REAL) AS s
FROM files
WHERE type IN ('audio', 'video')
AND content_unit_id IN (SELECT id FROM content_units WHERE properties ? 'duration' IS FALSE)
GROUP BY content_unit_id) AS t) AS b
WHERE id = b.content_unit_id;
WITH RECURSIVE rs AS (SELECT s.*
FROM sources s
WHERE s.id = 1754
UNION
SELECT s.*
FROM sources s
INNER JOIN rs ON s.parent_id = rs.id)
SELECT *
FROM rs;
-- kmedia congresses
WITH RECURSIVE rc AS (SELECT c.*
FROM catalogs c
WHERE c.id = 40
UNION
SELECT c.*
FROM catalogs c
INNER JOIN rc ON c.parent_id = rc.id)
SELECT count(cn.*)
FROM rc
INNER JOIN catalogs_containers cc ON rc.id = cc.catalog_id
INNER JOIN containers cn ON cc.container_id = cn.id;
-- all sources for translation (Dima Perkin)
COPY (
WITH RECURSIVE rec_sources AS (
SELECT s.id,
s.uid,
s.position,
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he') AS "he.name",
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'ru') AS "ru.name",
ARRAY [s.id] "path"
FROM sources s
WHERE s.parent_id IS NULL
UNION
SELECT s.id,
s.uid,
s.position,
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he') AS "he.name",
(SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'ru') AS "ru.name",
rs.path || s.id
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id
-- WHERE rs.depth < 2
)
SELECT id, uid, "he.name", "ru.name"
FROM rec_sources
ORDER BY path, position
) TO STDOUT WITH CSV HEADER;
-- all tags for translation
COPY (
WITH RECURSIVE rec_tags AS (
SELECT t.id,
t.uid,
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'he') AS "he.name",
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'ru') AS "ru.name",
ARRAY [t.id] "path"
FROM tags t
WHERE t.parent_id IS NULL
UNION
SELECT t.id,
t.uid,
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'he') AS "he.name",
(SELECT label
FROM tag_i18n
WHERE tag_id = t.id
AND language = 'ru') AS "ru.name",
rt.path || t.id
FROM tags t
INNER JOIN rec_tags rt ON t.parent_id = rt.id
)
SELECT *
FROM rec_tags
ORDER BY path
) TO STDOUT WITH CSV HEADER;
-- all sources for roza mappings
COPY (
WITH RECURSIVE rec_sources AS (
SELECT s.id,
s.position,
concat(ai.name, ', ', (SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) AS name,
ARRAY [s.id] "path"
FROM sources s
INNER JOIN authors_sources aas ON s.id = aas.source_id
INNER JOIN authors a ON a.id = aas.author_id
INNER JOIN author_i18n ai ON a.id = ai.author_id AND ai.language = 'he'
WHERE s.parent_id IS NULL
UNION
SELECT s.id,
s.position,
concat(rs.name, ', ', (SELECT name
FROM source_i18n
WHERE source_id = s.id
AND language = 'he')) AS name,
rs.path || s.id
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id
)
SELECT *
FROM rec_sources
ORDER BY path, position
) TO '/var/lib/postgres/data/all_sources_roza.csv' (
FORMAT CSV);
-- manual mapping of existing congresses in MDB
update collections
set properties = properties || '{"kmedia_id": 8024}'
where id = 10641;
update collections
set properties = properties || '{"kmedia_id": 8029}'
where id = 10642;
update collections
set properties = properties || '{"kmedia_id": 8027}'
where id = 10643;
update collections
set properties = properties || '{"kmedia_id": 8100}'
where id = 10644;
update collections
set properties = properties || '{"kmedia_id": 8084}'
where id = 10713;
update collections
set properties = properties || '{"kmedia_id": 8127}'
where id = 10813;
-- kmedia programs with their containers
copy (
WITH RECURSIVE rc AS (
SELECT c.*
FROM catalogs c
WHERE c.id = 3672
UNION
SELECT c.*
FROM catalogs c
INNER JOIN rc ON c.parent_id = rc.id
) SELECT rc.id, rc.parent_id, count(1), array_agg(cc.container_id)
FROM rc
INNER JOIN catalogs_containers cc ON rc.id = cc.catalog_id
GROUP BY rc.id, rc.parent_id
order by rc.parent_id, rc.id
) TO '/var/lib/postgres/data/programs_chapters2.csv' (
FORMAT CSV);
-- flatten parashat shavua
WITH RECURSIVE rc AS (SELECT c.*
FROM catalogs c
WHERE c.parent_id = 3624
UNION
SELECT c.*
FROM catalogs c
INNER JOIN rc ON c.parent_id = rc.id)
SELECT array_agg(cc.container_id)
FROM rc
inner join catalogs_containers cc on rc.id = cc.catalog_id;
WITH RECURSIVE rec_sources AS (SELECT s.id, concat(a.code, '/', s.name) path
FROM sources s
INNER JOIN authors_sources x ON s.id = x.source_id
INNER JOIN authors a ON x.author_id = a.id
WHERE s.parent_id IS NULL
UNION
SELECT s.id, concat(rs.path, '/', s.name)
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id)
SELECT *
FROM rec_sources;
update collections
set properties = properties || '{"kmedia_id": 7899}'
where id = 10651;
update collections
set properties = properties || '{"kmedia_id": 8159}'
where id = 10796;
-- unit original_language from historical send operations metadata
UPDATE content_units cu
SET properties = properties - 'original_language'
WHERE properties ->> 'original_language' = '';
UPDATE content_units cu
SET properties = properties || jsonb_build_object('original_language', tmp.lang)
FROM (SELECT DISTINCT ON (f.content_unit_id) f.content_unit_id AS cuid,
CASE
WHEN o.properties ->> 'language' = 'heb'
THEN 'he'
WHEN o.properties ->> 'language' = 'eng'
THEN 'en'
WHEN o.properties ->> 'language' = 'rus'
THEN 'ru'
WHEN o.properties ->> 'language' = 'mlt'
THEN 'zz'
ELSE 'xx' END AS lang
FROM operations o
INNER JOIN files_operations fo ON o.id = fo.operation_id AND o.type_id = 4
INNER JOIN files f ON fo.file_id = f.id AND f.content_unit_id IS NOT NULL
GROUP BY f.content_unit_id, o.id
ORDER BY f.content_unit_id, o.id DESC) AS tmp
WHERE cu.id = tmp.cuid;
-- cu missing name from historical send operations
SELECT cu.id, tmp.name
FROM content_units cu
LEFT JOIN content_unit_i18n cui ON cu.id = cui.content_unit_id
INNER JOIN (SELECT DISTINCT ON (f.content_unit_id) f.content_unit_id AS cuid,
o.properties ->> 'final_name' AS name
FROM operations o
INNER JOIN files_operations fo ON o.id = fo.operation_id AND o.type_id = 4
INNER JOIN files f ON fo.file_id = f.id AND f.content_unit_id IS NOT NULL
GROUP BY f.content_unit_id, o.id
ORDER BY f.content_unit_id, o.id DESC) AS tmp ON cu.id = tmp.cuid
WHERE cui.content_unit_id IS NULL;
-- add last update workflow id from send operation to unit
-- based on parsing the file name
UPDATE content_units cu
SET properties = properties || jsonb_build_object('workflow_id', tmp.wfid)
FROM (SELECT DISTINCT ON (cu.id) cu.id, cu.uid, (regexp_matches(f.name, '.*_([t|d][\d+]*)o\.mp4')) [ 1 ] AS wfid
FROM operations o
INNER JOIN files_operations fo ON o.id = fo.operation_id AND o.type_id = 4
INNER JOIN files f ON fo.file_id = f.id AND f.content_unit_id IS NOT NULL and f.name like '%o.mp4'
inner join content_units cu on f.content_unit_id = cu.id
GROUP BY cu.id, o.id, f.name
ORDER BY cu.id, o.id, f.name DESC) AS tmp
WHERE cu.id = tmp.id;
SELECT "content_units".*
FROM "content_units"
INNER JOIN content_units_sources cus ON id = cus.content_unit_id
WHERE (secure = 0 AND published IS TRUE)
AND "type_id" IN (11)
AND "cus"."source_id" IN
(1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813)
ORDER BY (properties->>'film_date') :: date desc, created_at desc
LIMIT 10;
SELECT "content_units".*
FROM "content_units"
INNER JOIN content_units_sources cus ON id = cus.content_unit_id
WHERE (secure = 0 AND published IS TRUE)
AND "type_id" IN (11)
AND "cus"."source_id" IN
(1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813)
group by content_units.id
ORDER BY (properties->>'film_date') :: date desc, created_at desc
LIMIT 10;
SELECT DISTINCT ON (id) *
FROM (SELECT "content_units".*
FROM "content_units"
INNER JOIN content_units_sources cus ON id = cus.content_unit_id
WHERE (secure = 0 AND published IS TRUE)
AND "type_id" IN (11)
AND "cus"."source_id" IN
(1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813)
ORDER BY (properties ->> 'film_date') :: DATE DESC, created_at DESC) AS t
LIMIT 10;
-- fix missing languages for files based on file name
update files
set language = 'he'
where content_unit_id = 35509
and name like 'heb_%';
update files
set language = 'en'
where content_unit_id = 35509
and name like 'eng_%';
update files
set language = 'ru'
where content_unit_id = 35509
and name like 'rus_%';
update files
set language = 'es'
where content_unit_id = 35509
and name like 'spa_%';
update files
set language = 'it'
where content_unit_id = 35509
and name like 'ita_%';
update files
set language = 'de'
where content_unit_id = 35509
and name like 'ger_%';
update files
set language = 'nl'
where content_unit_id = 35509
and name like 'dut_%';
update files
set language = 'fr'
where content_unit_id = 35509
and name like 'fre_%';
update files
set language = 'pt'
where content_unit_id = 35509
and name like 'por_%';
update files
set language = 'tr'
where content_unit_id = 35509
and name like 'trk_%';
update files
set language = 'pl'
where content_unit_id = 35509
and name like 'pol_%';
update files
set language = 'ar'
where content_unit_id = 35509
and name like 'arb_%';
update files
set language = 'hu'
where content_unit_id = 35509
and name like 'hun_%';
update files
set language = 'fi'
where content_unit_id = 35509
and name like 'fin_%';
update files
set language = 'lt'
where content_unit_id = 35509
and name like 'lit_%';
update files
set language = 'ja'
where content_unit_id = 35509
and name like 'jpn_%';
update files
set language = 'bg'
where content_unit_id = 35509
and name like 'bul_%';
update files
set language = 'ka'
where content_unit_id = 35509
and name like 'geo_%';
update files
set language = 'no'
where content_unit_id = 35509
and name like 'nor_%';
update files
set language = 'sv'
where content_unit_id = 35509
and name like 'swe_%';
update files
set language = 'hr'
where content_unit_id = 35509
and name like 'hrv_%';
update files
set language = 'zh'
where content_unit_id = 35509
and name like 'chn_%';
update files
set language = 'fa'
where content_unit_id = 35509
and name like 'far_%';
update files
set language = 'ro'
where content_unit_id = 35509
and name like 'ron_%';
update files
set language = 'hi'
where content_unit_id = 35509
and name like 'hin_%';
update files
set language = 'ua'
where content_unit_id = 35509
and name like 'ukr_%';
update files
set language = 'mk'
where content_unit_id = 35509
and name like 'mkd_%';
update files
set language = 'sl'
where content_unit_id = 35509
and name like 'slv_%';
update files
set language = 'lv'
where content_unit_id = 35509
and name like 'lav_%';
update files
set language = 'sk'
where content_unit_id = 35509
and name like 'slk_%';
update files
set language = 'cs'
where content_unit_id = 35509
and name like 'cze_%';
update files
set type = 'video',
mime_type = 'video/mp4'
where content_unit_id = 35509
and name like '%mp4';
update files
set type = 'audio',
mime_type = 'audio/mpeg'
where content_unit_id = 35509
and name like '%mp3';
update files
set type = 'text',
mime_type = 'application/msword'
where content_unit_id = 35509
and name ~ '\.docx?$';
WITH RECURSIVE rec_sources AS (SELECT s.id, s.uid, s.position, ARRAY [a.code, s.uid] "path"
FROM sources s
INNER JOIN authors_sources aas ON s.id = aas.source_id
INNER JOIN authors a ON a.id = aas.author_id
UNION
SELECT s.id, s.uid, s.position, rs.path || s.uid
FROM sources s
INNER JOIN rec_sources rs ON s.parent_id = rs.id)
SELECT cus.content_unit_id, array_agg(DISTINCT item)
FROM content_units_sources cus
INNER JOIN rec_sources AS rs ON cus.source_id = rs.id,
unnest(rs.path) item
GROUP BY cus.content_unit_id;
WITH RECURSIVE rec_tags AS (SELECT t.id, t.uid, ARRAY [t.uid] :: CHAR(8) [] "path"
FROM tags t
WHERE parent_id IS NULL
UNION
SELECT t.id, t.uid, (rt.path || t.uid) :: CHAR(8) []
FROM tags t
INNER JOIN rec_tags rt ON t.parent_id = rt.id)
SELECT cut.content_unit_id, array_agg(DISTINCT item)
FROM content_units_tags cut
INNER JOIN rec_tags AS rt ON cut.tag_id = rt.id,
unnest(rt.path) item
GROUP BY cut.content_unit_id;
SELECT cup.content_unit_id, array_agg(p.uid)
FROM content_units_persons cup
INNER JOIN persons p ON cup.person_id = p.id
GROUP BY cup.content_unit_id;
SELECT content_unit_id, array_agg(DISTINCT language)
FROM files
WHERE language NOT IN ('zz', 'xx')
AND content_unit_id IS NOT NULL
GROUP BY content_unit_id;
SELECT array_agg(DISTINCT id)
FROM collections
WHERE type_id = 5
AND properties -> 'genres' ?| ARRAY ['educational'];
SELECT c.id, max(cu.properties ->> 'film_date') max_film_date, count(cu.id)
FROM collections c
INNER JOIN collections_content_units ccu
ON c.id = ccu.collection_id AND c.type_id = 5 AND c.secure = 0 AND c.published IS TRUE
INNER JOIN content_units cu
ON ccu.content_unit_id = cu.id AND cu.secure = 0 AND cu.published IS TRUE AND cu.properties ? 'film_date'
GROUP BY c.id
ORDER BY max_film_date DESC;
-- fix missing properties for doc files
update files
set language = 'he',
type = 'text'
where name ~ '^heb.*\.docx?$';
update files
set language = 'en',
type = 'text'
where name ~ '^eng.*\.docx?$';
update files
set language = 'ru',
type = 'text'
where name ~ '^rus.*\.docx?$';
update files
set language = 'es',
type = 'text'
where name ~ '^spa.*\.docx?$';
update files
set language = 'it',
type = 'text'
where name ~ '^ita.*\.docx?$';
update files
set language = 'de',
type = 'text'
where name ~ '^ger.*\.docx?$';
update files
set language = 'nl',
type = 'text'
where name ~ '^dut.*\.docx?$';
update files
set language = 'fr',
type = 'text'
where name ~ '^fre.*\.docx?$';
update files
set language = 'pt',
type = 'text'
where name ~ '^por.*\.docx?$';
update files
set language = 'tr',
type = 'text'
where name ~ '^trk.*\.docx?$';
update files
set language = 'pl',
type = 'text'
where name ~ '^pol.*\.docx?$';
update files
set language = 'ar',
type = 'text'
where name ~ '^arb.*\.docx?$';
update files
set language = 'hu',
type = 'text'
where name ~ '^hun.*\.docx?$';
update files
set language = 'fi',
type = 'text'
where name ~ '^fin.*\.docx?$';
update files
set language = 'lt',
type = 'text'
where name ~ '^lit.*\.docx?$';
update files
set language = 'ja',
type = 'text'
where name ~ '^jpn.*\.docx?$';
update files
set language = 'bg',
type = 'text'
where name ~ '^bul.*\.docx?$';
update files
set language = 'ka',
type = 'text'
where name ~ '^geo.*\.docx?$';
update files
set language = 'no',
type = 'text'
where name ~ '^nor.*\.docx?$';
update files
set language = 'sv',
type = 'text'
where name ~ '^swe.*\.docx?$';
update files
set language = 'hr',
type = 'text'
where name ~ '^hrv.*\.docx?$';
update files
set language = 'zh',
type = 'text'
where name ~ '^chn.*\.docx?$';
update files
set language = 'fa',
type = 'text'
where name ~ '^far.*\.docx?$';
update files
set language = 'ro',
type = 'text'
where name ~ '^ron.*\.docx?$';