]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.1.4-3.1.5-upgrade-db.sql
Forward-port 3.1.5 upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.1.4-3.1.5-upgrade-db.sql
1 --Upgrade Script for 3.1.4 to 3.1.5
2 \set eg_version '''3.1.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.1.5', :eg_version);
5 SELECT evergreen.upgrade_deps_block_check('1119', :eg_version);
6
7 CREATE OR REPLACE FUNCTION asset.staff_ou_record_copy_count(org integer, rid bigint)
8  RETURNS TABLE(depth integer, org_unit integer, visible bigint, available bigint, unshadow bigint, transcendant integer)
9  LANGUAGE plpgsql
10 AS $function$
11 DECLARE
12     ans RECORD;
13     trans INT;
14 BEGIN
15     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
16
17     FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
18         RETURN QUERY
19         WITH available_statuses AS (SELECT ARRAY_AGG(id) AS ids FROM config.copy_status WHERE is_available),
20             cp AS(
21                 SELECT  cp.id,
22                         (cp.status = ANY (available_statuses.ids))::INT as available,
23                         (cl.opac_visible AND cp.opac_visible)::INT as opac_visible
24                   FROM
25                         available_statuses,
26                         actor.org_unit_descendants(ans.id) d
27                         JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
28                         JOIN asset.copy_location cl ON (cp.location = cl.id AND NOT cl.deleted)
29                         JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
30             ),
31             peer AS (
32                 SELECT  cp.id,
33                         (cp.status = ANY  (available_statuses.ids))::INT as available,
34                         (cl.opac_visible AND cp.opac_visible)::INT as opac_visible
35                 FROM
36                         available_statuses,
37                         actor.org_unit_descendants(ans.id) d
38                         JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
39                         JOIN asset.copy_location cl ON (cp.location = cl.id AND NOT cl.deleted)
40                         JOIN biblio.peer_bib_copy_map bp ON (bp.peer_record = rid AND bp.target_copy = cp.id)
41             )
42         SELECT ans.depth, ans.id, count(id), sum(x.available::int), sum(x.opac_visible::int), trans
43         FROM ((select * from cp) union (select * from peer)) x
44         GROUP BY 1,2,6;
45
46         IF NOT FOUND THEN
47             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
48         END IF;
49
50     END LOOP;
51     RETURN;
52 END;
53 $function$;
54
55 CREATE OR REPLACE FUNCTION asset.opac_ou_record_copy_count(org integer, rid bigint)
56  RETURNS TABLE(depth integer, org_unit integer, visible bigint, available bigint, unshadow bigint, transcendant integer)
57  LANGUAGE plpgsql
58 AS $function$
59 DECLARE
60     ans RECORD;
61     trans INT;
62 BEGIN
63     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
64
65     FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
66         RETURN QUERY
67         WITH org_list AS (SELECT ARRAY_AGG(id)::BIGINT[] AS orgs FROM actor.org_unit_descendants(ans.id) x),
68              available_statuses AS (SELECT ARRAY_AGG(id) AS ids FROM config.copy_status WHERE is_available),
69              mask AS (SELECT c_attrs FROM asset.patron_default_visibility_mask() x)
70         SELECT  ans.depth,
71                 ans.id,
72                 COUNT( av.id ),
73                 SUM( (cp.status = ANY (available_statuses.ids))::INT ),
74                 COUNT( av.id ),
75                 trans
76           FROM  mask,
77                 available_statuses,
78                 org_list,
79                 asset.copy_vis_attr_cache av
80                 JOIN asset.copy cp ON (cp.id = av.target_copy AND av.record = rid)
81                 JOIN asset.call_number cn ON (cp.call_number = cn.id AND not cn.deleted)
82           WHERE cp.circ_lib = ANY (org_list.orgs) AND av.vis_attr_vector @@ mask.c_attrs::query_int
83           GROUP BY 1,2,6;
84
85         IF NOT FOUND THEN
86             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
87         END IF;
88
89     END LOOP;
90
91     RETURN;
92 END;
93 $function$;
94
95 COMMIT;