4 SELECT evergreen.upgrade_deps_block_check('1090', :eg_version);
6 ALTER TABLE biblio.record_entry
7 ADD COLUMN merge_date TIMESTAMP WITH TIME ZONE,
8 ADD COLUMN merged_to BIGINT REFERENCES biblio.record_entry(id);
10 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
12 moved_objects INT := 0;
13 source_cn asset.call_number%ROWTYPE;
14 target_cn asset.call_number%ROWTYPE;
15 metarec metabib.metarecord%ROWTYPE;
16 hold action.hold_request%ROWTYPE;
17 ser_rec serial.record_entry%ROWTYPE;
18 ser_sub serial.subscription%ROWTYPE;
19 acq_lineitem acq.lineitem%ROWTYPE;
20 acq_request acq.user_request%ROWTYPE;
21 booking booking.resource_type%ROWTYPE;
22 source_part biblio.monograph_part%ROWTYPE;
23 target_part biblio.monograph_part%ROWTYPE;
24 multi_home biblio.peer_bib_copy_map%ROWTYPE;
31 -- move any 856 entries on records that have at least one MARC-mapped URI entry
32 SELECT INTO uri_count COUNT(*)
33 FROM asset.uri_call_number_map m
34 JOIN asset.call_number cn ON (m.call_number = cn.id)
35 WHERE cn.record = source_record;
39 -- This returns more nodes than you might expect:
40 -- 7 instead of 1 for an 856 with $u $y $9
41 SELECT COUNT(*) INTO counter
42 FROM oils_xpath_table(
45 'biblio.record_entry',
47 'id=' || source_record
50 FOR i IN 1 .. counter LOOP
51 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
53 ' ind1="' || FIRST(ind1) || '"' ||
54 ' ind2="' || FIRST(ind2) || '">' ||
56 '<subfield code="' || subfield || '">' ||
59 regexp_replace(data,'&','&','g'),
63 ) || '</subfield>', ''
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 prefix = source_cn.prefix
110 AND suffix = source_cn.suffix
111 AND owning_lib = source_cn.owning_lib
112 AND record = target_record
115 -- ... and if there's a conflicting one on the target ...
118 -- ... move the copies to that, and ...
120 SET call_number = target_cn.id
121 WHERE call_number = source_cn.id;
123 -- ... move V holds to the move-target call number
124 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
126 UPDATE action.hold_request
127 SET target = target_cn.id
130 moved_objects := moved_objects + 1;
133 UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
137 -- ... just move the call number to the target record
138 UPDATE asset.call_number
139 SET record = target_record
140 WHERE id = source_cn.id;
143 moved_objects := moved_objects + 1;
146 -- Find T holds targeting the source record ...
147 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
149 -- ... and move them to the target record
150 UPDATE action.hold_request
151 SET target = target_record
154 moved_objects := moved_objects + 1;
157 -- Find serial records targeting the source record ...
158 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
159 -- ... and move them to the target record
160 UPDATE serial.record_entry
161 SET record = target_record
162 WHERE id = ser_rec.id;
164 moved_objects := moved_objects + 1;
167 -- Find serial subscriptions targeting the source record ...
168 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
169 -- ... and move them to the target record
170 UPDATE serial.subscription
171 SET record_entry = target_record
172 WHERE id = ser_sub.id;
174 moved_objects := moved_objects + 1;
177 -- Find booking resource types targeting the source record ...
178 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
179 -- ... and move them to the target record
180 UPDATE booking.resource_type
181 SET record = target_record
182 WHERE id = booking.id;
184 moved_objects := moved_objects + 1;
187 -- Find acq lineitems targeting the source record ...
188 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
189 -- ... and move them to the target record
191 SET eg_bib_id = target_record
192 WHERE id = acq_lineitem.id;
194 moved_objects := moved_objects + 1;
197 -- Find acq user purchase requests targeting the source record ...
198 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
199 -- ... and move them to the target record
200 UPDATE acq.user_request
201 SET eg_bib = target_record
202 WHERE id = acq_request.id;
204 moved_objects := moved_objects + 1;
207 -- Find parts attached to the source ...
208 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
210 SELECT INTO target_part *
211 FROM biblio.monograph_part
212 WHERE label = source_part.label
213 AND record = target_record;
215 -- ... and if there's a conflicting one on the target ...
218 -- ... move the copy-part maps to that, and ...
219 UPDATE asset.copy_part_map
220 SET part = target_part.id
221 WHERE part = source_part.id;
223 -- ... move P holds to the move-target part
224 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
226 UPDATE action.hold_request
227 SET target = target_part.id
230 moved_objects := moved_objects + 1;
235 -- ... just move the part to the target record
236 UPDATE biblio.monograph_part
237 SET record = target_record
238 WHERE id = source_part.id;
241 moved_objects := moved_objects + 1;
244 -- Find multi_home items attached to the source ...
245 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
246 -- ... and move them to the target record
247 UPDATE biblio.peer_bib_copy_map
248 SET peer_record = target_record
249 WHERE id = multi_home.id;
251 moved_objects := moved_objects + 1;
254 -- And delete mappings where the item's home bib was merged with the peer bib
255 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
256 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
257 FROM asset.copy WHERE id = target_copy
260 -- Apply merge tracking
261 UPDATE biblio.record_entry
262 SET merge_date = NOW() WHERE id = target_record;
264 UPDATE biblio.record_entry
265 SET merge_date = NOW(), merged_to = target_record
266 WHERE id = source_record;
268 -- Finally, "delete" the source record
269 DELETE FROM biblio.record_entry WHERE id = source_record;
271 -- That's all, folks!
272 RETURN moved_objects;
274 $func$ LANGUAGE plpgsql;