]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0711.schema.reingest_avoid_collision_better.sql
LP#1917826: tweaks to data update
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0711.schema.reingest_avoid_collision_better.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0711', :eg_version);
4
5 CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries( bib_id BIGINT, skip_facet BOOL DEFAULT FALSE, skip_browse BOOL DEFAULT FALSE, skip_search BOOL DEFAULT FALSE ) RETURNS VOID AS $func$
6 DECLARE
7     fclass          RECORD;
8     ind_data        metabib.field_entry_template%ROWTYPE;
9     mbe_row         metabib.browse_entry%ROWTYPE;
10     mbe_id          BIGINT;
11     normalized_value    TEXT;
12 BEGIN
13     PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
14     IF NOT FOUND THEN
15         IF NOT skip_search THEN
16             FOR fclass IN SELECT * FROM config.metabib_class LOOP
17                 -- RAISE NOTICE 'Emptying out %', fclass.name;
18                 EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
19             END LOOP;
20         END IF;
21         IF NOT skip_facet THEN
22             DELETE FROM metabib.facet_entry WHERE source = bib_id;
23         END IF;
24         IF NOT skip_browse THEN
25             DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
26         END IF;
27     END IF;
28
29     FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) LOOP
30         IF ind_data.field < 0 THEN
31             ind_data.field = -1 * ind_data.field;
32         END IF;
33
34         IF ind_data.facet_field AND NOT skip_facet THEN
35             INSERT INTO metabib.facet_entry (field, source, value)
36                 VALUES (ind_data.field, ind_data.source, ind_data.value);
37         END IF;
38
39         IF ind_data.browse_field AND NOT skip_browse THEN
40             -- A caveat about this SELECT: this should take care of replacing
41             -- old mbe rows when data changes, but not if normalization (by
42             -- which I mean specifically the output of
43             -- evergreen.oils_tsearch2()) changes.  It may or may not be
44             -- expensive to add a comparison of index_vector to index_vector
45             -- to the WHERE clause below.
46             normalized_value := metabib.browse_normalize(
47                 ind_data.value, ind_data.field
48             );
49
50             SELECT INTO mbe_row * FROM metabib.browse_entry WHERE value = normalized_value;
51             IF FOUND THEN
52                 mbe_id := mbe_row.id;
53             ELSE
54                 INSERT INTO metabib.browse_entry (value) VALUES (normalized_value);
55                 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
56             END IF;
57
58             INSERT INTO metabib.browse_entry_def_map (entry, def, source)
59                 VALUES (mbe_id, ind_data.field, ind_data.source);
60         END IF;
61
62         IF ind_data.search_field AND NOT skip_search THEN
63             EXECUTE $$
64                 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
65                     VALUES ($$ ||
66                         quote_literal(ind_data.field) || $$, $$ ||
67                         quote_literal(ind_data.source) || $$, $$ ||
68                         quote_literal(ind_data.value) ||
69                     $$);$$;
70         END IF;
71
72     END LOOP;
73
74     RETURN;
75 END;
76 $func$ LANGUAGE PLPGSQL;
77
78 COMMIT;