liquid_feedback_core

diff update/core-update.v2.2.6-v3.0.1.sql @ 436:34cc98defa8b

2 bugfixes: Error in core-update.v2.2.6-v3.0.1.sql fixed; Always set "initiative"."first_preference_votes" (also if no votes are found)
author jbe
date Wed Jul 16 16:28:35 2014 +0200 (2014-07-16)
parents 044a2b65c707
children
line diff
     1.1 --- a/update/core-update.v2.2.6-v3.0.1.sql	Fri Jul 11 01:44:25 2014 +0200
     1.2 +++ b/update/core-update.v2.2.6-v3.0.1.sql	Wed Jul 16 16:28:35 2014 +0200
     1.3 @@ -336,4 +336,235 @@
     1.4      END;
     1.5    $$;
     1.6  
     1.7 +ALTER TABLE "initiative" ADD COLUMN "first_preference_votes" INT4;
     1.8 +
     1.9 +ALTER TABLE "initiative" DROP CONSTRAINT "non_admitted_initiatives_cant_contain_voting_results";
    1.10 +ALTER TABLE "initiative" ADD CONSTRAINT "non_admitted_initiatives_cant_contain_voting_results" CHECK (
    1.11 +          ( "admitted" NOTNULL AND "admitted" = TRUE ) OR
    1.12 +          ( "first_preference_votes" ISNULL AND
    1.13 +            "positive_votes" ISNULL AND "negative_votes" ISNULL AND
    1.14 +            "direct_majority" ISNULL AND "indirect_majority" ISNULL AND
    1.15 +            "schulze_rank" ISNULL AND
    1.16 +            "better_than_status_quo" ISNULL AND "worse_than_status_quo" ISNULL AND
    1.17 +            "reverse_beat_path" ISNULL AND "multistage_majority" ISNULL AND
    1.18 +            "eligible" ISNULL AND "winner" ISNULL AND "rank" ISNULL ) );
    1.19 +
    1.20 +COMMENT ON COLUMN "initiative"."first_preference_votes" IS 'Number of direct and delegating voters who ranked this initiative as their first choice';
    1.21 +COMMENT ON COLUMN "initiative"."positive_votes"         IS 'Number of direct and delegating voters who ranked this initiative better than the status quo';
    1.22 +COMMENT ON COLUMN "initiative"."negative_votes"         IS 'Number of direct and delegating voters who ranked this initiative worse than the status quo';
    1.23 +
    1.24 +-- UPDATE TABLE "vote" SET "grade" = 0 WHERE "grade" ISNULL;  -- should not be necessary
    1.25 +ALTER TABLE "vote" ALTER COLUMN "grade" SET NOT NULL;
    1.26 +
    1.27 +ALTER TABLE "vote" ADD COLUMN "first_preference" BOOLEAN;
    1.28 +
    1.29 +ALTER TABLE "vote" ADD
    1.30 +        CONSTRAINT "first_preference_flag_only_set_on_positive_grades"
    1.31 +        CHECK ("grade" > 0 OR "first_preference" ISNULL);
    1.32 +
    1.33 +COMMENT ON COLUMN "vote"."first_preference" IS 'Value is automatically set after voting is finished. For positive grades, this value is set to true for the highest (i.e. best) grade.';
    1.34 + 
    1.35 +INSERT INTO "temporary_transaction_data" ("key", "value")
    1.36 +  VALUES ('override_protection_triggers', TRUE::TEXT);
    1.37 +
    1.38 +UPDATE "vote" SET "first_preference" = "subquery"."first_preference"
    1.39 +  FROM (
    1.40 +    SELECT
    1.41 +      "vote"."initiative_id",
    1.42 +      "vote"."member_id",
    1.43 +      CASE WHEN "vote"."grade" > 0 THEN
    1.44 +        CASE WHEN "vote"."grade" = max("agg"."grade") THEN TRUE ELSE FALSE END
    1.45 +      ELSE NULL
    1.46 +      END AS "first_preference"
    1.47 +    FROM "vote"
    1.48 +    JOIN "initiative"  -- NOTE: due to missing index on issue_id
    1.49 +    ON "vote"."issue_id" = "initiative"."issue_id"
    1.50 +    JOIN "vote" AS "agg"
    1.51 +    ON "initiative"."id" = "agg"."initiative_id"
    1.52 +    AND "vote"."member_id" = "agg"."member_id"
    1.53 +    GROUP BY "vote"."initiative_id", "vote"."member_id"
    1.54 +  ) AS "subquery"
    1.55 +  WHERE "vote"."initiative_id" = "subquery"."initiative_id"
    1.56 +  AND "vote"."member_id" = "subquery"."member_id";
    1.57 +
    1.58 +DELETE FROM "temporary_transaction_data"
    1.59 +  WHERE "key" = 'override_protection_triggers';
    1.60 +
    1.61 +UPDATE "initiative"
    1.62 +  SET "first_preference_votes" = coalesce("subquery"."sum", 0)
    1.63 +  FROM (
    1.64 +    SELECT "vote"."initiative_id", sum("direct_voter"."weight")
    1.65 +    FROM "vote" JOIN "direct_voter"
    1.66 +    ON "vote"."issue_id" = "direct_voter"."issue_id"
    1.67 +    AND "vote"."member_id" = "direct_voter"."member_id"
    1.68 +    WHERE "vote"."first_preference"
    1.69 +    GROUP BY "vote"."initiative_id"
    1.70 +  ) AS "subquery"
    1.71 +  WHERE "initiative"."admitted"
    1.72 +  AND "initiative"."id" = "subquery"."initiative_id";
    1.73 +
    1.74 +-- reconstruct battle data (originating from LiquidFeedback Core before v2.0.0)
    1.75 +-- to avoid future data loss when executing "clean_issue" to delete voting data:
    1.76 +INSERT INTO "battle" (
    1.77 +    "issue_id",
    1.78 +    "winning_initiative_id",
    1.79 +    "losing_initiative_id",
    1.80 +    "count"
    1.81 +  ) SELECT
    1.82 +    "battle_view"."issue_id",
    1.83 +    "battle_view"."winning_initiative_id",
    1.84 +    "battle_view"."losing_initiative_id",
    1.85 +    "battle_view"."count"
    1.86 +  FROM (
    1.87 +    SELECT
    1.88 +      "issue"."id" AS "issue_id",
    1.89 +      "winning_initiative"."id" AS "winning_initiative_id",
    1.90 +      "losing_initiative"."id" AS "losing_initiative_id",
    1.91 +      sum(
    1.92 +        CASE WHEN
    1.93 +          coalesce("better_vote"."grade", 0) >
    1.94 +          coalesce("worse_vote"."grade", 0)
    1.95 +        THEN "direct_voter"."weight" ELSE 0 END
    1.96 +      ) AS "count"
    1.97 +    FROM "issue"
    1.98 +    LEFT JOIN "direct_voter"
    1.99 +    ON "issue"."id" = "direct_voter"."issue_id"
   1.100 +    JOIN "battle_participant" AS "winning_initiative"
   1.101 +      ON "issue"."id" = "winning_initiative"."issue_id"
   1.102 +    JOIN "battle_participant" AS "losing_initiative"
   1.103 +      ON "issue"."id" = "losing_initiative"."issue_id"
   1.104 +    LEFT JOIN "vote" AS "better_vote"
   1.105 +      ON "direct_voter"."member_id" = "better_vote"."member_id"
   1.106 +      AND "winning_initiative"."id" = "better_vote"."initiative_id"
   1.107 +    LEFT JOIN "vote" AS "worse_vote"
   1.108 +      ON "direct_voter"."member_id" = "worse_vote"."member_id"
   1.109 +      AND "losing_initiative"."id" = "worse_vote"."initiative_id"
   1.110 +    WHERE "issue"."state" IN ('finished_with_winner', 'finished_without_winner')
   1.111 +    AND "winning_initiative"."id" != "losing_initiative"."id"
   1.112 +    -- NOTE: comparisons with status-quo are intentionally omitted to mark
   1.113 +    --       issues that were counted prior LiquidFeedback Core v2.0.0
   1.114 +    GROUP BY
   1.115 +      "issue"."id",
   1.116 +      "winning_initiative"."id",
   1.117 +      "losing_initiative"."id"
   1.118 +  ) AS "battle_view"
   1.119 +  LEFT JOIN "battle"
   1.120 +  ON "battle_view"."winning_initiative_id" = "battle"."winning_initiative_id"
   1.121 +  AND "battle_view"."losing_initiative_id" = "battle"."losing_initiative_id"
   1.122 +  WHERE "battle" ISNULL;
   1.123 +
   1.124 +CREATE OR REPLACE FUNCTION "close_voting"("issue_id_p" "issue"."id"%TYPE)
   1.125 +  RETURNS VOID
   1.126 +  LANGUAGE 'plpgsql' VOLATILE AS $$
   1.127 +    DECLARE
   1.128 +      "area_id_v"   "area"."id"%TYPE;
   1.129 +      "unit_id_v"   "unit"."id"%TYPE;
   1.130 +      "member_id_v" "member"."id"%TYPE;
   1.131 +    BEGIN
   1.132 +      PERFORM "require_transaction_isolation"();
   1.133 +      SELECT "area_id" INTO "area_id_v" FROM "issue" WHERE "id" = "issue_id_p";
   1.134 +      SELECT "unit_id" INTO "unit_id_v" FROM "area"  WHERE "id" = "area_id_v";
   1.135 +      -- override protection triggers:
   1.136 +      INSERT INTO "temporary_transaction_data" ("key", "value")
   1.137 +        VALUES ('override_protection_triggers', TRUE::TEXT);
   1.138 +      -- delete timestamp of voting comment:
   1.139 +      UPDATE "direct_voter" SET "comment_changed" = NULL
   1.140 +        WHERE "issue_id" = "issue_id_p";
   1.141 +      -- delete delegating votes (in cases of manual reset of issue state):
   1.142 +      DELETE FROM "delegating_voter"
   1.143 +        WHERE "issue_id" = "issue_id_p";
   1.144 +      -- delete votes from non-privileged voters:
   1.145 +      DELETE FROM "direct_voter"
   1.146 +        USING (
   1.147 +          SELECT
   1.148 +            "direct_voter"."member_id"
   1.149 +          FROM "direct_voter"
   1.150 +          JOIN "member" ON "direct_voter"."member_id" = "member"."id"
   1.151 +          LEFT JOIN "privilege"
   1.152 +          ON "privilege"."unit_id" = "unit_id_v"
   1.153 +          AND "privilege"."member_id" = "direct_voter"."member_id"
   1.154 +          WHERE "direct_voter"."issue_id" = "issue_id_p" AND (
   1.155 +            "member"."active" = FALSE OR
   1.156 +            "privilege"."voting_right" ISNULL OR
   1.157 +            "privilege"."voting_right" = FALSE
   1.158 +          )
   1.159 +        ) AS "subquery"
   1.160 +        WHERE "direct_voter"."issue_id" = "issue_id_p"
   1.161 +        AND "direct_voter"."member_id" = "subquery"."member_id";
   1.162 +      -- consider delegations:
   1.163 +      UPDATE "direct_voter" SET "weight" = 1
   1.164 +        WHERE "issue_id" = "issue_id_p";
   1.165 +      PERFORM "add_vote_delegations"("issue_id_p");
   1.166 +      -- mark first preferences:
   1.167 +      UPDATE "vote" SET "first_preference" = "subquery"."first_preference"
   1.168 +        FROM (
   1.169 +          SELECT
   1.170 +            "vote"."initiative_id",
   1.171 +            "vote"."member_id",
   1.172 +            CASE WHEN "vote"."grade" > 0 THEN
   1.173 +              CASE WHEN "vote"."grade" = max("agg"."grade") THEN TRUE ELSE FALSE END
   1.174 +            ELSE NULL
   1.175 +            END AS "first_preference"
   1.176 +          FROM "vote"
   1.177 +          JOIN "initiative"  -- NOTE: due to missing index on issue_id
   1.178 +          ON "vote"."issue_id" = "initiative"."issue_id"
   1.179 +          JOIN "vote" AS "agg"
   1.180 +          ON "initiative"."id" = "agg"."initiative_id"
   1.181 +          AND "vote"."member_id" = "agg"."member_id"
   1.182 +          GROUP BY "vote"."initiative_id", "vote"."member_id"
   1.183 +        ) AS "subquery"
   1.184 +        WHERE "vote"."issue_id" = "issue_id_p"
   1.185 +        AND "vote"."initiative_id" = "subquery"."initiative_id"
   1.186 +        AND "vote"."member_id" = "subquery"."member_id";
   1.187 +      -- finish overriding protection triggers (avoids garbage):
   1.188 +      DELETE FROM "temporary_transaction_data"
   1.189 +        WHERE "key" = 'override_protection_triggers';
   1.190 +      -- materialize battle_view:
   1.191 +      -- NOTE: "closed" column of issue must be set at this point
   1.192 +      DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
   1.193 +      INSERT INTO "battle" (
   1.194 +        "issue_id",
   1.195 +        "winning_initiative_id", "losing_initiative_id",
   1.196 +        "count"
   1.197 +      ) SELECT
   1.198 +        "issue_id",
   1.199 +        "winning_initiative_id", "losing_initiative_id",
   1.200 +        "count"
   1.201 +        FROM "battle_view" WHERE "issue_id" = "issue_id_p";
   1.202 +      -- set voter count:
   1.203 +      UPDATE "issue" SET
   1.204 +        "voter_count" = (
   1.205 +          SELECT coalesce(sum("weight"), 0)
   1.206 +          FROM "direct_voter" WHERE "issue_id" = "issue_id_p"
   1.207 +        )
   1.208 +        WHERE "id" = "issue_id_p";
   1.209 +      -- calculate "first_preference_votes":
   1.210 +      UPDATE "initiative"
   1.211 +        SET "first_preference_votes" = coalesce("subquery"."sum", 0)
   1.212 +        FROM (
   1.213 +          SELECT "vote"."initiative_id", sum("direct_voter"."weight")
   1.214 +          FROM "vote" JOIN "direct_voter"
   1.215 +          ON "vote"."issue_id" = "direct_voter"."issue_id"
   1.216 +          AND "vote"."member_id" = "direct_voter"."member_id"
   1.217 +          WHERE "vote"."first_preference"
   1.218 +          GROUP BY "vote"."initiative_id"
   1.219 +        ) AS "subquery"
   1.220 +        WHERE "initiative"."issue_id" = "issue_id_p"
   1.221 +        AND "initiative"."admitted"
   1.222 +        AND "initiative"."id" = "subquery"."initiative_id";
   1.223 +      -- copy "positive_votes" and "negative_votes" from "battle" table:
   1.224 +      UPDATE "initiative" SET
   1.225 +        "positive_votes" = "battle_win"."count",
   1.226 +        "negative_votes" = "battle_lose"."count"
   1.227 +        FROM "battle" AS "battle_win", "battle" AS "battle_lose"
   1.228 +        WHERE
   1.229 +          "battle_win"."issue_id" = "issue_id_p" AND
   1.230 +          "battle_win"."winning_initiative_id" = "initiative"."id" AND
   1.231 +          "battle_win"."losing_initiative_id" ISNULL AND
   1.232 +          "battle_lose"."issue_id" = "issue_id_p" AND
   1.233 +          "battle_lose"."losing_initiative_id" = "initiative"."id" AND
   1.234 +          "battle_lose"."winning_initiative_id" ISNULL;
   1.235 +    END;
   1.236 +  $$;
   1.237 +
   1.238  COMMIT;

Impressum / About Us