1 --Upgrade Script for 2.2.4 to 2.2.5
2 \set eg_version '''2.2.5'''
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.2.5', :eg_version);
5 -- Only consider main entry headings for bib overlay
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0750', :eg_version);
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$
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[];
23 IF auth_id IS NULL THEN
27 -- Default to the LoC controll set
28 SELECT control_set INTO cset FROM authority.record_entry WHERE id = auth_id;
30 -- if none, make a best guess
32 SELECT control_set INTO cset
33 FROM authority.control_set_authority_field
35 SELECT UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marc::XML)::TEXT[])
36 FROM authority.record_entry
42 -- if STILL none, no-op change
46 XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
47 XMLELEMENT( name leader, '00881nam a2200193 4500'),
50 XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
53 XMLATTRIBUTES('d' AS code),
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 || '$]' );
73 XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
74 XMLELEMENT( name leader, '00881nam a2200193 4500'),
78 XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
81 XMLATTRIBUTES('r' AS code),
82 ARRAY_TO_STRING(replace_rules,',')
87 $f$ STABLE LANGUAGE PLPGSQL;
89 -- Change the two argument form of vandelay.merge_record_xml to
90 -- prevent bibliographic record destruction when there is nothing to
94 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$
96 dyn_profile vandelay.compile_profile%ROWTYPE;
104 IF target_marc IS NULL OR template_marc IS NULL THEN
105 -- RAISE NOTICE 'no marc for target or template record';
109 dyn_profile := vandelay.compile_profile( template_marc );
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;
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.
119 ELSIF dyn_profile.replace_rule <> '' THEN
120 trgt_marc = target_marc;
121 tmpl_marc = template_marc;
122 replace_rule = dyn_profile.replace_rule;
124 tmp_marc = target_marc;
125 trgt_marc = template_marc;
126 tmpl_marc = tmp_marc;
127 replace_rule = dyn_profile.preserve_rule;
130 RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule );
135 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
137 merge_profile vandelay.merge_profile%ROWTYPE;
138 dyn_profile vandelay.compile_profile%ROWTYPE;
148 SELECT b.marc INTO eg_marc
149 FROM biblio.record_entry b
153 IF eg_marc IS NULL OR v_marc IS NULL THEN
154 -- RAISE NOTICE 'no marc for template or bib record';
158 dyn_profile := vandelay.compile_profile( v_marc );
160 IF merge_profile_id IS NOT NULL THEN
161 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
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,''), ',');
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;
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"
178 ELSIF dyn_profile.replace_rule <> '' THEN
179 source_marc = v_marc;
180 target_marc = eg_marc;
181 replace_rule = dyn_profile.replace_rule;
183 source_marc = eg_marc;
184 target_marc = v_marc;
185 replace_rule = dyn_profile.preserve_rule;
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 )
193 -- RAISE NOTICE 'update of biblio.record_entry failed';
202 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
204 merge_profile vandelay.merge_profile%ROWTYPE;
205 dyn_profile vandelay.compile_profile%ROWTYPE;
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)
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)
224 IF eg_marc IS NULL OR v_marc IS NULL THEN
225 -- RAISE NOTICE 'no marc for vandelay or authority record';
229 dyn_profile := vandelay.compile_profile( v_marc );
231 IF merge_profile_id IS NOT NULL THEN
232 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
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,''), ',');
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;
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"
249 ELSIF dyn_profile.replace_rule <> '' THEN
250 source_marc = v_marc;
251 target_marc = eg_marc;
252 replace_rule = dyn_profile.replace_rule;
254 source_marc = eg_marc;
255 target_marc = v_marc;
256 replace_rule = dyn_profile.preserve_rule;
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 )
264 UPDATE vandelay.queued_authority_record
265 SET imported_as = eg_id,
267 WHERE id = import_id;
271 -- RAISE NOTICE 'update of authority.record_entry failed';