1 -- Evergreen DB patch 0648.function.merge_record_assets_meets_acq.sql
6 -- check whether patch can be applied
7 SELECT evergreen.upgrade_deps_block_check('0648', :eg_version);
9 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
11 moved_objects INT := 0;
12 source_cn asset.call_number%ROWTYPE;
13 target_cn asset.call_number%ROWTYPE;
14 metarec metabib.metarecord%ROWTYPE;
15 hold action.hold_request%ROWTYPE;
16 ser_rec serial.record_entry%ROWTYPE;
17 ser_sub serial.subscription%ROWTYPE;
18 acq_lineitem acq.lineitem%ROWTYPE;
19 acq_request acq.user_request%ROWTYPE;
20 booking booking.resource_type%ROWTYPE;
21 source_part biblio.monograph_part%ROWTYPE;
22 target_part biblio.monograph_part%ROWTYPE;
23 multi_home biblio.peer_bib_copy_map%ROWTYPE;
30 -- move any 856 entries on records that have at least one MARC-mapped URI entry
31 SELECT INTO uri_count COUNT(*)
32 FROM asset.uri_call_number_map m
33 JOIN asset.call_number cn ON (m.call_number = cn.id)
34 WHERE cn.record = source_record;
38 SELECT COUNT(*) INTO counter
39 FROM oils_xpath_table(
42 'biblio.record_entry',
44 'id=' || source_record
47 FOR i IN 1 .. counter LOOP
48 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
50 ' ind1="' || FIRST(ind1) || '"' ||
51 ' ind2="' || FIRST(ind2) || '">' ||
54 '<subfield code="' || subfield || '">' ||
57 regexp_replace(data,'&','&','g'),
63 ) || '</datafield>' INTO uri_datafield
64 FROM oils_xpath_table(
67 'biblio.record_entry',
68 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
69 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
70 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
71 '//*[@tag="856"][position()=' || i || ']/*[@code]',
72 'id=' || source_record
73 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
75 uri_text := uri_text || uri_datafield;
78 IF uri_text <> '' THEN
79 UPDATE biblio.record_entry
80 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
81 WHERE id = target_record;
86 -- Find and move metarecords to the target record
88 FROM metabib.metarecord
89 WHERE master_record = source_record;
92 UPDATE metabib.metarecord
93 SET master_record = target_record,
95 WHERE id = metarec.id;
97 moved_objects := moved_objects + 1;
100 -- Find call numbers attached to the source ...
101 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
103 SELECT INTO target_cn *
104 FROM asset.call_number
105 WHERE label = source_cn.label
106 AND owning_lib = source_cn.owning_lib
107 AND record = target_record;
109 -- ... and if there's a conflicting one on the target ...
112 -- ... move the copies to that, and ...
114 SET call_number = target_cn.id
115 WHERE call_number = source_cn.id;
117 -- ... move V holds to the move-target call number
118 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
120 UPDATE action.hold_request
121 SET target = target_cn.id
124 moved_objects := moved_objects + 1;
129 -- ... just move the call number to the target record
130 UPDATE asset.call_number
131 SET record = target_record
132 WHERE id = source_cn.id;
135 moved_objects := moved_objects + 1;
138 -- Find T holds targeting the source record ...
139 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
141 -- ... and move them to the target record
142 UPDATE action.hold_request
143 SET target = target_record
146 moved_objects := moved_objects + 1;
149 -- Find serial records targeting the source record ...
150 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
151 -- ... and move them to the target record
152 UPDATE serial.record_entry
153 SET record = target_record
154 WHERE id = ser_rec.id;
156 moved_objects := moved_objects + 1;
159 -- Find serial subscriptions targeting the source record ...
160 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
161 -- ... and move them to the target record
162 UPDATE serial.subscription
163 SET record_entry = target_record
164 WHERE id = ser_sub.id;
166 moved_objects := moved_objects + 1;
169 -- Find booking resource types targeting the source record ...
170 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
171 -- ... and move them to the target record
172 UPDATE booking.resource_type
173 SET record = target_record
174 WHERE id = booking.id;
176 moved_objects := moved_objects + 1;
179 -- Find acq lineitems targeting the source record ...
180 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
181 -- ... and move them to the target record
183 SET eg_bib_id = target_record
184 WHERE id = acq_lineitem.id;
186 moved_objects := moved_objects + 1;
189 -- Find acq user purchase requests targeting the source record ...
190 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
191 -- ... and move them to the target record
192 UPDATE acq.user_request
193 SET eg_bib = target_record
194 WHERE id = acq_request.id;
196 moved_objects := moved_objects + 1;
199 -- Find parts attached to the source ...
200 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
202 SELECT INTO target_part *
203 FROM biblio.monograph_part
204 WHERE label = source_part.label
205 AND record = target_record;
207 -- ... and if there's a conflicting one on the target ...
210 -- ... move the copy-part maps to that, and ...
211 UPDATE asset.copy_part_map
212 SET part = target_part.id
213 WHERE part = source_part.id;
215 -- ... move P holds to the move-target part
216 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
218 UPDATE action.hold_request
219 SET target = target_part.id
222 moved_objects := moved_objects + 1;
227 -- ... just move the part to the target record
228 UPDATE biblio.monograph_part
229 SET record = target_record
230 WHERE id = source_part.id;
233 moved_objects := moved_objects + 1;
236 -- Find multi_home items attached to the source ...
237 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
238 -- ... and move them to the target record
239 UPDATE biblio.peer_bib_copy_map
240 SET peer_record = target_record
241 WHERE id = multi_home.id;
243 moved_objects := moved_objects + 1;
246 -- And delete mappings where the item's home bib was merged with the peer bib
247 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
248 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
249 FROM asset.copy WHERE id = target_copy
252 -- Finally, "delete" the source record
253 DELETE FROM biblio.record_entry WHERE id = source_record;
255 -- That's all, folks!
256 RETURN moved_objects;
258 $func$ LANGUAGE plpgsql;