liquid_feedback_core

diff core.sql @ 385:e474e9e1240a

Code cleanup regarding temporary storage of session/transaction data to disable protection triggers
author jbe
date Sat Mar 23 13:05:43 2013 +0100 (2013-03-23)
parents 1c991490f075
children ae69cf82c05f
line diff
     1.1 --- a/core.sql	Fri Mar 22 22:46:51 2013 +0100
     1.2 +++ b/core.sql	Sat Mar 23 13:05:43 2013 +0100
     1.3 @@ -7,7 +7,7 @@
     1.4  BEGIN;
     1.5  
     1.6  CREATE VIEW "liquid_feedback_version" AS
     1.7 -  SELECT * FROM (VALUES ('2.2.2', 2, 2, 2))
     1.8 +  SELECT * FROM (VALUES ('2.2.3', 2, 2, 3))
     1.9    AS "subquery"("string", "major", "minor", "revision");
    1.10  
    1.11  
    1.12 @@ -54,15 +54,15 @@
    1.13  -------------------------
    1.14  
    1.15  
    1.16 -CREATE TABLE "internal_session_store" (
    1.17 -        PRIMARY KEY ("backend_pid", "key"),
    1.18 -        "backend_pid"           INT4,
    1.19 +CREATE TABLE "temporary_transaction_data" (
    1.20 +        PRIMARY KEY ("txid", "key"),
    1.21 +        "txid"                  INT8            DEFAULT txid_current(),
    1.22          "key"                   TEXT,
    1.23          "value"                 TEXT            NOT NULL );
    1.24  
    1.25 -COMMENT ON TABLE "internal_session_store" IS 'Table to store session variables; shall be emptied before a transaction is committed';
    1.26 -
    1.27 -COMMENT ON COLUMN "internal_session_store"."backend_pid" IS 'Value returned by function pg_backend_pid()';
    1.28 +COMMENT ON TABLE "temporary_transaction_data" IS 'Table to store temporary transaction data; shall be emptied before a transaction is committed';
    1.29 +
    1.30 +COMMENT ON COLUMN "temporary_transaction_data"."txid" IS 'Value returned by function txid_current(); should be added to WHERE clause, when doing SELECT on this table, but ignored when doing DELETE on this table';
    1.31  
    1.32  
    1.33  CREATE TABLE "system_setting" (
    1.34 @@ -1599,8 +1599,8 @@
    1.35        "issue_row"  "issue"%ROWTYPE;
    1.36      BEGIN
    1.37        IF EXISTS (
    1.38 -        SELECT NULL FROM "internal_session_store"
    1.39 -        WHERE "backend_pid" = pg_backend_pid()
    1.40 +        SELECT NULL FROM "temporary_transaction_data"
    1.41 +        WHERE "txid" = txid_current()
    1.42          AND "key" = 'override_protection_triggers'
    1.43          AND "value" = TRUE::TEXT
    1.44        ) THEN
    1.45 @@ -3671,9 +3671,8 @@
    1.46        SELECT "area_id" INTO "area_id_v" FROM "issue" WHERE "id" = "issue_id_p";
    1.47        SELECT "unit_id" INTO "unit_id_v" FROM "area"  WHERE "id" = "area_id_v";
    1.48        -- override protection triggers:
    1.49 -      DELETE FROM "internal_session_store";
    1.50 -      INSERT INTO "internal_session_store" ("backend_pid", "key", "value")
    1.51 -        VALUES (pg_backend_pid(), 'override_protection_triggers', TRUE::TEXT);
    1.52 +      INSERT INTO "temporary_transaction_data" ("key", "value")
    1.53 +        VALUES ('override_protection_triggers', TRUE::TEXT);
    1.54        -- delete timestamp of voting comment:
    1.55        UPDATE "direct_voter" SET "comment_changed" = NULL
    1.56          WHERE "issue_id" = "issue_id_p";
    1.57 @@ -3702,8 +3701,9 @@
    1.58        UPDATE "direct_voter" SET "weight" = 1
    1.59          WHERE "issue_id" = "issue_id_p";
    1.60        PERFORM "add_vote_delegations"("issue_id_p");
    1.61 -      -- finish overriding protection triggers (mandatory, as pids may be reused):
    1.62 -      DELETE FROM "internal_session_store";
    1.63 +      -- finish overriding protection triggers (avoids garbage):
    1.64 +      DELETE FROM "temporary_transaction_data"
    1.65 +        WHERE "key" = 'override_protection_triggers';
    1.66        -- materialize battle_view:
    1.67        -- NOTE: "closed" column of issue must be set at this point
    1.68        DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
    1.69 @@ -4344,17 +4344,14 @@
    1.70  CREATE FUNCTION "clean_issue"("issue_id_p" "issue"."id"%TYPE)
    1.71    RETURNS VOID
    1.72    LANGUAGE 'plpgsql' VOLATILE AS $$
    1.73 -    DECLARE
    1.74 -      "issue_row" "issue"%ROWTYPE;
    1.75      BEGIN
    1.76 -      SELECT * INTO "issue_row"
    1.77 -        FROM "issue" WHERE "id" = "issue_id_p"
    1.78 -        FOR UPDATE;
    1.79 -      IF "issue_row"."cleaned" ISNULL THEN
    1.80 -        UPDATE "issue" SET
    1.81 -          "state"  = 'voting',
    1.82 -          "closed" = NULL
    1.83 -          WHERE "id" = "issue_id_p";
    1.84 +      IF EXISTS (
    1.85 +        SELECT NULL FROM "issue" WHERE "id" = "issue_id_p" AND "cleaned" ISNULL
    1.86 +      ) THEN
    1.87 +        -- override protection triggers:
    1.88 +        INSERT INTO "temporary_transaction_data" ("key", "value")
    1.89 +          VALUES ('override_protection_triggers', TRUE::TEXT);
    1.90 +        -- clean data:
    1.91          DELETE FROM "delegating_voter"
    1.92            WHERE "issue_id" = "issue_id_p";
    1.93          DELETE FROM "direct_voter"
    1.94 @@ -4375,11 +4372,11 @@
    1.95            USING "initiative"  -- NOTE: due to missing index on issue_id
    1.96            WHERE "initiative"."issue_id" = "issue_id_p"
    1.97            AND "supporter"."initiative_id" = "initiative_id";
    1.98 -        UPDATE "issue" SET
    1.99 -          "state"   = "issue_row"."state",
   1.100 -          "closed"  = "issue_row"."closed",
   1.101 -          "cleaned" = now()
   1.102 -          WHERE "id" = "issue_id_p";
   1.103 +        -- mark issue as cleaned:
   1.104 +        UPDATE "issue" SET "cleaned" = now() WHERE "id" = "issue_id_p";
   1.105 +        -- finish overriding protection triggers (avoids garbage):
   1.106 +        DELETE FROM "temporary_transaction_data"
   1.107 +          WHERE "key" = 'override_protection_triggers';
   1.108        END IF;
   1.109        RETURN;
   1.110      END;
   1.111 @@ -4451,6 +4448,7 @@
   1.112    RETURNS VOID
   1.113    LANGUAGE 'plpgsql' VOLATILE AS $$
   1.114      BEGIN
   1.115 +      DELETE FROM "temporary_transaction_data";
   1.116        DELETE FROM "member" WHERE "activated" ISNULL;
   1.117        UPDATE "member" SET
   1.118          "invite_code"                  = NULL,

Impressum / About Us