1 --Upgrade Script for 3.0.1 to 3.0.2
2 \set eg_version '''3.0.2'''
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.0.2', :eg_version);
6 SELECT evergreen.upgrade_deps_block_check('1079', :eg_version); -- rhamby/cesardv/gmcharlt
8 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
10 moved_objects INT := 0;
11 source_cn asset.call_number%ROWTYPE;
12 target_cn asset.call_number%ROWTYPE;
13 metarec metabib.metarecord%ROWTYPE;
14 hold action.hold_request%ROWTYPE;
15 ser_rec serial.record_entry%ROWTYPE;
16 ser_sub serial.subscription%ROWTYPE;
17 acq_lineitem acq.lineitem%ROWTYPE;
18 acq_request acq.user_request%ROWTYPE;
19 booking booking.resource_type%ROWTYPE;
20 source_part biblio.monograph_part%ROWTYPE;
21 target_part biblio.monograph_part%ROWTYPE;
22 multi_home biblio.peer_bib_copy_map%ROWTYPE;
29 -- move any 856 entries on records that have at least one MARC-mapped URI entry
30 SELECT INTO uri_count COUNT(*)
31 FROM asset.uri_call_number_map m
32 JOIN asset.call_number cn ON (m.call_number = cn.id)
33 WHERE cn.record = source_record;
37 -- This returns more nodes than you might expect:
38 -- 7 instead of 1 for an 856 with $u $y $9
39 SELECT COUNT(*) INTO counter
40 FROM oils_xpath_table(
43 'biblio.record_entry',
45 'id=' || source_record
48 FOR i IN 1 .. counter LOOP
49 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
51 ' ind1="' || FIRST(ind1) || '"' ||
52 ' ind2="' || FIRST(ind2) || '">' ||
54 '<subfield code="' || subfield || '">' ||
57 regexp_replace(data,'&','&','g'),
61 ) || '</subfield>', ''
62 ) || '</datafield>' INTO uri_datafield
63 FROM oils_xpath_table(
66 'biblio.record_entry',
67 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
68 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
69 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
70 '//*[@tag="856"][position()=' || i || ']/*[@code]',
71 'id=' || source_record
72 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
74 -- As most of the results will be NULL, protect against NULLifying
75 -- the valid content that we do generate
76 uri_text := uri_text || COALESCE(uri_datafield, '');
79 IF uri_text <> '' THEN
80 UPDATE biblio.record_entry
81 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
82 WHERE id = target_record;
87 -- Find and move metarecords to the target record
89 FROM metabib.metarecord
90 WHERE master_record = source_record;
93 UPDATE metabib.metarecord
94 SET master_record = target_record,
96 WHERE id = metarec.id;
98 moved_objects := moved_objects + 1;
101 -- Find call numbers attached to the source ...
102 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
104 SELECT INTO target_cn *
105 FROM asset.call_number
106 WHERE label = source_cn.label
107 AND prefix = source_cn.prefix
108 AND suffix = source_cn.suffix
109 AND owning_lib = source_cn.owning_lib
110 AND record = target_record
113 -- ... and if there's a conflicting one on the target ...
116 -- ... move the copies to that, and ...
118 SET call_number = target_cn.id
119 WHERE call_number = source_cn.id;
121 -- ... move V holds to the move-target call number
122 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
124 UPDATE action.hold_request
125 SET target = target_cn.id
128 moved_objects := moved_objects + 1;
131 UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
135 -- ... just move the call number to the target record
136 UPDATE asset.call_number
137 SET record = target_record
138 WHERE id = source_cn.id;
141 moved_objects := moved_objects + 1;
144 -- Find T holds targeting the source record ...
145 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
147 -- ... and move them to the target record
148 UPDATE action.hold_request
149 SET target = target_record
152 moved_objects := moved_objects + 1;
155 -- Find serial records targeting the source record ...
156 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
157 -- ... and move them to the target record
158 UPDATE serial.record_entry
159 SET record = target_record
160 WHERE id = ser_rec.id;
162 moved_objects := moved_objects + 1;
165 -- Find serial subscriptions targeting the source record ...
166 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
167 -- ... and move them to the target record
168 UPDATE serial.subscription
169 SET record_entry = target_record
170 WHERE id = ser_sub.id;
172 moved_objects := moved_objects + 1;
175 -- Find booking resource types targeting the source record ...
176 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
177 -- ... and move them to the target record
178 UPDATE booking.resource_type
179 SET record = target_record
180 WHERE id = booking.id;
182 moved_objects := moved_objects + 1;
185 -- Find acq lineitems targeting the source record ...
186 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
187 -- ... and move them to the target record
189 SET eg_bib_id = target_record
190 WHERE id = acq_lineitem.id;
192 moved_objects := moved_objects + 1;
195 -- Find acq user purchase requests targeting the source record ...
196 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
197 -- ... and move them to the target record
198 UPDATE acq.user_request
199 SET eg_bib = target_record
200 WHERE id = acq_request.id;
202 moved_objects := moved_objects + 1;
205 -- Find parts attached to the source ...
206 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
208 SELECT INTO target_part *
209 FROM biblio.monograph_part
210 WHERE label = source_part.label
211 AND record = target_record;
213 -- ... and if there's a conflicting one on the target ...
216 -- ... move the copy-part maps to that, and ...
217 UPDATE asset.copy_part_map
218 SET part = target_part.id
219 WHERE part = source_part.id;
221 -- ... move P holds to the move-target part
222 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
224 UPDATE action.hold_request
225 SET target = target_part.id
228 moved_objects := moved_objects + 1;
233 -- ... just move the part to the target record
234 UPDATE biblio.monograph_part
235 SET record = target_record
236 WHERE id = source_part.id;
239 moved_objects := moved_objects + 1;
242 -- Find multi_home items attached to the source ...
243 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
244 -- ... and move them to the target record
245 UPDATE biblio.peer_bib_copy_map
246 SET peer_record = target_record
247 WHERE id = multi_home.id;
249 moved_objects := moved_objects + 1;
252 -- And delete mappings where the item's home bib was merged with the peer bib
253 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
254 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
255 FROM asset.copy WHERE id = target_copy
258 -- Finally, "delete" the source record
259 DELETE FROM biblio.record_entry WHERE id = source_record;
261 -- That's all, folks!
262 RETURN moved_objects;
264 $func$ LANGUAGE plpgsql;
267 SELECT evergreen.upgrade_deps_block_check('1080', :eg_version); -- miker/jboyer/gmcharlt
269 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
271 ocn asset.call_number%ROWTYPE;
272 ncn asset.call_number%ROWTYPE;
276 IF TG_TABLE_NAME = 'peer_bib_copy_map' THEN -- Only needs ON INSERT OR DELETE, so handle separately
277 IF TG_OP = 'INSERT' THEN
278 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
281 asset.calculate_copy_visibility_attribute_set(NEW.target_copy)
285 ELSIF TG_OP = 'DELETE' THEN
286 DELETE FROM asset.copy_vis_attr_cache
287 WHERE record = OLD.peer_record AND target_copy = OLD.target_copy;
293 IF TG_OP = 'INSERT' THEN -- Handles ON INSERT. ON UPDATE is below.
294 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
295 SELECT * INTO ncn FROM asset.call_number cn WHERE id = NEW.call_number;
296 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
299 asset.calculate_copy_visibility_attribute_set(NEW.id)
301 ELSIF TG_TABLE_NAME = 'record_entry' THEN
302 NEW.vis_attr_vector := biblio.calculate_bib_visibility_attribute_set(NEW.id);
308 -- handle items first, since with circulation activity
309 -- their statuses change frequently
310 IF TG_TABLE_NAME IN ('copy', 'unit') THEN -- This handles ON UPDATE OR DELETE. ON INSERT above
312 IF TG_OP = 'DELETE' THEN -- Shouldn't get here, normally
313 DELETE FROM asset.copy_vis_attr_cache WHERE target_copy = OLD.id;
317 SELECT * INTO ncn FROM asset.call_number cn WHERE id = NEW.call_number;
319 IF OLD.deleted <> NEW.deleted THEN
321 DELETE FROM asset.copy_vis_attr_cache WHERE target_copy = OLD.id;
323 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
326 asset.calculate_copy_visibility_attribute_set(NEW.id)
331 ELSIF OLD.call_number <> NEW.call_number THEN
332 SELECT * INTO ocn FROM asset.call_number cn WHERE id = OLD.call_number;
334 IF ncn.record <> ocn.record THEN
335 UPDATE biblio.record_entry
336 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(ncn.record)
337 WHERE id = ocn.record;
339 -- We have to use a record-specific WHERE clause
340 -- to avoid modifying the entries for peer-bib copies.
341 UPDATE asset.copy_vis_attr_cache
342 SET target_copy = NEW.id,
344 WHERE target_copy = OLD.id
345 AND record = ocn.record;
349 IF OLD.location <> NEW.location OR
350 OLD.status <> NEW.status OR
351 OLD.opac_visible <> NEW.opac_visible OR
352 OLD.circ_lib <> NEW.circ_lib
354 -- Any of these could change visibility, but
355 -- we'll save some queries and not try to calculate
356 -- the change directly. We want to update peer-bib
357 -- entries in this case, unlike above.
358 UPDATE asset.copy_vis_attr_cache
359 SET target_copy = NEW.id,
360 vis_attr_vector = asset.calculate_copy_visibility_attribute_set(NEW.id)
361 WHERE target_copy = OLD.id;
365 ELSIF TG_TABLE_NAME = 'call_number' THEN -- Only ON UPDATE. Copy handler will deal with ON INSERT OR DELETE.
367 IF OLD.record <> NEW.record THEN
368 IF NEW.label = '##URI##' THEN
369 UPDATE biblio.record_entry
370 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(OLD.record)
371 WHERE id = OLD.record;
373 UPDATE biblio.record_entry
374 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(NEW.record)
375 WHERE id = NEW.record;
378 UPDATE asset.copy_vis_attr_cache
379 SET record = NEW.record,
380 vis_attr_vector = asset.calculate_copy_visibility_attribute_set(target_copy)
381 WHERE target_copy IN (SELECT id FROM asset.copy WHERE call_number = NEW.id)
382 AND record = OLD.record;
384 ELSIF OLD.owning_lib <> NEW.owning_lib THEN
385 UPDATE asset.copy_vis_attr_cache
386 SET vis_attr_vector = asset.calculate_copy_visibility_attribute_set(target_copy)
387 WHERE target_copy IN (SELECT id FROM asset.copy WHERE call_number = NEW.id)
388 AND record = NEW.record;
390 IF NEW.label = '##URI##' THEN
391 UPDATE biblio.record_entry
392 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(OLD.record)
393 WHERE id = OLD.record;
397 ELSIF TG_TABLE_NAME = 'record_entry' THEN -- Only handles ON UPDATE OR DELETE
399 IF TG_OP = 'DELETE' THEN -- Shouldn't get here, normally
400 DELETE FROM asset.copy_vis_attr_cache WHERE record = OLD.id;
402 ELSIF OLD.source <> NEW.source THEN
403 NEW.vis_attr_vector := biblio.calculate_bib_visibility_attribute_set(NEW.id);
410 $func$ LANGUAGE PLPGSQL;
413 SELECT evergreen.upgrade_deps_block_check('1081', :eg_version); -- jboyer/gmcharlt
415 DROP TRIGGER IF EXISTS inherit_copy_bucket_item_target_copy_fkey ON container.copy_bucket_item;
416 DROP TRIGGER IF EXISTS inherit_import_item_imported_as_fkey ON vandelay.import_item;
417 DROP TRIGGER IF EXISTS inherit_asset_copy_note_copy_fkey ON asset.copy_note;
418 DROP TRIGGER IF EXISTS inherit_asset_copy_tag_copy_map_copy_fkey ON asset.copy_tag_copy_map;
420 CREATE CONSTRAINT TRIGGER inherit_copy_bucket_item_target_copy_fkey
421 AFTER UPDATE OR INSERT ON container.copy_bucket_item
422 DEFERRABLE FOR EACH ROW EXECUTE PROCEDURE evergreen.container_copy_bucket_item_target_copy_inh_fkey();
423 CREATE CONSTRAINT TRIGGER inherit_import_item_imported_as_fkey
424 AFTER UPDATE OR INSERT ON vandelay.import_item
425 DEFERRABLE FOR EACH ROW EXECUTE PROCEDURE evergreen.vandelay_import_item_imported_as_inh_fkey();
426 CREATE CONSTRAINT TRIGGER inherit_asset_copy_note_copy_fkey
427 AFTER UPDATE OR INSERT ON asset.copy_note
428 DEFERRABLE FOR EACH ROW EXECUTE PROCEDURE evergreen.asset_copy_note_owning_copy_inh_fkey();
429 CREATE CONSTRAINT TRIGGER inherit_asset_copy_tag_copy_map_copy_fkey
430 AFTER UPDATE OR INSERT ON asset.copy_tag_copy_map
431 DEFERRABLE FOR EACH ROW EXECUTE PROCEDURE evergreen.asset_copy_tag_copy_map_copy_inh_fkey();
435 SELECT evergreen.upgrade_deps_block_check('1082', :eg_version); -- jboyer/gmcharlt
437 DELETE FROM asset.copy_vis_attr_cache WHERE target_copy IN (SELECT id FROM asset.copy WHERE deleted);
439 -- Evergreen DB patch XXXX.schema.qualify_unaccent_refs.sql
441 -- LP#1671150 Fix unaccent() function call in evergreen.unaccent_and_squash()
445 -- check whether patch can be applied
446 SELECT evergreen.upgrade_deps_block_check('1083', :eg_version);
448 CREATE OR REPLACE FUNCTION evergreen.unaccent_and_squash ( IN arg text) RETURNS text
449 IMMUTABLE STRICT AS $$
451 RETURN evergreen.lowercase(public.unaccent('public.unaccent', regexp_replace(arg, '[\s[:punct:]]','','g')));
455 -- Drop indexes if present, so that we can re-create them
456 DROP INDEX IF EXISTS actor.actor_usr_first_given_name_unaccent_idx;
457 DROP INDEX IF EXISTS actor.actor_usr_second_given_name_unaccent_idx;
458 DROP INDEX IF EXISTS actor.actor_usr_family_name_unaccent_idx;
459 DROP INDEX IF EXISTS actor.actor_usr_usrname_unaccent_idx;
461 -- Create (or re-create) indexes -- they may be missing if pg_restore failed to create
462 -- them due to the previously unqualified call to unaccent()
463 CREATE INDEX actor_usr_first_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(first_given_name));
464 CREATE INDEX actor_usr_second_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(second_given_name));
465 CREATE INDEX actor_usr_family_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(family_name));
466 CREATE INDEX actor_usr_usrname_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(usrname));
469 SELECT evergreen.upgrade_deps_block_check('1084', :eg_version);
471 INSERT INTO config.usr_setting_type (name, label, description, datatype)
472 VALUES ('webstaff.cat.copy.templates', 'Web Client Copy Editor Templates', 'Web Client Copy Editor Templates', 'object');