]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0445.schema.replace-connect_by.sql
LP#1638299: Stamping upgrade scripts for authority infrastructure work
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0445.schema.replace-connect_by.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0445'); -- miker
4
5 CREATE OR REPLACE FUNCTION actor.org_unit_descendants( INT, INT ) RETURNS SETOF actor.org_unit AS $$
6     WITH RECURSIVE descendant_depth AS (
7         SELECT  ou.id,
8                 ou.parent_ou,
9                 out.depth
10           FROM  actor.org_unit ou
11                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
12                 JOIN anscestor_depth ad ON (ad.id = ou.id)
13           WHERE ad.depth = $2
14             UNION ALL
15         SELECT  ou.id,
16                 ou.parent_ou,
17                 out.depth
18           FROM  actor.org_unit ou
19                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
20                 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
21     ), anscestor_depth AS (
22         SELECT  ou.id,
23                 ou.parent_ou,
24                 out.depth
25           FROM  actor.org_unit ou
26                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
27           WHERE ou.id = $1
28             UNION ALL
29         SELECT  ou.id,
30                 ou.parent_ou,
31                 out.depth
32           FROM  actor.org_unit ou
33                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
34                 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
35     ) SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id);
36 $$ LANGUAGE SQL;
37
38 CREATE OR REPLACE FUNCTION actor.org_unit_descendants( INT ) RETURNS SETOF actor.org_unit AS $$
39     WITH RECURSIVE descendant_depth AS (
40         SELECT  ou.id,
41                 ou.parent_ou,
42                 out.depth
43           FROM  actor.org_unit ou
44                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
45           WHERE ou.id = $1
46             UNION ALL
47         SELECT  ou.id,
48                 ou.parent_ou,
49                 out.depth
50           FROM  actor.org_unit ou
51                 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
52                 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
53     ) SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id);
54 $$ LANGUAGE SQL;
55
56 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors( INT ) RETURNS SETOF actor.org_unit AS $$
57     WITH RECURSIVE anscestor_depth AS (
58         SELECT  ou.id,
59                 ou.parent_ou
60           FROM  actor.org_unit ou
61           WHERE ou.id = $1
62             UNION ALL
63         SELECT  ou.id,
64                 ou.parent_ou
65           FROM  actor.org_unit ou
66                 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
67     ) SELECT ou.* FROM actor.org_unit ou JOIN anscestor_depth USING (id);
68 $$ LANGUAGE SQL;
69
70 COMMIT;
71