]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0648.function.merge_record_assets_meets_acq.sql
LP#1248734: (follow-up) add new indexes to schema update script
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0648.function.merge_record_assets_meets_acq.sql
1 -- Evergreen DB patch 0648.function.merge_record_assets_meets_acq.sql
2 --
3 BEGIN;
4
5
6 -- check whether patch can be applied
7 SELECT evergreen.upgrade_deps_block_check('0648', :eg_version);
8
9 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
10 DECLARE
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;
24     uri_count     INT := 0;
25     counter       INT := 0;
26     uri_datafield TEXT;
27     uri_text      TEXT := '';
28 BEGIN
29
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;
35
36     IF uri_count > 0 THEN
37         
38         SELECT  COUNT(*) INTO counter
39           FROM  oils_xpath_table(
40                     'id',
41                     'marc',
42                     'biblio.record_entry',
43                     '//*[@tag="856"]',
44                     'id=' || source_record
45                 ) as t(i int,c text);
46     
47         FOR i IN 1 .. counter LOOP
48             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
49                         ' tag="856"' ||
50                         ' ind1="' || FIRST(ind1) || '"'  ||
51                         ' ind2="' || FIRST(ind2) || '">' ||
52                         array_to_string(
53                             array_accum(
54                                 '<subfield code="' || subfield || '">' ||
55                                 regexp_replace(
56                                     regexp_replace(
57                                         regexp_replace(data,'&','&amp;','g'),
58                                         '>', '&gt;', 'g'
59                                     ),
60                                     '<', '&lt;', 'g'
61                                 ) || '</subfield>'
62                             ), ''
63                         ) || '</datafield>' INTO uri_datafield
64               FROM  oils_xpath_table(
65                         'id',
66                         'marc',
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);
74
75             uri_text := uri_text || uri_datafield;
76         END LOOP;
77
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;
82         END IF;
83
84     END IF;
85
86         -- Find and move metarecords to the target record
87         SELECT  INTO metarec *
88           FROM  metabib.metarecord
89           WHERE master_record = source_record;
90
91         IF FOUND THEN
92                 UPDATE  metabib.metarecord
93                   SET   master_record = target_record,
94                         mods = NULL
95                   WHERE id = metarec.id;
96
97                 moved_objects := moved_objects + 1;
98         END IF;
99
100         -- Find call numbers attached to the source ...
101         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
102
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;
108
109                 -- ... and if there's a conflicting one on the target ...
110                 IF FOUND THEN
111
112                         -- ... move the copies to that, and ...
113                         UPDATE  asset.copy
114                           SET   call_number = target_cn.id
115                           WHERE call_number = source_cn.id;
116
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
119                 
120                                 UPDATE  action.hold_request
121                                   SET   target = target_cn.id
122                                   WHERE id = hold.id;
123                 
124                                 moved_objects := moved_objects + 1;
125                         END LOOP;
126
127                 -- ... if not ...
128                 ELSE
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;
133                 END IF;
134
135                 moved_objects := moved_objects + 1;
136         END LOOP;
137
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
140
141                 -- ... and move them to the target record
142                 UPDATE  action.hold_request
143                   SET   target = target_record
144                   WHERE id = hold.id;
145
146                 moved_objects := moved_objects + 1;
147         END LOOP;
148
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;
155
156                 moved_objects := moved_objects + 1;
157         END LOOP;
158
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;
165
166                 moved_objects := moved_objects + 1;
167         END LOOP;
168
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;
175
176                 moved_objects := moved_objects + 1;
177         END LOOP;
178
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
182                 UPDATE  acq.lineitem
183                   SET   eg_bib_id = target_record
184                   WHERE id = acq_lineitem.id;
185
186                 moved_objects := moved_objects + 1;
187         END LOOP;
188
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;
195
196                 moved_objects := moved_objects + 1;
197         END LOOP;
198
199         -- Find parts attached to the source ...
200         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
201
202                 SELECT  INTO target_part *
203                   FROM  biblio.monograph_part
204                   WHERE label = source_part.label
205                         AND record = target_record;
206
207                 -- ... and if there's a conflicting one on the target ...
208                 IF FOUND THEN
209
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;
214
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
217                 
218                                 UPDATE  action.hold_request
219                                   SET   target = target_part.id
220                                   WHERE id = hold.id;
221                 
222                                 moved_objects := moved_objects + 1;
223                         END LOOP;
224
225                 -- ... if not ...
226                 ELSE
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;
231                 END IF;
232
233                 moved_objects := moved_objects + 1;
234         END LOOP;
235
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;
242
243                 moved_objects := moved_objects + 1;
244         END LOOP;
245
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
250         );
251
252     -- Finally, "delete" the source record
253     DELETE FROM biblio.record_entry WHERE id = source_record;
254
255         -- That's all, folks!
256         RETURN moved_objects;
257 END;
258 $func$ LANGUAGE plpgsql;
259
260
261
262
263 COMMIT;