]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.7.1-3.7.2-upgrade-db.sql
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.7.1-3.7.2-upgrade-db.sql
1 --Upgrade Script for 3.7.1 to 3.7.2
2 \set eg_version '''3.7.2'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.7.2', :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('1273', :eg_version);
120
121 INSERT into config.org_unit_setting_type
122 ( name, grp, label, description, datatype )
123 SELECT  'opac.did_you_mean.max_suggestions',
124         'opac',
125         'Maximum number of spelling suggestions that may be offered',
126         'If set to -1, provide "best" suggestion if mispelled; if set higher than 0, the maximum suggestions that can be provided; if set to 0, disable suggestions.',
127         'integer'
128   WHERE NOT EXISTS (SELECT 1 FROM config.org_unit_setting_type WHERE name = 'opac.did_you_mean.max_suggestions');
129
130
131
132 SELECT evergreen.upgrade_deps_block_check('1279', :eg_version);
133
134 UPDATE config.org_unit_setting_type SET fm_class='cnal', datatype='link' WHERE name='ui.patron.default_inet_access_level';
135
136
137
138 SELECT evergreen.upgrade_deps_block_check('1282', :eg_version);
139
140 CREATE OR REPLACE FUNCTION search.symspell_lookup(
141         raw_input text,
142         search_class text,
143         verbosity integer DEFAULT 2,
144         xfer_case boolean DEFAULT false,
145         count_threshold integer DEFAULT 1,
146         soundex_weight integer DEFAULT 0,
147         pg_trgm_weight integer DEFAULT 0,
148         kbdist_weight integer DEFAULT 0
149 ) RETURNS SETOF search.symspell_lookup_output
150  LANGUAGE plpgsql
151 AS $function$
152 DECLARE
153     prefix_length INT;
154     maxED         INT;
155     good_suggs  HSTORE;
156     word_list   TEXT[];
157     edit_list   TEXT[] := '{}';
158     seen_list   TEXT[] := '{}';
159     output      search.symspell_lookup_output;
160     output_list search.symspell_lookup_output[];
161     entry       RECORD;
162     entry_key   TEXT;
163     prefix_key  TEXT;
164     sugg        TEXT;
165     input       TEXT;
166     word        TEXT;
167     w_pos       INT := -1;
168     smallest_ed INT := -1;
169     global_ed   INT;
170     i_len       INT;
171     l_maxED     INT;
172 BEGIN
173     SELECT value::INT INTO prefix_length FROM config.internal_flag WHERE name = 'symspell.prefix_length' AND enabled;
174     prefix_length := COALESCE(prefix_length, 6);
175
176     SELECT value::INT INTO maxED FROM config.internal_flag WHERE name = 'symspell.max_edit_distance' AND enabled;
177     maxED := COALESCE(maxED, 3);
178
179     word_list := ARRAY_AGG(x) FROM search.symspell_parse_words(raw_input) x;
180
181     -- Common case exact match test for preformance
182     IF verbosity = 0 AND CARDINALITY(word_list) = 1 AND CHARACTER_LENGTH(word_list[1]) <= prefix_length THEN
183         EXECUTE
184           'SELECT  '||search_class||'_suggestions AS suggestions,
185                    '||search_class||'_count AS count,
186                    prefix_key
187              FROM  search.symspell_dictionary
188              WHERE prefix_key = $1
189                    AND '||search_class||'_count >= $2
190                    AND '||search_class||'_suggestions @> ARRAY[$1]'
191           INTO entry USING evergreen.lowercase(word_list[1]), COALESCE(count_threshold,1);
192         IF entry.prefix_key IS NOT NULL THEN
193             output.lev_distance := 0; -- definitionally
194             output.prefix_key := entry.prefix_key;
195             output.prefix_key_count := entry.count;
196             output.suggestion_count := entry.count;
197             output.input := word_list[1];
198             IF xfer_case THEN
199                 output.suggestion := search.symspell_transfer_casing(output.input, entry.prefix_key);
200             ELSE
201                 output.suggestion := entry.prefix_key;
202             END IF;
203             output.norm_input := entry.prefix_key;
204             output.qwerty_kb_match := 1;
205             output.pg_trgm_sim := 1;
206             output.soundex_sim := 1;
207             RETURN NEXT output;
208             RETURN;
209         END IF;
210     END IF;
211
212     <<word_loop>>
213     FOREACH word IN ARRAY word_list LOOP
214         w_pos := w_pos + 1;
215         input := evergreen.lowercase(word);
216         i_len := CHARACTER_LENGTH(input);
217         l_maxED := maxED;
218
219         IF CHARACTER_LENGTH(input) > prefix_length THEN
220             prefix_key := SUBSTRING(input FROM 1 FOR prefix_length);
221             edit_list := ARRAY[input,prefix_key] || search.symspell_generate_edits(prefix_key, 1, l_maxED);
222         ELSE
223             edit_list := input || search.symspell_generate_edits(input, 1, l_maxED);
224         END IF;
225
226         SELECT ARRAY_AGG(x ORDER BY CHARACTER_LENGTH(x) DESC) INTO edit_list FROM UNNEST(edit_list) x;
227
228         output_list := '{}';
229         seen_list := '{}';
230         global_ed := NULL;
231
232         <<entry_key_loop>>
233         FOREACH entry_key IN ARRAY edit_list LOOP
234             smallest_ed := -1;
235             IF global_ed IS NOT NULL THEN
236                 smallest_ed := global_ed;
237             END IF;
238
239             FOR entry IN EXECUTE
240                 'SELECT  '||search_class||'_suggestions AS suggestions,
241                          '||search_class||'_count AS count,
242                          prefix_key
243                    FROM  search.symspell_dictionary
244                    WHERE prefix_key = $1
245                          AND '||search_class||'_suggestions IS NOT NULL'
246                 USING entry_key
247             LOOP
248
249                 SELECT  HSTORE(
250                             ARRAY_AGG(
251                                 ARRAY[s, evergreen.levenshtein_damerau_edistance(input,s,l_maxED)::TEXT]
252                                     ORDER BY evergreen.levenshtein_damerau_edistance(input,s,l_maxED) DESC
253                             )
254                         )
255                   INTO  good_suggs
256                   FROM  UNNEST(entry.suggestions) s
257                   WHERE (ABS(CHARACTER_LENGTH(s) - i_len) <= maxEd AND evergreen.levenshtein_damerau_edistance(input,s,l_maxED) BETWEEN 0 AND l_maxED)
258                         AND NOT seen_list @> ARRAY[s];
259
260                 CONTINUE WHEN good_suggs IS NULL;
261
262                 FOR sugg, output.suggestion_count IN EXECUTE
263                     'SELECT  prefix_key, '||search_class||'_count
264                        FROM  search.symspell_dictionary
265                        WHERE prefix_key = ANY ($1)
266                              AND '||search_class||'_count >= $2'
267                     USING AKEYS(good_suggs), COALESCE(count_threshold,1)
268                 LOOP
269
270                     output.lev_distance := good_suggs->sugg;
271                     seen_list := seen_list || sugg;
272
273                     -- Track the smallest edit distance among suggestions from this prefix key.
274                     IF smallest_ed = -1 OR output.lev_distance < smallest_ed THEN
275                         smallest_ed := output.lev_distance;
276                     END IF;
277
278                     -- Track the smallest edit distance for all prefix keys for this word.
279                     IF global_ed IS NULL OR smallest_ed < global_ed THEN
280                         global_ed = smallest_ed;
281                         -- And if low verbosity, ignore suggs with a larger distance from here on.
282                         IF verbosity <= 1 THEN
283                             l_maxED := global_ed;
284                         END IF;
285                     END IF;
286
287                     -- Lev distance is our main similarity measure. While
288                     -- trgm or soundex similarity could be the main filter,
289                     -- Lev is both language agnostic and faster.
290                     --
291                     -- Here we will skip suggestions that have a longer edit distance
292                     -- than the shortest we've already found. This is simply an
293                     -- optimization that allows us to avoid further processing
294                     -- of this entry. It would be filtered out later.
295                     CONTINUE WHEN output.lev_distance > global_ed AND verbosity <= 1;
296
297                     -- If we have an exact match on the suggestion key we can also avoid
298                     -- some function calls.
299                     IF output.lev_distance = 0 THEN
300                         output.qwerty_kb_match := 1;
301                         output.pg_trgm_sim := 1;
302                         output.soundex_sim := 1;
303                     ELSE
304                         IF kbdist_weight THEN
305                             output.qwerty_kb_match := evergreen.qwerty_keyboard_distance_match(input, sugg);
306                         ELSE
307                             output.qwerty_kb_match := 0;
308                         END IF;
309                         IF pg_trgm_weight THEN
310                             output.pg_trgm_sim := similarity(input, sugg);
311                         ELSE
312                             output.pg_trgm_sim := 0;
313                         END IF;
314                         IF soundex_weight THEN
315                             output.soundex_sim := difference(input, sugg) / 4.0;
316                         ELSE
317                             output.soundex_sim := 0;
318                         END IF;
319                     END IF;
320
321                     -- Fill in some fields
322                     IF xfer_case AND input <> word THEN
323                         output.suggestion := search.symspell_transfer_casing(word, sugg);
324                     ELSE
325                         output.suggestion := sugg;
326                     END IF;
327                     output.prefix_key := entry.prefix_key;
328                     output.prefix_key_count := entry.count;
329                     output.input := word;
330                     output.norm_input := input;
331                     output.word_pos := w_pos;
332
333                     -- We can't "cache" a set of generated records directly, so
334                     -- here we build up an array of search.symspell_lookup_output
335                     -- records that we can revivicate later as a table using UNNEST().
336                     output_list := output_list || output;
337
338                     EXIT entry_key_loop WHEN smallest_ed = 0 AND verbosity = 0; -- exact match early exit
339                     CONTINUE entry_key_loop WHEN smallest_ed = 0 AND verbosity = 1; -- exact match early jump to the next key
340
341                 END LOOP; -- loop over suggestions
342             END LOOP; -- loop over entries
343         END LOOP; -- loop over entry_keys
344
345         -- Now we're done examining this word
346         IF verbosity = 0 THEN
347             -- Return the "best" suggestion from the smallest edit
348             -- distance group.  We define best based on the weighting
349             -- of the non-lev similarity measures and use the suggestion
350             -- use count to break ties.
351             RETURN QUERY
352                 SELECT * FROM UNNEST(output_list)
353                     ORDER BY lev_distance,
354                         (soundex_sim * COALESCE(soundex_weight,0))
355                             + (pg_trgm_sim * COALESCE(pg_trgm_weight,0))
356                             + (qwerty_kb_match * COALESCE(kbdist_weight,0)) DESC,
357                         suggestion_count DESC
358                         LIMIT 1;
359         ELSIF verbosity = 1 THEN
360             -- Return all suggestions from the smallest
361             -- edit distance group.
362             RETURN QUERY
363                 SELECT * FROM UNNEST(output_list) WHERE lev_distance = smallest_ed
364                     ORDER BY (soundex_sim * COALESCE(soundex_weight,0))
365                             + (pg_trgm_sim * COALESCE(pg_trgm_weight,0))
366                             + (qwerty_kb_match * COALESCE(kbdist_weight,0)) DESC,
367                         suggestion_count DESC;
368         ELSIF verbosity = 2 THEN
369             -- Return everything we find, along with relevant stats
370             RETURN QUERY
371                 SELECT * FROM UNNEST(output_list)
372                     ORDER BY lev_distance,
373                         (soundex_sim * COALESCE(soundex_weight,0))
374                             + (pg_trgm_sim * COALESCE(pg_trgm_weight,0))
375                             + (qwerty_kb_match * COALESCE(kbdist_weight,0)) DESC,
376                         suggestion_count DESC;
377         ELSIF verbosity = 3 THEN
378             -- Return everything we find from the two smallest edit distance groups
379             RETURN QUERY
380                 SELECT * FROM UNNEST(output_list)
381                     WHERE lev_distance IN (SELECT DISTINCT lev_distance FROM UNNEST(output_list) ORDER BY 1 LIMIT 2)
382                     ORDER BY lev_distance,
383                         (soundex_sim * COALESCE(soundex_weight,0))
384                             + (pg_trgm_sim * COALESCE(pg_trgm_weight,0))
385                             + (qwerty_kb_match * COALESCE(kbdist_weight,0)) DESC,
386                         suggestion_count DESC;
387         ELSIF verbosity = 4 THEN
388             -- Return everything we find from the two smallest edit distance groups that are NOT 0 distance
389             RETURN QUERY
390                 SELECT * FROM UNNEST(output_list)
391                     WHERE lev_distance IN (SELECT DISTINCT lev_distance FROM UNNEST(output_list) WHERE lev_distance > 0 ORDER BY 1 LIMIT 2)
392                     ORDER BY lev_distance,
393                         (soundex_sim * COALESCE(soundex_weight,0))
394                             + (pg_trgm_sim * COALESCE(pg_trgm_weight,0))
395                             + (qwerty_kb_match * COALESCE(kbdist_weight,0)) DESC,
396                         suggestion_count DESC;
397         END IF;
398     END LOOP; -- loop over words
399 END;
400 $function$;
401
402
403
404 SELECT evergreen.upgrade_deps_block_check('1283', :eg_version); -- rhamby/ehardy/jboyer
405
406 UPDATE asset.call_number SET record = -1 WHERE id = -1 AND record != -1;
407
408 CREATE RULE protect_bre_id_neg1 AS ON UPDATE TO biblio.record_entry WHERE OLD.id = -1 DO INSTEAD NOTHING;
409 CREATE RULE protect_acl_id_1 AS ON UPDATE TO asset.copy_location WHERE OLD.id = 1 DO INSTEAD NOTHING;
410 CREATE RULE protect_acn_id_neg1 AS ON UPDATE TO asset.call_number WHERE OLD.id = -1 DO INSTEAD NOTHING;
411
412 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
413 DECLARE
414     moved_objects INT := 0;
415     source_cn     asset.call_number%ROWTYPE;
416     target_cn     asset.call_number%ROWTYPE;
417     metarec       metabib.metarecord%ROWTYPE;
418     hold          action.hold_request%ROWTYPE;
419     ser_rec       serial.record_entry%ROWTYPE;
420     ser_sub       serial.subscription%ROWTYPE;
421     acq_lineitem  acq.lineitem%ROWTYPE;
422     acq_request   acq.user_request%ROWTYPE;
423     booking       booking.resource_type%ROWTYPE;
424     source_part   biblio.monograph_part%ROWTYPE;
425     target_part   biblio.monograph_part%ROWTYPE;
426     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
427     uri_count     INT := 0;
428     counter       INT := 0;
429     uri_datafield TEXT;
430     uri_text      TEXT := '';
431 BEGIN
432
433     -- we don't merge bib -1 
434     IF target_record = -1 OR source_record = -1 THEN 
435        RETURN 0;
436     END IF;
437
438     -- move any 856 entries on records that have at least one MARC-mapped URI entry
439     SELECT  INTO uri_count COUNT(*)
440       FROM  asset.uri_call_number_map m
441             JOIN asset.call_number cn ON (m.call_number = cn.id)
442       WHERE cn.record = source_record;
443
444     IF uri_count > 0 THEN
445         
446         -- This returns more nodes than you might expect:
447         -- 7 instead of 1 for an 856 with $u $y $9
448         SELECT  COUNT(*) INTO counter
449           FROM  oils_xpath_table(
450                     'id',
451                     'marc',
452                     'biblio.record_entry',
453                     '//*[@tag="856"]',
454                     'id=' || source_record
455                 ) as t(i int,c text);
456     
457         FOR i IN 1 .. counter LOOP
458             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
459             ' tag="856"' ||
460             ' ind1="' || FIRST(ind1) || '"'  ||
461             ' ind2="' || FIRST(ind2) || '">' ||
462                         STRING_AGG(
463                             '<subfield code="' || subfield || '">' ||
464                             regexp_replace(
465                                 regexp_replace(
466                                     regexp_replace(data,'&','&amp;','g'),
467                                     '>', '&gt;', 'g'
468                                 ),
469                                 '<', '&lt;', 'g'
470                             ) || '</subfield>', ''
471                         ) || '</datafield>' INTO uri_datafield
472               FROM  oils_xpath_table(
473                         'id',
474                         'marc',
475                         'biblio.record_entry',
476                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
477                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
478                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
479                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
480                         'id=' || source_record
481                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
482
483             -- As most of the results will be NULL, protect against NULLifying
484             -- the valid content that we do generate
485             uri_text := uri_text || COALESCE(uri_datafield, '');
486         END LOOP;
487
488         IF uri_text <> '' THEN
489             UPDATE  biblio.record_entry
490               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
491               WHERE id = target_record;
492         END IF;
493
494     END IF;
495
496     -- Find and move metarecords to the target record
497     SELECT    INTO metarec *
498       FROM    metabib.metarecord
499       WHERE    master_record = source_record;
500
501     IF FOUND THEN
502         UPDATE    metabib.metarecord
503           SET    master_record = target_record,
504             mods = NULL
505           WHERE    id = metarec.id;
506
507         moved_objects := moved_objects + 1;
508     END IF;
509
510     -- Find call numbers attached to the source ...
511     FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
512
513         SELECT    INTO target_cn *
514           FROM    asset.call_number
515           WHERE    label = source_cn.label
516             AND prefix = source_cn.prefix
517             AND suffix = source_cn.suffix
518             AND owning_lib = source_cn.owning_lib
519             AND record = target_record
520             AND NOT deleted;
521
522         -- ... and if there's a conflicting one on the target ...
523         IF FOUND THEN
524
525             -- ... move the copies to that, and ...
526             UPDATE    asset.copy
527               SET    call_number = target_cn.id
528               WHERE    call_number = source_cn.id;
529
530             -- ... move V holds to the move-target call number
531             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
532         
533                 UPDATE    action.hold_request
534                   SET    target = target_cn.id
535                   WHERE    id = hold.id;
536         
537                 moved_objects := moved_objects + 1;
538             END LOOP;
539         
540             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
541
542         -- ... if not ...
543         ELSE
544             -- ... just move the call number to the target record
545             UPDATE    asset.call_number
546               SET    record = target_record
547               WHERE    id = source_cn.id;
548         END IF;
549
550         moved_objects := moved_objects + 1;
551     END LOOP;
552
553     -- Find T holds targeting the source record ...
554     FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
555
556         -- ... and move them to the target record
557         UPDATE    action.hold_request
558           SET    target = target_record
559           WHERE    id = hold.id;
560
561         moved_objects := moved_objects + 1;
562     END LOOP;
563
564     -- Find serial records targeting the source record ...
565     FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
566         -- ... and move them to the target record
567         UPDATE    serial.record_entry
568           SET    record = target_record
569           WHERE    id = ser_rec.id;
570
571         moved_objects := moved_objects + 1;
572     END LOOP;
573
574     -- Find serial subscriptions targeting the source record ...
575     FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
576         -- ... and move them to the target record
577         UPDATE    serial.subscription
578           SET    record_entry = target_record
579           WHERE    id = ser_sub.id;
580
581         moved_objects := moved_objects + 1;
582     END LOOP;
583
584     -- Find booking resource types targeting the source record ...
585     FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
586         -- ... and move them to the target record
587         UPDATE    booking.resource_type
588           SET    record = target_record
589           WHERE    id = booking.id;
590
591         moved_objects := moved_objects + 1;
592     END LOOP;
593
594     -- Find acq lineitems targeting the source record ...
595     FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
596         -- ... and move them to the target record
597         UPDATE    acq.lineitem
598           SET    eg_bib_id = target_record
599           WHERE    id = acq_lineitem.id;
600
601         moved_objects := moved_objects + 1;
602     END LOOP;
603
604     -- Find acq user purchase requests targeting the source record ...
605     FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
606         -- ... and move them to the target record
607         UPDATE    acq.user_request
608           SET    eg_bib = target_record
609           WHERE    id = acq_request.id;
610
611         moved_objects := moved_objects + 1;
612     END LOOP;
613
614     -- Find parts attached to the source ...
615     FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
616
617         SELECT    INTO target_part *
618           FROM    biblio.monograph_part
619           WHERE    label = source_part.label
620             AND record = target_record;
621
622         -- ... and if there's a conflicting one on the target ...
623         IF FOUND THEN
624
625             -- ... move the copy-part maps to that, and ...
626             UPDATE    asset.copy_part_map
627               SET    part = target_part.id
628               WHERE    part = source_part.id;
629
630             -- ... move P holds to the move-target part
631             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
632         
633                 UPDATE    action.hold_request
634                   SET    target = target_part.id
635                   WHERE    id = hold.id;
636         
637                 moved_objects := moved_objects + 1;
638             END LOOP;
639
640         -- ... if not ...
641         ELSE
642             -- ... just move the part to the target record
643             UPDATE    biblio.monograph_part
644               SET    record = target_record
645               WHERE    id = source_part.id;
646         END IF;
647
648         moved_objects := moved_objects + 1;
649     END LOOP;
650
651     -- Find multi_home items attached to the source ...
652     FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
653         -- ... and move them to the target record
654         UPDATE    biblio.peer_bib_copy_map
655           SET    peer_record = target_record
656           WHERE    id = multi_home.id;
657
658         moved_objects := moved_objects + 1;
659     END LOOP;
660
661     -- And delete mappings where the item's home bib was merged with the peer bib
662     DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
663         SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
664         FROM asset.copy WHERE id = target_copy
665     );
666
667     -- Apply merge tracking
668     UPDATE biblio.record_entry 
669         SET merge_date = NOW() WHERE id = target_record;
670
671     UPDATE biblio.record_entry
672         SET merge_date = NOW(), merged_to = target_record
673         WHERE id = source_record;
674
675     -- replace book bag entries of source_record with target_record
676     UPDATE container.biblio_record_entry_bucket_item
677         SET target_biblio_record_entry = target_record
678         WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
679         AND target_biblio_record_entry = source_record;
680
681     -- Finally, "delete" the source record
682     UPDATE biblio.record_entry SET active = FALSE WHERE id = source_record;
683     DELETE FROM biblio.record_entry WHERE id = source_record;
684
685     -- That's all, folks!
686     RETURN moved_objects;
687 END;
688 $func$ LANGUAGE plpgsql;
689
690
691
692
693 SELECT evergreen.upgrade_deps_block_check('1294', :eg_version); -- mmorgan / tlittle / JBoyer
694
695 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
696 VALUES (
697     'eg.grid.admin.local.container.carousel_org_unit', 'gui', 'object',
698     oils_i18n_gettext(
699         'eg.grid.admin.local.container.carousel_org_unit',
700         'Grid Config: eg.grid.admin.local.container.carousel_org_unit',
701         'cwst', 'label'
702     )
703 ), (
704     'eg.grid.admin.container.carousel', 'gui', 'object',
705     oils_i18n_gettext(
706         'eg.grid.admin.container.carousel',
707         'Grid Config: eg.grid.admin.container.carousel',
708         'cwst', 'label'
709     )
710 ), (
711     'eg.grid.admin.server.config.carousel_type', 'gui', 'object',
712     oils_i18n_gettext(
713         'eg.grid.admin.server.config.carousel_type',
714         'Grid Config: eg.grid.admin.server.config.carousel_type',
715         'cwst', 'label'
716     )
717 );
718
719
720 SELECT evergreen.upgrade_deps_block_check('1302', :eg_version);
721
722 UPDATE config.org_unit_setting_type
723     SET description = oils_i18n_gettext(
724         'ui.circ.items_out.longoverdue',
725         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
726         'or "Other/Special Circulations") the circulation '||
727         'should appear while checked out, and B. Whether the circulation should '||
728         'continue to appear in the "Other" tab when checked in with '||
729         'outstanding fines.  '||
730         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
731         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
732         'coust',
733         'description'
734     )
735     WHERE name = 'ui.circ.items_out.longoverdue';
736
737 UPDATE config.org_unit_setting_type
738     set description = oils_i18n_gettext(
739         'ui.circ.items_out.lost',
740         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
741         'or "Other/Special Circulations") the circulation '||
742         'should appear while checked out, and B. Whether the circulation should '||
743         'continue to appear in the "Other" tab when checked in with '||
744         'outstanding fines.  '||
745         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
746         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
747         'coust',
748         'description'
749     )
750     WHERE name = 'ui.circ.items_out.lost';
751
752 UPDATE config.org_unit_setting_type
753     set description = oils_i18n_gettext(
754         'ui.circ.items_out.claimsreturned',
755         'Value is a numeric code, describing: A. In which tab ("Items Checked Out", '||
756         'or "Other/Special Circulations") the circulation '||
757         'should appear while checked out, and B. Whether the circulation should '||
758         'continue to appear in the "Other" tab when checked in with '||
759         'outstanding fines.  '||
760         '1 = (A) "Items", (B) "Other".  2 = (A) "Other", (B) "Other".  ' ||
761         '5 = (A) "Items", (B) do not display.  6 = (A) "Other", (B) do not display.',
762         'coust',
763         'description'
764     )
765     WHERE name = 'ui.circ.items_out.claimsreturned';
766
767
768 SELECT evergreen.upgrade_deps_block_check('1303', :eg_version);
769
770 DROP INDEX authority.authority_full_rec_value_index;
771 CREATE INDEX authority_full_rec_value_index ON authority.full_rec (SUBSTRING(value FOR 1024));
772
773 DROP INDEX authority.authority_full_rec_value_tpo_index;
774 CREATE INDEX authority_full_rec_value_tpo_index ON authority.full_rec (SUBSTRING(value FOR 1024) text_pattern_ops);
775
776
777 COMMIT;
778
779 -- Update auditor tables to catch changes to source tables.
780 --   Can be removed/skipped if there were no schema changes.
781 SELECT auditor.update_auditors();