]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.2.4-2.2.5-upgrade-db.sql
LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.2.4-2.2.5-upgrade-db.sql
1 --Upgrade Script for 2.2.4 to 2.2.5
2 \set eg_version '''2.2.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.2.5', :eg_version);
5 -- Only consider main entry headings for bib overlay
6
7
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0750', :eg_version);
10
11
12 -- Function to generate an ephemeral overlay template from an authority record
13 CREATE OR REPLACE FUNCTION authority.generate_overlay_template (source_xml TEXT) RETURNS TEXT AS $f$
14 DECLARE
15     cset                INT;
16     main_entry          authority.control_set_authority_field%ROWTYPE;
17     bib_field           authority.control_set_bib_field%ROWTYPE;
18     auth_id             INT DEFAULT oils_xpath_string('//*[@tag="901"]/*[local-name()="subfield" and @code="c"]', source_xml)::INT;
19     replace_data        XML[] DEFAULT '{}'::XML[];
20     replace_rules       TEXT[] DEFAULT '{}'::TEXT[];
21     auth_field          XML[];
22 BEGIN
23     IF auth_id IS NULL THEN
24         RETURN NULL;
25     END IF;
26
27     -- Default to the LoC controll set
28     SELECT control_set INTO cset FROM authority.record_entry WHERE id = auth_id;
29
30     -- if none, make a best guess
31     IF cset IS NULL THEN
32         SELECT  control_set INTO cset
33           FROM  authority.control_set_authority_field
34           WHERE tag IN (
35                     SELECT  UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marc::XML)::TEXT[])
36                       FROM  authority.record_entry
37                       WHERE id = auth_id
38                 )
39           LIMIT 1;
40     END IF;
41
42     -- if STILL none, no-op change
43     IF cset IS NULL THEN
44         RETURN XMLELEMENT(
45             name record,
46             XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
47             XMLELEMENT( name leader, '00881nam a2200193   4500'),
48             XMLELEMENT(
49                 name datafield,
50                 XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
51                 XMLELEMENT(
52                     name subfield,
53                     XMLATTRIBUTES('d' AS code),
54                     '901c'
55                 )
56             )
57         )::TEXT;
58     END IF;
59
60     FOR main_entry IN SELECT * FROM authority.control_set_authority_field acsaf WHERE acsaf.control_set = cset AND acsaf.main_entry IS NULL LOOP
61         auth_field := XPATH('//*[@tag="'||main_entry.tag||'"][1]',source_xml::XML);
62         IF ARRAY_LENGTH(auth_field,1) > 0 THEN
63             FOR bib_field IN SELECT * FROM authority.control_set_bib_field WHERE authority_field = main_entry.id LOOP
64                 replace_data := replace_data || XMLELEMENT( name datafield, XMLATTRIBUTES(bib_field.tag AS tag), XPATH('//*[local-name()="subfield"]',auth_field[1])::XML[]);
65                 replace_rules := replace_rules || ( bib_field.tag || main_entry.sf_list || E'[0~\\)' || auth_id || '$]' );
66             END LOOP;
67             EXIT;
68         END IF;
69     END LOOP;
70
71     RETURN XMLELEMENT(
72         name record,
73         XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
74         XMLELEMENT( name leader, '00881nam a2200193   4500'),
75         replace_data,
76         XMLELEMENT(
77             name datafield,
78             XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
79             XMLELEMENT(
80                 name subfield,
81                 XMLATTRIBUTES('r' AS code),
82                 ARRAY_TO_STRING(replace_rules,',')
83             )
84         )
85     )::TEXT;
86 END;
87 $f$ STABLE LANGUAGE PLPGSQL;
88
89 -- Change the two argument form of vandelay.merge_record_xml to
90 -- prevent bibliographic record destruction when there is nothing to
91 -- do.
92
93
94 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$
95 DECLARE
96     dyn_profile     vandelay.compile_profile%ROWTYPE;
97     replace_rule    TEXT;
98     tmp_marc        TEXT;
99     trgt_marc        TEXT;
100     tmpl_marc        TEXT;
101     match_count     INT;
102 BEGIN
103
104     IF target_marc IS NULL OR template_marc IS NULL THEN
105         -- RAISE NOTICE 'no marc for target or template record';
106         RETURN NULL;
107     END IF;
108
109     dyn_profile := vandelay.compile_profile( template_marc );
110
111     IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
112         -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
113         RETURN NULL;
114     END IF;
115
116     IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' THEN
117         --Since we have nothing to do, just return what we were given.
118         RETURN target_marc;
119     ELSIF dyn_profile.replace_rule <> '' THEN
120         trgt_marc = target_marc;
121         tmpl_marc = template_marc;
122         replace_rule = dyn_profile.replace_rule;
123     ELSE
124         tmp_marc = target_marc;
125         trgt_marc = template_marc;
126         tmpl_marc = tmp_marc;
127         replace_rule = dyn_profile.preserve_rule;
128     END IF;
129
130     RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule );
131
132 END;
133 $$ LANGUAGE PLPGSQL;
134
135 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
136 DECLARE
137     merge_profile   vandelay.merge_profile%ROWTYPE;
138     dyn_profile     vandelay.compile_profile%ROWTYPE;
139     editor_string   TEXT;
140     editor_id       INT;
141     source_marc     TEXT;
142     target_marc     TEXT;
143     eg_marc         TEXT;
144     replace_rule    TEXT;
145     match_count     INT;
146 BEGIN
147
148     SELECT  b.marc INTO eg_marc
149       FROM  biblio.record_entry b
150       WHERE b.id = eg_id
151       LIMIT 1;
152
153     IF eg_marc IS NULL OR v_marc IS NULL THEN
154         -- RAISE NOTICE 'no marc for template or bib record';
155         RETURN FALSE;
156     END IF;
157
158     dyn_profile := vandelay.compile_profile( v_marc );
159
160     IF merge_profile_id IS NOT NULL THEN
161         SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
162         IF FOUND THEN
163             dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
164             dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
165             dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
166             dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
167         END IF;
168     END IF;
169
170     IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
171         -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
172         RETURN FALSE;
173     END IF;
174
175     IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
176         --Since we have nothing to do, just return a NOOP "we did it"
177         RETURN TRUE;
178     ELSIF dyn_profile.replace_rule <> '' THEN
179         source_marc = v_marc;
180         target_marc = eg_marc;
181         replace_rule = dyn_profile.replace_rule;
182     ELSE
183         source_marc = eg_marc;
184         target_marc = v_marc;
185         replace_rule = dyn_profile.preserve_rule;
186     END IF;
187
188     UPDATE  biblio.record_entry
189       SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
190       WHERE id = eg_id;
191
192     IF NOT FOUND THEN
193         -- RAISE NOTICE 'update of biblio.record_entry failed';
194         RETURN FALSE;
195     END IF;
196
197     RETURN TRUE;
198
199 END;
200 $$ LANGUAGE PLPGSQL;
201
202 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
203 DECLARE
204     merge_profile   vandelay.merge_profile%ROWTYPE;
205     dyn_profile     vandelay.compile_profile%ROWTYPE;
206     source_marc     TEXT;
207     target_marc     TEXT;
208     eg_marc         TEXT;
209     v_marc          TEXT;
210     replace_rule    TEXT;
211     match_count     INT;
212 BEGIN
213
214     SELECT  b.marc INTO eg_marc
215       FROM  authority.record_entry b
216             JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
217       LIMIT 1;
218
219     SELECT  q.marc INTO v_marc
220       FROM  vandelay.queued_record q
221             JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
222       LIMIT 1;
223
224     IF eg_marc IS NULL OR v_marc IS NULL THEN
225         -- RAISE NOTICE 'no marc for vandelay or authority record';
226         RETURN FALSE;
227     END IF;
228
229     dyn_profile := vandelay.compile_profile( v_marc );
230
231     IF merge_profile_id IS NOT NULL THEN
232         SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
233         IF FOUND THEN
234             dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
235             dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
236             dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
237             dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
238         END IF;
239     END IF;
240
241     IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
242         -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
243         RETURN FALSE;
244     END IF;
245
246     IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
247         --Since we have nothing to do, just return a NOOP "we did it"
248         RETURN TRUE;
249     ELSIF dyn_profile.replace_rule <> '' THEN
250         source_marc = v_marc;
251         target_marc = eg_marc;
252         replace_rule = dyn_profile.replace_rule;
253     ELSE
254         source_marc = eg_marc;
255         target_marc = v_marc;
256         replace_rule = dyn_profile.preserve_rule;
257     END IF;
258
259     UPDATE  authority.record_entry
260       SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
261       WHERE id = eg_id;
262
263     IF FOUND THEN
264         UPDATE  vandelay.queued_authority_record
265           SET   imported_as = eg_id,
266                 import_time = NOW()
267           WHERE id = import_id;
268         RETURN TRUE;
269     END IF;
270
271     -- RAISE NOTICE 'update of authority.record_entry failed';
272
273     RETURN FALSE;
274
275 END;
276 $$ LANGUAGE PLPGSQL;
277
278 COMMIT;