1 -- Remove unused old materialized view
2 drop materialized
view context_node_ngrams_view
;
7 (select jsonb_array_elements_text(ngrams_repo_element
->'children') as term
10 (select ngrams_repo_element
->>'root' as term
13 (select child_ngrams.term
, ngrams.terms
15 left join ngrams
on child_ngrams.term
= ngrams.terms
16 where ngrams.terms
is null
20 select parent_ngrams.term
, ngrams.terms
22 left join ngrams
on parent_ngrams.term
= ngrams.terms
23 where ngrams.terms
is null
24 and parent_ngrams.term
is not null)
28 INSERT INTO ngrams (terms
) select term
from query
;
32 CREATE OR REPLACE FUNCTION check_node_stories_json()
35 missing_ngrams_exist
boolean;
38 (SELECT jsonb_array_elements_text(NEW.ngrams_repo_element
->'children') AS term
),
40 (SELECT NEW.ngrams_repo_element
->>'root' AS term
),
42 ngrams_child_parent
AS
43 (SELECT child_ngrams.term
, ngrams.terms
45 LEFT JOIN ngrams
ON child_ngrams.term
= ngrams.terms
46 WHERE ngrams.terms
IS NULL
50 SELECT parent_ngrams.term
, ngrams.terms
52 LEFT JOIN ngrams
ON parent_ngrams.term
= ngrams.terms
53 WHERE ngrams.terms
IS NULL
54 AND parent_ngrams.term
IS NOT NULL)
56 SELECT EXISTS(SELECT * FROM ngrams_child_parent
) INTO missing_ngrams_exist
;
58 IF missing_ngrams_exist
THEN
59 RAISE
EXCEPTION 'node_stories: ngrams are missing: %', row_to_json(NEW);
66 CREATE OR REPLACE TRIGGER check_node_stories_json_trg
67 AFTER INSERT OR UPDATE
70 EXECUTE PROCEDURE check_node_stories_json();
73 CREATE OR REPLACE FUNCTION check_ngrams_json()
76 missing_ngrams_exist
boolean;
79 (SELECT jsonb_array_elements_text(ngrams_repo_element
->'children') AS term
81 WHERE term
= OLD.terms
),
83 (SELECT ngrams_repo_element
->>'root' AS term
85 WHERE term
= OLD.terms
),
86 child_parent_ngrams
AS
87 (SELECT * FROM child_ngrams
88 UNION SELECT * FROM parent_ngrams
)
90 SELECT EXISTS(SELECT * FROM child_parent_ngrams
) INTO missing_ngrams_exist
;
92 IF missing_ngrams_exist
THEN
93 RAISE
EXCEPTION 'ngrams are missing: %', row_to_json(OLD);
100 CREATE OR REPLACE TRIGGER check_ngrams_json_trg
104 EXECUTE PROCEDURE check_ngrams_json();