]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1079.schema.fix_asset_merge.sql
LP1779158 Vandelay workstation setting repairs/additions
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1079.schema.fix_asset_merge.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1079', :eg_version); -- rhamby/cesardv/gmcharlt
4
5 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
6 DECLARE
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;
20     uri_count     INT := 0;
21     counter       INT := 0;
22     uri_datafield TEXT;
23     uri_text      TEXT := '';
24 BEGIN
25
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;
31
32     IF uri_count > 0 THEN
33         
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(
38                     'id',
39                     'marc',
40                     'biblio.record_entry',
41                     '//*[@tag="856"]',
42                     'id=' || source_record
43                 ) as t(i int,c text);
44     
45         FOR i IN 1 .. counter LOOP
46             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
47                         ' tag="856"' ||
48                         ' ind1="' || FIRST(ind1) || '"'  ||
49                         ' ind2="' || FIRST(ind2) || '">' ||
50                         STRING_AGG(
51                             '<subfield code="' || subfield || '">' ||
52                             regexp_replace(
53                                 regexp_replace(
54                                     regexp_replace(data,'&','&amp;','g'),
55                                     '>', '&gt;', 'g'
56                                 ),
57                                 '<', '&lt;', 'g'
58                             ) || '</subfield>', ''
59                         ) || '</datafield>' INTO uri_datafield
60               FROM  oils_xpath_table(
61                         'id',
62                         'marc',
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);
70
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, '');
74         END LOOP;
75
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;
80         END IF;
81
82     END IF;
83
84         -- Find and move metarecords to the target record
85         SELECT  INTO metarec *
86           FROM  metabib.metarecord
87           WHERE master_record = source_record;
88
89         IF FOUND THEN
90                 UPDATE  metabib.metarecord
91                   SET   master_record = target_record,
92                         mods = NULL
93                   WHERE id = metarec.id;
94
95                 moved_objects := moved_objects + 1;
96         END IF;
97
98         -- Find call numbers attached to the source ...
99         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
100
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
108                         AND NOT deleted;
109
110                 -- ... and if there's a conflicting one on the target ...
111                 IF FOUND THEN
112
113                         -- ... move the copies to that, and ...
114                         UPDATE  asset.copy
115                           SET   call_number = target_cn.id
116                           WHERE call_number = source_cn.id;
117
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
120                 
121                                 UPDATE  action.hold_request
122                                   SET   target = target_cn.id
123                                   WHERE id = hold.id;
124                 
125                                 moved_objects := moved_objects + 1;
126                         END LOOP;
127         
128             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
129
130                 -- ... if not ...
131                 ELSE
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;
136                 END IF;
137
138                 moved_objects := moved_objects + 1;
139         END LOOP;
140
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
143
144                 -- ... and move them to the target record
145                 UPDATE  action.hold_request
146                   SET   target = target_record
147                   WHERE id = hold.id;
148
149                 moved_objects := moved_objects + 1;
150         END LOOP;
151
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;
158
159                 moved_objects := moved_objects + 1;
160         END LOOP;
161
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;
168
169                 moved_objects := moved_objects + 1;
170         END LOOP;
171
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;
178
179                 moved_objects := moved_objects + 1;
180         END LOOP;
181
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
185                 UPDATE  acq.lineitem
186                   SET   eg_bib_id = target_record
187                   WHERE id = acq_lineitem.id;
188
189                 moved_objects := moved_objects + 1;
190         END LOOP;
191
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;
198
199                 moved_objects := moved_objects + 1;
200         END LOOP;
201
202         -- Find parts attached to the source ...
203         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
204
205                 SELECT  INTO target_part *
206                   FROM  biblio.monograph_part
207                   WHERE label = source_part.label
208                         AND record = target_record;
209
210                 -- ... and if there's a conflicting one on the target ...
211                 IF FOUND THEN
212
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;
217
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
220                 
221                                 UPDATE  action.hold_request
222                                   SET   target = target_part.id
223                                   WHERE id = hold.id;
224                 
225                                 moved_objects := moved_objects + 1;
226                         END LOOP;
227
228                 -- ... if not ...
229                 ELSE
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;
234                 END IF;
235
236                 moved_objects := moved_objects + 1;
237         END LOOP;
238
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;
245
246                 moved_objects := moved_objects + 1;
247         END LOOP;
248
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
253         );
254
255     -- Finally, "delete" the source record
256     DELETE FROM biblio.record_entry WHERE id = source_record;
257
258         -- That's all, folks!
259         RETURN moved_objects;
260 END;
261 $func$ LANGUAGE plpgsql;
262
263 COMMIT;