]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1341.schema.dym_delayed_reify.sql
LP#1931737: Stamping upgrade scripts
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1341.schema.dym_delayed_reify.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1340', :eg_version);
4
5 CREATE OR REPLACE FUNCTION search.disable_symspell_reification () RETURNS VOID AS $f$
6     INSERT INTO config.internal_flag (name,enabled)
7       VALUES ('ingest.disable_symspell_reification',TRUE)
8     ON CONFLICT (name) DO UPDATE SET enabled = TRUE;
9 $f$ LANGUAGE SQL;
10
11 CREATE OR REPLACE FUNCTION search.enable_symspell_reification () RETURNS VOID AS $f$
12     UPDATE config.internal_flag SET enabled = FALSE WHERE name = 'ingest.disable_symspell_reification';
13 $f$ LANGUAGE SQL;
14
15 CREATE OR REPLACE FUNCTION search.symspell_dictionary_full_reify () RETURNS SETOF search.symspell_dictionary AS $f$
16  WITH new_rows AS (
17     DELETE FROM search.symspell_dictionary_updates RETURNING *
18  ), computed_rows AS ( -- this collapses the rows deleted into the format we need for UPSERT
19     SELECT  SUM(keyword_count)    AS keyword_count,
20             SUM(title_count)      AS title_count,
21             SUM(author_count)     AS author_count,
22             SUM(subject_count)    AS subject_count,
23             SUM(series_count)     AS series_count,
24             SUM(identifier_count) AS identifier_count,
25
26             prefix_key,
27
28             ARRAY_REMOVE(ARRAY_AGG(DISTINCT keyword_suggestions[1]), NULL)    AS keyword_suggestions,
29             ARRAY_REMOVE(ARRAY_AGG(DISTINCT title_suggestions[1]), NULL)      AS title_suggestions,
30             ARRAY_REMOVE(ARRAY_AGG(DISTINCT author_suggestions[1]), NULL)     AS author_suggestions,
31             ARRAY_REMOVE(ARRAY_AGG(DISTINCT subject_suggestions[1]), NULL)    AS subject_suggestions,
32             ARRAY_REMOVE(ARRAY_AGG(DISTINCT series_suggestions[1]), NULL)     AS series_suggestions,
33             ARRAY_REMOVE(ARRAY_AGG(DISTINCT identifier_suggestions[1]), NULL) AS identifier_suggestions
34       FROM  new_rows
35       GROUP BY prefix_key
36  )
37  INSERT INTO search.symspell_dictionary AS d SELECT * FROM computed_rows
38  ON CONFLICT (prefix_key) DO UPDATE SET
39     keyword_count = GREATEST(0, d.keyword_count + EXCLUDED.keyword_count),
40     keyword_suggestions = evergreen.text_array_merge_unique(EXCLUDED.keyword_suggestions,d.keyword_suggestions),
41
42     title_count = GREATEST(0, d.title_count + EXCLUDED.title_count),
43     title_suggestions = evergreen.text_array_merge_unique(EXCLUDED.title_suggestions,d.title_suggestions),
44
45     author_count = GREATEST(0, d.author_count + EXCLUDED.author_count),
46     author_suggestions = evergreen.text_array_merge_unique(EXCLUDED.author_suggestions,d.author_suggestions),
47
48     subject_count = GREATEST(0, d.subject_count + EXCLUDED.subject_count),
49     subject_suggestions = evergreen.text_array_merge_unique(EXCLUDED.subject_suggestions,d.subject_suggestions),
50
51     series_count = GREATEST(0, d.series_count + EXCLUDED.series_count),
52     series_suggestions = evergreen.text_array_merge_unique(EXCLUDED.series_suggestions,d.series_suggestions),
53
54     identifier_count = GREATEST(0, d.identifier_count + EXCLUDED.identifier_count),
55     identifier_suggestions = evergreen.text_array_merge_unique(EXCLUDED.identifier_suggestions,d.identifier_suggestions)
56  RETURNING *;
57 $f$ LANGUAGE SQL;
58
59 -- Updated again to check for delayed symspell reification
60 CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries(
61     bib_id BIGINT,
62     skip_facet BOOL DEFAULT FALSE,
63     skip_display BOOL DEFAULT FALSE,
64     skip_browse BOOL DEFAULT FALSE,
65     skip_search BOOL DEFAULT FALSE,
66     only_fields INT[] DEFAULT '{}'::INT[]
67 ) RETURNS VOID AS $func$
68 DECLARE
69     fclass          RECORD;
70     ind_data        metabib.field_entry_template%ROWTYPE;
71     mbe_row         metabib.browse_entry%ROWTYPE;
72     mbe_id          BIGINT;
73     b_skip_facet    BOOL;
74     b_skip_display    BOOL;
75     b_skip_browse   BOOL;
76     b_skip_search   BOOL;
77     value_prepped   TEXT;
78     field_list      INT[] := only_fields;
79     field_types     TEXT[] := '{}'::TEXT[];
80 BEGIN
81
82     IF field_list = '{}'::INT[] THEN
83         SELECT ARRAY_AGG(id) INTO field_list FROM config.metabib_field;
84     END IF;
85
86     SELECT COALESCE(NULLIF(skip_facet, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_facet_indexing' AND enabled)) INTO b_skip_facet;
87     SELECT COALESCE(NULLIF(skip_display, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_display_indexing' AND enabled)) INTO b_skip_display;
88     SELECT COALESCE(NULLIF(skip_browse, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_browse_indexing' AND enabled)) INTO b_skip_browse;
89     SELECT COALESCE(NULLIF(skip_search, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_search_indexing' AND enabled)) INTO b_skip_search;
90
91     IF NOT b_skip_facet THEN field_types := field_types || '{facet}'; END IF;
92     IF NOT b_skip_display THEN field_types := field_types || '{display}'; END IF;
93     IF NOT b_skip_browse THEN field_types := field_types || '{browse}'; END IF;
94     IF NOT b_skip_search THEN field_types := field_types || '{search}'; END IF;
95
96     PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
97     IF NOT FOUND THEN
98         IF NOT b_skip_search THEN
99             FOR fclass IN SELECT * FROM config.metabib_class LOOP
100                 -- RAISE NOTICE 'Emptying out %', fclass.name;
101                 EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
102             END LOOP;
103         END IF;
104         IF NOT b_skip_facet THEN
105             DELETE FROM metabib.facet_entry WHERE source = bib_id;
106         END IF;
107         IF NOT b_skip_display THEN
108             DELETE FROM metabib.display_entry WHERE source = bib_id;
109         END IF;
110         IF NOT b_skip_browse THEN
111             DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
112         END IF;
113     END IF;
114
115     FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id, ' ', field_types, field_list ) LOOP
116
117     -- don't store what has been normalized away
118         CONTINUE WHEN ind_data.value IS NULL;
119
120         IF ind_data.field < 0 THEN
121             ind_data.field = -1 * ind_data.field;
122         END IF;
123
124         IF ind_data.facet_field AND NOT b_skip_facet THEN
125             INSERT INTO metabib.facet_entry (field, source, value)
126                 VALUES (ind_data.field, ind_data.source, ind_data.value);
127         END IF;
128
129         IF ind_data.display_field AND NOT b_skip_display THEN
130             INSERT INTO metabib.display_entry (field, source, value)
131                 VALUES (ind_data.field, ind_data.source, ind_data.value);
132         END IF;
133
134
135         IF ind_data.browse_field AND NOT b_skip_browse THEN
136             -- A caveat about this SELECT: this should take care of replacing
137             -- old mbe rows when data changes, but not if normalization (by
138             -- which I mean specifically the output of
139             -- evergreen.oils_tsearch2()) changes.  It may or may not be
140             -- expensive to add a comparison of index_vector to index_vector
141             -- to the WHERE clause below.
142
143             CONTINUE WHEN ind_data.sort_value IS NULL;
144
145             value_prepped := metabib.browse_normalize(ind_data.value, ind_data.field);
146             IF ind_data.browse_nocase THEN
147                 SELECT INTO mbe_row * FROM metabib.browse_entry
148                     WHERE evergreen.lowercase(value) = evergreen.lowercase(value_prepped) AND sort_value = ind_data.sort_value
149                     ORDER BY sort_value, value LIMIT 1; -- gotta pick something, I guess
150             ELSE
151                 SELECT INTO mbe_row * FROM metabib.browse_entry
152                     WHERE value = value_prepped AND sort_value = ind_data.sort_value;
153             END IF;
154
155             IF FOUND THEN
156                 mbe_id := mbe_row.id;
157             ELSE
158                 INSERT INTO metabib.browse_entry
159                     ( value, sort_value ) VALUES
160                     ( value_prepped, ind_data.sort_value );
161
162                 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
163             END IF;
164
165             INSERT INTO metabib.browse_entry_def_map (entry, def, source, authority)
166                 VALUES (mbe_id, ind_data.field, ind_data.source, ind_data.authority);
167         END IF;
168
169         IF ind_data.search_field AND NOT b_skip_search THEN
170             -- Avoid inserting duplicate rows
171             EXECUTE 'SELECT 1 FROM metabib.' || ind_data.field_class ||
172                 '_field_entry WHERE field = $1 AND source = $2 AND value = $3'
173                 INTO mbe_id USING ind_data.field, ind_data.source, ind_data.value;
174                 -- RAISE NOTICE 'Search for an already matching row returned %', mbe_id;
175             IF mbe_id IS NULL THEN
176                 EXECUTE $$
177                 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
178                     VALUES ($$ ||
179                         quote_literal(ind_data.field) || $$, $$ ||
180                         quote_literal(ind_data.source) || $$, $$ ||
181                         quote_literal(ind_data.value) ||
182                     $$);$$;
183             END IF;
184         END IF;
185
186     END LOOP;
187
188     IF NOT b_skip_search THEN
189         PERFORM metabib.update_combined_index_vectors(bib_id);
190         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_symspell_reification' AND enabled;
191         IF NOT FOUND THEN
192             PERFORM search.symspell_dictionary_reify();
193         END IF;
194     END IF;
195
196     RETURN;
197 END;
198 $func$ LANGUAGE PLPGSQL;
199
200 COMMIT;
201