]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0761.function.merge_record_assets_deleted_call_numbers.sql
LP#1178377: Make bib source optional element from unapi.bre
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0761.function.merge_record_assets_deleted_call_numbers.sql
1 -- Evergreen DB patch XXXX.function.merge_record_assets_deleted_call_numbers.sql
2 --
3 BEGIN;
4
5 -- check whether patch can be applied
6 SELECT evergreen.upgrade_deps_block_check('0761', :eg_version);
7
8 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
9 DECLARE
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;
23     uri_count     INT := 0;
24     counter       INT := 0;
25     uri_datafield TEXT;
26     uri_text      TEXT := '';
27 BEGIN
28
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;
34
35     IF uri_count > 0 THEN
36         
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(
41                     'id',
42                     'marc',
43                     'biblio.record_entry',
44                     '//*[@tag="856"]',
45                     'id=' || source_record
46                 ) as t(i int,c text);
47     
48         FOR i IN 1 .. counter LOOP
49             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
50                         ' tag="856"' ||
51                         ' ind1="' || FIRST(ind1) || '"'  ||
52                         ' ind2="' || FIRST(ind2) || '">' ||
53                         array_to_string(
54                             array_accum(
55                                 '<subfield code="' || subfield || '">' ||
56                                 regexp_replace(
57                                     regexp_replace(
58                                         regexp_replace(data,'&','&amp;','g'),
59                                         '>', '&gt;', 'g'
60                                     ),
61                                     '<', '&lt;', 'g'
62                                 ) || '</subfield>'
63                             ), ''
64                         ) || '</datafield>' INTO uri_datafield
65               FROM  oils_xpath_table(
66                         'id',
67                         'marc',
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);
75
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, '');
79         END LOOP;
80
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;
85         END IF;
86
87     END IF;
88
89         -- Find and move metarecords to the target record
90         SELECT  INTO metarec *
91           FROM  metabib.metarecord
92           WHERE master_record = source_record;
93
94         IF FOUND THEN
95                 UPDATE  metabib.metarecord
96                   SET   master_record = target_record,
97                         mods = NULL
98                   WHERE id = metarec.id;
99
100                 moved_objects := moved_objects + 1;
101         END IF;
102
103         -- Find call numbers attached to the source ...
104         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
105
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
111                         AND NOT deleted;
112
113                 -- ... and if there's a conflicting one on the target ...
114                 IF FOUND THEN
115
116                         -- ... move the copies to that, and ...
117                         UPDATE  asset.copy
118                           SET   call_number = target_cn.id
119                           WHERE call_number = source_cn.id;
120
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
123                 
124                                 UPDATE  action.hold_request
125                                   SET   target = target_cn.id
126                                   WHERE id = hold.id;
127                 
128                                 moved_objects := moved_objects + 1;
129                         END LOOP;
130
131                 -- ... if not ...
132                 ELSE
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;
137                 END IF;
138
139                 moved_objects := moved_objects + 1;
140         END LOOP;
141
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
144
145                 -- ... and move them to the target record
146                 UPDATE  action.hold_request
147                   SET   target = target_record
148                   WHERE id = hold.id;
149
150                 moved_objects := moved_objects + 1;
151         END LOOP;
152
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;
159
160                 moved_objects := moved_objects + 1;
161         END LOOP;
162
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;
169
170                 moved_objects := moved_objects + 1;
171         END LOOP;
172
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;
179
180                 moved_objects := moved_objects + 1;
181         END LOOP;
182
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
186                 UPDATE  acq.lineitem
187                   SET   eg_bib_id = target_record
188                   WHERE id = acq_lineitem.id;
189
190                 moved_objects := moved_objects + 1;
191         END LOOP;
192
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;
199
200                 moved_objects := moved_objects + 1;
201         END LOOP;
202
203         -- Find parts attached to the source ...
204         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
205
206                 SELECT  INTO target_part *
207                   FROM  biblio.monograph_part
208                   WHERE label = source_part.label
209                         AND record = target_record;
210
211                 -- ... and if there's a conflicting one on the target ...
212                 IF FOUND THEN
213
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;
218
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
221                 
222                                 UPDATE  action.hold_request
223                                   SET   target = target_part.id
224                                   WHERE id = hold.id;
225                 
226                                 moved_objects := moved_objects + 1;
227                         END LOOP;
228
229                 -- ... if not ...
230                 ELSE
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;
235                 END IF;
236
237                 moved_objects := moved_objects + 1;
238         END LOOP;
239
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;
246
247                 moved_objects := moved_objects + 1;
248         END LOOP;
249
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
254         );
255
256     -- Finally, "delete" the source record
257     DELETE FROM biblio.record_entry WHERE id = source_record;
258
259         -- That's all, folks!
260         RETURN moved_objects;
261 END;
262 $func$ LANGUAGE plpgsql;
263
264 COMMIT;