
jbe@70: BEGIN;
jbe@70:  
jbe@70: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@70:   SELECT * FROM (VALUES ('1.2.6', 1, 2, 6))
jbe@70:   AS "subquery"("string", "major", "minor", "revision");
jbe@70: 
jbe@70: CREATE VIEW "active_delegation" AS
jbe@70:   SELECT "delegation".* FROM "delegation"
jbe@70:   JOIN "member" ON "delegation"."truster_id" = "member"."id"
jbe@70:   WHERE "member"."active" = TRUE;
jbe@70: 
jbe@70: COMMENT ON VIEW "active_delegation" IS 'Delegations where the truster_id refers to an active member';
jbe@70: 
jbe@70: CREATE OR REPLACE VIEW "global_delegation" AS
jbe@70:   SELECT * FROM "active_delegation"
jbe@70:   WHERE "scope" = 'global';
jbe@70: 
jbe@70: COMMENT ON VIEW "global_delegation" IS 'Global delegations from active members';
jbe@70: 
jbe@70: CREATE OR REPLACE VIEW "area_delegation" AS
jbe@70:   SELECT DISTINCT ON ("area"."id", "delegation"."truster_id")
jbe@70:     "area"."id" AS "area_id",
jbe@70:     "delegation"."id",
jbe@70:     "delegation"."truster_id",
jbe@70:     "delegation"."trustee_id",
jbe@70:     "delegation"."scope"
jbe@70:   FROM "area" JOIN "active_delegation" AS "delegation"
jbe@70:   ON "delegation"."scope" = 'global'
jbe@70:   OR "delegation"."area_id" = "area"."id"
jbe@70:   ORDER BY
jbe@70:     "area"."id",
jbe@70:     "delegation"."truster_id",
jbe@70:     "delegation"."scope" DESC;
jbe@70: 
jbe@70: COMMENT ON VIEW "area_delegation" IS 'Resulting area delegations from active members';
jbe@70: 
jbe@70: CREATE OR REPLACE VIEW "issue_delegation" AS
jbe@70:   SELECT DISTINCT ON ("issue"."id", "delegation"."truster_id")
jbe@70:     "issue"."id" AS "issue_id",
jbe@70:     "delegation"."id",
jbe@70:     "delegation"."truster_id",
jbe@70:     "delegation"."trustee_id",
jbe@70:     "delegation"."scope"
jbe@70:   FROM "issue" JOIN "active_delegation" AS "delegation"
jbe@70:   ON "delegation"."scope" = 'global'
jbe@70:   OR "delegation"."area_id" = "issue"."area_id"
jbe@70:   OR "delegation"."issue_id" = "issue"."id"
jbe@70:   ORDER BY
jbe@70:     "issue"."id",
jbe@70:     "delegation"."truster_id",
jbe@70:     "delegation"."scope" DESC;
jbe@70: 
jbe@70: COMMENT ON VIEW "issue_delegation" IS 'Resulting issue delegations from active members';
jbe@70: 
jbe@70: COMMIT;




