Forward port 2.5.5 and 2.6.1 upgrade scripts
[working/Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.5.4-2.5.5-upgrade-db.sql
1 --Upgrade Script for 2.5.4 to 2.5.5
2 \set eg_version '''2.5.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.5.5', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('0880', :eg_version);
7
8 CREATE OR REPLACE FUNCTION authority.calculate_authority_linking(
9     rec_id BIGINT, rec_control_set INT, rec_marc_xml XML
10 ) RETURNS SETOF authority.authority_linking AS $func$
11 DECLARE
12     acsaf       authority.control_set_authority_field%ROWTYPE;
13     link        TEXT;
14     aal         authority.authority_linking%ROWTYPE;
15 BEGIN
16     IF rec_control_set IS NULL THEN
17         -- No control_set on record?  Guess at one
18         SELECT control_set INTO rec_control_set
19             FROM authority.control_set_authority_field
20             WHERE tag IN (
21                 SELECT UNNEST(
22                     XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc_xml)::TEXT[]
23                 )
24             ) LIMIT 1;
25
26         IF NOT FOUND THEN
27             RAISE WARNING 'Could not even guess at control set for authority record %', rec_id;
28             RETURN;
29         END IF;
30     END IF;
31
32     aal.source := rec_id;
33
34     FOR acsaf IN
35         SELECT * FROM authority.control_set_authority_field
36         WHERE control_set = rec_control_set
37             AND linking_subfield IS NOT NULL
38             AND main_entry IS NOT NULL
39     LOOP
40         -- Loop over the trailing-number contents of all linking subfields
41         FOR link IN
42             SELECT  SUBSTRING( x::TEXT, '\d+$' )
43               FROM  UNNEST(
44                         XPATH(
45                             '//*[@tag="'
46                                 || acsaf.tag
47                                 || '"]/*[@code="'
48                                 || acsaf.linking_subfield
49                                 || '"]/text()',
50                             rec_marc_xml
51                         )
52                     ) x
53         LOOP
54
55             -- Ignore links that are null, malformed, circular, or point to
56             -- non-existent authority records.
57             IF link IS NOT NULL AND link::BIGINT <> rec_id THEN
58                 PERFORM * FROM authority.record_entry WHERE id = link::BIGINT;
59                 IF FOUND THEN
60                     aal.target := link::BIGINT;
61                     aal.field := acsaf.id;
62                     RETURN NEXT aal;
63                 END IF;
64             END IF;
65         END LOOP;
66     END LOOP;
67 END;
68 $func$ LANGUAGE PLPGSQL;
69
70
71
72 SELECT evergreen.upgrade_deps_block_check('0881', :eg_version);
73
74 UPDATE config.org_unit_setting_type
75     SET description = replace(replace(description,'Original','Physical'),'"ol"','"physical_loc"')
76     WHERE name = 'opac.org_unit_hiding.depth';
77
78 COMMIT;