Unwrapped upgrade script for oua_force_order
authorThomas Berezansky <tsbere@mvlc.org>
Thu, 11 Aug 2011 13:36:36 +0000 (09:36 -0400)
committerMike Rylander <mrylander@gmail.com>
Tue, 23 Aug 2011 18:29:17 +0000 (14:29 -0400)
Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.oua_force_order.sql [new file with mode: 0644]

diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.oua_force_order.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.oua_force_order.sql
new file mode 100644 (file)
index 0000000..84d864b
--- /dev/null
@@ -0,0 +1,11 @@
+CREATE OR REPLACE FUNCTION actor.org_unit_ancestors( INT ) RETURNS SETOF actor.org_unit AS $$
+    WITH RECURSIVE org_unit_ancestors_distance(id, distance) AS (
+            SELECT $1, 0
+        UNION
+            SELECT ou.parent_ou, ouad.distance+1
+            FROM actor.org_unit ou JOIN org_unit_ancestors_distance ouad ON (ou.id = ouad.id)
+            WHERE ou.parent_ou IS NOT NULL
+    )
+    SELECT ou.* FROM actor.org_unit ou JOIN org_unit_ancestors_distance ouad USING (id) ORDER BY ouad.distance;
+$$ LANGUAGE SQL ROWS 1;
+