]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.2.6-2.2.7-upgrade-db.sql
DB upgrade script for 2.2.7
[working/Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.2.6-2.2.7-upgrade-db.sql
1 --Upgrade Script for 2.2.6 to 2.2.7
2 \set eg_version '''2.2.7'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.2.7', :eg_version);
5 -- Evergreen DB patch XXXX.function.merge_record_assets_deleted_call_numbers.sql
6 --
7
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0761', :eg_version);
10
11 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
12 DECLARE
13     moved_objects INT := 0;
14     source_cn     asset.call_number%ROWTYPE;
15     target_cn     asset.call_number%ROWTYPE;
16     metarec       metabib.metarecord%ROWTYPE;
17     hold          action.hold_request%ROWTYPE;
18     ser_rec       serial.record_entry%ROWTYPE;
19     ser_sub       serial.subscription%ROWTYPE;
20     acq_lineitem  acq.lineitem%ROWTYPE;
21     acq_request   acq.user_request%ROWTYPE;
22     booking       booking.resource_type%ROWTYPE;
23     source_part   biblio.monograph_part%ROWTYPE;
24     target_part   biblio.monograph_part%ROWTYPE;
25     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
26     uri_count     INT := 0;
27     counter       INT := 0;
28     uri_datafield TEXT;
29     uri_text      TEXT := '';
30 BEGIN
31
32     -- move any 856 entries on records that have at least one MARC-mapped URI entry
33     SELECT  INTO uri_count COUNT(*)
34       FROM  asset.uri_call_number_map m
35             JOIN asset.call_number cn ON (m.call_number = cn.id)
36       WHERE cn.record = source_record;
37
38     IF uri_count > 0 THEN
39         
40         -- This returns more nodes than you might expect:
41         -- 7 instead of 1 for an 856 with $u $y $9
42         SELECT  COUNT(*) INTO counter
43           FROM  oils_xpath_table(
44                     'id',
45                     'marc',
46                     'biblio.record_entry',
47                     '//*[@tag="856"]',
48                     'id=' || source_record
49                 ) as t(i int,c text);
50     
51         FOR i IN 1 .. counter LOOP
52             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
53                         ' tag="856"' ||
54                         ' ind1="' || FIRST(ind1) || '"'  ||
55                         ' ind2="' || FIRST(ind2) || '">' ||
56                         array_to_string(
57                             array_accum(
58                                 '<subfield code="' || subfield || '">' ||
59                                 regexp_replace(
60                                     regexp_replace(
61                                         regexp_replace(data,'&','&amp;','g'),
62                                         '>', '&gt;', 'g'
63                                     ),
64                                     '<', '&lt;', 'g'
65                                 ) || '</subfield>'
66                             ), ''
67                         ) || '</datafield>' INTO uri_datafield
68               FROM  oils_xpath_table(
69                         'id',
70                         'marc',
71                         'biblio.record_entry',
72                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
73                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
74                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
75                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
76                         'id=' || source_record
77                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
78
79             -- As most of the results will be NULL, protect against NULLifying
80             -- the valid content that we do generate
81             uri_text := uri_text || COALESCE(uri_datafield, '');
82         END LOOP;
83
84         IF uri_text <> '' THEN
85             UPDATE  biblio.record_entry
86               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
87               WHERE id = target_record;
88         END IF;
89
90     END IF;
91
92         -- Find and move metarecords to the target record
93         SELECT  INTO metarec *
94           FROM  metabib.metarecord
95           WHERE master_record = source_record;
96
97         IF FOUND THEN
98                 UPDATE  metabib.metarecord
99                   SET   master_record = target_record,
100                         mods = NULL
101                   WHERE id = metarec.id;
102
103                 moved_objects := moved_objects + 1;
104         END IF;
105
106         -- Find call numbers attached to the source ...
107         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
108
109                 SELECT  INTO target_cn *
110                   FROM  asset.call_number
111                   WHERE label = source_cn.label
112                         AND owning_lib = source_cn.owning_lib
113                         AND record = target_record
114                         AND NOT deleted;
115
116                 -- ... and if there's a conflicting one on the target ...
117                 IF FOUND THEN
118
119                         -- ... move the copies to that, and ...
120                         UPDATE  asset.copy
121                           SET   call_number = target_cn.id
122                           WHERE call_number = source_cn.id;
123
124                         -- ... move V holds to the move-target call number
125                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
126                 
127                                 UPDATE  action.hold_request
128                                   SET   target = target_cn.id
129                                   WHERE id = hold.id;
130                 
131                                 moved_objects := moved_objects + 1;
132                         END LOOP;
133
134                 -- ... if not ...
135                 ELSE
136                         -- ... just move the call number to the target record
137                         UPDATE  asset.call_number
138                           SET   record = target_record
139                           WHERE id = source_cn.id;
140                 END IF;
141
142                 moved_objects := moved_objects + 1;
143         END LOOP;
144
145         -- Find T holds targeting the source record ...
146         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
147
148                 -- ... and move them to the target record
149                 UPDATE  action.hold_request
150                   SET   target = target_record
151                   WHERE id = hold.id;
152
153                 moved_objects := moved_objects + 1;
154         END LOOP;
155
156         -- Find serial records targeting the source record ...
157         FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
158                 -- ... and move them to the target record
159                 UPDATE  serial.record_entry
160                   SET   record = target_record
161                   WHERE id = ser_rec.id;
162
163                 moved_objects := moved_objects + 1;
164         END LOOP;
165
166         -- Find serial subscriptions targeting the source record ...
167         FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
168                 -- ... and move them to the target record
169                 UPDATE  serial.subscription
170                   SET   record_entry = target_record
171                   WHERE id = ser_sub.id;
172
173                 moved_objects := moved_objects + 1;
174         END LOOP;
175
176         -- Find booking resource types targeting the source record ...
177         FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
178                 -- ... and move them to the target record
179                 UPDATE  booking.resource_type
180                   SET   record = target_record
181                   WHERE id = booking.id;
182
183                 moved_objects := moved_objects + 1;
184         END LOOP;
185
186         -- Find acq lineitems targeting the source record ...
187         FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
188                 -- ... and move them to the target record
189                 UPDATE  acq.lineitem
190                   SET   eg_bib_id = target_record
191                   WHERE id = acq_lineitem.id;
192
193                 moved_objects := moved_objects + 1;
194         END LOOP;
195
196         -- Find acq user purchase requests targeting the source record ...
197         FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
198                 -- ... and move them to the target record
199                 UPDATE  acq.user_request
200                   SET   eg_bib = target_record
201                   WHERE id = acq_request.id;
202
203                 moved_objects := moved_objects + 1;
204         END LOOP;
205
206         -- Find parts attached to the source ...
207         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
208
209                 SELECT  INTO target_part *
210                   FROM  biblio.monograph_part
211                   WHERE label = source_part.label
212                         AND record = target_record;
213
214                 -- ... and if there's a conflicting one on the target ...
215                 IF FOUND THEN
216
217                         -- ... move the copy-part maps to that, and ...
218                         UPDATE  asset.copy_part_map
219                           SET   part = target_part.id
220                           WHERE part = source_part.id;
221
222                         -- ... move P holds to the move-target part
223                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
224                 
225                                 UPDATE  action.hold_request
226                                   SET   target = target_part.id
227                                   WHERE id = hold.id;
228                 
229                                 moved_objects := moved_objects + 1;
230                         END LOOP;
231
232                 -- ... if not ...
233                 ELSE
234                         -- ... just move the part to the target record
235                         UPDATE  biblio.monograph_part
236                           SET   record = target_record
237                           WHERE id = source_part.id;
238                 END IF;
239
240                 moved_objects := moved_objects + 1;
241         END LOOP;
242
243         -- Find multi_home items attached to the source ...
244         FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
245                 -- ... and move them to the target record
246                 UPDATE  biblio.peer_bib_copy_map
247                   SET   peer_record = target_record
248                   WHERE id = multi_home.id;
249
250                 moved_objects := moved_objects + 1;
251         END LOOP;
252
253         -- And delete mappings where the item's home bib was merged with the peer bib
254         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
255                 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
256                 FROM asset.copy WHERE id = target_copy
257         );
258
259     -- Finally, "delete" the source record
260     DELETE FROM biblio.record_entry WHERE id = source_record;
261
262         -- That's all, folks!
263         RETURN moved_objects;
264 END;
265 $func$ LANGUAGE plpgsql;
266
267
268 SELECT evergreen.upgrade_deps_block_check('0764', :eg_version);
269
270 UPDATE config.z3950_source
271     SET host = 'lx2.loc.gov', port = 210, db = 'LCDB'
272     WHERE name = 'loc'
273         AND host = 'z3950.loc.gov'
274         AND port = 7090
275         AND db = 'Voyager';
276
277 UPDATE config.z3950_attr
278     SET format = 6
279     WHERE source = 'loc'
280         AND name = 'lccn'
281         AND format = 1;
282
283
284
285 SELECT evergreen.upgrade_deps_block_check('0770', :eg_version);
286
287 CREATE OR REPLACE FUNCTION evergreen.get_barcodes(select_ou INT, type TEXT, in_barcode TEXT) RETURNS SETOF evergreen.barcode_set AS $$
288 DECLARE
289     cur_barcode TEXT;
290     barcode_len INT;
291     completion_len  INT;
292     asset_barcodes  TEXT[];
293     actor_barcodes  TEXT[];
294     do_asset    BOOL = false;
295     do_serial   BOOL = false;
296     do_booking  BOOL = false;
297     do_actor    BOOL = false;
298     completion_set  config.barcode_completion%ROWTYPE;
299 BEGIN
300
301     IF position('asset' in type) > 0 THEN
302         do_asset = true;
303     END IF;
304     IF position('serial' in type) > 0 THEN
305         do_serial = true;
306     END IF;
307     IF position('booking' in type) > 0 THEN
308         do_booking = true;
309     END IF;
310     IF do_asset OR do_serial OR do_booking THEN
311         asset_barcodes = asset_barcodes || in_barcode;
312     END IF;
313     IF position('actor' in type) > 0 THEN
314         do_actor = true;
315         actor_barcodes = actor_barcodes || in_barcode;
316     END IF;
317
318     barcode_len := length(in_barcode);
319
320     FOR completion_set IN
321       SELECT * FROM config.barcode_completion
322         WHERE active
323         AND org_unit IN (SELECT aou.id FROM actor.org_unit_ancestors(select_ou) aou)
324         LOOP
325         IF completion_set.prefix IS NULL THEN
326             completion_set.prefix := '';
327         END IF;
328         IF completion_set.suffix IS NULL THEN
329             completion_set.suffix := '';
330         END IF;
331         IF completion_set.length = 0 OR completion_set.padding IS NULL OR length(completion_set.padding) = 0 THEN
332             cur_barcode = completion_set.prefix || in_barcode || completion_set.suffix;
333         ELSE
334             completion_len = completion_set.length - length(completion_set.prefix) - length(completion_set.suffix);
335             IF completion_len >= barcode_len THEN
336                 IF completion_set.padding_end THEN
337                     cur_barcode = rpad(in_barcode, completion_len, completion_set.padding);
338                 ELSE
339                     cur_barcode = lpad(in_barcode, completion_len, completion_set.padding);
340                 END IF;
341                 cur_barcode = completion_set.prefix || cur_barcode || completion_set.suffix;
342             END IF;
343         END IF;
344         IF completion_set.actor THEN
345             actor_barcodes = actor_barcodes || cur_barcode;
346         END IF;
347         IF completion_set.asset THEN
348             asset_barcodes = asset_barcodes || cur_barcode;
349         END IF;
350     END LOOP;
351
352     IF do_asset AND do_serial THEN
353         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM ONLY asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
354         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
355     ELSIF do_asset THEN
356         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
357     ELSIF do_serial THEN
358         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
359     END IF;
360     IF do_booking THEN
361         RETURN QUERY SELECT 'booking'::TEXT, id::BIGINT, barcode FROM booking.resource WHERE barcode = ANY(asset_barcodes);
362     END IF;
363     IF do_actor THEN
364         RETURN QUERY SELECT 'actor'::TEXT, c.usr::BIGINT, c.barcode FROM actor.card c JOIN actor.usr u ON c.usr = u.id WHERE
365             ((c.barcode = ANY(actor_barcodes) AND c.active) OR c.barcode = in_barcode) AND NOT u.deleted ORDER BY usr;
366     END IF;
367     RETURN;
368 END;
369 $$ LANGUAGE plpgsql;
370
371 -- Evergreen DB patch 0783.schema.enforce_use_id_for_tcn.sql
372 --
373 -- Sets the TCN value in the biblio.record_entry row to bib ID,
374 -- if the appropriate setting is in place
375 --
376
377 -- check whether patch can be applied
378 SELECT evergreen.upgrade_deps_block_check('0783', :eg_version);
379
380 -- FIXME: add/check SQL statements to perform the upgrade
381 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
382 use strict;
383 use MARC::Record;
384 use MARC::File::XML (BinaryEncoding => 'UTF-8');
385 use MARC::Charset;
386 use Encode;
387 use Unicode::Normalize;
388
389 MARC::Charset->assume_unicode(1);
390
391 my $schema = $_TD->{table_schema};
392 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
393
394 my @old901s = $marc->field('901');
395 $marc->delete_fields(@old901s);
396
397 if ($schema eq 'biblio') {
398     my $tcn_value = $_TD->{new}{tcn_value};
399
400     # Set TCN value to record ID?
401     my $id_as_tcn = spi_exec_query("
402         SELECT enabled
403         FROM config.global_flag
404         WHERE name = 'cat.bib.use_id_for_tcn'
405     ");
406     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
407         $tcn_value = $_TD->{new}{id}; 
408         $_TD->{new}{tcn_value} = $tcn_value;
409     }
410
411     my $new_901 = MARC::Field->new("901", " ", " ",
412         "a" => $tcn_value,
413         "b" => $_TD->{new}{tcn_source},
414         "c" => $_TD->{new}{id},
415         "t" => $schema
416     );
417
418     if ($_TD->{new}{owner}) {
419         $new_901->add_subfields("o" => $_TD->{new}{owner});
420     }
421
422     if ($_TD->{new}{share_depth}) {
423         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
424     }
425
426     $marc->append_fields($new_901);
427 } elsif ($schema eq 'authority') {
428     my $new_901 = MARC::Field->new("901", " ", " ",
429         "c" => $_TD->{new}{id},
430         "t" => $schema,
431     );
432     $marc->append_fields($new_901);
433 } elsif ($schema eq 'serial') {
434     my $new_901 = MARC::Field->new("901", " ", " ",
435         "c" => $_TD->{new}{id},
436         "t" => $schema,
437         "o" => $_TD->{new}{owning_lib},
438     );
439
440     if ($_TD->{new}{record}) {
441         $new_901->add_subfields("r" => $_TD->{new}{record});
442     }
443
444     $marc->append_fields($new_901);
445 } else {
446     my $new_901 = MARC::Field->new("901", " ", " ",
447         "c" => $_TD->{new}{id},
448         "t" => $schema,
449     );
450     $marc->append_fields($new_901);
451 }
452
453 my $xml = $marc->as_xml_record();
454 $xml =~ s/\n//sgo;
455 $xml =~ s/^<\?xml.+\?\s*>//go;
456 $xml =~ s/>\s+</></go;
457 $xml =~ s/\p{Cc}//go;
458
459 # Embed a version of OpenILS::Application::AppUtils->entityize()
460 # to avoid having to set PERL5LIB for PostgreSQL as well
461
462 # If we are going to convert non-ASCII characters to XML entities,
463 # we had better be dealing with a UTF8 string to begin with
464 $xml = decode_utf8($xml);
465
466 $xml = NFC($xml);
467
468 # Convert raw ampersands to entities
469 $xml =~ s/&(?!\S+;)/&amp;/gso;
470
471 # Convert Unicode characters to entities
472 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
473
474 $xml =~ s/[\x00-\x1f]//go;
475 $_TD->{new}{marc} = $xml;
476
477 return "MODIFY";
478 $func$ LANGUAGE PLPERLU;
479
480
481 COMMIT;