]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.floating_groups.sql
48425804770a97e38849349f95cf47b363eb05d4
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.floating_groups.sql
1 CREATE TABLE config.floating_group (
2     id      SERIAL PRIMARY KEY, 
3     name    TEXT UNIQUE NOT NULL,
4     manual  BOOL NOT NULL DEFAULT FALSE
5     );
6
7 CREATE TABLE config.floating_group_member (
8     id              SERIAL PRIMARY KEY,
9     floating_group  INT NOT NULL REFERENCES config.floating_group (id),
10     org_unit        INT NOT NULL REFERENCES actor.org_unit (id),
11     stop_depth      INT NOT NULL DEFAULT 0,
12     max_depth       INT,
13     exclude         BOOL NOT NULL DEFAULT FALSE
14     );
15
16 CREATE OR REPLACE FUNCTION evergreen.can_float( copy_floating_group integer, from_ou integer, to_ou integer ) RETURNS BOOL AS $f$
17 DECLARE
18     float_member config.floating_group_member%ROWTYPE;
19     shared_ou_depth INT;
20     to_ou_depth INT;
21 BEGIN
22     -- Grab the shared OU depth. If this is less than the stop depth later we ignore the entry.
23     SELECT INTO shared_ou_depth max(depth) FROM actor.org_unit_common_ancestors( from_ou, to_ou ) aou JOIN actor.org_unit_type aout ON aou.ou_type = aout.id;
24     -- Grab the to ou depth. If this is greater than max depth we ignore the entry.
25     SELECT INTO to_ou_depth depth FROM actor.org_unit aou JOIN actor.org_unit_type aout ON aou.ou_type = aout.id WHERE aou.id = to_ou;
26     -- Grab float members that apply. We don't care what we get beyond wanting excluded ones first.
27     SELECT INTO float_member *
28         FROM
29             config.floating_group_member cfgm
30             JOIN actor.org_unit aou ON cfgm.org_unit = aou.id
31             JOIN actor.org_unit_type aout ON aou.ou_type = aout.id
32         WHERE
33             cfgm.floating_group = copy_floating_group
34             AND to_ou IN (SELECT id FROM actor.org_unit_descendants(aou.id))
35             AND cfgm.stop_depth <= shared_ou_depth
36             AND (cfgm.max_depth IS NULL OR to_ou_depth <= max_depth)
37         ORDER BY
38             exclude DESC;
39     -- If we found something then we want to return the opposite of the exclude flag
40     IF FOUND THEN
41         RETURN NOT float_member.exclude;
42     END IF;
43     -- Otherwise no floating.
44     RETURN false;
45 END;
46 $f$ LANGUAGE PLPGSQL;
47
48 INSERT INTO config.floating_group(name) VALUES ('Everywhere');
49 INSERT INTO config.floating_group_member(floating_group, org_unit) VALUES (1, 1);
50
51 -- We need to drop these before we can update asset.copy
52 DROP VIEW auditor.asset_copy_lifecycle;
53 DROP VIEW auditor.serial_unit_lifecycle;
54
55 -- Update the appropriate auditor tables
56 ALTER TABLE auditor.asset_copy_history
57     ALTER COLUMN floating DROP DEFAULT,
58     ALTER COLUMN floating DROP NOT NULL,
59     ALTER COLUMN floating TYPE int USING CASE WHEN floating THEN 1 ELSE NULL END;
60 ALTER TABLE auditor.serial_unit_history
61     ALTER COLUMN floating DROP DEFAULT,
62     ALTER COLUMN floating DROP NOT NULL,
63     ALTER COLUMN floating TYPE int USING CASE WHEN floating THEN 1 ELSE NULL END;
64
65 -- Update asset.copy itself (does not appear to trigger update triggers!)
66 ALTER TABLE asset.copy
67     ALTER COLUMN floating DROP DEFAULT,
68     ALTER COLUMN floating DROP NOT NULL,
69     ALTER COLUMN floating TYPE int USING CASE WHEN floating THEN 1 ELSE NULL END;
70
71 ALTER TABLE asset.copy ADD CONSTRAINT asset_copy_floating_fkey FOREIGN KEY (floating) REFERENCES config.floating_group (id) DEFERRABLE INITIALLY DEFERRED;
72
73 -- Update asset.copy_template too
74 ALTER TABLE asset.copy_template
75     ALTER COLUMN floating TYPE int USING CASE WHEN floating THEN 1 ELSE NULL END;
76 ALTER TABLE asset.copy_template ADD CONSTRAINT asset_copy_template_floating_fkey FOREIGN KEY (floating) REFERENCES config.floating_group (id) DEFERRABLE INITIALLY DEFERRED;
77
78 INSERT INTO permission.perm_list( code, description) VALUES
79 ('ADMIN_FLOAT_GROUPS', 'Allows administration of floating groups');
80
81 -- And lets just update all auditors to re-create those lifecycle views
82 SELECT auditor.update_auditors();