1 -- Evergreen DB patch XXXX.function.merge_record_assets_deleted_call_numbers.sql
5 -- check whether patch can be applied
6 SELECT evergreen.upgrade_deps_block_check('0761', :eg_version);
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) || '">' ||
55 '<subfield code="' || subfield || '">' ||
58 regexp_replace(data,'&','&','g'),
64 ) || '</datafield>' INTO uri_datafield
65 FROM oils_xpath_table(
68 'biblio.record_entry',
69 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
70 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
71 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
72 '//*[@tag="856"][position()=' || i || ']/*[@code]',
73 'id=' || source_record
74 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
76 -- As most of the results will be NULL, protect against NULLifying
77 -- the valid content that we do generate
78 uri_text := uri_text || COALESCE(uri_datafield, '');
81 IF uri_text <> '' THEN
82 UPDATE biblio.record_entry
83 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
84 WHERE id = target_record;
89 -- Find and move metarecords to the target record
91 FROM metabib.metarecord
92 WHERE master_record = source_record;
95 UPDATE metabib.metarecord
96 SET master_record = target_record,
98 WHERE id = metarec.id;
100 moved_objects := moved_objects + 1;
103 -- Find call numbers attached to the source ...
104 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
106 SELECT INTO target_cn *
107 FROM asset.call_number
108 WHERE label = source_cn.label
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;
133 -- ... just move the call number to the target record
134 UPDATE asset.call_number
135 SET record = target_record
136 WHERE id = source_cn.id;
139 moved_objects := moved_objects + 1;
142 -- Find T holds targeting the source record ...
143 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
145 -- ... and move them to the target record
146 UPDATE action.hold_request
147 SET target = target_record
150 moved_objects := moved_objects + 1;
153 -- Find serial records targeting the source record ...
154 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
155 -- ... and move them to the target record
156 UPDATE serial.record_entry
157 SET record = target_record
158 WHERE id = ser_rec.id;
160 moved_objects := moved_objects + 1;
163 -- Find serial subscriptions targeting the source record ...
164 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
165 -- ... and move them to the target record
166 UPDATE serial.subscription
167 SET record_entry = target_record
168 WHERE id = ser_sub.id;
170 moved_objects := moved_objects + 1;
173 -- Find booking resource types targeting the source record ...
174 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
175 -- ... and move them to the target record
176 UPDATE booking.resource_type
177 SET record = target_record
178 WHERE id = booking.id;
180 moved_objects := moved_objects + 1;
183 -- Find acq lineitems targeting the source record ...
184 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
185 -- ... and move them to the target record
187 SET eg_bib_id = target_record
188 WHERE id = acq_lineitem.id;
190 moved_objects := moved_objects + 1;
193 -- Find acq user purchase requests targeting the source record ...
194 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
195 -- ... and move them to the target record
196 UPDATE acq.user_request
197 SET eg_bib = target_record
198 WHERE id = acq_request.id;
200 moved_objects := moved_objects + 1;
203 -- Find parts attached to the source ...
204 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
206 SELECT INTO target_part *
207 FROM biblio.monograph_part
208 WHERE label = source_part.label
209 AND record = target_record;
211 -- ... and if there's a conflicting one on the target ...
214 -- ... move the copy-part maps to that, and ...
215 UPDATE asset.copy_part_map
216 SET part = target_part.id
217 WHERE part = source_part.id;
219 -- ... move P holds to the move-target part
220 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
222 UPDATE action.hold_request
223 SET target = target_part.id
226 moved_objects := moved_objects + 1;
231 -- ... just move the part to the target record
232 UPDATE biblio.monograph_part
233 SET record = target_record
234 WHERE id = source_part.id;
237 moved_objects := moved_objects + 1;
240 -- Find multi_home items attached to the source ...
241 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
242 -- ... and move them to the target record
243 UPDATE biblio.peer_bib_copy_map
244 SET peer_record = target_record
245 WHERE id = multi_home.id;
247 moved_objects := moved_objects + 1;
250 -- And delete mappings where the item's home bib was merged with the peer bib
251 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
252 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
253 FROM asset.copy WHERE id = target_copy
256 -- Finally, "delete" the source record
257 DELETE FROM biblio.record_entry WHERE id = source_record;
259 -- That's all, folks!
260 RETURN moved_objects;
262 $func$ LANGUAGE plpgsql;