3 SELECT evergreen.upgrade_deps_block_check('1107', :eg_version);
5 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
7 moved_objects INT := 0;
8 source_cn asset.call_number%ROWTYPE;
9 target_cn asset.call_number%ROWTYPE;
10 metarec metabib.metarecord%ROWTYPE;
11 hold action.hold_request%ROWTYPE;
12 ser_rec serial.record_entry%ROWTYPE;
13 ser_sub serial.subscription%ROWTYPE;
14 acq_lineitem acq.lineitem%ROWTYPE;
15 acq_request acq.user_request%ROWTYPE;
16 booking booking.resource_type%ROWTYPE;
17 source_part biblio.monograph_part%ROWTYPE;
18 target_part biblio.monograph_part%ROWTYPE;
19 multi_home biblio.peer_bib_copy_map%ROWTYPE;
26 -- move any 856 entries on records that have at least one MARC-mapped URI entry
27 SELECT INTO uri_count COUNT(*)
28 FROM asset.uri_call_number_map m
29 JOIN asset.call_number cn ON (m.call_number = cn.id)
30 WHERE cn.record = source_record;
34 -- This returns more nodes than you might expect:
35 -- 7 instead of 1 for an 856 with $u $y $9
36 SELECT COUNT(*) INTO counter
37 FROM oils_xpath_table(
40 'biblio.record_entry',
42 'id=' || source_record
45 FOR i IN 1 .. counter LOOP
46 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
48 ' ind1="' || FIRST(ind1) || '"' ||
49 ' ind2="' || FIRST(ind2) || '">' ||
51 '<subfield code="' || subfield || '">' ||
54 regexp_replace(data,'&','&','g'),
58 ) || '</subfield>', ''
59 ) || '</datafield>' INTO uri_datafield
60 FROM oils_xpath_table(
63 'biblio.record_entry',
64 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
65 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
66 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
67 '//*[@tag="856"][position()=' || i || ']/*[@code]',
68 'id=' || source_record
69 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
71 -- As most of the results will be NULL, protect against NULLifying
72 -- the valid content that we do generate
73 uri_text := uri_text || COALESCE(uri_datafield, '');
76 IF uri_text <> '' THEN
77 UPDATE biblio.record_entry
78 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
79 WHERE id = target_record;
84 -- Find and move metarecords to the target record
86 FROM metabib.metarecord
87 WHERE master_record = source_record;
90 UPDATE metabib.metarecord
91 SET master_record = target_record,
93 WHERE id = metarec.id;
95 moved_objects := moved_objects + 1;
98 -- Find call numbers attached to the source ...
99 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
101 SELECT INTO target_cn *
102 FROM asset.call_number
103 WHERE label = source_cn.label
104 AND prefix = source_cn.prefix
105 AND suffix = source_cn.suffix
106 AND owning_lib = source_cn.owning_lib
107 AND record = target_record
110 -- ... and if there's a conflicting one on the target ...
113 -- ... move the copies to that, and ...
115 SET call_number = target_cn.id
116 WHERE call_number = source_cn.id;
118 -- ... move V holds to the move-target call number
119 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
121 UPDATE action.hold_request
122 SET target = target_cn.id
125 moved_objects := moved_objects + 1;
128 UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
132 -- ... just move the call number to the target record
133 UPDATE asset.call_number
134 SET record = target_record
135 WHERE id = source_cn.id;
138 moved_objects := moved_objects + 1;
141 -- Find T holds targeting the source record ...
142 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
144 -- ... and move them to the target record
145 UPDATE action.hold_request
146 SET target = target_record
149 moved_objects := moved_objects + 1;
152 -- Find serial records targeting the source record ...
153 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
154 -- ... and move them to the target record
155 UPDATE serial.record_entry
156 SET record = target_record
157 WHERE id = ser_rec.id;
159 moved_objects := moved_objects + 1;
162 -- Find serial subscriptions targeting the source record ...
163 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
164 -- ... and move them to the target record
165 UPDATE serial.subscription
166 SET record_entry = target_record
167 WHERE id = ser_sub.id;
169 moved_objects := moved_objects + 1;
172 -- Find booking resource types targeting the source record ...
173 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
174 -- ... and move them to the target record
175 UPDATE booking.resource_type
176 SET record = target_record
177 WHERE id = booking.id;
179 moved_objects := moved_objects + 1;
182 -- Find acq lineitems targeting the source record ...
183 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
184 -- ... and move them to the target record
186 SET eg_bib_id = target_record
187 WHERE id = acq_lineitem.id;
189 moved_objects := moved_objects + 1;
192 -- Find acq user purchase requests targeting the source record ...
193 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
194 -- ... and move them to the target record
195 UPDATE acq.user_request
196 SET eg_bib = target_record
197 WHERE id = acq_request.id;
199 moved_objects := moved_objects + 1;
202 -- Find parts attached to the source ...
203 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
205 SELECT INTO target_part *
206 FROM biblio.monograph_part
207 WHERE label = source_part.label
208 AND record = target_record;
210 -- ... and if there's a conflicting one on the target ...
213 -- ... move the copy-part maps to that, and ...
214 UPDATE asset.copy_part_map
215 SET part = target_part.id
216 WHERE part = source_part.id;
218 -- ... move P holds to the move-target part
219 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
221 UPDATE action.hold_request
222 SET target = target_part.id
225 moved_objects := moved_objects + 1;
230 -- ... just move the part to the target record
231 UPDATE biblio.monograph_part
232 SET record = target_record
233 WHERE id = source_part.id;
236 moved_objects := moved_objects + 1;
239 -- Find multi_home items attached to the source ...
240 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
241 -- ... and move them to the target record
242 UPDATE biblio.peer_bib_copy_map
243 SET peer_record = target_record
244 WHERE id = multi_home.id;
246 moved_objects := moved_objects + 1;
249 -- And delete mappings where the item's home bib was merged with the peer bib
250 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
251 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
252 FROM asset.copy WHERE id = target_copy
255 -- replace book bag entries of source_record with target_record
256 UPDATE container.biblio_record_entry_bucket_item
257 SET target_biblio_record_entry = target_record
258 WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
259 AND target_biblio_record_entry = source_record;
261 -- Finally, "delete" the source record
262 DELETE FROM biblio.record_entry WHERE id = source_record;
264 -- That's all, folks!
265 RETURN moved_objects;
267 $func$ LANGUAGE plpgsql;