]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.5.1-3.5.2-upgrade-db.sql
Forward-port 3.5.1-3.5.2 version upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.5.1-3.5.2-upgrade-db.sql
1 --Upgrade Script for 3.5.1 to 3.5.2
2 \set eg_version '''3.5.2'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.5.2', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1214', :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                         STRING_AGG(
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                         ) || '</datafield>' INTO uri_datafield
63               FROM  oils_xpath_table(
64                         'id',
65                         'marc',
66                         'biblio.record_entry',
67                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
68                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
69                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
70                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
71                         'id=' || source_record
72                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
73
74             -- As most of the results will be NULL, protect against NULLifying
75             -- the valid content that we do generate
76             uri_text := uri_text || COALESCE(uri_datafield, '');
77         END LOOP;
78
79         IF uri_text <> '' THEN
80             UPDATE  biblio.record_entry
81               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
82               WHERE id = target_record;
83         END IF;
84
85     END IF;
86
87         -- Find and move metarecords to the target record
88         SELECT  INTO metarec *
89           FROM  metabib.metarecord
90           WHERE master_record = source_record;
91
92         IF FOUND THEN
93                 UPDATE  metabib.metarecord
94                   SET   master_record = target_record,
95                         mods = NULL
96                   WHERE id = metarec.id;
97
98                 moved_objects := moved_objects + 1;
99         END IF;
100
101         -- Find call numbers attached to the source ...
102         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
103
104                 SELECT  INTO target_cn *
105                   FROM  asset.call_number
106                   WHERE label = source_cn.label
107             AND prefix = source_cn.prefix
108             AND suffix = source_cn.suffix
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             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
132
133                 -- ... if not ...
134                 ELSE
135                         -- ... just move the call number to the target record
136                         UPDATE  asset.call_number
137                           SET   record = target_record
138                           WHERE id = source_cn.id;
139                 END IF;
140
141                 moved_objects := moved_objects + 1;
142         END LOOP;
143
144         -- Find T holds targeting the source record ...
145         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
146
147                 -- ... and move them to the target record
148                 UPDATE  action.hold_request
149                   SET   target = target_record
150                   WHERE id = hold.id;
151
152                 moved_objects := moved_objects + 1;
153         END LOOP;
154
155         -- Find serial records targeting the source record ...
156         FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
157                 -- ... and move them to the target record
158                 UPDATE  serial.record_entry
159                   SET   record = target_record
160                   WHERE id = ser_rec.id;
161
162                 moved_objects := moved_objects + 1;
163         END LOOP;
164
165         -- Find serial subscriptions targeting the source record ...
166         FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
167                 -- ... and move them to the target record
168                 UPDATE  serial.subscription
169                   SET   record_entry = target_record
170                   WHERE id = ser_sub.id;
171
172                 moved_objects := moved_objects + 1;
173         END LOOP;
174
175         -- Find booking resource types targeting the source record ...
176         FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
177                 -- ... and move them to the target record
178                 UPDATE  booking.resource_type
179                   SET   record = target_record
180                   WHERE id = booking.id;
181
182                 moved_objects := moved_objects + 1;
183         END LOOP;
184
185         -- Find acq lineitems targeting the source record ...
186         FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
187                 -- ... and move them to the target record
188                 UPDATE  acq.lineitem
189                   SET   eg_bib_id = target_record
190                   WHERE id = acq_lineitem.id;
191
192                 moved_objects := moved_objects + 1;
193         END LOOP;
194
195         -- Find acq user purchase requests targeting the source record ...
196         FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
197                 -- ... and move them to the target record
198                 UPDATE  acq.user_request
199                   SET   eg_bib = target_record
200                   WHERE id = acq_request.id;
201
202                 moved_objects := moved_objects + 1;
203         END LOOP;
204
205         -- Find parts attached to the source ...
206         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
207
208                 SELECT  INTO target_part *
209                   FROM  biblio.monograph_part
210                   WHERE label = source_part.label
211                         AND record = target_record;
212
213                 -- ... and if there's a conflicting one on the target ...
214                 IF FOUND THEN
215
216                         -- ... move the copy-part maps to that, and ...
217                         UPDATE  asset.copy_part_map
218                           SET   part = target_part.id
219                           WHERE part = source_part.id;
220
221                         -- ... move P holds to the move-target part
222                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
223                 
224                                 UPDATE  action.hold_request
225                                   SET   target = target_part.id
226                                   WHERE id = hold.id;
227                 
228                                 moved_objects := moved_objects + 1;
229                         END LOOP;
230
231                 -- ... if not ...
232                 ELSE
233                         -- ... just move the part to the target record
234                         UPDATE  biblio.monograph_part
235                           SET   record = target_record
236                           WHERE id = source_part.id;
237                 END IF;
238
239                 moved_objects := moved_objects + 1;
240         END LOOP;
241
242         -- Find multi_home items attached to the source ...
243         FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
244                 -- ... and move them to the target record
245                 UPDATE  biblio.peer_bib_copy_map
246                   SET   peer_record = target_record
247                   WHERE id = multi_home.id;
248
249                 moved_objects := moved_objects + 1;
250         END LOOP;
251
252         -- And delete mappings where the item's home bib was merged with the peer bib
253         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
254                 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
255                 FROM asset.copy WHERE id = target_copy
256         );
257
258     -- Apply merge tracking
259     UPDATE biblio.record_entry 
260         SET merge_date = NOW() WHERE id = target_record;
261
262     UPDATE biblio.record_entry
263         SET merge_date = NOW(), merged_to = target_record
264         WHERE id = source_record;
265
266     -- replace book bag entries of source_record with target_record
267     UPDATE container.biblio_record_entry_bucket_item
268         SET target_biblio_record_entry = target_record
269         WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
270         AND target_biblio_record_entry = source_record;
271
272     -- Finally, "delete" the source record
273     UPDATE biblio.record_entry SET active = FALSE WHERE id = source_record;
274     DELETE FROM biblio.record_entry WHERE id = source_record;
275
276         -- That's all, folks!
277         RETURN moved_objects;
278 END;
279 $func$ LANGUAGE plpgsql;
280
281
282 SELECT evergreen.upgrade_deps_block_check('1215', :eg_version);
283
284 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
285 VALUES (
286     'eg.orgselect.cat.catalog.wide_holds', 'gui', 'integer',
287     oils_i18n_gettext(
288         'eg.orgselect.cat.catalog.wide_holds',
289         'Default org unit for catalog holds org unit selector',
290         'cwst', 'label'
291     )
292 );
293
294
295
296
297 SELECT evergreen.upgrade_deps_block_check('1228', :eg_version);
298
299 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
300     SELECT  aou.*
301       FROM  actor.org_unit AS aou
302             JOIN (
303                 (SELECT au.id, t.depth FROM actor.org_unit_ancestors($1) AS au JOIN actor.org_unit_type t ON (au.ou_type = t.id))
304                     UNION
305                 (SELECT au.id, t.depth FROM actor.org_unit_descendants($1) AS au JOIN actor.org_unit_type t ON (au.ou_type = t.id))
306             ) AS ad ON (aou.id=ad.id)
307       ORDER BY ad.depth;
308 $$ LANGUAGE SQL STABLE;
309
310
311
312 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('1236', :eg_version);
313
314 CREATE OR REPLACE VIEW action.all_circulation_combined_types AS
315  SELECT acirc.id AS id,
316     acirc.xact_start,
317     acirc.circ_lib,
318     acirc.circ_staff,
319     acirc.create_time,
320     ac_acirc.circ_modifier AS item_type,
321     'regular_circ'::text AS circ_type
322    FROM action.circulation acirc,
323     asset.copy ac_acirc
324   WHERE acirc.target_copy = ac_acirc.id
325 UNION ALL
326  SELECT ancc.id::BIGINT AS id,
327     ancc.circ_time AS xact_start,
328     ancc.circ_lib,
329     ancc.staff AS circ_staff,
330     ancc.circ_time AS create_time,
331     cnct_ancc.name AS item_type,
332     'non-cat_circ'::text AS circ_type
333    FROM action.non_cataloged_circulation ancc,
334     config.non_cataloged_type cnct_ancc
335   WHERE ancc.item_type = cnct_ancc.id
336 UNION ALL
337  SELECT aihu.id::BIGINT AS id,
338     aihu.use_time AS xact_start,
339     aihu.org_unit AS circ_lib,
340     aihu.staff AS circ_staff,
341     aihu.use_time AS create_time,
342     ac_aihu.circ_modifier AS item_type,
343     'in-house_use'::text AS circ_type
344    FROM action.in_house_use aihu,
345     asset.copy ac_aihu
346   WHERE aihu.item = ac_aihu.id
347 UNION ALL
348  SELECT ancihu.id::BIGINT AS id,
349     ancihu.use_time AS xact_start,
350     ancihu.org_unit AS circ_lib,
351     ancihu.staff AS circ_staff,
352     ancihu.use_time AS create_time,
353     cnct_ancihu.name AS item_type,
354     'non-cat-in-house_use'::text AS circ_type
355    FROM action.non_cat_in_house_use ancihu,
356     config.non_cataloged_type cnct_ancihu
357   WHERE ancihu.item_type = cnct_ancihu.id
358 UNION ALL
359  SELECT aacirc.id AS id,
360     aacirc.xact_start,
361     aacirc.circ_lib,
362     aacirc.circ_staff,
363     aacirc.create_time,
364     ac_aacirc.circ_modifier AS item_type,
365     'aged_circ'::text AS circ_type
366    FROM action.aged_circulation aacirc,
367     asset.copy ac_aacirc
368   WHERE aacirc.target_copy = ac_aacirc.id;
369
370
371
372 SELECT evergreen.upgrade_deps_block_check('1238', :eg_version);
373
374 INSERT INTO permission.perm_list ( id, code, description ) VALUES
375  ( 625, 'VIEW_BOOKING_RESERVATION', oils_i18n_gettext(625,
376     'View booking reservations', 'ppl', 'description')),
377  ( 626, 'VIEW_BOOKING_RESERVATION_ATTR_MAP', oils_i18n_gettext(626,
378     'View booking reservation attribute maps', 'ppl', 'description'))
379 ;
380
381
382 SELECT evergreen.upgrade_deps_block_check('1239', :eg_version);
383
384 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
385 VALUES (
386     'eg.grid.booking.pull_list', 'gui', 'object',
387     oils_i18n_gettext(
388         'booking.pull_list',
389         'Grid Config: Booking Pull List',
390         'cwst', 'label')
391 );
392
393
394 SELECT evergreen.upgrade_deps_block_check('1241', :eg_version);
395
396 SET CONSTRAINTS ALL IMMEDIATE; -- to address "pending trigger events" error
397
398 -- Dedupe the table before applying the script.  Preserve the original to allow the admin to delete it manually later.
399 CREATE TABLE reporter.schedule_original (LIKE reporter.schedule);
400 INSERT INTO reporter.schedule_original SELECT * FROM reporter.schedule;
401 TRUNCATE reporter.schedule;
402 INSERT INTO reporter.schedule (SELECT DISTINCT ON (report, folder, runner, run_time) id, report, folder, runner, run_time, start_time, complete_time, email, excel_format, html_format, csv_format, chart_pie, chart_bar, chart_line, error_code, error_text FROM reporter.schedule_original);
403 \qecho NOTE: This has created a backup of the original reporter.schedule
404 \qecho table, named reporter.schedule_original.  Once you are sure that everything
405 \qecho works as expected, you can delete that table by issuing the following:
406 \qecho
407 \qecho  'DROP TABLE reporter.schedule_original;'
408 \qecho
409
410 -- Explicitly supply the name because it is referenced in clark-kent.pl
411 CREATE UNIQUE INDEX rpt_sched_recurrence_once_idx ON reporter.schedule (report,folder,runner,run_time,COALESCE(email,''));
412
413
414
415 -- check whether patch can be applied
416 SELECT evergreen.upgrade_deps_block_check('1242', :eg_version);
417
418 -- Long Overdue
419 UPDATE config.org_unit_setting_type
420 SET description = oils_i18n_gettext(
421         'ui.circ.items_out.longoverdue',
422 'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
423 'or "Other/Special Circulations") the circulation '||
424 'should appear while checked out, and B. Whether the circulation should '||
425 'continue to appear in the "Other" tab when checked in with '||
426 'oustanding fines.  '||
427 '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
428 '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
429         'coust',
430         'description'
431     )
432 WHERE NAME = 'ui.circ.items_out.longoverdue';
433
434 -- Lost
435 UPDATE config.org_unit_setting_type
436 SET description = oils_i18n_gettext(
437         'ui.circ.items_out.lost',
438 'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
439 'or "Other/Special Circulations") the circulation '||
440 'should appear while checked out, and B. Whether the circulation should '||
441 'continue to appear in the "Other" tab when checked in with '||
442 'oustanding fines.  '||
443 '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
444 '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
445         'coust',
446         'description'
447     )
448 WHERE NAME = 'ui.circ.items_out.lost';
449
450 -- Claims Returned
451 UPDATE config.org_unit_setting_type
452 SET description = oils_i18n_gettext(
453         'ui.circ.items_out.claimsreturned',
454 'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
455 'or "Other/Special Circulations") the circulation '||
456 'should appear while checked out, and B. Whether the circulation should '||
457 'continue to appear in the "Other" tab when checked in with '||
458 'oustanding fines.  '||
459 '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
460 '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
461         'coust',
462         'description'
463     )
464 WHERE NAME = 'ui.circ.items_out.claimsreturned';
465
466 COMMIT;
467
468 -- Update auditor tables to catch changes to source tables.
469 --   Can be removed/skipped if there were no schema changes.
470 SELECT auditor.update_auditors();