]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0839.data.alternative-title-indexing.sql
LP#1206936 - Fix wrong billing info in money.transaction_billing_summary
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0839.data.alternative-title-indexing.sql
1 BEGIN;
2
3 -- check whether patch can be applied
4 SELECT evergreen.upgrade_deps_block_check('0839', :eg_version);
5
6 UPDATE config.metabib_field
7 SET
8     xpath = $$//mods32:mods/mods32:titleInfo[mods32:title and starts-with(@type,'alternative')]$$,
9     browse_sort_xpath = $$*[local-name() != "nonSort"]$$,
10     browse_xpath = NULL
11 WHERE
12     field_class = 'title' AND name = 'alternative' ;
13
14 COMMIT;
15
16 -- The following function only appears in the upgrade script and not the
17 -- baseline schema because it's not necessary in the latter (and it's a
18 -- temporary function).  It just serves to do a hopefully cheaper, more
19 -- focused reingest just to hit the alternative title index.
20
21 -- This cribs from the guts of metabib.reingest_metabib_field_entries(),
22 -- and if it actually is a timesaver over a full reingest, then at some
23 -- point in the future it would be nice if we broke it out into a separate
24 -- function to make things like this easier.
25
26 CREATE OR REPLACE FUNCTION pg_temp.alternative_title_reingest( bib_id BIGINT ) RETURNS VOID AS $func$
27 DECLARE
28     ind_data        metabib.field_entry_template%ROWTYPE;
29     mbe_row         metabib.browse_entry%ROWTYPE;
30     mbe_id          BIGINT;
31     b_skip_facet    BOOL := false;
32     b_skip_browse   BOOL := false;
33     b_skip_search   BOOL := false;
34     alt_title       INT;
35     value_prepped   TEXT;
36 BEGIN
37     SELECT INTO alt_title id FROM config.metabib_field WHERE field_class = 'title' AND name = 'alternative';
38     FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) WHERE field = alt_title LOOP
39         IF ind_data.field < 0 THEN
40             ind_data.field = -1 * ind_data.field;
41         END IF;
42
43         IF ind_data.facet_field AND NOT b_skip_facet THEN
44             INSERT INTO metabib.facet_entry (field, source, value)
45                 VALUES (ind_data.field, ind_data.source, ind_data.value);
46         END IF;
47
48         IF ind_data.browse_field AND NOT b_skip_browse THEN
49             -- A caveat about this SELECT: this should take care of replacing
50             -- old mbe rows when data changes, but not if normalization (by
51             -- which I mean specifically the output of
52             -- evergreen.oils_tsearch2()) changes.  It may or may not be
53             -- expensive to add a comparison of index_vector to index_vector
54             -- to the WHERE clause below.
55
56             value_prepped := metabib.browse_normalize(ind_data.value, ind_data.field);
57             SELECT INTO mbe_row * FROM metabib.browse_entry
58                 WHERE value = value_prepped AND sort_value = ind_data.sort_value;
59
60             IF FOUND THEN
61                 mbe_id := mbe_row.id;
62             ELSE
63                 INSERT INTO metabib.browse_entry
64                     ( value, sort_value ) VALUES
65                     ( value_prepped, ind_data.sort_value );
66
67                 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
68             END IF;
69
70             INSERT INTO metabib.browse_entry_def_map (entry, def, source, authority)
71                 VALUES (mbe_id, ind_data.field, ind_data.source, ind_data.authority);
72         END IF;
73
74         -- Avoid inserting duplicate rows, but retain granularity of being
75         -- able to search browse fields with "starts with" type operators
76         -- (for example, for titles of songs in music albums)
77         IF (ind_data.search_field OR ind_data.browse_field) AND NOT b_skip_search THEN
78             EXECUTE 'SELECT 1 FROM metabib.' || ind_data.field_class ||
79                 '_field_entry WHERE field = $1 AND source = $2 AND value = $3'
80                 INTO mbe_id USING ind_data.field, ind_data.source, ind_data.value;
81                 -- RAISE NOTICE 'Search for an already matching row returned %', mbe_id;
82             IF mbe_id IS NULL THEN
83                 EXECUTE $$
84                 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
85                     VALUES ($$ ||
86                         quote_literal(ind_data.field) || $$, $$ ||
87                         quote_literal(ind_data.source) || $$, $$ ||
88                         quote_literal(ind_data.value) ||
89                     $$);$$;
90             END IF;
91         END IF;
92
93     END LOOP;
94
95     IF NOT b_skip_search THEN
96         PERFORM metabib.update_combined_index_vectors(bib_id);
97     END IF;
98
99     RETURN;
100 END;
101 $func$ LANGUAGE PLPGSQL;
102
103 \qecho This is a partial reingest of your bib records. It may take a while.
104
105 SELECT pg_temp.alternative_title_reingest(id) FROM biblio.record_entry WHERE NOT deleted;