]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.6.4-3.6.5-upgrade-db.sql
LP 2061136 follow-up: ng lint --fix
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.6.4-3.6.5-upgrade-db.sql
1 --Upgrade Script for 3.6.4 to 3.6.5
2 \set eg_version '''3.6.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.6.5', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1266', :eg_version);
7
8 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
9 VALUES (
10     'eg.grid.catalog.record.copies', 'gui', 'object',
11     oils_i18n_gettext(
12         'eg.grid.catalog.record.copies',
13         'Grid Config: eg.grid.catalog.record.copies',
14         'cwst', 'label')
15     );
16
17
18 SELECT evergreen.upgrade_deps_block_check('1268', :eg_version);
19
20 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
21 VALUES (
22     'eg.staff.catalog.results.show_more', 'gui', 'bool',
23     oils_i18n_gettext(
24         'eg.staff.catalog.results.show_more',
25         'Show more details in Angular staff catalog',
26         'cwst', 'label'
27     )
28 );
29
30
31 SELECT evergreen.upgrade_deps_block_check('1269', :eg_version);
32
33 WITH perms_to_add AS
34     (SELECT id FROM
35     permission.perm_list
36     WHERE code IN ('VIEW_BOOKING_RESERVATION', 'VIEW_BOOKING_RESERVATION_ATTR_MAP'))
37
38 INSERT INTO permission.grp_perm_map (grp, perm, depth, grantable)
39     SELECT grp, perms_to_add.id as perm, depth, grantable
40         FROM perms_to_add,
41         permission.grp_perm_map
42         
43         --- Don't add the permissions if they have already been assigned
44         WHERE grp NOT IN
45             (SELECT DISTINCT grp FROM permission.grp_perm_map
46             INNER JOIN perms_to_add ON perm=perms_to_add.id)
47             
48         --- Anybody who can view resources should also see reservations
49         --- at the same level
50         AND perm = (
51             SELECT id
52                 FROM permission.perm_list
53                 WHERE code = 'VIEW_BOOKING_RESOURCE'
54         );
55
56
57
58 SELECT evergreen.upgrade_deps_block_check('1270', :eg_version);
59
60 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'BKS', 39, 1, ' ');
61 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'COM', 39, 1, ' ');
62 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'MAP', 39, 1, ' ');
63 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'MIX', 39, 1, ' ');
64 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'REC', 39, 1, ' ');
65 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'SCO', 39, 1, ' ');
66 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'SER', 39, 1, ' ');
67 INSERT INTO config.marc21_ff_pos_map (fixed_field, tag, rec_type,start_pos, length, default_val) VALUES ('Srce', '008', 'VIS', 39, 1, ' ');
68
69
70 INSERT INTO config.record_attr_definition (name,label,fixed_field) values ('srce','Srce','Srce');
71
72 INSERT INTO config.coded_value_map (id, ctype, code, value) VALUES
73 (1750, 'srce', ' ', oils_i18n_gettext('1750', 'National bibliographic agency', 'ccvm', 'value')),
74 (1751, 'srce', 'c', oils_i18n_gettext('1751', 'Cooperative cataloging program', 'ccvm', 'value')),
75 (1752, 'srce', 'd', oils_i18n_gettext('1752', 'Other', 'ccvm', 'value'));
76
77
78 SELECT evergreen.upgrade_deps_block_check('1272', :eg_version);
79
80 DO $$
81 BEGIN
82
83   PERFORM FROM config.usr_setting_type WHERE name = 'circ.collections.exempt';
84
85   IF NOT FOUND THEN
86
87     INSERT INTO config.usr_setting_type (
88       name,
89       opac_visible,
90       label,
91       description,
92       datatype,
93       reg_default
94     ) VALUES (
95       'circ.collections.exempt',
96       FALSE,
97       oils_i18n_gettext(
98         'circ.collections.exempt',
99         'Collections: Exempt',
100         'cust',
101         'label'
102       ),
103       oils_i18n_gettext(
104         'circ.collections.exempt',
105         'User is exempt from collections tracking/processing',
106         'cust',
107         'description'
108       ),
109       'bool',
110       'false'
111     );
112
113   END IF;
114
115 END
116 $$;
117
118
119 SELECT evergreen.upgrade_deps_block_check('1279', :eg_version);
120
121 UPDATE config.org_unit_setting_type SET fm_class='cnal', datatype='link' WHERE name='ui.patron.default_inet_access_level';
122
123
124
125 SELECT evergreen.upgrade_deps_block_check('1283', :eg_version); -- rhamby/ehardy/jboyer
126
127 UPDATE asset.call_number SET record = -1 WHERE id = -1 AND record != -1;
128
129 CREATE RULE protect_bre_id_neg1 AS ON UPDATE TO biblio.record_entry WHERE OLD.id = -1 DO INSTEAD NOTHING;
130 CREATE RULE protect_acl_id_1 AS ON UPDATE TO asset.copy_location WHERE OLD.id = 1 DO INSTEAD NOTHING;
131 CREATE RULE protect_acn_id_neg1 AS ON UPDATE TO asset.call_number WHERE OLD.id = -1 DO INSTEAD NOTHING;
132
133 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
134 DECLARE
135     moved_objects INT := 0;
136     source_cn     asset.call_number%ROWTYPE;
137     target_cn     asset.call_number%ROWTYPE;
138     metarec       metabib.metarecord%ROWTYPE;
139     hold          action.hold_request%ROWTYPE;
140     ser_rec       serial.record_entry%ROWTYPE;
141     ser_sub       serial.subscription%ROWTYPE;
142     acq_lineitem  acq.lineitem%ROWTYPE;
143     acq_request   acq.user_request%ROWTYPE;
144     booking       booking.resource_type%ROWTYPE;
145     source_part   biblio.monograph_part%ROWTYPE;
146     target_part   biblio.monograph_part%ROWTYPE;
147     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
148     uri_count     INT := 0;
149     counter       INT := 0;
150     uri_datafield TEXT;
151     uri_text      TEXT := '';
152 BEGIN
153
154     -- we don't merge bib -1 
155     IF target_record = -1 OR source_record = -1 THEN 
156        RETURN 0;
157     END IF;
158
159     -- move any 856 entries on records that have at least one MARC-mapped URI entry
160     SELECT  INTO uri_count COUNT(*)
161       FROM  asset.uri_call_number_map m
162             JOIN asset.call_number cn ON (m.call_number = cn.id)
163       WHERE cn.record = source_record;
164
165     IF uri_count > 0 THEN
166         
167         -- This returns more nodes than you might expect:
168         -- 7 instead of 1 for an 856 with $u $y $9
169         SELECT  COUNT(*) INTO counter
170           FROM  oils_xpath_table(
171                     'id',
172                     'marc',
173                     'biblio.record_entry',
174                     '//*[@tag="856"]',
175                     'id=' || source_record
176                 ) as t(i int,c text);
177     
178         FOR i IN 1 .. counter LOOP
179             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
180             ' tag="856"' ||
181             ' ind1="' || FIRST(ind1) || '"'  ||
182             ' ind2="' || FIRST(ind2) || '">' ||
183                         STRING_AGG(
184                             '<subfield code="' || subfield || '">' ||
185                             regexp_replace(
186                                 regexp_replace(
187                                     regexp_replace(data,'&','&amp;','g'),
188                                     '>', '&gt;', 'g'
189                                 ),
190                                 '<', '&lt;', 'g'
191                             ) || '</subfield>', ''
192                         ) || '</datafield>' INTO uri_datafield
193               FROM  oils_xpath_table(
194                         'id',
195                         'marc',
196                         'biblio.record_entry',
197                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
198                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
199                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
200                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
201                         'id=' || source_record
202                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
203
204             -- As most of the results will be NULL, protect against NULLifying
205             -- the valid content that we do generate
206             uri_text := uri_text || COALESCE(uri_datafield, '');
207         END LOOP;
208
209         IF uri_text <> '' THEN
210             UPDATE  biblio.record_entry
211               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
212               WHERE id = target_record;
213         END IF;
214
215     END IF;
216
217     -- Find and move metarecords to the target record
218     SELECT    INTO metarec *
219       FROM    metabib.metarecord
220       WHERE    master_record = source_record;
221
222     IF FOUND THEN
223         UPDATE    metabib.metarecord
224           SET    master_record = target_record,
225             mods = NULL
226           WHERE    id = metarec.id;
227
228         moved_objects := moved_objects + 1;
229     END IF;
230
231     -- Find call numbers attached to the source ...
232     FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
233
234         SELECT    INTO target_cn *
235           FROM    asset.call_number
236           WHERE    label = source_cn.label
237             AND prefix = source_cn.prefix
238             AND suffix = source_cn.suffix
239             AND owning_lib = source_cn.owning_lib
240             AND record = target_record
241             AND NOT deleted;
242
243         -- ... and if there's a conflicting one on the target ...
244         IF FOUND THEN
245
246             -- ... move the copies to that, and ...
247             UPDATE    asset.copy
248               SET    call_number = target_cn.id
249               WHERE    call_number = source_cn.id;
250
251             -- ... move V holds to the move-target call number
252             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
253         
254                 UPDATE    action.hold_request
255                   SET    target = target_cn.id
256                   WHERE    id = hold.id;
257         
258                 moved_objects := moved_objects + 1;
259             END LOOP;
260         
261             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
262
263         -- ... if not ...
264         ELSE
265             -- ... just move the call number to the target record
266             UPDATE    asset.call_number
267               SET    record = target_record
268               WHERE    id = source_cn.id;
269         END IF;
270
271         moved_objects := moved_objects + 1;
272     END LOOP;
273
274     -- Find T holds targeting the source record ...
275     FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
276
277         -- ... and move them to the target record
278         UPDATE    action.hold_request
279           SET    target = target_record
280           WHERE    id = hold.id;
281
282         moved_objects := moved_objects + 1;
283     END LOOP;
284
285     -- Find serial records targeting the source record ...
286     FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
287         -- ... and move them to the target record
288         UPDATE    serial.record_entry
289           SET    record = target_record
290           WHERE    id = ser_rec.id;
291
292         moved_objects := moved_objects + 1;
293     END LOOP;
294
295     -- Find serial subscriptions targeting the source record ...
296     FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
297         -- ... and move them to the target record
298         UPDATE    serial.subscription
299           SET    record_entry = target_record
300           WHERE    id = ser_sub.id;
301
302         moved_objects := moved_objects + 1;
303     END LOOP;
304
305     -- Find booking resource types targeting the source record ...
306     FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
307         -- ... and move them to the target record
308         UPDATE    booking.resource_type
309           SET    record = target_record
310           WHERE    id = booking.id;
311
312         moved_objects := moved_objects + 1;
313     END LOOP;
314
315     -- Find acq lineitems targeting the source record ...
316     FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
317         -- ... and move them to the target record
318         UPDATE    acq.lineitem
319           SET    eg_bib_id = target_record
320           WHERE    id = acq_lineitem.id;
321
322         moved_objects := moved_objects + 1;
323     END LOOP;
324
325     -- Find acq user purchase requests targeting the source record ...
326     FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
327         -- ... and move them to the target record
328         UPDATE    acq.user_request
329           SET    eg_bib = target_record
330           WHERE    id = acq_request.id;
331
332         moved_objects := moved_objects + 1;
333     END LOOP;
334
335     -- Find parts attached to the source ...
336     FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
337
338         SELECT    INTO target_part *
339           FROM    biblio.monograph_part
340           WHERE    label = source_part.label
341             AND record = target_record;
342
343         -- ... and if there's a conflicting one on the target ...
344         IF FOUND THEN
345
346             -- ... move the copy-part maps to that, and ...
347             UPDATE    asset.copy_part_map
348               SET    part = target_part.id
349               WHERE    part = source_part.id;
350
351             -- ... move P holds to the move-target part
352             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
353         
354                 UPDATE    action.hold_request
355                   SET    target = target_part.id
356                   WHERE    id = hold.id;
357         
358                 moved_objects := moved_objects + 1;
359             END LOOP;
360
361         -- ... if not ...
362         ELSE
363             -- ... just move the part to the target record
364             UPDATE    biblio.monograph_part
365               SET    record = target_record
366               WHERE    id = source_part.id;
367         END IF;
368
369         moved_objects := moved_objects + 1;
370     END LOOP;
371
372     -- Find multi_home items attached to the source ...
373     FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
374         -- ... and move them to the target record
375         UPDATE    biblio.peer_bib_copy_map
376           SET    peer_record = target_record
377           WHERE    id = multi_home.id;
378
379         moved_objects := moved_objects + 1;
380     END LOOP;
381
382     -- And delete mappings where the item's home bib was merged with the peer bib
383     DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
384         SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
385         FROM asset.copy WHERE id = target_copy
386     );
387
388     -- Apply merge tracking
389     UPDATE biblio.record_entry 
390         SET merge_date = NOW() WHERE id = target_record;
391
392     UPDATE biblio.record_entry
393         SET merge_date = NOW(), merged_to = target_record
394         WHERE id = source_record;
395
396     -- replace book bag entries of source_record with target_record
397     UPDATE container.biblio_record_entry_bucket_item
398         SET target_biblio_record_entry = target_record
399         WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
400         AND target_biblio_record_entry = source_record;
401
402     -- Finally, "delete" the source record
403     UPDATE biblio.record_entry SET active = FALSE WHERE id = source_record;
404     DELETE FROM biblio.record_entry WHERE id = source_record;
405
406     -- That's all, folks!
407     RETURN moved_objects;
408 END;
409 $func$ LANGUAGE plpgsql;
410
411
412
413
414 SELECT evergreen.upgrade_deps_block_check('1294', :eg_version); -- mmorgan / tlittle / JBoyer
415
416 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
417 VALUES (
418     'eg.grid.admin.local.container.carousel_org_unit', 'gui', 'object',
419     oils_i18n_gettext(
420         'eg.grid.admin.local.container.carousel_org_unit',
421         'Grid Config: eg.grid.admin.local.container.carousel_org_unit',
422         'cwst', 'label'
423     )
424 ), (
425     'eg.grid.admin.container.carousel', 'gui', 'object',
426     oils_i18n_gettext(
427         'eg.grid.admin.container.carousel',
428         'Grid Config: eg.grid.admin.container.carousel',
429         'cwst', 'label'
430     )
431 ), (
432     'eg.grid.admin.server.config.carousel_type', 'gui', 'object',
433     oils_i18n_gettext(
434         'eg.grid.admin.server.config.carousel_type',
435         'Grid Config: eg.grid.admin.server.config.carousel_type',
436         'cwst', 'label'
437     )
438 );
439
440
441 SELECT evergreen.upgrade_deps_block_check('1302', :eg_version);
442
443 UPDATE config.org_unit_setting_type
444     SET description = oils_i18n_gettext(
445         'ui.circ.items_out.longoverdue',
446         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
447         'or "Other/Special Circulations") the circulation '||
448         'should appear while checked out, and B. Whether the circulation should '||
449         'continue to appear in the "Other" tab when checked in with '||
450         'outstanding fines.  '||
451         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
452         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
453         'coust',
454         'description'
455     )
456     WHERE name = 'ui.circ.items_out.longoverdue';
457
458 UPDATE config.org_unit_setting_type
459     set description = oils_i18n_gettext(
460         'ui.circ.items_out.lost',
461         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
462         'or "Other/Special Circulations") the circulation '||
463         'should appear while checked out, and B. Whether the circulation should '||
464         'continue to appear in the "Other" tab when checked in with '||
465         'outstanding fines.  '||
466         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
467         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
468         'coust',
469         'description'
470     )
471     WHERE name = 'ui.circ.items_out.lost';
472
473 UPDATE config.org_unit_setting_type
474     set description = oils_i18n_gettext(
475         'ui.circ.items_out.claimsreturned',
476         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
477         'or "Other/Special Circulations") the circulation '||
478         'should appear while checked out, and B. Whether the circulation should '||
479         'continue to appear in the "Other" tab when checked in with '||
480         'outstanding fines.  '||
481         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
482         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
483         'coust',
484         'description'
485     )
486     WHERE name = 'ui.circ.items_out.claimsreturned';
487
488
489 SELECT evergreen.upgrade_deps_block_check('1303', :eg_version);
490
491 DROP INDEX authority.authority_full_rec_value_index;
492 CREATE INDEX authority_full_rec_value_index ON authority.full_rec (SUBSTRING(value FOR 1024));
493
494 DROP INDEX authority.authority_full_rec_value_tpo_index;
495 CREATE INDEX authority_full_rec_value_tpo_index ON authority.full_rec (SUBSTRING(value FOR 1024) text_pattern_ops);
496
497
498 COMMIT;
499
500 -- Update auditor tables to catch changes to source tables.
501 --   Can be removed/skipped if there were no schema changes.
502 SELECT auditor.update_auditors();