liquid_feedback_core

annotate core.sql @ 253:389200fd973d

Added field "participating_member_id" to result of "delegation_info" function
author jbe
date Sun May 20 17:20:50 2012 +0200 (2012-05-20)
parents 7b8966b801e5
children a1db85ce10ea
rev   line source
jbe@0 1
jbe@92 2 -- Execute the following command manually for PostgreSQL prior version 9.0:
jbe@92 3 -- CREATE LANGUAGE plpgsql;
jbe@0 4
jbe@0 5 -- NOTE: In PostgreSQL every UNIQUE constraint implies creation of an index
jbe@0 6
jbe@0 7 BEGIN;
jbe@0 8
jbe@5 9 CREATE VIEW "liquid_feedback_version" AS
jbe@253 10 SELECT * FROM (VALUES ('2.0.10', 2, 0, 10))
jbe@5 11 AS "subquery"("string", "major", "minor", "revision");
jbe@5 12
jbe@0 13
jbe@0 14
jbe@7 15 ----------------------
jbe@7 16 -- Full text search --
jbe@7 17 ----------------------
jbe@7 18
jbe@7 19
jbe@7 20 CREATE FUNCTION "text_search_query"("query_text_p" TEXT)
jbe@7 21 RETURNS TSQUERY
jbe@7 22 LANGUAGE 'plpgsql' IMMUTABLE AS $$
jbe@7 23 BEGIN
jbe@7 24 RETURN plainto_tsquery('pg_catalog.simple', "query_text_p");
jbe@7 25 END;
jbe@7 26 $$;
jbe@7 27
jbe@7 28 COMMENT ON FUNCTION "text_search_query"(TEXT) IS 'Usage: WHERE "text_search_data" @@ "text_search_query"(''<user query>'')';
jbe@7 29
jbe@7 30
jbe@7 31 CREATE FUNCTION "highlight"
jbe@7 32 ( "body_p" TEXT,
jbe@7 33 "query_text_p" TEXT )
jbe@7 34 RETURNS TEXT
jbe@7 35 LANGUAGE 'plpgsql' IMMUTABLE AS $$
jbe@7 36 BEGIN
jbe@7 37 RETURN ts_headline(
jbe@7 38 'pg_catalog.simple',
jbe@8 39 replace(replace("body_p", e'\\', e'\\\\'), '*', e'\\*'),
jbe@7 40 "text_search_query"("query_text_p"),
jbe@7 41 'StartSel=* StopSel=* HighlightAll=TRUE' );
jbe@7 42 END;
jbe@7 43 $$;
jbe@7 44
jbe@7 45 COMMENT ON FUNCTION "highlight"
jbe@7 46 ( "body_p" TEXT,
jbe@7 47 "query_text_p" TEXT )
jbe@7 48 IS 'For a given a user query this function encapsulates all matches with asterisks. Asterisks and backslashes being already present are preceeded with one extra backslash.';
jbe@7 49
jbe@7 50
jbe@7 51
jbe@0 52 -------------------------
jbe@0 53 -- Tables and indicies --
jbe@0 54 -------------------------
jbe@0 55
jbe@8 56
jbe@104 57 CREATE TABLE "system_setting" (
jbe@104 58 "member_ttl" INTERVAL );
jbe@104 59 CREATE UNIQUE INDEX "system_setting_singleton_idx" ON "system_setting" ((1));
jbe@104 60
jbe@104 61 COMMENT ON TABLE "system_setting" IS 'This table contains only one row with different settings in each column.';
jbe@104 62 COMMENT ON INDEX "system_setting_singleton_idx" IS 'This index ensures that "system_setting" only contains one row maximum.';
jbe@104 63
jbe@184 64 COMMENT ON COLUMN "system_setting"."member_ttl" IS 'Time after members get their "active" flag set to FALSE, if they do not show any activity.';
jbe@104 65
jbe@104 66
jbe@111 67 CREATE TABLE "contingent" (
jbe@111 68 "time_frame" INTERVAL PRIMARY KEY,
jbe@111 69 "text_entry_limit" INT4,
jbe@111 70 "initiative_limit" INT4 );
jbe@111 71
jbe@111 72 COMMENT ON TABLE "contingent" IS 'Amount of text entries or initiatives a user may create within a given time frame. Only one row needs to be fulfilled for a member to be allowed to post. This table must not be empty.';
jbe@111 73
jbe@111 74 COMMENT ON COLUMN "contingent"."text_entry_limit" IS 'Number of new drafts or suggestions to be submitted by each member within the given time frame';
jbe@111 75 COMMENT ON COLUMN "contingent"."initiative_limit" IS 'Number of new initiatives to be opened by each member within a given time frame';
jbe@111 76
jbe@111 77
jbe@113 78 CREATE TYPE "notify_level" AS ENUM
jbe@113 79 ('none', 'voting', 'verification', 'discussion', 'all');
jbe@113 80
jbe@113 81 COMMENT ON TYPE "notify_level" IS 'Level of notification: ''none'' = no notifications, ''voting'' = notifications about finished issues and issues in voting, ''verification'' = notifications about finished issues, issues in voting and verification phase, ''discussion'' = notifications about everything except issues in admission phase, ''all'' = notifications about everything';
jbe@113 82
jbe@113 83
jbe@0 84 CREATE TABLE "member" (
jbe@0 85 "id" SERIAL4 PRIMARY KEY,
jbe@13 86 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@181 87 "invite_code" TEXT UNIQUE,
jbe@232 88 "invite_code_expiry" TIMESTAMPTZ,
jbe@182 89 "admin_comment" TEXT,
jbe@181 90 "activated" TIMESTAMPTZ,
jbe@184 91 "last_activity" DATE,
jbe@42 92 "last_login" TIMESTAMPTZ,
jbe@45 93 "login" TEXT UNIQUE,
jbe@0 94 "password" TEXT,
jbe@99 95 "locked" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@181 96 "active" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@0 97 "admin" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@221 98 "lang" TEXT,
jbe@7 99 "notify_email" TEXT,
jbe@11 100 "notify_email_unconfirmed" TEXT,
jbe@11 101 "notify_email_secret" TEXT UNIQUE,
jbe@11 102 "notify_email_secret_expiry" TIMESTAMPTZ,
jbe@55 103 "notify_email_lock_expiry" TIMESTAMPTZ,
jbe@225 104 "notify_level" "notify_level",
jbe@11 105 "password_reset_secret" TEXT UNIQUE,
jbe@11 106 "password_reset_secret_expiry" TIMESTAMPTZ,
jbe@225 107 "name" TEXT UNIQUE,
jbe@7 108 "identification" TEXT UNIQUE,
jbe@214 109 "authentication" TEXT,
jbe@7 110 "organizational_unit" TEXT,
jbe@7 111 "internal_posts" TEXT,
jbe@7 112 "realname" TEXT,
jbe@7 113 "birthday" DATE,
jbe@7 114 "address" TEXT,
jbe@7 115 "email" TEXT,
jbe@7 116 "xmpp_address" TEXT,
jbe@7 117 "website" TEXT,
jbe@7 118 "phone" TEXT,
jbe@7 119 "mobile_phone" TEXT,
jbe@7 120 "profession" TEXT,
jbe@7 121 "external_memberships" TEXT,
jbe@7 122 "external_posts" TEXT,
jbe@159 123 "formatting_engine" TEXT,
jbe@7 124 "statement" TEXT,
jbe@181 125 "text_search_data" TSVECTOR,
jbe@184 126 CONSTRAINT "active_requires_activated_and_last_activity"
jbe@225 127 CHECK ("active" = FALSE OR ("activated" NOTNULL AND "last_activity" NOTNULL)),
jbe@225 128 CONSTRAINT "name_not_null_if_activated"
jbe@225 129 CHECK ("activated" ISNULL OR "name" NOTNULL) );
jbe@0 130 CREATE INDEX "member_active_idx" ON "member" ("active");
jbe@8 131 CREATE INDEX "member_text_search_data_idx" ON "member" USING gin ("text_search_data");
jbe@7 132 CREATE TRIGGER "update_text_search_data"
jbe@7 133 BEFORE INSERT OR UPDATE ON "member"
jbe@7 134 FOR EACH ROW EXECUTE PROCEDURE
jbe@7 135 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
jbe@7 136 "name", "identification", "organizational_unit", "internal_posts",
jbe@7 137 "realname", "external_memberships", "external_posts", "statement" );
jbe@0 138
jbe@0 139 COMMENT ON TABLE "member" IS 'Users of the system, e.g. members of an organization';
jbe@0 140
jbe@181 141 COMMENT ON COLUMN "member"."created" IS 'Creation of member record and/or invite code';
jbe@181 142 COMMENT ON COLUMN "member"."invite_code" IS 'Optional invite code, to allow a member to initialize his/her account the first time';
jbe@232 143 COMMENT ON COLUMN "member"."invite_code_expiry" IS 'Expiry data/time for "invite_code"';
jbe@182 144 COMMENT ON COLUMN "member"."admin_comment" IS 'Hidden comment for administrative purposes';
jbe@207 145 COMMENT ON COLUMN "member"."activated" IS 'Timestamp of first activation of account (i.e. usage of "invite_code"); required to be set for "active" members';
jbe@184 146 COMMENT ON COLUMN "member"."last_activity" IS 'Date of last activity of member; required to be set for "active" members';
jbe@103 147 COMMENT ON COLUMN "member"."last_login" IS 'Timestamp of last login';
jbe@10 148 COMMENT ON COLUMN "member"."login" IS 'Login name';
jbe@10 149 COMMENT ON COLUMN "member"."password" IS 'Password (preferably as crypto-hash, depending on the frontend or access layer)';
jbe@99 150 COMMENT ON COLUMN "member"."locked" IS 'Locked members can not log in.';
jbe@184 151 COMMENT ON COLUMN "member"."active" IS 'Memberships, support and votes are taken into account when corresponding members are marked as active. Automatically set to FALSE, if "last_activity" is older than "system_setting"."member_ttl".';
jbe@10 152 COMMENT ON COLUMN "member"."admin" IS 'TRUE for admins, which can administrate other users and setup policies and areas';
jbe@221 153 COMMENT ON COLUMN "member"."lang" IS 'Language code of the preferred language of the member';
jbe@10 154 COMMENT ON COLUMN "member"."notify_email" IS 'Email address where notifications of the system are sent to';
jbe@10 155 COMMENT ON COLUMN "member"."notify_email_unconfirmed" IS 'Unconfirmed email address provided by the member to be copied into "notify_email" field after verification';
jbe@10 156 COMMENT ON COLUMN "member"."notify_email_secret" IS 'Secret sent to the address in "notify_email_unconformed"';
jbe@10 157 COMMENT ON COLUMN "member"."notify_email_secret_expiry" IS 'Expiry date/time for "notify_email_secret"';
jbe@55 158 COMMENT ON COLUMN "member"."notify_email_lock_expiry" IS 'Date/time until no further email confirmation mails may be sent (abuse protection)';
jbe@225 159 COMMENT ON COLUMN "member"."notify_level" IS 'Selects which event notifications are to be sent to the "notify_email" mail address, may be NULL if member did not make any selection yet';
jbe@225 160 COMMENT ON COLUMN "member"."name" IS 'Distinct name of the member, may be NULL if account has not been activated yet';
jbe@10 161 COMMENT ON COLUMN "member"."identification" IS 'Optional identification number or code of the member';
jbe@214 162 COMMENT ON COLUMN "member"."authentication" IS 'Information about how this member was authenticated';
jbe@10 163 COMMENT ON COLUMN "member"."organizational_unit" IS 'Branch or division of the organization the member belongs to';
jbe@10 164 COMMENT ON COLUMN "member"."internal_posts" IS 'Posts (offices) of the member inside the organization';
jbe@10 165 COMMENT ON COLUMN "member"."realname" IS 'Real name of the member, may be identical with "name"';
jbe@10 166 COMMENT ON COLUMN "member"."email" IS 'Published email address of the member; not used for system notifications';
jbe@10 167 COMMENT ON COLUMN "member"."external_memberships" IS 'Other organizations the member is involved in';
jbe@10 168 COMMENT ON COLUMN "member"."external_posts" IS 'Posts (offices) outside the organization';
jbe@159 169 COMMENT ON COLUMN "member"."formatting_engine" IS 'Allows different formatting engines (i.e. wiki formats) to be used for "member"."statement"';
jbe@207 170 COMMENT ON COLUMN "member"."statement" IS 'Freely chosen text of the member for his/her profile';
jbe@7 171
jbe@7 172
jbe@185 173 CREATE TYPE "application_access_level" AS ENUM
jbe@185 174 ('member', 'full', 'pseudonymous', 'anonymous');
jbe@185 175
jbe@185 176 COMMENT ON TYPE "application_access_level" IS 'Access privileges for applications using the API';
jbe@185 177
jbe@185 178
jbe@185 179 CREATE TABLE "member_application" (
jbe@185 180 "id" SERIAL8 PRIMARY KEY,
jbe@185 181 UNIQUE ("member_id", "name"),
jbe@185 182 "member_id" INT4 NOT NULL REFERENCES "member" ("id")
jbe@185 183 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@185 184 "name" TEXT NOT NULL,
jbe@185 185 "comment" TEXT,
jbe@185 186 "access_level" "application_access_level" NOT NULL,
jbe@190 187 "key" TEXT NOT NULL UNIQUE,
jbe@185 188 "last_usage" TIMESTAMPTZ );
jbe@185 189
jbe@185 190 COMMENT ON TABLE "member_application" IS 'Registered application being allowed to use the API';
jbe@185 191
jbe@185 192
jbe@13 193 CREATE TABLE "member_history" (
jbe@13 194 "id" SERIAL8 PRIMARY KEY,
jbe@13 195 "member_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@13 196 "until" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@42 197 "active" BOOLEAN NOT NULL,
jbe@13 198 "name" TEXT NOT NULL );
jbe@45 199 CREATE INDEX "member_history_member_id_idx" ON "member_history" ("member_id");
jbe@13 200
jbe@57 201 COMMENT ON TABLE "member_history" IS 'Filled by trigger; keeps information about old names and active flag of members';
jbe@13 202
jbe@13 203 COMMENT ON COLUMN "member_history"."id" IS 'Primary key, which can be used to sort entries correctly (and time warp resistant)';
jbe@57 204 COMMENT ON COLUMN "member_history"."until" IS 'Timestamp until the data was valid';
jbe@13 205
jbe@13 206
jbe@159 207 CREATE TABLE "rendered_member_statement" (
jbe@159 208 PRIMARY KEY ("member_id", "format"),
jbe@159 209 "member_id" INT8 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@159 210 "format" TEXT,
jbe@159 211 "content" TEXT NOT NULL );
jbe@159 212
jbe@159 213 COMMENT ON TABLE "rendered_member_statement" IS 'This table may be used by frontends to cache "rendered" member statements (e.g. HTML output generated from wiki text)';
jbe@9 214
jbe@9 215
jbe@9 216 CREATE TABLE "setting" (
jbe@9 217 PRIMARY KEY ("member_id", "key"),
jbe@9 218 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@9 219 "key" TEXT NOT NULL,
jbe@9 220 "value" TEXT NOT NULL );
jbe@9 221 CREATE INDEX "setting_key_idx" ON "setting" ("key");
jbe@9 222
jbe@38 223 COMMENT ON TABLE "setting" IS 'Place to store a frontend specific setting for members as a string';
jbe@9 224
jbe@9 225 COMMENT ON COLUMN "setting"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
jbe@9 226
jbe@9 227
jbe@16 228 CREATE TABLE "setting_map" (
jbe@16 229 PRIMARY KEY ("member_id", "key", "subkey"),
jbe@16 230 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@16 231 "key" TEXT NOT NULL,
jbe@16 232 "subkey" TEXT NOT NULL,
jbe@16 233 "value" TEXT NOT NULL );
jbe@16 234 CREATE INDEX "setting_map_key_idx" ON "setting_map" ("key");
jbe@16 235
jbe@23 236 COMMENT ON TABLE "setting_map" IS 'Place to store a frontend specific setting for members as a map of key value pairs';
jbe@16 237
jbe@16 238 COMMENT ON COLUMN "setting_map"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
jbe@16 239 COMMENT ON COLUMN "setting_map"."subkey" IS 'Key of a map entry';
jbe@16 240 COMMENT ON COLUMN "setting_map"."value" IS 'Value of a map entry';
jbe@16 241
jbe@16 242
jbe@23 243 CREATE TABLE "member_relation_setting" (
jbe@23 244 PRIMARY KEY ("member_id", "key", "other_member_id"),
jbe@23 245 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 246 "key" TEXT NOT NULL,
jbe@23 247 "other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 248 "value" TEXT NOT NULL );
jbe@23 249
jbe@38 250 COMMENT ON TABLE "member_relation_setting" IS 'Place to store a frontend specific setting related to relations between members as a string';
jbe@23 251
jbe@23 252
jbe@7 253 CREATE TYPE "member_image_type" AS ENUM ('photo', 'avatar');
jbe@7 254
jbe@7 255 COMMENT ON TYPE "member_image_type" IS 'Types of images for a member';
jbe@7 256
jbe@7 257
jbe@7 258 CREATE TABLE "member_image" (
jbe@7 259 PRIMARY KEY ("member_id", "image_type", "scaled"),
jbe@7 260 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@7 261 "image_type" "member_image_type",
jbe@7 262 "scaled" BOOLEAN,
jbe@7 263 "content_type" TEXT,
jbe@7 264 "data" BYTEA NOT NULL );
jbe@7 265
jbe@7 266 COMMENT ON TABLE "member_image" IS 'Images of members';
jbe@7 267
jbe@7 268 COMMENT ON COLUMN "member_image"."scaled" IS 'FALSE for original image, TRUE for scaled version of the image';
jbe@0 269
jbe@0 270
jbe@4 271 CREATE TABLE "member_count" (
jbe@5 272 "calculated" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
jbe@5 273 "total_count" INT4 NOT NULL );
jbe@4 274
jbe@5 275 COMMENT ON TABLE "member_count" IS 'Contains one row which contains the total count of active(!) members and a timestamp indicating when the total member count and area member counts were calculated';
jbe@4 276
jbe@5 277 COMMENT ON COLUMN "member_count"."calculated" IS 'timestamp indicating when the total member count and area member counts were calculated';
jbe@5 278 COMMENT ON COLUMN "member_count"."total_count" IS 'Total count of active(!) members';
jbe@4 279
jbe@4 280
jbe@0 281 CREATE TABLE "contact" (
jbe@0 282 PRIMARY KEY ("member_id", "other_member_id"),
jbe@0 283 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 284 "other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@11 285 "public" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@11 286 CONSTRAINT "cant_save_yourself_as_contact"
jbe@11 287 CHECK ("member_id" != "other_member_id") );
jbe@113 288 CREATE INDEX "contact_other_member_id_idx" ON "contact" ("other_member_id");
jbe@0 289
jbe@0 290 COMMENT ON TABLE "contact" IS 'Contact lists';
jbe@0 291
jbe@0 292 COMMENT ON COLUMN "contact"."member_id" IS 'Member having the contact list';
jbe@0 293 COMMENT ON COLUMN "contact"."other_member_id" IS 'Member referenced in the contact list';
jbe@0 294 COMMENT ON COLUMN "contact"."public" IS 'TRUE = display contact publically';
jbe@0 295
jbe@0 296
jbe@113 297 CREATE TABLE "ignored_member" (
jbe@113 298 PRIMARY KEY ("member_id", "other_member_id"),
jbe@113 299 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@113 300 "other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@113 301 CREATE INDEX "ignored_member_other_member_id_idx" ON "ignored_member" ("other_member_id");
jbe@113 302
jbe@113 303 COMMENT ON TABLE "ignored_member" IS 'Possibility to filter other members';
jbe@113 304
jbe@113 305 COMMENT ON COLUMN "ignored_member"."member_id" IS 'Member ignoring someone';
jbe@113 306 COMMENT ON COLUMN "ignored_member"."other_member_id" IS 'Member being ignored';
jbe@113 307
jbe@113 308
jbe@220 309 CREATE TABLE "session" (
jbe@220 310 "ident" TEXT PRIMARY KEY,
jbe@220 311 "additional_secret" TEXT,
jbe@220 312 "expiry" TIMESTAMPTZ NOT NULL DEFAULT now() + '24 hours',
jbe@220 313 "member_id" INT8 REFERENCES "member" ("id") ON DELETE SET NULL,
jbe@220 314 "lang" TEXT );
jbe@220 315 CREATE INDEX "session_expiry_idx" ON "session" ("expiry");
jbe@220 316
jbe@220 317 COMMENT ON TABLE "session" IS 'Sessions, i.e. for a web-frontend or API layer';
jbe@220 318
jbe@220 319 COMMENT ON COLUMN "session"."ident" IS 'Secret session identifier (i.e. random string)';
jbe@220 320 COMMENT ON COLUMN "session"."additional_secret" IS 'Additional field to store a secret, which can be used against CSRF attacks';
jbe@220 321 COMMENT ON COLUMN "session"."member_id" IS 'Reference to member, who is logged in';
jbe@220 322 COMMENT ON COLUMN "session"."lang" IS 'Language code of the selected language';
jbe@220 323
jbe@220 324
jbe@0 325 CREATE TABLE "policy" (
jbe@0 326 "id" SERIAL4 PRIMARY KEY,
jbe@9 327 "index" INT4 NOT NULL,
jbe@0 328 "active" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@0 329 "name" TEXT NOT NULL UNIQUE,
jbe@0 330 "description" TEXT NOT NULL DEFAULT '',
jbe@0 331 "admission_time" INTERVAL NOT NULL,
jbe@0 332 "discussion_time" INTERVAL NOT NULL,
jbe@3 333 "verification_time" INTERVAL NOT NULL,
jbe@0 334 "voting_time" INTERVAL NOT NULL,
jbe@0 335 "issue_quorum_num" INT4 NOT NULL,
jbe@0 336 "issue_quorum_den" INT4 NOT NULL,
jbe@0 337 "initiative_quorum_num" INT4 NOT NULL,
jbe@10 338 "initiative_quorum_den" INT4 NOT NULL,
jbe@167 339 "direct_majority_num" INT4 NOT NULL DEFAULT 1,
jbe@167 340 "direct_majority_den" INT4 NOT NULL DEFAULT 2,
jbe@167 341 "direct_majority_strict" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@167 342 "direct_majority_positive" INT4 NOT NULL DEFAULT 0,
jbe@167 343 "direct_majority_non_negative" INT4 NOT NULL DEFAULT 0,
jbe@167 344 "indirect_majority_num" INT4 NOT NULL DEFAULT 1,
jbe@167 345 "indirect_majority_den" INT4 NOT NULL DEFAULT 2,
jbe@167 346 "indirect_majority_strict" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@167 347 "indirect_majority_positive" INT4 NOT NULL DEFAULT 0,
jbe@167 348 "indirect_majority_non_negative" INT4 NOT NULL DEFAULT 0,
jbe@167 349 "no_reverse_beat_path" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@167 350 "no_multistage_majority" BOOLEAN NOT NULL DEFAULT FALSE );
jbe@0 351 CREATE INDEX "policy_active_idx" ON "policy" ("active");
jbe@0 352
jbe@0 353 COMMENT ON TABLE "policy" IS 'Policies for a particular proceeding type (timelimits, quorum)';
jbe@0 354
jbe@9 355 COMMENT ON COLUMN "policy"."index" IS 'Determines the order in listings';
jbe@0 356 COMMENT ON COLUMN "policy"."active" IS 'TRUE = policy can be used for new issues';
jbe@207 357 COMMENT ON COLUMN "policy"."admission_time" IS 'Maximum duration of issue state ''admission''; Maximum time an issue stays open without being "accepted"';
jbe@207 358 COMMENT ON COLUMN "policy"."discussion_time" IS 'Duration of issue state ''discussion''; Regular time until an issue is "half_frozen" after being "accepted"';
jbe@207 359 COMMENT ON COLUMN "policy"."verification_time" IS 'Duration of issue state ''verification''; Regular time until an issue is "fully_frozen" (e.g. entering issue state ''voting'') after being "half_frozen"';
jbe@207 360 COMMENT ON COLUMN "policy"."voting_time" IS 'Duration of issue state ''voting''; Time after an issue is "fully_frozen" but not "closed" (duration of issue state ''voting'')';
jbe@207 361 COMMENT ON COLUMN "policy"."issue_quorum_num" IS 'Numerator of potential supporter quorum to be reached by one initiative of an issue to be "accepted" and enter issue state ''discussion''';
jbe@207 362 COMMENT ON COLUMN "policy"."issue_quorum_den" IS 'Denominator of potential supporter quorum to be reached by one initiative of an issue to be "accepted" and enter issue state ''discussion''';
jbe@10 363 COMMENT ON COLUMN "policy"."initiative_quorum_num" IS 'Numerator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
jbe@10 364 COMMENT ON COLUMN "policy"."initiative_quorum_den" IS 'Denominator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
jbe@167 365 COMMENT ON COLUMN "policy"."direct_majority_num" IS 'Numerator of fraction of neccessary direct majority for initiatives to be attainable as winner';
jbe@167 366 COMMENT ON COLUMN "policy"."direct_majority_den" IS 'Denominator of fraction of neccessary direct majority for initaitives to be attainable as winner';
jbe@167 367 COMMENT ON COLUMN "policy"."direct_majority_strict" IS 'If TRUE, then the direct majority must be strictly greater than "direct_majority_num"/"direct_majority_den", otherwise it may also be equal.';
jbe@167 368 COMMENT ON COLUMN "policy"."direct_majority_positive" IS 'Absolute number of "positive_votes" neccessary for an initiative to be attainable as winner';
jbe@167 369 COMMENT ON COLUMN "policy"."direct_majority_non_negative" IS 'Absolute number of sum of "positive_votes" and abstentions neccessary for an initiative to be attainable as winner';
jbe@167 370 COMMENT ON COLUMN "policy"."indirect_majority_num" IS 'Numerator of fraction of neccessary indirect majority (through beat path) for initiatives to be attainable as winner';
jbe@167 371 COMMENT ON COLUMN "policy"."indirect_majority_den" IS 'Denominator of fraction of neccessary indirect majority (through beat path) for initiatives to be attainable as winner';
jbe@167 372 COMMENT ON COLUMN "policy"."indirect_majority_strict" IS 'If TRUE, then the indirect majority must be strictly greater than "indirect_majority_num"/"indirect_majority_den", otherwise it may also be equal.';
jbe@167 373 COMMENT ON COLUMN "policy"."indirect_majority_positive" IS 'Absolute number of votes in favor of the winner neccessary in a beat path to the status quo for an initaitive to be attainable as winner';
jbe@167 374 COMMENT ON COLUMN "policy"."indirect_majority_non_negative" IS 'Absolute number of sum of votes in favor and abstentions in a beat path to the status quo for an initiative to be attainable as winner';
jbe@158 375 COMMENT ON COLUMN "policy"."no_reverse_beat_path" IS 'Causes initiatives with "reverse_beat_path" flag to not be "eligible", thus disallowing them to be winner. See comment on column "initiative"."reverse_beat_path". This option ensures both that a winning initiative is never tied in a (weak) condorcet paradox with the status quo and a winning initiative always beats the status quo directly with a simple majority.';
jbe@167 376 COMMENT ON COLUMN "policy"."no_multistage_majority" IS 'Causes initiatives with "multistage_majority" flag to not be "eligible", thus disallowing them to be winner. See comment on column "initiative"."multistage_majority". This disqualifies initiatives which could cause an instable result. An instable result in this meaning is a result such that repeating the ballot with same preferences but with the winner of the first ballot as status quo would lead to a different winner in the second ballot. If there are no direct majorities required for the winner, or if in direct comparison only simple majorities are required and "no_reverse_beat_path" is true, then results are always stable and this flag does not have any effect on the winner (but still affects the "eligible" flag of an "initiative").';
jbe@0 377
jbe@0 378
jbe@97 379 CREATE TABLE "unit" (
jbe@97 380 "id" SERIAL4 PRIMARY KEY,
jbe@97 381 "parent_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@97 382 "active" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@97 383 "name" TEXT NOT NULL,
jbe@97 384 "description" TEXT NOT NULL DEFAULT '',
jbe@97 385 "member_count" INT4,
jbe@97 386 "text_search_data" TSVECTOR );
jbe@97 387 CREATE INDEX "unit_root_idx" ON "unit" ("id") WHERE "parent_id" ISNULL;
jbe@97 388 CREATE INDEX "unit_parent_id_idx" ON "unit" ("parent_id");
jbe@97 389 CREATE INDEX "unit_active_idx" ON "unit" ("active");
jbe@97 390 CREATE INDEX "unit_text_search_data_idx" ON "unit" USING gin ("text_search_data");
jbe@97 391 CREATE TRIGGER "update_text_search_data"
jbe@97 392 BEFORE INSERT OR UPDATE ON "unit"
jbe@97 393 FOR EACH ROW EXECUTE PROCEDURE
jbe@97 394 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
jbe@97 395 "name", "description" );
jbe@97 396
jbe@97 397 COMMENT ON TABLE "unit" IS 'Organizational units organized as trees; Delegations are not inherited through these trees.';
jbe@97 398
jbe@97 399 COMMENT ON COLUMN "unit"."parent_id" IS 'Parent id of tree node; Multiple roots allowed';
jbe@212 400 COMMENT ON COLUMN "unit"."active" IS 'TRUE means new issues can be created in areas of this unit';
jbe@97 401 COMMENT ON COLUMN "unit"."member_count" IS 'Count of members as determined by column "voting_right" in table "privilege"';
jbe@97 402
jbe@97 403
jbe@203 404 CREATE TABLE "unit_setting" (
jbe@203 405 PRIMARY KEY ("member_id", "key", "unit_id"),
jbe@203 406 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@203 407 "key" TEXT NOT NULL,
jbe@203 408 "unit_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@203 409 "value" TEXT NOT NULL );
jbe@203 410
jbe@203 411 COMMENT ON TABLE "unit_setting" IS 'Place for frontend to store unit specific settings of members as strings';
jbe@203 412
jbe@203 413
jbe@0 414 CREATE TABLE "area" (
jbe@0 415 "id" SERIAL4 PRIMARY KEY,
jbe@97 416 "unit_id" INT4 NOT NULL REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 417 "active" BOOLEAN NOT NULL DEFAULT TRUE,
jbe@0 418 "name" TEXT NOT NULL,
jbe@4 419 "description" TEXT NOT NULL DEFAULT '',
jbe@5 420 "direct_member_count" INT4,
jbe@5 421 "member_weight" INT4,
jbe@7 422 "text_search_data" TSVECTOR );
jbe@97 423 CREATE INDEX "area_unit_id_idx" ON "area" ("unit_id");
jbe@0 424 CREATE INDEX "area_active_idx" ON "area" ("active");
jbe@8 425 CREATE INDEX "area_text_search_data_idx" ON "area" USING gin ("text_search_data");
jbe@7 426 CREATE TRIGGER "update_text_search_data"
jbe@7 427 BEFORE INSERT OR UPDATE ON "area"
jbe@7 428 FOR EACH ROW EXECUTE PROCEDURE
jbe@7 429 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
jbe@7 430 "name", "description" );
jbe@0 431
jbe@0 432 COMMENT ON TABLE "area" IS 'Subject areas';
jbe@0 433
jbe@5 434 COMMENT ON COLUMN "area"."active" IS 'TRUE means new issues can be created in this area';
jbe@5 435 COMMENT ON COLUMN "area"."direct_member_count" IS 'Number of active members of that area (ignoring their weight), as calculated from view "area_member_count"';
jbe@5 436 COMMENT ON COLUMN "area"."member_weight" IS 'Same as "direct_member_count" but respecting delegations';
jbe@0 437
jbe@0 438
jbe@23 439 CREATE TABLE "area_setting" (
jbe@23 440 PRIMARY KEY ("member_id", "key", "area_id"),
jbe@23 441 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 442 "key" TEXT NOT NULL,
jbe@23 443 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 444 "value" TEXT NOT NULL );
jbe@23 445
jbe@23 446 COMMENT ON TABLE "area_setting" IS 'Place for frontend to store area specific settings of members as strings';
jbe@23 447
jbe@23 448
jbe@9 449 CREATE TABLE "allowed_policy" (
jbe@9 450 PRIMARY KEY ("area_id", "policy_id"),
jbe@9 451 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@9 452 "policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@9 453 "default_policy" BOOLEAN NOT NULL DEFAULT FALSE );
jbe@9 454 CREATE UNIQUE INDEX "allowed_policy_one_default_per_area_idx" ON "allowed_policy" ("area_id") WHERE "default_policy";
jbe@9 455
jbe@9 456 COMMENT ON TABLE "allowed_policy" IS 'Selects which policies can be used in each area';
jbe@9 457
jbe@9 458 COMMENT ON COLUMN "allowed_policy"."default_policy" IS 'One policy per area can be set as default.';
jbe@9 459
jbe@9 460
jbe@21 461 CREATE TYPE "snapshot_event" AS ENUM ('periodic', 'end_of_admission', 'half_freeze', 'full_freeze');
jbe@21 462
jbe@21 463 COMMENT ON TYPE "snapshot_event" IS 'Reason for snapshots: ''periodic'' = due to periodic recalculation, ''end_of_admission'' = saved state at end of admission period, ''half_freeze'' = saved state at end of discussion period, ''full_freeze'' = saved state at end of verification period';
jbe@8 464
jbe@8 465
jbe@112 466 CREATE TYPE "issue_state" AS ENUM (
jbe@112 467 'admission', 'discussion', 'verification', 'voting',
jbe@113 468 'canceled_revoked_before_accepted',
jbe@113 469 'canceled_issue_not_accepted',
jbe@113 470 'canceled_after_revocation_during_discussion',
jbe@113 471 'canceled_after_revocation_during_verification',
jbe@113 472 'calculation',
jbe@113 473 'canceled_no_initiative_admitted',
jbe@112 474 'finished_without_winner', 'finished_with_winner');
jbe@111 475
jbe@111 476 COMMENT ON TYPE "issue_state" IS 'State of issues';
jbe@111 477
jbe@111 478
jbe@0 479 CREATE TABLE "issue" (
jbe@0 480 "id" SERIAL4 PRIMARY KEY,
jbe@0 481 "area_id" INT4 NOT NULL REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 482 "policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
jbe@111 483 "state" "issue_state" NOT NULL DEFAULT 'admission',
jbe@0 484 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@0 485 "accepted" TIMESTAMPTZ,
jbe@3 486 "half_frozen" TIMESTAMPTZ,
jbe@3 487 "fully_frozen" TIMESTAMPTZ,
jbe@0 488 "closed" TIMESTAMPTZ,
jbe@0 489 "ranks_available" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@59 490 "cleaned" TIMESTAMPTZ,
jbe@22 491 "admission_time" INTERVAL NOT NULL,
jbe@22 492 "discussion_time" INTERVAL NOT NULL,
jbe@22 493 "verification_time" INTERVAL NOT NULL,
jbe@22 494 "voting_time" INTERVAL NOT NULL,
jbe@0 495 "snapshot" TIMESTAMPTZ,
jbe@8 496 "latest_snapshot_event" "snapshot_event",
jbe@0 497 "population" INT4,
jbe@4 498 "voter_count" INT4,
jbe@170 499 "status_quo_schulze_rank" INT4,
jbe@111 500 CONSTRAINT "valid_state" CHECK ((
jbe@3 501 ("accepted" ISNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
jbe@3 502 ("accepted" ISNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
jbe@3 503 ("accepted" NOTNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
jbe@34 504 ("accepted" NOTNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
jbe@3 505 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
jbe@34 506 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
jbe@3 507 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
jbe@3 508 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
jbe@111 509 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" NOTNULL AND "ranks_available" = TRUE)) AND (
jbe@111 510 ("state" = 'admission' AND "closed" ISNULL AND "accepted" ISNULL) OR
jbe@111 511 ("state" = 'discussion' AND "closed" ISNULL AND "accepted" NOTNULL AND "half_frozen" ISNULL) OR
jbe@111 512 ("state" = 'verification' AND "closed" ISNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL) OR
jbe@111 513 ("state" = 'voting' AND "closed" ISNULL AND "fully_frozen" NOTNULL) OR
jbe@113 514 ("state" = 'canceled_revoked_before_accepted' AND "closed" NOTNULL AND "accepted" ISNULL) OR
jbe@113 515 ("state" = 'canceled_issue_not_accepted' AND "closed" NOTNULL AND "accepted" ISNULL) OR
jbe@113 516 ("state" = 'canceled_after_revocation_during_discussion' AND "closed" NOTNULL AND "half_frozen" ISNULL) OR
jbe@113 517 ("state" = 'canceled_after_revocation_during_verification' AND "closed" NOTNULL AND "fully_frozen" ISNULL) OR
jbe@113 518 ("state" = 'calculation' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "ranks_available" = FALSE) OR
jbe@113 519 ("state" = 'canceled_no_initiative_admitted' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "ranks_available" = TRUE) OR
jbe@113 520 ("state" = 'finished_without_winner' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "ranks_available" = TRUE) OR
jbe@113 521 ("state" = 'finished_with_winner' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "ranks_available" = TRUE)
jbe@111 522 )),
jbe@3 523 CONSTRAINT "state_change_order" CHECK (
jbe@10 524 "created" <= "accepted" AND
jbe@10 525 "accepted" <= "half_frozen" AND
jbe@10 526 "half_frozen" <= "fully_frozen" AND
jbe@3 527 "fully_frozen" <= "closed" ),
jbe@61 528 CONSTRAINT "only_closed_issues_may_be_cleaned" CHECK (
jbe@61 529 "cleaned" ISNULL OR "closed" NOTNULL ),
jbe@10 530 CONSTRAINT "last_snapshot_on_full_freeze"
jbe@10 531 CHECK ("snapshot" = "fully_frozen"), -- NOTE: snapshot can be set, while frozen is NULL yet
jbe@10 532 CONSTRAINT "freeze_requires_snapshot"
jbe@10 533 CHECK ("fully_frozen" ISNULL OR "snapshot" NOTNULL),
jbe@10 534 CONSTRAINT "set_both_or_none_of_snapshot_and_latest_snapshot_event"
jbe@10 535 CHECK ("snapshot" NOTNULL = "latest_snapshot_event" NOTNULL) );
jbe@0 536 CREATE INDEX "issue_area_id_idx" ON "issue" ("area_id");
jbe@0 537 CREATE INDEX "issue_policy_id_idx" ON "issue" ("policy_id");
jbe@16 538 CREATE INDEX "issue_created_idx" ON "issue" ("created");
jbe@16 539 CREATE INDEX "issue_accepted_idx" ON "issue" ("accepted");
jbe@16 540 CREATE INDEX "issue_half_frozen_idx" ON "issue" ("half_frozen");
jbe@16 541 CREATE INDEX "issue_fully_frozen_idx" ON "issue" ("fully_frozen");
jbe@16 542 CREATE INDEX "issue_closed_idx" ON "issue" ("closed");
jbe@0 543 CREATE INDEX "issue_created_idx_open" ON "issue" ("created") WHERE "closed" ISNULL;
jbe@16 544 CREATE INDEX "issue_closed_idx_canceled" ON "issue" ("closed") WHERE "fully_frozen" ISNULL;
jbe@0 545
jbe@0 546 COMMENT ON TABLE "issue" IS 'Groups of initiatives';
jbe@0 547
jbe@170 548 COMMENT ON COLUMN "issue"."accepted" IS 'Point in time, when one initiative of issue reached the "issue_quorum"';
jbe@170 549 COMMENT ON COLUMN "issue"."half_frozen" IS 'Point in time, when "discussion_time" has elapsed; Frontends must ensure that for half_frozen issues a) initiatives are not revoked, b) no new drafts are created, c) no initiators are added or removed.';
jbe@170 550 COMMENT ON COLUMN "issue"."fully_frozen" IS 'Point in time, when "verification_time" has elapsed and voting has started; Frontends must ensure that for fully_frozen issues additionally to the restrictions for half_frozen issues a) initiatives are not created, b) no interest is created or removed, c) no supporters are added or removed, d) no opinions are created, changed or deleted.';
jbe@170 551 COMMENT ON COLUMN "issue"."closed" IS 'Point in time, when "admission_time" or "voting_time" have elapsed, and issue is no longer active; Frontends must ensure that for closed issues additionally to the restrictions for half_frozen and fully_frozen issues a) no voter is added or removed to/from the direct_voter table, b) no votes are added, modified or removed.';
jbe@170 552 COMMENT ON COLUMN "issue"."ranks_available" IS 'TRUE = ranks have been calculated';
jbe@170 553 COMMENT ON COLUMN "issue"."cleaned" IS 'Point in time, when discussion data and votes had been deleted';
jbe@170 554 COMMENT ON COLUMN "issue"."admission_time" IS 'Copied from "policy" table at creation of issue';
jbe@170 555 COMMENT ON COLUMN "issue"."discussion_time" IS 'Copied from "policy" table at creation of issue';
jbe@170 556 COMMENT ON COLUMN "issue"."verification_time" IS 'Copied from "policy" table at creation of issue';
jbe@170 557 COMMENT ON COLUMN "issue"."voting_time" IS 'Copied from "policy" table at creation of issue';
jbe@170 558 COMMENT ON COLUMN "issue"."snapshot" IS 'Point in time, when snapshot tables have been updated and "population" and *_count values were precalculated';
jbe@170 559 COMMENT ON COLUMN "issue"."latest_snapshot_event" IS 'Event type of latest snapshot for issue; Can be used to select the latest snapshot data in the snapshot tables';
jbe@170 560 COMMENT ON COLUMN "issue"."population" IS 'Sum of "weight" column in table "direct_population_snapshot"';
jbe@170 561 COMMENT ON COLUMN "issue"."voter_count" IS 'Total number of direct and delegating voters; This value is related to the final voting, while "population" is related to snapshots before the final voting';
jbe@170 562 COMMENT ON COLUMN "issue"."status_quo_schulze_rank" IS 'Schulze rank of status quo, as calculated by "calculate_ranks" function';
jbe@0 563
jbe@0 564
jbe@23 565 CREATE TABLE "issue_setting" (
jbe@23 566 PRIMARY KEY ("member_id", "key", "issue_id"),
jbe@23 567 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 568 "key" TEXT NOT NULL,
jbe@23 569 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 570 "value" TEXT NOT NULL );
jbe@23 571
jbe@23 572 COMMENT ON TABLE "issue_setting" IS 'Place for frontend to store issue specific settings of members as strings';
jbe@23 573
jbe@23 574
jbe@0 575 CREATE TABLE "initiative" (
jbe@0 576 UNIQUE ("issue_id", "id"), -- index needed for foreign-key on table "vote"
jbe@0 577 "issue_id" INT4 NOT NULL REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 578 "id" SERIAL4 PRIMARY KEY,
jbe@0 579 "name" TEXT NOT NULL,
jbe@8 580 "discussion_url" TEXT,
jbe@0 581 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@0 582 "revoked" TIMESTAMPTZ,
jbe@112 583 "revoked_by_member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
jbe@14 584 "suggested_initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 585 "admitted" BOOLEAN,
jbe@0 586 "supporter_count" INT4,
jbe@0 587 "informed_supporter_count" INT4,
jbe@0 588 "satisfied_supporter_count" INT4,
jbe@0 589 "satisfied_informed_supporter_count" INT4,
jbe@0 590 "positive_votes" INT4,
jbe@0 591 "negative_votes" INT4,
jbe@167 592 "direct_majority" BOOLEAN,
jbe@167 593 "indirect_majority" BOOLEAN,
jbe@170 594 "schulze_rank" INT4,
jbe@167 595 "better_than_status_quo" BOOLEAN,
jbe@167 596 "worse_than_status_quo" BOOLEAN,
jbe@158 597 "reverse_beat_path" BOOLEAN,
jbe@154 598 "multistage_majority" BOOLEAN,
jbe@154 599 "eligible" BOOLEAN,
jbe@126 600 "winner" BOOLEAN,
jbe@0 601 "rank" INT4,
jbe@7 602 "text_search_data" TSVECTOR,
jbe@112 603 CONSTRAINT "all_or_none_of_revoked_and_revoked_by_member_id_must_be_null"
jbe@112 604 CHECK ("revoked" NOTNULL = "revoked_by_member_id" NOTNULL),
jbe@14 605 CONSTRAINT "non_revoked_initiatives_cant_suggest_other"
jbe@14 606 CHECK ("revoked" NOTNULL OR "suggested_initiative_id" ISNULL),
jbe@0 607 CONSTRAINT "revoked_initiatives_cant_be_admitted"
jbe@0 608 CHECK ("revoked" ISNULL OR "admitted" ISNULL),
jbe@128 609 CONSTRAINT "non_admitted_initiatives_cant_contain_voting_results" CHECK (
jbe@128 610 ( "admitted" NOTNULL AND "admitted" = TRUE ) OR
jbe@167 611 ( "positive_votes" ISNULL AND "negative_votes" ISNULL AND
jbe@167 612 "direct_majority" ISNULL AND "indirect_majority" ISNULL AND
jbe@173 613 "schulze_rank" ISNULL AND
jbe@167 614 "better_than_status_quo" ISNULL AND "worse_than_status_quo" ISNULL AND
jbe@167 615 "reverse_beat_path" ISNULL AND "multistage_majority" ISNULL AND
jbe@173 616 "eligible" ISNULL AND "winner" ISNULL AND "rank" ISNULL ) ),
jbe@173 617 CONSTRAINT "better_excludes_worse" CHECK (NOT ("better_than_status_quo" AND "worse_than_status_quo")),
jbe@175 618 CONSTRAINT "minimum_requirement_to_be_eligible" CHECK (
jbe@175 619 "eligible" = FALSE OR
jbe@175 620 ("direct_majority" AND "indirect_majority" AND "better_than_status_quo") ),
jbe@175 621 CONSTRAINT "winner_must_be_eligible" CHECK ("winner"=FALSE OR "eligible"=TRUE),
jbe@175 622 CONSTRAINT "winner_must_have_first_rank" CHECK ("winner"=FALSE OR "rank"=1),
jbe@176 623 CONSTRAINT "eligible_at_first_rank_is_winner" CHECK ("eligible"=FALSE OR "rank"!=1 OR "winner"=TRUE),
jbe@173 624 CONSTRAINT "unique_rank_per_issue" UNIQUE ("issue_id", "rank") );
jbe@16 625 CREATE INDEX "initiative_created_idx" ON "initiative" ("created");
jbe@16 626 CREATE INDEX "initiative_revoked_idx" ON "initiative" ("revoked");
jbe@8 627 CREATE INDEX "initiative_text_search_data_idx" ON "initiative" USING gin ("text_search_data");
jbe@7 628 CREATE TRIGGER "update_text_search_data"
jbe@7 629 BEFORE INSERT OR UPDATE ON "initiative"
jbe@7 630 FOR EACH ROW EXECUTE PROCEDURE
jbe@8 631 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
jbe@8 632 "name", "discussion_url");
jbe@0 633
jbe@10 634 COMMENT ON TABLE "initiative" IS 'Group of members publishing drafts for resolutions to be passed; Frontends must ensure that initiatives of half_frozen issues are not revoked, and that initiatives of fully_frozen or closed issues are neither revoked nor created.';
jbe@0 635
jbe@210 636 COMMENT ON COLUMN "initiative"."discussion_url" IS 'URL pointing to a discussion platform for this initiative';
jbe@210 637 COMMENT ON COLUMN "initiative"."revoked" IS 'Point in time, when one initiator decided to revoke the initiative';
jbe@210 638 COMMENT ON COLUMN "initiative"."revoked_by_member_id" IS 'Member, who decided to revoke the initiative';
jbe@210 639 COMMENT ON COLUMN "initiative"."admitted" IS 'TRUE, if initiative reaches the "initiative_quorum" when freezing the issue';
jbe@0 640 COMMENT ON COLUMN "initiative"."supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
jbe@0 641 COMMENT ON COLUMN "initiative"."informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
jbe@0 642 COMMENT ON COLUMN "initiative"."satisfied_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
jbe@0 643 COMMENT ON COLUMN "initiative"."satisfied_informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
jbe@210 644 COMMENT ON COLUMN "initiative"."positive_votes" IS 'Calculated from table "direct_voter"';
jbe@210 645 COMMENT ON COLUMN "initiative"."negative_votes" IS 'Calculated from table "direct_voter"';
jbe@210 646 COMMENT ON COLUMN "initiative"."direct_majority" IS 'TRUE, if "positive_votes"/("positive_votes"+"negative_votes") is strictly greater or greater-equal than "direct_majority_num"/"direct_majority_den", and "positive_votes" is greater-equal than "direct_majority_positive", and ("positive_votes"+abstentions) is greater-equal than "direct_majority_non_negative"';
jbe@210 647 COMMENT ON COLUMN "initiative"."indirect_majority" IS 'Same as "direct_majority", but also considering indirect beat paths';
jbe@210 648 COMMENT ON COLUMN "initiative"."schulze_rank" IS 'Schulze-Ranking without tie-breaking';
jbe@210 649 COMMENT ON COLUMN "initiative"."better_than_status_quo" IS 'TRUE, if initiative has a schulze-ranking better than the status quo (without tie-breaking)';
jbe@210 650 COMMENT ON COLUMN "initiative"."worse_than_status_quo" IS 'TRUE, if initiative has a schulze-ranking worse than the status quo (without tie-breaking)';
jbe@210 651 COMMENT ON COLUMN "initiative"."reverse_beat_path" IS 'TRUE, if there is a beat path (may include ties) from this initiative to the status quo';
jbe@210 652 COMMENT ON COLUMN "initiative"."multistage_majority" IS 'TRUE, if either (a) this initiative has no better rank than the status quo, or (b) there exists a better ranked initiative X, which directly beats this initiative, and either more voters prefer X to this initiative than voters preferring X to the status quo or less voters prefer this initiative to X than voters preferring the status quo to X';
jbe@210 653 COMMENT ON COLUMN "initiative"."eligible" IS 'Initiative has a "direct_majority" and an "indirect_majority", is "better_than_status_quo" and depending on selected policy the initiative has no "reverse_beat_path" or "multistage_majority"';
jbe@210 654 COMMENT ON COLUMN "initiative"."winner" IS 'Winner is the "eligible" initiative with best "schulze_rank" and in case of ties with lowest "id"';
jbe@210 655 COMMENT ON COLUMN "initiative"."rank" IS 'Unique ranking for all "admitted" initiatives per issue; lower rank is better; a winner always has rank 1, but rank 1 does not imply that an initiative is winner; initiatives with "direct_majority" AND "indirect_majority" always have a better (lower) rank than other initiatives';
jbe@0 656
jbe@0 657
jbe@61 658 CREATE TABLE "battle" (
jbe@126 659 "issue_id" INT4 NOT NULL,
jbe@61 660 "winning_initiative_id" INT4,
jbe@61 661 FOREIGN KEY ("issue_id", "winning_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@61 662 "losing_initiative_id" INT4,
jbe@61 663 FOREIGN KEY ("issue_id", "losing_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@126 664 "count" INT4 NOT NULL,
jbe@126 665 CONSTRAINT "initiative_ids_not_equal" CHECK (
jbe@126 666 "winning_initiative_id" != "losing_initiative_id" OR
jbe@126 667 ( ("winning_initiative_id" NOTNULL AND "losing_initiative_id" ISNULL) OR
jbe@126 668 ("winning_initiative_id" ISNULL AND "losing_initiative_id" NOTNULL) ) ) );
jbe@126 669 CREATE UNIQUE INDEX "battle_winning_losing_idx" ON "battle" ("issue_id", "winning_initiative_id", "losing_initiative_id");
jbe@126 670 CREATE UNIQUE INDEX "battle_winning_null_idx" ON "battle" ("issue_id", "winning_initiative_id") WHERE "losing_initiative_id" ISNULL;
jbe@126 671 CREATE UNIQUE INDEX "battle_null_losing_idx" ON "battle" ("issue_id", "losing_initiative_id") WHERE "winning_initiative_id" ISNULL;
jbe@126 672
jbe@126 673 COMMENT ON TABLE "battle" IS 'Number of members preferring one initiative to another; Filled by "battle_view" when closing an issue; NULL as initiative_id denotes virtual "status-quo" initiative';
jbe@61 674
jbe@61 675
jbe@113 676 CREATE TABLE "ignored_initiative" (
jbe@113 677 PRIMARY KEY ("initiative_id", "member_id"),
jbe@113 678 "initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@113 679 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@113 680 CREATE INDEX "ignored_initiative_member_id_idx" ON "ignored_initiative" ("member_id");
jbe@113 681
jbe@113 682 COMMENT ON TABLE "ignored_initiative" IS 'Possibility to filter initiatives';
jbe@113 683
jbe@113 684
jbe@23 685 CREATE TABLE "initiative_setting" (
jbe@23 686 PRIMARY KEY ("member_id", "key", "initiative_id"),
jbe@23 687 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 688 "key" TEXT NOT NULL,
jbe@23 689 "initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 690 "value" TEXT NOT NULL );
jbe@23 691
jbe@23 692 COMMENT ON TABLE "initiative_setting" IS 'Place for frontend to store initiative specific settings of members as strings';
jbe@23 693
jbe@23 694
jbe@0 695 CREATE TABLE "draft" (
jbe@0 696 UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "supporter"
jbe@0 697 "initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 698 "id" SERIAL8 PRIMARY KEY,
jbe@0 699 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@0 700 "author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
jbe@9 701 "formatting_engine" TEXT,
jbe@7 702 "content" TEXT NOT NULL,
jbe@7 703 "text_search_data" TSVECTOR );
jbe@16 704 CREATE INDEX "draft_created_idx" ON "draft" ("created");
jbe@9 705 CREATE INDEX "draft_author_id_created_idx" ON "draft" ("author_id", "created");
jbe@8 706 CREATE INDEX "draft_text_search_data_idx" ON "draft" USING gin ("text_search_data");
jbe@7 707 CREATE TRIGGER "update_text_search_data"
jbe@7 708 BEFORE INSERT OR UPDATE ON "draft"
jbe@7 709 FOR EACH ROW EXECUTE PROCEDURE
jbe@7 710 tsvector_update_trigger('text_search_data', 'pg_catalog.simple', "content");
jbe@0 711
jbe@10 712 COMMENT ON TABLE "draft" IS 'Drafts of initiatives to solve issues; Frontends must ensure that new drafts for initiatives of half_frozen, fully_frozen or closed issues can''t be created.';
jbe@0 713
jbe@9 714 COMMENT ON COLUMN "draft"."formatting_engine" IS 'Allows different formatting engines (i.e. wiki formats) to be used';
jbe@9 715 COMMENT ON COLUMN "draft"."content" IS 'Text of the draft in a format depending on the field "formatting_engine"';
jbe@9 716
jbe@0 717
jbe@63 718 CREATE TABLE "rendered_draft" (
jbe@63 719 PRIMARY KEY ("draft_id", "format"),
jbe@63 720 "draft_id" INT8 REFERENCES "draft" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@63 721 "format" TEXT,
jbe@63 722 "content" TEXT NOT NULL );
jbe@63 723
jbe@63 724 COMMENT ON TABLE "rendered_draft" IS 'This table may be used by frontends to cache "rendered" drafts (e.g. HTML output generated from wiki text)';
jbe@63 725
jbe@63 726
jbe@0 727 CREATE TABLE "suggestion" (
jbe@0 728 UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "opinion"
jbe@0 729 "initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 730 "id" SERIAL8 PRIMARY KEY,
jbe@160 731 "draft_id" INT8 NOT NULL,
jbe@160 732 FOREIGN KEY ("initiative_id", "draft_id") REFERENCES "draft" ("initiative_id", "id") ON DELETE NO ACTION ON UPDATE CASCADE,
jbe@0 733 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@0 734 "author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
jbe@0 735 "name" TEXT NOT NULL,
jbe@159 736 "formatting_engine" TEXT,
jbe@159 737 "content" TEXT NOT NULL DEFAULT '',
jbe@7 738 "text_search_data" TSVECTOR,
jbe@0 739 "minus2_unfulfilled_count" INT4,
jbe@0 740 "minus2_fulfilled_count" INT4,
jbe@0 741 "minus1_unfulfilled_count" INT4,
jbe@0 742 "minus1_fulfilled_count" INT4,
jbe@0 743 "plus1_unfulfilled_count" INT4,
jbe@0 744 "plus1_fulfilled_count" INT4,
jbe@0 745 "plus2_unfulfilled_count" INT4,
jbe@0 746 "plus2_fulfilled_count" INT4 );
jbe@16 747 CREATE INDEX "suggestion_created_idx" ON "suggestion" ("created");
jbe@9 748 CREATE INDEX "suggestion_author_id_created_idx" ON "suggestion" ("author_id", "created");
jbe@8 749 CREATE INDEX "suggestion_text_search_data_idx" ON "suggestion" USING gin ("text_search_data");
jbe@7 750 CREATE TRIGGER "update_text_search_data"
jbe@7 751 BEFORE INSERT OR UPDATE ON "suggestion"
jbe@7 752 FOR EACH ROW EXECUTE PROCEDURE
jbe@7 753 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
jbe@159 754 "name", "content");
jbe@0 755
jbe@10 756 COMMENT ON TABLE "suggestion" IS 'Suggestions to initiators, to change the current draft; must not be deleted explicitly, as they vanish automatically if the last opinion is deleted';
jbe@0 757
jbe@160 758 COMMENT ON COLUMN "suggestion"."draft_id" IS 'Draft, which the author has seen when composing the suggestion; should always be set by a frontend, but defaults to current draft of the initiative (implemented by trigger "default_for_draft_id")';
jbe@0 759 COMMENT ON COLUMN "suggestion"."minus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 760 COMMENT ON COLUMN "suggestion"."minus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 761 COMMENT ON COLUMN "suggestion"."minus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 762 COMMENT ON COLUMN "suggestion"."minus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 763 COMMENT ON COLUMN "suggestion"."plus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 764 COMMENT ON COLUMN "suggestion"."plus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 765 COMMENT ON COLUMN "suggestion"."plus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 766 COMMENT ON COLUMN "suggestion"."plus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
jbe@0 767
jbe@0 768
jbe@159 769 CREATE TABLE "rendered_suggestion" (
jbe@159 770 PRIMARY KEY ("suggestion_id", "format"),
jbe@159 771 "suggestion_id" INT8 REFERENCES "suggestion" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@159 772 "format" TEXT,
jbe@159 773 "content" TEXT NOT NULL );
jbe@159 774
jbe@159 775 COMMENT ON TABLE "rendered_suggestion" IS 'This table may be used by frontends to cache "rendered" drafts (e.g. HTML output generated from wiki text)';
jbe@159 776
jbe@159 777
jbe@23 778 CREATE TABLE "suggestion_setting" (
jbe@23 779 PRIMARY KEY ("member_id", "key", "suggestion_id"),
jbe@23 780 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 781 "key" TEXT NOT NULL,
jbe@23 782 "suggestion_id" INT8 REFERENCES "suggestion" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@23 783 "value" TEXT NOT NULL );
jbe@23 784
jbe@23 785 COMMENT ON TABLE "suggestion_setting" IS 'Place for frontend to store suggestion specific settings of members as strings';
jbe@23 786
jbe@23 787
jbe@97 788 CREATE TABLE "privilege" (
jbe@97 789 PRIMARY KEY ("unit_id", "member_id"),
jbe@97 790 "unit_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@97 791 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@97 792 "admin_manager" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@97 793 "unit_manager" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@97 794 "area_manager" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@97 795 "voting_right_manager" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@97 796 "voting_right" BOOLEAN NOT NULL DEFAULT TRUE );
jbe@97 797
jbe@97 798 COMMENT ON TABLE "privilege" IS 'Members rights related to each unit';
jbe@97 799
jbe@207 800 COMMENT ON COLUMN "privilege"."admin_manager" IS 'Grant/revoke admin privileges to/from other members';
jbe@207 801 COMMENT ON COLUMN "privilege"."unit_manager" IS 'Create and disable sub units';
jbe@207 802 COMMENT ON COLUMN "privilege"."area_manager" IS 'Create and disable areas and set area parameters';
jbe@207 803 COMMENT ON COLUMN "privilege"."voting_right_manager" IS 'Select which members are allowed to discuss and vote within the unit';
jbe@97 804 COMMENT ON COLUMN "privilege"."voting_right" IS 'Right to discuss and vote';
jbe@97 805
jbe@97 806
jbe@0 807 CREATE TABLE "membership" (
jbe@0 808 PRIMARY KEY ("area_id", "member_id"),
jbe@0 809 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@169 810 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@0 811 CREATE INDEX "membership_member_id_idx" ON "membership" ("member_id");
jbe@0 812
jbe@0 813 COMMENT ON TABLE "membership" IS 'Interest of members in topic areas';
jbe@0 814
jbe@0 815
jbe@0 816 CREATE TABLE "interest" (
jbe@0 817 PRIMARY KEY ("issue_id", "member_id"),
jbe@0 818 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@148 819 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@0 820 CREATE INDEX "interest_member_id_idx" ON "interest" ("member_id");
jbe@0 821
jbe@10 822 COMMENT ON TABLE "interest" IS 'Interest of members in a particular issue; Frontends must ensure that interest for fully_frozen or closed issues is not added or removed.';
jbe@0 823
jbe@0 824
jbe@0 825 CREATE TABLE "initiator" (
jbe@0 826 PRIMARY KEY ("initiative_id", "member_id"),
jbe@0 827 "initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 828 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@14 829 "accepted" BOOLEAN );
jbe@0 830 CREATE INDEX "initiator_member_id_idx" ON "initiator" ("member_id");
jbe@0 831
jbe@10 832 COMMENT ON TABLE "initiator" IS 'Members who are allowed to post new drafts; Frontends must ensure that initiators are not added or removed from half_frozen, fully_frozen or closed initiatives.';
jbe@0 833
jbe@14 834 COMMENT ON COLUMN "initiator"."accepted" IS 'If "accepted" is NULL, then the member was invited to be a co-initiator, but has not answered yet. If it is TRUE, the member has accepted the invitation, if it is FALSE, the member has rejected the invitation.';
jbe@0 835
jbe@0 836
jbe@0 837 CREATE TABLE "supporter" (
jbe@0 838 "issue_id" INT4 NOT NULL,
jbe@0 839 PRIMARY KEY ("initiative_id", "member_id"),
jbe@0 840 "initiative_id" INT4,
jbe@0 841 "member_id" INT4,
jbe@0 842 "draft_id" INT8 NOT NULL,
jbe@10 843 FOREIGN KEY ("issue_id", "member_id") REFERENCES "interest" ("issue_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@160 844 FOREIGN KEY ("initiative_id", "draft_id") REFERENCES "draft" ("initiative_id", "id") ON DELETE NO ACTION ON UPDATE CASCADE );
jbe@0 845 CREATE INDEX "supporter_member_id_idx" ON "supporter" ("member_id");
jbe@0 846
jbe@10 847 COMMENT ON TABLE "supporter" IS 'Members who support an initiative (conditionally); Frontends must ensure that supporters are not added or removed from fully_frozen or closed initiatives.';
jbe@0 848
jbe@207 849 COMMENT ON COLUMN "supporter"."issue_id" IS 'WARNING: No index: For selections use column "initiative_id" and join via table "initiative" where neccessary';
jbe@160 850 COMMENT ON COLUMN "supporter"."draft_id" IS 'Latest seen draft; should always be set by a frontend, but defaults to current draft of the initiative (implemented by trigger "default_for_draft_id")';
jbe@84 851
jbe@0 852
jbe@0 853 CREATE TABLE "opinion" (
jbe@0 854 "initiative_id" INT4 NOT NULL,
jbe@0 855 PRIMARY KEY ("suggestion_id", "member_id"),
jbe@0 856 "suggestion_id" INT8,
jbe@0 857 "member_id" INT4,
jbe@0 858 "degree" INT2 NOT NULL CHECK ("degree" >= -2 AND "degree" <= 2 AND "degree" != 0),
jbe@0 859 "fulfilled" BOOLEAN NOT NULL DEFAULT FALSE,
jbe@42 860 FOREIGN KEY ("initiative_id", "suggestion_id") REFERENCES "suggestion" ("initiative_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 861 FOREIGN KEY ("initiative_id", "member_id") REFERENCES "supporter" ("initiative_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@10 862 CREATE INDEX "opinion_member_id_initiative_id_idx" ON "opinion" ("member_id", "initiative_id");
jbe@0 863
jbe@10 864 COMMENT ON TABLE "opinion" IS 'Opinion on suggestions (criticism related to initiatives); Frontends must ensure that opinions are not created modified or deleted when related to fully_frozen or closed issues.';
jbe@0 865
jbe@0 866 COMMENT ON COLUMN "opinion"."degree" IS '2 = fulfillment required for support; 1 = fulfillment desired; -1 = fulfillment unwanted; -2 = fulfillment cancels support';
jbe@0 867
jbe@0 868
jbe@97 869 CREATE TYPE "delegation_scope" AS ENUM ('unit', 'area', 'issue');
jbe@97 870
jbe@97 871 COMMENT ON TYPE "delegation_scope" IS 'Scope for delegations: ''unit'', ''area'', or ''issue'' (order is relevant)';
jbe@10 872
jbe@10 873
jbe@0 874 CREATE TABLE "delegation" (
jbe@0 875 "id" SERIAL8 PRIMARY KEY,
jbe@0 876 "truster_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@86 877 "trustee_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@10 878 "scope" "delegation_scope" NOT NULL,
jbe@97 879 "unit_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 880 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 881 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 882 CONSTRAINT "cant_delegate_to_yourself" CHECK ("truster_id" != "trustee_id"),
jbe@97 883 CONSTRAINT "no_unit_delegation_to_null"
jbe@97 884 CHECK ("trustee_id" NOTNULL OR "scope" != 'unit'),
jbe@10 885 CONSTRAINT "area_id_and_issue_id_set_according_to_scope" CHECK (
jbe@97 886 ("scope" = 'unit' AND "unit_id" NOTNULL AND "area_id" ISNULL AND "issue_id" ISNULL ) OR
jbe@97 887 ("scope" = 'area' AND "unit_id" ISNULL AND "area_id" NOTNULL AND "issue_id" ISNULL ) OR
jbe@97 888 ("scope" = 'issue' AND "unit_id" ISNULL AND "area_id" ISNULL AND "issue_id" NOTNULL) ),
jbe@97 889 UNIQUE ("unit_id", "truster_id"),
jbe@74 890 UNIQUE ("area_id", "truster_id"),
jbe@74 891 UNIQUE ("issue_id", "truster_id") );
jbe@0 892 CREATE INDEX "delegation_truster_id_idx" ON "delegation" ("truster_id");
jbe@0 893 CREATE INDEX "delegation_trustee_id_idx" ON "delegation" ("trustee_id");
jbe@0 894
jbe@0 895 COMMENT ON TABLE "delegation" IS 'Delegation of vote-weight to other members';
jbe@0 896
jbe@97 897 COMMENT ON COLUMN "delegation"."unit_id" IS 'Reference to unit, if delegation is unit-wide, otherwise NULL';
jbe@0 898 COMMENT ON COLUMN "delegation"."area_id" IS 'Reference to area, if delegation is area-wide, otherwise NULL';
jbe@0 899 COMMENT ON COLUMN "delegation"."issue_id" IS 'Reference to issue, if delegation is issue-wide, otherwise NULL';
jbe@0 900
jbe@0 901
jbe@0 902 CREATE TABLE "direct_population_snapshot" (
jbe@0 903 PRIMARY KEY ("issue_id", "event", "member_id"),
jbe@0 904 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 905 "event" "snapshot_event",
jbe@45 906 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@54 907 "weight" INT4 );
jbe@0 908 CREATE INDEX "direct_population_snapshot_member_id_idx" ON "direct_population_snapshot" ("member_id");
jbe@0 909
jbe@0 910 COMMENT ON TABLE "direct_population_snapshot" IS 'Snapshot of active members having either a "membership" in the "area" or an "interest" in the "issue"';
jbe@0 911
jbe@148 912 COMMENT ON COLUMN "direct_population_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
jbe@148 913 COMMENT ON COLUMN "direct_population_snapshot"."weight" IS 'Weight of member (1 or higher) according to "delegating_population_snapshot"';
jbe@0 914
jbe@0 915
jbe@0 916 CREATE TABLE "delegating_population_snapshot" (
jbe@0 917 PRIMARY KEY ("issue_id", "event", "member_id"),
jbe@0 918 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 919 "event" "snapshot_event",
jbe@45 920 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@8 921 "weight" INT4,
jbe@10 922 "scope" "delegation_scope" NOT NULL,
jbe@0 923 "delegate_member_ids" INT4[] NOT NULL );
jbe@0 924 CREATE INDEX "delegating_population_snapshot_member_id_idx" ON "delegating_population_snapshot" ("member_id");
jbe@0 925
jbe@0 926 COMMENT ON TABLE "direct_population_snapshot" IS 'Delegations increasing the weight of entries in the "direct_population_snapshot" table';
jbe@0 927
jbe@0 928 COMMENT ON COLUMN "delegating_population_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
jbe@0 929 COMMENT ON COLUMN "delegating_population_snapshot"."member_id" IS 'Delegating member';
jbe@8 930 COMMENT ON COLUMN "delegating_population_snapshot"."weight" IS 'Intermediate weight';
jbe@0 931 COMMENT ON COLUMN "delegating_population_snapshot"."delegate_member_ids" IS 'Chain of members who act as delegates; last entry referes to "member_id" column of table "direct_population_snapshot"';
jbe@0 932
jbe@0 933
jbe@0 934 CREATE TABLE "direct_interest_snapshot" (
jbe@0 935 PRIMARY KEY ("issue_id", "event", "member_id"),
jbe@0 936 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 937 "event" "snapshot_event",
jbe@45 938 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@144 939 "weight" INT4 );
jbe@0 940 CREATE INDEX "direct_interest_snapshot_member_id_idx" ON "direct_interest_snapshot" ("member_id");
jbe@0 941
jbe@0 942 COMMENT ON TABLE "direct_interest_snapshot" IS 'Snapshot of active members having an "interest" in the "issue"';
jbe@0 943
jbe@0 944 COMMENT ON COLUMN "direct_interest_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
jbe@0 945 COMMENT ON COLUMN "direct_interest_snapshot"."weight" IS 'Weight of member (1 or higher) according to "delegating_interest_snapshot"';
jbe@0 946
jbe@0 947
jbe@0 948 CREATE TABLE "delegating_interest_snapshot" (
jbe@0 949 PRIMARY KEY ("issue_id", "event", "member_id"),
jbe@0 950 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 951 "event" "snapshot_event",
jbe@45 952 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@8 953 "weight" INT4,
jbe@10 954 "scope" "delegation_scope" NOT NULL,
jbe@0 955 "delegate_member_ids" INT4[] NOT NULL );
jbe@0 956 CREATE INDEX "delegating_interest_snapshot_member_id_idx" ON "delegating_interest_snapshot" ("member_id");
jbe@0 957
jbe@0 958 COMMENT ON TABLE "delegating_interest_snapshot" IS 'Delegations increasing the weight of entries in the "direct_interest_snapshot" table';
jbe@0 959
jbe@0 960 COMMENT ON COLUMN "delegating_interest_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
jbe@0 961 COMMENT ON COLUMN "delegating_interest_snapshot"."member_id" IS 'Delegating member';
jbe@8 962 COMMENT ON COLUMN "delegating_interest_snapshot"."weight" IS 'Intermediate weight';
jbe@0 963 COMMENT ON COLUMN "delegating_interest_snapshot"."delegate_member_ids" IS 'Chain of members who act as delegates; last entry referes to "member_id" column of table "direct_interest_snapshot"';
jbe@0 964
jbe@0 965
jbe@0 966 CREATE TABLE "direct_supporter_snapshot" (
jbe@0 967 "issue_id" INT4 NOT NULL,
jbe@0 968 PRIMARY KEY ("initiative_id", "event", "member_id"),
jbe@0 969 "initiative_id" INT4,
jbe@0 970 "event" "snapshot_event",
jbe@45 971 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@204 972 "draft_id" INT8 NOT NULL,
jbe@0 973 "informed" BOOLEAN NOT NULL,
jbe@0 974 "satisfied" BOOLEAN NOT NULL,
jbe@0 975 FOREIGN KEY ("issue_id", "initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@204 976 FOREIGN KEY ("initiative_id", "draft_id") REFERENCES "draft" ("initiative_id", "id") ON DELETE NO ACTION ON UPDATE CASCADE,
jbe@0 977 FOREIGN KEY ("issue_id", "event", "member_id") REFERENCES "direct_interest_snapshot" ("issue_id", "event", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@0 978 CREATE INDEX "direct_supporter_snapshot_member_id_idx" ON "direct_supporter_snapshot" ("member_id");
jbe@0 979
jbe@8 980 COMMENT ON TABLE "direct_supporter_snapshot" IS 'Snapshot of supporters of initiatives (weight is stored in "direct_interest_snapshot")';
jbe@0 981
jbe@207 982 COMMENT ON COLUMN "direct_supporter_snapshot"."issue_id" IS 'WARNING: No index: For selections use column "initiative_id" and join via table "initiative" where neccessary';
jbe@0 983 COMMENT ON COLUMN "direct_supporter_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
jbe@0 984 COMMENT ON COLUMN "direct_supporter_snapshot"."informed" IS 'Supporter has seen the latest draft of the initiative';
jbe@0 985 COMMENT ON COLUMN "direct_supporter_snapshot"."satisfied" IS 'Supporter has no "critical_opinion"s';
jbe@0 986
jbe@0 987
jbe@113 988 CREATE TABLE "non_voter" (
jbe@113 989 PRIMARY KEY ("issue_id", "member_id"),
jbe@113 990 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@113 991 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@113 992 CREATE INDEX "non_voter_member_id_idx" ON "non_voter" ("member_id");
jbe@113 993
jbe@113 994 COMMENT ON TABLE "non_voter" IS 'Members who decided to not vote directly on an issue';
jbe@113 995
jbe@113 996
jbe@0 997 CREATE TABLE "direct_voter" (
jbe@0 998 PRIMARY KEY ("issue_id", "member_id"),
jbe@0 999 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@45 1000 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@169 1001 "weight" INT4 );
jbe@0 1002 CREATE INDEX "direct_voter_member_id_idx" ON "direct_voter" ("member_id");
jbe@0 1003
jbe@10 1004 COMMENT ON TABLE "direct_voter" IS 'Members having directly voted for/against initiatives of an issue; Frontends must ensure that no voters are added or removed to/from this table when the issue has been closed.';
jbe@0 1005
jbe@207 1006 COMMENT ON COLUMN "direct_voter"."weight" IS 'Weight of member (1 or higher) according to "delegating_voter" table';
jbe@0 1007
jbe@0 1008
jbe@0 1009 CREATE TABLE "delegating_voter" (
jbe@0 1010 PRIMARY KEY ("issue_id", "member_id"),
jbe@0 1011 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@45 1012 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
jbe@8 1013 "weight" INT4,
jbe@10 1014 "scope" "delegation_scope" NOT NULL,
jbe@0 1015 "delegate_member_ids" INT4[] NOT NULL );
jbe@52 1016 CREATE INDEX "delegating_voter_member_id_idx" ON "delegating_voter" ("member_id");
jbe@0 1017
jbe@0 1018 COMMENT ON TABLE "delegating_voter" IS 'Delegations increasing the weight of entries in the "direct_voter" table';
jbe@0 1019
jbe@0 1020 COMMENT ON COLUMN "delegating_voter"."member_id" IS 'Delegating member';
jbe@8 1021 COMMENT ON COLUMN "delegating_voter"."weight" IS 'Intermediate weight';
jbe@0 1022 COMMENT ON COLUMN "delegating_voter"."delegate_member_ids" IS 'Chain of members who act as delegates; last entry referes to "member_id" column of table "direct_voter"';
jbe@0 1023
jbe@0 1024
jbe@0 1025 CREATE TABLE "vote" (
jbe@0 1026 "issue_id" INT4 NOT NULL,
jbe@0 1027 PRIMARY KEY ("initiative_id", "member_id"),
jbe@0 1028 "initiative_id" INT4,
jbe@0 1029 "member_id" INT4,
jbe@0 1030 "grade" INT4,
jbe@0 1031 FOREIGN KEY ("issue_id", "initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@0 1032 FOREIGN KEY ("issue_id", "member_id") REFERENCES "direct_voter" ("issue_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
jbe@0 1033 CREATE INDEX "vote_member_id_idx" ON "vote" ("member_id");
jbe@0 1034
jbe@10 1035 COMMENT ON TABLE "vote" IS 'Manual and delegated votes without abstentions; Frontends must ensure that no votes are added modified or removed when the issue has been closed.';
jbe@0 1036
jbe@207 1037 COMMENT ON COLUMN "vote"."issue_id" IS 'WARNING: No index: For selections use column "initiative_id" and join via table "initiative" where neccessary';
jbe@207 1038 COMMENT ON COLUMN "vote"."grade" IS 'Values smaller than zero mean reject, values greater than zero mean acceptance, zero or missing row means abstention. Preferences are expressed by different positive or negative numbers.';
jbe@0 1039
jbe@0 1040
jbe@111 1041 CREATE TABLE "issue_comment" (
jbe@111 1042 PRIMARY KEY ("issue_id", "member_id"),
jbe@111 1043 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1044 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1045 "changed" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@111 1046 "formatting_engine" TEXT,
jbe@111 1047 "content" TEXT NOT NULL,
jbe@111 1048 "text_search_data" TSVECTOR );
jbe@111 1049 CREATE INDEX "issue_comment_member_id_idx" ON "issue_comment" ("member_id");
jbe@111 1050 CREATE INDEX "issue_comment_text_search_data_idx" ON "issue_comment" USING gin ("text_search_data");
jbe@111 1051 CREATE TRIGGER "update_text_search_data"
jbe@111 1052 BEFORE INSERT OR UPDATE ON "issue_comment"
jbe@111 1053 FOR EACH ROW EXECUTE PROCEDURE
jbe@111 1054 tsvector_update_trigger('text_search_data', 'pg_catalog.simple', "content");
jbe@111 1055
jbe@111 1056 COMMENT ON TABLE "issue_comment" IS 'Place to store free comments of members related to issues';
jbe@111 1057
jbe@111 1058 COMMENT ON COLUMN "issue_comment"."changed" IS 'Time the comment was last changed';
jbe@111 1059
jbe@111 1060
jbe@111 1061 CREATE TABLE "rendered_issue_comment" (
jbe@111 1062 PRIMARY KEY ("issue_id", "member_id", "format"),
jbe@111 1063 FOREIGN KEY ("issue_id", "member_id")
jbe@111 1064 REFERENCES "issue_comment" ("issue_id", "member_id")
jbe@111 1065 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1066 "issue_id" INT4,
jbe@111 1067 "member_id" INT4,
jbe@111 1068 "format" TEXT,
jbe@111 1069 "content" TEXT NOT NULL );
jbe@111 1070
jbe@111 1071 COMMENT ON TABLE "rendered_issue_comment" IS 'This table may be used by frontends to cache "rendered" issue comments (e.g. HTML output generated from wiki text)';
jbe@111 1072
jbe@111 1073
jbe@111 1074 CREATE TABLE "voting_comment" (
jbe@111 1075 PRIMARY KEY ("issue_id", "member_id"),
jbe@111 1076 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1077 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1078 "changed" TIMESTAMPTZ,
jbe@111 1079 "formatting_engine" TEXT,
jbe@111 1080 "content" TEXT NOT NULL,
jbe@111 1081 "text_search_data" TSVECTOR );
jbe@111 1082 CREATE INDEX "voting_comment_member_id_idx" ON "voting_comment" ("member_id");
jbe@111 1083 CREATE INDEX "voting_comment_text_search_data_idx" ON "voting_comment" USING gin ("text_search_data");
jbe@111 1084 CREATE TRIGGER "update_text_search_data"
jbe@111 1085 BEFORE INSERT OR UPDATE ON "voting_comment"
jbe@111 1086 FOR EACH ROW EXECUTE PROCEDURE
jbe@111 1087 tsvector_update_trigger('text_search_data', 'pg_catalog.simple', "content");
jbe@111 1088
jbe@111 1089 COMMENT ON TABLE "voting_comment" IS 'Storage for comments of voters to be published after voting has finished.';
jbe@111 1090
jbe@111 1091 COMMENT ON COLUMN "voting_comment"."changed" IS 'Is to be set or updated by the frontend, if comment was inserted or updated AFTER the issue has been closed. Otherwise it shall be set to NULL.';
jbe@111 1092
jbe@111 1093
jbe@111 1094 CREATE TABLE "rendered_voting_comment" (
jbe@111 1095 PRIMARY KEY ("issue_id", "member_id", "format"),
jbe@111 1096 FOREIGN KEY ("issue_id", "member_id")
jbe@111 1097 REFERENCES "voting_comment" ("issue_id", "member_id")
jbe@111 1098 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@111 1099 "issue_id" INT4,
jbe@111 1100 "member_id" INT4,
jbe@111 1101 "format" TEXT,
jbe@111 1102 "content" TEXT NOT NULL );
jbe@111 1103
jbe@111 1104 COMMENT ON TABLE "rendered_voting_comment" IS 'This table may be used by frontends to cache "rendered" voting comments (e.g. HTML output generated from wiki text)';
jbe@111 1105
jbe@111 1106
jbe@112 1107 CREATE TYPE "event_type" AS ENUM (
jbe@112 1108 'issue_state_changed',
jbe@112 1109 'initiative_created_in_new_issue',
jbe@112 1110 'initiative_created_in_existing_issue',
jbe@112 1111 'initiative_revoked',
jbe@112 1112 'new_draft_created',
jbe@112 1113 'suggestion_created');
jbe@112 1114
jbe@112 1115 COMMENT ON TYPE "event_type" IS 'Type used for column "event" of table "event"';
jbe@112 1116
jbe@112 1117
jbe@112 1118 CREATE TABLE "event" (
jbe@112 1119 "id" SERIAL8 PRIMARY KEY,
jbe@112 1120 "occurrence" TIMESTAMPTZ NOT NULL DEFAULT now(),
jbe@112 1121 "event" "event_type" NOT NULL,
jbe@112 1122 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
jbe@112 1123 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
jbe@112 1124 "state" "issue_state" CHECK ("state" != 'calculation'),
jbe@112 1125 "initiative_id" INT4,
jbe@112 1126 "draft_id" INT8,
jbe@112 1127 "suggestion_id" INT8,
jbe@112 1128 FOREIGN KEY ("issue_id", "initiative_id")
jbe@112 1129 REFERENCES "initiative" ("issue_id", "id")
jbe@112 1130 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@112 1131 FOREIGN KEY ("initiative_id", "draft_id")
jbe@112 1132 REFERENCES "draft" ("initiative_id", "id")
jbe@112 1133 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@112 1134 FOREIGN KEY ("initiative_id", "suggestion_id")
jbe@112 1135 REFERENCES "suggestion" ("initiative_id", "id")
jbe@112 1136 ON DELETE CASCADE ON UPDATE CASCADE,
jbe@112 1137 CONSTRAINT "null_constraints_for_issue_state_changed" CHECK (
jbe@112 1138 "event" != 'issue_state_changed' OR (
jbe@112 1139 "member_id" ISNULL AND
jbe@112 1140 "issue_id" NOTNULL AND
jbe@113 1141 "state" NOTNULL AND
jbe@112 1142 "initiative_id" ISNULL AND
jbe@112 1143 "draft_id" ISNULL AND
jbe@112 1144 "suggestion_id" ISNULL )),
jbe@112 1145 CONSTRAINT "null_constraints_for_initiative_creation_or_revocation_or_new_draft" CHECK (
jbe@112 1146 "event" NOT IN (
jbe@112 1147 'initiative_created_in_new_issue',
jbe@112 1148 'initiative_created_in_existing_issue',
jbe@112 1149 'initiative_revoked',
jbe@112 1150 'new_draft_created'
jbe@112 1151 ) OR (
jbe@112 1152 "member_id" NOTNULL AND
jbe@112 1153 "issue_id" NOTNULL AND
jbe@113 1154 "state" NOTNULL AND
jbe@112 1155 "initiative_id" NOTNULL AND
jbe@112 1156 "draft_id" NOTNULL AND
jbe@112 1157 "suggestion_id" ISNULL )),
jbe@112 1158 CONSTRAINT "null_constraints_for_suggestion_creation" CHECK (
jbe@112 1159 "event" != 'suggestion_created' OR (
jbe@112 1160 "member_id" NOTNULL AND
jbe@112 1161 "issue_id" NOTNULL AND
jbe@113 1162 "state" NOTNULL AND
jbe@112 1163 "initiative_id" NOTNULL AND
jbe@112 1164 "draft_id" ISNULL AND
jbe@112 1165 "suggestion_id" NOTNULL )) );
jbe@223 1166 CREATE INDEX "event_occurrence_idx" ON "event" ("occurrence");
jbe@112 1167
jbe@112 1168 COMMENT ON TABLE "event" IS 'Event table, automatically filled by triggers';
jbe@112 1169
jbe@114 1170 COMMENT ON COLUMN "event"."occurrence" IS 'Point in time, when event occurred';
jbe@114 1171 COMMENT ON COLUMN "event"."event" IS 'Type of event (see TYPE "event_type")';
jbe@114 1172 COMMENT ON COLUMN "event"."member_id" IS 'Member who caused the event, if applicable';
jbe@114 1173 COMMENT ON COLUMN "event"."state" IS 'If issue_id is set: state of affected issue; If state changed: new state';
jbe@114 1174
jbe@112 1175
jbe@222 1176 CREATE TABLE "notification_sent" (
jbe@222 1177 "event_id" INT8 NOT NULL );
jbe@222 1178 CREATE UNIQUE INDEX "notification_sent_singleton_idx" ON "notification_sent" ((1));
jbe@222 1179
jbe@222 1180 COMMENT ON TABLE "notification_sent" IS 'This table stores one row with the last event_id, for which notifications have been sent out';
jbe@222 1181 COMMENT ON INDEX "notification_sent_singleton_idx" IS 'This index ensures that "notification_sent" only contains one row maximum.';
jbe@222 1182
jbe@222 1183
jbe@112 1184
jbe@112 1185 ----------------------------------------------
jbe@112 1186 -- Writing of history entries and event log --
jbe@112 1187 ----------------------------------------------
jbe@13 1188
jbe@181 1189
jbe@13 1190 CREATE FUNCTION "write_member_history_trigger"()
jbe@13 1191 RETURNS TRIGGER
jbe@13 1192 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@13 1193 BEGIN
jbe@42 1194 IF
jbe@230 1195 ( NEW."active" != OLD."active" OR
jbe@230 1196 NEW."name" != OLD."name" ) AND
jbe@230 1197 OLD."activated" NOTNULL
jbe@42 1198 THEN
jbe@42 1199 INSERT INTO "member_history"
jbe@57 1200 ("member_id", "active", "name")
jbe@57 1201 VALUES (NEW."id", OLD."active", OLD."name");
jbe@13 1202 END IF;
jbe@13 1203 RETURN NULL;
jbe@13 1204 END;
jbe@13 1205 $$;
jbe@13 1206
jbe@13 1207 CREATE TRIGGER "write_member_history"
jbe@13 1208 AFTER UPDATE ON "member" FOR EACH ROW EXECUTE PROCEDURE
jbe@13 1209 "write_member_history_trigger"();
jbe@13 1210
jbe@13 1211 COMMENT ON FUNCTION "write_member_history_trigger"() IS 'Implementation of trigger "write_member_history" on table "member"';
jbe@57 1212 COMMENT ON TRIGGER "write_member_history" ON "member" IS 'When changing certain fields of a member, create a history entry in "member_history" table';
jbe@13 1213
jbe@13 1214
jbe@112 1215 CREATE FUNCTION "write_event_issue_state_changed_trigger"()
jbe@112 1216 RETURNS TRIGGER
jbe@112 1217 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@112 1218 BEGIN
jbe@112 1219 IF NEW."state" != OLD."state" AND NEW."state" != 'calculation' THEN
jbe@112 1220 INSERT INTO "event" ("event", "issue_id", "state")
jbe@112 1221 VALUES ('issue_state_changed', NEW."id", NEW."state");
jbe@112 1222 END IF;
jbe@112 1223 RETURN NULL;
jbe@112 1224 END;
jbe@112 1225 $$;
jbe@112 1226
jbe@112 1227 CREATE TRIGGER "write_event_issue_state_changed"
jbe@112 1228 AFTER UPDATE ON "issue" FOR EACH ROW EXECUTE PROCEDURE
jbe@112 1229 "write_event_issue_state_changed_trigger"();
jbe@112 1230
jbe@112 1231 COMMENT ON FUNCTION "write_event_issue_state_changed_trigger"() IS 'Implementation of trigger "write_event_issue_state_changed" on table "issue"';
jbe@112 1232 COMMENT ON TRIGGER "write_event_issue_state_changed" ON "issue" IS 'Create entry in "event" table on "state" change';
jbe@112 1233
jbe@112 1234
jbe@112 1235 CREATE FUNCTION "write_event_initiative_or_draft_created_trigger"()
jbe@112 1236 RETURNS TRIGGER
jbe@112 1237 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@112 1238 DECLARE
jbe@112 1239 "initiative_row" "initiative"%ROWTYPE;
jbe@113 1240 "issue_row" "issue"%ROWTYPE;
jbe@112 1241 "event_v" "event_type";
jbe@112 1242 BEGIN
jbe@112 1243 SELECT * INTO "initiative_row" FROM "initiative"
jbe@112 1244 WHERE "id" = NEW."initiative_id";
jbe@113 1245 SELECT * INTO "issue_row" FROM "issue"
jbe@113 1246 WHERE "id" = "initiative_row"."issue_id";
jbe@112 1247 IF EXISTS (
jbe@112 1248 SELECT NULL FROM "draft"
jbe@112 1249 WHERE "initiative_id" = NEW."initiative_id"
jbe@112 1250 AND "id" != NEW."id"
jbe@112 1251 ) THEN
jbe@112 1252 "event_v" := 'new_draft_created';
jbe@112 1253 ELSE
jbe@112 1254 IF EXISTS (
jbe@112 1255 SELECT NULL FROM "initiative"
jbe@112 1256 WHERE "issue_id" = "initiative_row"."issue_id"
jbe@112 1257 AND "id" != "initiative_row"."id"
jbe@112 1258 ) THEN
jbe@112 1259 "event_v" := 'initiative_created_in_existing_issue';
jbe@112 1260 ELSE
jbe@112 1261 "event_v" := 'initiative_created_in_new_issue';
jbe@112 1262 END IF;
jbe@112 1263 END IF;
jbe@112 1264 INSERT INTO "event" (
jbe@112 1265 "event", "member_id",
jbe@113 1266 "issue_id", "state", "initiative_id", "draft_id"
jbe@112 1267 ) VALUES (
jbe@112 1268 "event_v",
jbe@112 1269 NEW."author_id",
jbe@112 1270 "initiative_row"."issue_id",
jbe@113 1271 "issue_row"."state",
jbe@112 1272 "initiative_row"."id",
jbe@112 1273 NEW."id" );
jbe@112 1274 RETURN NULL;
jbe@112 1275 END;
jbe@112 1276 $$;
jbe@112 1277
jbe@112 1278 CREATE TRIGGER "write_event_initiative_or_draft_created"
jbe@112 1279 AFTER INSERT ON "draft" FOR EACH ROW EXECUTE PROCEDURE
jbe@112 1280 "write_event_initiative_or_draft_created_trigger"();
jbe@112 1281
jbe@112 1282 COMMENT ON FUNCTION "write_event_initiative_or_draft_created_trigger"() IS 'Implementation of trigger "write_event_initiative_or_draft_created" on table "issue"';
jbe@112 1283 COMMENT ON TRIGGER "write_event_initiative_or_draft_created" ON "draft" IS 'Create entry in "event" table on draft creation';
jbe@112 1284
jbe@112 1285
jbe@112 1286 CREATE FUNCTION "write_event_initiative_revoked_trigger"()
jbe@112 1287 RETURNS TRIGGER
jbe@112 1288 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@113 1289 DECLARE
jbe@231 1290 "issue_row" "issue"%ROWTYPE;
jbe@231 1291 "draft_id_v" "draft"."id"%TYPE;
jbe@112 1292 BEGIN
jbe@112 1293 IF OLD."revoked" ISNULL AND NEW."revoked" NOTNULL THEN
jbe@231 1294 SELECT * INTO "issue_row" FROM "issue"
jbe@231 1295 WHERE "id" = NEW."issue_id";
jbe@231 1296 SELECT "id" INTO "draft_id_v" FROM "current_draft"
jbe@231 1297 WHERE "initiative_id" = NEW."id";
jbe@112 1298 INSERT INTO "event" (
jbe@231 1299 "event", "member_id", "issue_id", "state", "initiative_id", "draft_id"
jbe@112 1300 ) VALUES (
jbe@112 1301 'initiative_revoked',
jbe@112 1302 NEW."revoked_by_member_id",
jbe@112 1303 NEW."issue_id",
jbe@113 1304 "issue_row"."state",
jbe@231 1305 NEW."id",
jbe@231 1306 "draft_id_v");
jbe@112 1307 END IF;
jbe@112 1308 RETURN NULL;
jbe@112 1309 END;
jbe@112 1310 $$;
jbe@112 1311
jbe@112 1312 CREATE TRIGGER "write_event_initiative_revoked"
jbe@112 1313 AFTER UPDATE ON "initiative" FOR EACH ROW EXECUTE PROCEDURE
jbe@112 1314 "write_event_initiative_revoked_trigger"();
jbe@112 1315
jbe@112 1316 COMMENT ON FUNCTION "write_event_initiative_revoked_trigger"() IS 'Implementation of trigger "write_event_initiative_revoked" on table "issue"';
jbe@112 1317 COMMENT ON TRIGGER "write_event_initiative_revoked" ON "initiative" IS 'Create entry in "event" table, when an initiative is revoked';
jbe@112 1318
jbe@112 1319
jbe@112 1320 CREATE FUNCTION "write_event_suggestion_created_trigger"()
jbe@112 1321 RETURNS TRIGGER
jbe@112 1322 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@112 1323 DECLARE
jbe@112 1324 "initiative_row" "initiative"%ROWTYPE;
jbe@113 1325 "issue_row" "issue"%ROWTYPE;
jbe@112 1326 BEGIN
jbe@112 1327 SELECT * INTO "initiative_row" FROM "initiative"
jbe@112 1328 WHERE "id" = NEW."initiative_id";
jbe@113 1329 SELECT * INTO "issue_row" FROM "issue"
jbe@113 1330 WHERE "id" = "initiative_row"."issue_id";
jbe@112 1331 INSERT INTO "event" (
jbe@112 1332 "event", "member_id",
jbe@113 1333 "issue_id", "state", "initiative_id", "suggestion_id"
jbe@112 1334 ) VALUES (
jbe@112 1335 'suggestion_created',
jbe@112 1336 NEW."author_id",
jbe@112 1337 "initiative_row"."issue_id",
jbe@113 1338 "issue_row"."state",
jbe@112 1339 "initiative_row"."id",
jbe@112 1340 NEW."id" );
jbe@112 1341 RETURN NULL;
jbe@112 1342 END;
jbe@112 1343 $$;
jbe@112 1344
jbe@112 1345 CREATE TRIGGER "write_event_suggestion_created"
jbe@112 1346 AFTER INSERT ON "suggestion" FOR EACH ROW EXECUTE PROCEDURE
jbe@112 1347 "write_event_suggestion_created_trigger"();
jbe@112 1348
jbe@112 1349 COMMENT ON FUNCTION "write_event_suggestion_created_trigger"() IS 'Implementation of trigger "write_event_suggestion_created" on table "issue"';
jbe@112 1350 COMMENT ON TRIGGER "write_event_suggestion_created" ON "suggestion" IS 'Create entry in "event" table on suggestion creation';
jbe@112 1351
jbe@112 1352
jbe@13 1353
jbe@0 1354 ----------------------------
jbe@0 1355 -- Additional constraints --
jbe@0 1356 ----------------------------
jbe@0 1357
jbe@0 1358
jbe@0 1359 CREATE FUNCTION "issue_requires_first_initiative_trigger"()
jbe@0 1360 RETURNS TRIGGER
jbe@0 1361 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1362 BEGIN
jbe@0 1363 IF NOT EXISTS (
jbe@0 1364 SELECT NULL FROM "initiative" WHERE "issue_id" = NEW."id"
jbe@0 1365 ) THEN
jbe@0 1366 --RAISE 'Cannot create issue without an initial initiative.' USING
jbe@0 1367 -- ERRCODE = 'integrity_constraint_violation',
jbe@0 1368 -- HINT = 'Create issue, initiative, and draft within the same transaction.';
jbe@0 1369 RAISE EXCEPTION 'Cannot create issue without an initial initiative.';
jbe@0 1370 END IF;
jbe@0 1371 RETURN NULL;
jbe@0 1372 END;
jbe@0 1373 $$;
jbe@0 1374
jbe@0 1375 CREATE CONSTRAINT TRIGGER "issue_requires_first_initiative"
jbe@0 1376 AFTER INSERT OR UPDATE ON "issue" DEFERRABLE INITIALLY DEFERRED
jbe@0 1377 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1378 "issue_requires_first_initiative_trigger"();
jbe@0 1379
jbe@0 1380 COMMENT ON FUNCTION "issue_requires_first_initiative_trigger"() IS 'Implementation of trigger "issue_requires_first_initiative" on table "issue"';
jbe@0 1381 COMMENT ON TRIGGER "issue_requires_first_initiative" ON "issue" IS 'Ensure that new issues have at least one initiative';
jbe@0 1382
jbe@0 1383
jbe@0 1384 CREATE FUNCTION "last_initiative_deletes_issue_trigger"()
jbe@0 1385 RETURNS TRIGGER
jbe@0 1386 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1387 DECLARE
jbe@0 1388 "reference_lost" BOOLEAN;
jbe@0 1389 BEGIN
jbe@0 1390 IF TG_OP = 'DELETE' THEN
jbe@0 1391 "reference_lost" := TRUE;
jbe@0 1392 ELSE
jbe@0 1393 "reference_lost" := NEW."issue_id" != OLD."issue_id";
jbe@0 1394 END IF;
jbe@0 1395 IF
jbe@0 1396 "reference_lost" AND NOT EXISTS (
jbe@0 1397 SELECT NULL FROM "initiative" WHERE "issue_id" = OLD."issue_id"
jbe@0 1398 )
jbe@0 1399 THEN
jbe@0 1400 DELETE FROM "issue" WHERE "id" = OLD."issue_id";
jbe@0 1401 END IF;
jbe@0 1402 RETURN NULL;
jbe@0 1403 END;
jbe@0 1404 $$;
jbe@0 1405
jbe@0 1406 CREATE CONSTRAINT TRIGGER "last_initiative_deletes_issue"
jbe@0 1407 AFTER UPDATE OR DELETE ON "initiative" DEFERRABLE INITIALLY DEFERRED
jbe@0 1408 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1409 "last_initiative_deletes_issue_trigger"();
jbe@0 1410
jbe@0 1411 COMMENT ON FUNCTION "last_initiative_deletes_issue_trigger"() IS 'Implementation of trigger "last_initiative_deletes_issue" on table "initiative"';
jbe@0 1412 COMMENT ON TRIGGER "last_initiative_deletes_issue" ON "initiative" IS 'Removing the last initiative of an issue deletes the issue';
jbe@0 1413
jbe@0 1414
jbe@0 1415 CREATE FUNCTION "initiative_requires_first_draft_trigger"()
jbe@0 1416 RETURNS TRIGGER
jbe@0 1417 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1418 BEGIN
jbe@0 1419 IF NOT EXISTS (
jbe@0 1420 SELECT NULL FROM "draft" WHERE "initiative_id" = NEW."id"
jbe@0 1421 ) THEN
jbe@0 1422 --RAISE 'Cannot create initiative without an initial draft.' USING
jbe@0 1423 -- ERRCODE = 'integrity_constraint_violation',
jbe@0 1424 -- HINT = 'Create issue, initiative and draft within the same transaction.';
jbe@0 1425 RAISE EXCEPTION 'Cannot create initiative without an initial draft.';
jbe@0 1426 END IF;
jbe@0 1427 RETURN NULL;
jbe@0 1428 END;
jbe@0 1429 $$;
jbe@0 1430
jbe@0 1431 CREATE CONSTRAINT TRIGGER "initiative_requires_first_draft"
jbe@0 1432 AFTER INSERT OR UPDATE ON "initiative" DEFERRABLE INITIALLY DEFERRED
jbe@0 1433 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1434 "initiative_requires_first_draft_trigger"();
jbe@0 1435
jbe@0 1436 COMMENT ON FUNCTION "initiative_requires_first_draft_trigger"() IS 'Implementation of trigger "initiative_requires_first_draft" on table "initiative"';
jbe@0 1437 COMMENT ON TRIGGER "initiative_requires_first_draft" ON "initiative" IS 'Ensure that new initiatives have at least one draft';
jbe@0 1438
jbe@0 1439
jbe@0 1440 CREATE FUNCTION "last_draft_deletes_initiative_trigger"()
jbe@0 1441 RETURNS TRIGGER
jbe@0 1442 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1443 DECLARE
jbe@0 1444 "reference_lost" BOOLEAN;
jbe@0 1445 BEGIN
jbe@0 1446 IF TG_OP = 'DELETE' THEN
jbe@0 1447 "reference_lost" := TRUE;
jbe@0 1448 ELSE
jbe@0 1449 "reference_lost" := NEW."initiative_id" != OLD."initiative_id";
jbe@0 1450 END IF;
jbe@0 1451 IF
jbe@0 1452 "reference_lost" AND NOT EXISTS (
jbe@0 1453 SELECT NULL FROM "draft" WHERE "initiative_id" = OLD."initiative_id"
jbe@0 1454 )
jbe@0 1455 THEN
jbe@0 1456 DELETE FROM "initiative" WHERE "id" = OLD."initiative_id";
jbe@0 1457 END IF;
jbe@0 1458 RETURN NULL;
jbe@0 1459 END;
jbe@0 1460 $$;
jbe@0 1461
jbe@0 1462 CREATE CONSTRAINT TRIGGER "last_draft_deletes_initiative"
jbe@0 1463 AFTER UPDATE OR DELETE ON "draft" DEFERRABLE INITIALLY DEFERRED
jbe@0 1464 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1465 "last_draft_deletes_initiative_trigger"();
jbe@0 1466
jbe@0 1467 COMMENT ON FUNCTION "last_draft_deletes_initiative_trigger"() IS 'Implementation of trigger "last_draft_deletes_initiative" on table "draft"';
jbe@0 1468 COMMENT ON TRIGGER "last_draft_deletes_initiative" ON "draft" IS 'Removing the last draft of an initiative deletes the initiative';
jbe@0 1469
jbe@0 1470
jbe@0 1471 CREATE FUNCTION "suggestion_requires_first_opinion_trigger"()
jbe@0 1472 RETURNS TRIGGER
jbe@0 1473 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1474 BEGIN
jbe@0 1475 IF NOT EXISTS (
jbe@0 1476 SELECT NULL FROM "opinion" WHERE "suggestion_id" = NEW."id"
jbe@0 1477 ) THEN
jbe@0 1478 RAISE EXCEPTION 'Cannot create a suggestion without an opinion.';
jbe@0 1479 END IF;
jbe@0 1480 RETURN NULL;
jbe@0 1481 END;
jbe@0 1482 $$;
jbe@0 1483
jbe@0 1484 CREATE CONSTRAINT TRIGGER "suggestion_requires_first_opinion"
jbe@0 1485 AFTER INSERT OR UPDATE ON "suggestion" DEFERRABLE INITIALLY DEFERRED
jbe@0 1486 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1487 "suggestion_requires_first_opinion_trigger"();
jbe@0 1488
jbe@0 1489 COMMENT ON FUNCTION "suggestion_requires_first_opinion_trigger"() IS 'Implementation of trigger "suggestion_requires_first_opinion" on table "suggestion"';
jbe@0 1490 COMMENT ON TRIGGER "suggestion_requires_first_opinion" ON "suggestion" IS 'Ensure that new suggestions have at least one opinion';
jbe@0 1491
jbe@0 1492
jbe@0 1493 CREATE FUNCTION "last_opinion_deletes_suggestion_trigger"()
jbe@0 1494 RETURNS TRIGGER
jbe@0 1495 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1496 DECLARE
jbe@0 1497 "reference_lost" BOOLEAN;
jbe@0 1498 BEGIN
jbe@0 1499 IF TG_OP = 'DELETE' THEN
jbe@0 1500 "reference_lost" := TRUE;
jbe@0 1501 ELSE
jbe@0 1502 "reference_lost" := NEW."suggestion_id" != OLD."suggestion_id";
jbe@0 1503 END IF;
jbe@0 1504 IF
jbe@0 1505 "reference_lost" AND NOT EXISTS (
jbe@0 1506 SELECT NULL FROM "opinion" WHERE "suggestion_id" = OLD."suggestion_id"
jbe@0 1507 )
jbe@0 1508 THEN
jbe@0 1509 DELETE FROM "suggestion" WHERE "id" = OLD."suggestion_id";
jbe@0 1510 END IF;
jbe@0 1511 RETURN NULL;
jbe@0 1512 END;
jbe@0 1513 $$;
jbe@0 1514
jbe@0 1515 CREATE CONSTRAINT TRIGGER "last_opinion_deletes_suggestion"
jbe@0 1516 AFTER UPDATE OR DELETE ON "opinion" DEFERRABLE INITIALLY DEFERRED
jbe@0 1517 FOR EACH ROW EXECUTE PROCEDURE
jbe@0 1518 "last_opinion_deletes_suggestion_trigger"();
jbe@0 1519
jbe@0 1520 COMMENT ON FUNCTION "last_opinion_deletes_suggestion_trigger"() IS 'Implementation of trigger "last_opinion_deletes_suggestion" on table "opinion"';
jbe@0 1521 COMMENT ON TRIGGER "last_opinion_deletes_suggestion" ON "opinion" IS 'Removing the last opinion of a suggestion deletes the suggestion';
jbe@0 1522
jbe@0 1523
jbe@0 1524
jbe@20 1525 ---------------------------------------------------------------
jbe@20 1526 -- Ensure that votes are not modified when issues are frozen --
jbe@20 1527 ---------------------------------------------------------------
jbe@20 1528
jbe@20 1529 -- NOTE: Frontends should ensure this anyway, but in case of programming
jbe@20 1530 -- errors the following triggers ensure data integrity.
jbe@20 1531
jbe@20 1532
jbe@20 1533 CREATE FUNCTION "forbid_changes_on_closed_issue_trigger"()
jbe@20 1534 RETURNS TRIGGER
jbe@20 1535 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@20 1536 DECLARE
jbe@32 1537 "issue_id_v" "issue"."id"%TYPE;
jbe@32 1538 "issue_row" "issue"%ROWTYPE;
jbe@20 1539 BEGIN
jbe@32 1540 IF TG_OP = 'DELETE' THEN
jbe@32 1541 "issue_id_v" := OLD."issue_id";
jbe@32 1542 ELSE
jbe@32 1543 "issue_id_v" := NEW."issue_id";
jbe@32 1544 END IF;
jbe@20 1545 SELECT INTO "issue_row" * FROM "issue"
jbe@32 1546 WHERE "id" = "issue_id_v" FOR SHARE;
jbe@20 1547 IF "issue_row"."closed" NOTNULL THEN
jbe@20 1548 RAISE EXCEPTION 'Tried to modify data belonging to a closed issue.';
jbe@20 1549 END IF;
jbe@20 1550 RETURN NULL;
jbe@20 1551 END;
jbe@20 1552 $$;
jbe@20 1553
jbe@20 1554 CREATE TRIGGER "forbid_changes_on_closed_issue"
jbe@20 1555 AFTER INSERT OR UPDATE OR DELETE ON "direct_voter"
jbe@20 1556 FOR EACH ROW EXECUTE PROCEDURE
jbe@20 1557 "forbid_changes_on_closed_issue_trigger"();
jbe@20 1558
jbe@20 1559 CREATE TRIGGER "forbid_changes_on_closed_issue"
jbe@20 1560 AFTER INSERT OR UPDATE OR DELETE ON "delegating_voter"
jbe@20 1561 FOR EACH ROW EXECUTE PROCEDURE
jbe@20 1562 "forbid_changes_on_closed_issue_trigger"();
jbe@20 1563
jbe@20 1564 CREATE TRIGGER "forbid_changes_on_closed_issue"
jbe@20 1565 AFTER INSERT OR UPDATE OR DELETE ON "vote"
jbe@20 1566 FOR EACH ROW EXECUTE PROCEDURE
jbe@20 1567 "forbid_changes_on_closed_issue_trigger"();
jbe@20 1568
jbe@20 1569 COMMENT ON FUNCTION "forbid_changes_on_closed_issue_trigger"() IS 'Implementation of triggers "forbid_changes_on_closed_issue" on tables "direct_voter", "delegating_voter" and "vote"';
jbe@20 1570 COMMENT ON TRIGGER "forbid_changes_on_closed_issue" ON "direct_voter" IS 'Ensures that frontends can''t tamper with votings of closed issues, in case of programming errors';
jbe@20 1571 COMMENT ON TRIGGER "forbid_changes_on_closed_issue" ON "delegating_voter" IS 'Ensures that frontends can''t tamper with votings of closed issues, in case of programming errors';
jbe@20 1572 COMMENT ON TRIGGER "forbid_changes_on_closed_issue" ON "vote" IS 'Ensures that frontends can''t tamper with votings of closed issues, in case of programming errors';
jbe@20 1573
jbe@20 1574
jbe@20 1575
jbe@0 1576 --------------------------------------------------------------------
jbe@0 1577 -- Auto-retrieval of fields only needed for referential integrity --
jbe@0 1578 --------------------------------------------------------------------
jbe@0 1579
jbe@20 1580
jbe@0 1581 CREATE FUNCTION "autofill_issue_id_trigger"()
jbe@0 1582 RETURNS TRIGGER
jbe@0 1583 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1584 BEGIN
jbe@0 1585 IF NEW."issue_id" ISNULL THEN
jbe@0 1586 SELECT "issue_id" INTO NEW."issue_id"
jbe@0 1587 FROM "initiative" WHERE "id" = NEW."initiative_id";
jbe@0 1588 END IF;
jbe@0 1589 RETURN NEW;
jbe@0 1590 END;
jbe@0 1591 $$;
jbe@0 1592
jbe@0 1593 CREATE TRIGGER "autofill_issue_id" BEFORE INSERT ON "supporter"
jbe@0 1594 FOR EACH ROW EXECUTE PROCEDURE "autofill_issue_id_trigger"();
jbe@0 1595
jbe@0 1596 CREATE TRIGGER "autofill_issue_id" BEFORE INSERT ON "vote"
jbe@0 1597 FOR EACH ROW EXECUTE PROCEDURE "autofill_issue_id_trigger"();
jbe@0 1598
jbe@0 1599 COMMENT ON FUNCTION "autofill_issue_id_trigger"() IS 'Implementation of triggers "autofill_issue_id" on tables "supporter" and "vote"';
jbe@0 1600 COMMENT ON TRIGGER "autofill_issue_id" ON "supporter" IS 'Set "issue_id" field automatically, if NULL';
jbe@0 1601 COMMENT ON TRIGGER "autofill_issue_id" ON "vote" IS 'Set "issue_id" field automatically, if NULL';
jbe@0 1602
jbe@0 1603
jbe@0 1604 CREATE FUNCTION "autofill_initiative_id_trigger"()
jbe@0 1605 RETURNS TRIGGER
jbe@0 1606 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1607 BEGIN
jbe@0 1608 IF NEW."initiative_id" ISNULL THEN
jbe@0 1609 SELECT "initiative_id" INTO NEW."initiative_id"
jbe@0 1610 FROM "suggestion" WHERE "id" = NEW."suggestion_id";
jbe@0 1611 END IF;
jbe@0 1612 RETURN NEW;
jbe@0 1613 END;
jbe@0 1614 $$;
jbe@0 1615
jbe@0 1616 CREATE TRIGGER "autofill_initiative_id" BEFORE INSERT ON "opinion"
jbe@0 1617 FOR EACH ROW EXECUTE PROCEDURE "autofill_initiative_id_trigger"();
jbe@0 1618
jbe@0 1619 COMMENT ON FUNCTION "autofill_initiative_id_trigger"() IS 'Implementation of trigger "autofill_initiative_id" on table "opinion"';
jbe@0 1620 COMMENT ON TRIGGER "autofill_initiative_id" ON "opinion" IS 'Set "initiative_id" field automatically, if NULL';
jbe@0 1621
jbe@0 1622
jbe@0 1623
jbe@4 1624 -----------------------------------------------------
jbe@4 1625 -- Automatic calculation of certain default values --
jbe@4 1626 -----------------------------------------------------
jbe@0 1627
jbe@22 1628
jbe@22 1629 CREATE FUNCTION "copy_timings_trigger"()
jbe@22 1630 RETURNS TRIGGER
jbe@22 1631 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@22 1632 DECLARE
jbe@22 1633 "policy_row" "policy"%ROWTYPE;
jbe@22 1634 BEGIN
jbe@22 1635 SELECT * INTO "policy_row" FROM "policy"
jbe@22 1636 WHERE "id" = NEW."policy_id";
jbe@22 1637 IF NEW."admission_time" ISNULL THEN
jbe@22 1638 NEW."admission_time" := "policy_row"."admission_time";
jbe@22 1639 END IF;
jbe@22 1640 IF NEW."discussion_time" ISNULL THEN
jbe@22 1641 NEW."discussion_time" := "policy_row"."discussion_time";
jbe@22 1642 END IF;
jbe@22 1643 IF NEW."verification_time" ISNULL THEN
jbe@22 1644 NEW."verification_time" := "policy_row"."verification_time";
jbe@22 1645 END IF;
jbe@22 1646 IF NEW."voting_time" ISNULL THEN
jbe@22 1647 NEW."voting_time" := "policy_row"."voting_time";
jbe@22 1648 END IF;
jbe@22 1649 RETURN NEW;
jbe@22 1650 END;
jbe@22 1651 $$;
jbe@22 1652
jbe@22 1653 CREATE TRIGGER "copy_timings" BEFORE INSERT OR UPDATE ON "issue"
jbe@22 1654 FOR EACH ROW EXECUTE PROCEDURE "copy_timings_trigger"();
jbe@22 1655
jbe@22 1656 COMMENT ON FUNCTION "copy_timings_trigger"() IS 'Implementation of trigger "copy_timings" on table "issue"';
jbe@22 1657 COMMENT ON TRIGGER "copy_timings" ON "issue" IS 'If timing fields are NULL, copy values from policy.';
jbe@22 1658
jbe@22 1659
jbe@160 1660 CREATE FUNCTION "default_for_draft_id_trigger"()
jbe@2 1661 RETURNS TRIGGER
jbe@2 1662 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@2 1663 BEGIN
jbe@2 1664 IF NEW."draft_id" ISNULL THEN
jbe@2 1665 SELECT "id" INTO NEW."draft_id" FROM "current_draft"
jbe@2 1666 WHERE "initiative_id" = NEW."initiative_id";
jbe@2 1667 END IF;
jbe@2 1668 RETURN NEW;
jbe@2 1669 END;
jbe@2 1670 $$;
jbe@2 1671
jbe@160 1672 CREATE TRIGGER "default_for_draft_id" BEFORE INSERT OR UPDATE ON "suggestion"
jbe@160 1673 FOR EACH ROW EXECUTE PROCEDURE "default_for_draft_id_trigger"();
jbe@2 1674 CREATE TRIGGER "default_for_draft_id" BEFORE INSERT OR UPDATE ON "supporter"
jbe@160 1675 FOR EACH ROW EXECUTE PROCEDURE "default_for_draft_id_trigger"();
jbe@160 1676
jbe@160 1677 COMMENT ON FUNCTION "default_for_draft_id_trigger"() IS 'Implementation of trigger "default_for_draft" on tables "supporter" and "suggestion"';
jbe@160 1678 COMMENT ON TRIGGER "default_for_draft_id" ON "suggestion" IS 'If "draft_id" is NULL, then use the current draft of the initiative as default';
jbe@160 1679 COMMENT ON TRIGGER "default_for_draft_id" ON "supporter" IS 'If "draft_id" is NULL, then use the current draft of the initiative as default';
jbe@2 1680
jbe@2 1681
jbe@0 1682
jbe@0 1683 ----------------------------------------
jbe@0 1684 -- Automatic creation of dependencies --
jbe@0 1685 ----------------------------------------
jbe@0 1686
jbe@22 1687
jbe@0 1688 CREATE FUNCTION "autocreate_interest_trigger"()
jbe@0 1689 RETURNS TRIGGER
jbe@0 1690 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1691 BEGIN
jbe@0 1692 IF NOT EXISTS (
jbe@0 1693 SELECT NULL FROM "initiative" JOIN "interest"
jbe@0 1694 ON "initiative"."issue_id" = "interest"."issue_id"
jbe@0 1695 WHERE "initiative"."id" = NEW."initiative_id"
jbe@0 1696 AND "interest"."member_id" = NEW."member_id"
jbe@0 1697 ) THEN
jbe@0 1698 BEGIN
jbe@0 1699 INSERT INTO "interest" ("issue_id", "member_id")
jbe@0 1700 SELECT "issue_id", NEW."member_id"
jbe@0 1701 FROM "initiative" WHERE "id" = NEW."initiative_id";
jbe@0 1702 EXCEPTION WHEN unique_violation THEN END;
jbe@0 1703 END IF;
jbe@0 1704 RETURN NEW;
jbe@0 1705 END;
jbe@0 1706 $$;
jbe@0 1707
jbe@0 1708 CREATE TRIGGER "autocreate_interest" BEFORE INSERT ON "supporter"
jbe@0 1709 FOR EACH ROW EXECUTE PROCEDURE "autocreate_interest_trigger"();
jbe@0 1710
jbe@0 1711 COMMENT ON FUNCTION "autocreate_interest_trigger"() IS 'Implementation of trigger "autocreate_interest" on table "supporter"';
jbe@0 1712 COMMENT ON TRIGGER "autocreate_interest" ON "supporter" IS 'Supporting an initiative implies interest in the issue, thus automatically creates an entry in the "interest" table';
jbe@0 1713
jbe@0 1714
jbe@0 1715 CREATE FUNCTION "autocreate_supporter_trigger"()
jbe@0 1716 RETURNS TRIGGER
jbe@0 1717 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 1718 BEGIN
jbe@0 1719 IF NOT EXISTS (
jbe@0 1720 SELECT NULL FROM "suggestion" JOIN "supporter"
jbe@0 1721 ON "suggestion"."initiative_id" = "supporter"."initiative_id"
jbe@0 1722 WHERE "suggestion"."id" = NEW."suggestion_id"
jbe@0 1723 AND "supporter"."member_id" = NEW."member_id"
jbe@0 1724 ) THEN
jbe@0 1725 BEGIN
jbe@0 1726 INSERT INTO "supporter" ("initiative_id", "member_id")
jbe@0 1727 SELECT "initiative_id", NEW."member_id"
jbe@0 1728 FROM "suggestion" WHERE "id" = NEW."suggestion_id";
jbe@0 1729 EXCEPTION WHEN unique_violation THEN END;
jbe@0 1730 END IF;
jbe@0 1731 RETURN NEW;
jbe@0 1732 END;
jbe@0 1733 $$;
jbe@0 1734
jbe@0 1735 CREATE TRIGGER "autocreate_supporter" BEFORE INSERT ON "opinion"
jbe@0 1736 FOR EACH ROW EXECUTE PROCEDURE "autocreate_supporter_trigger"();
jbe@0 1737
jbe@0 1738 COMMENT ON FUNCTION "autocreate_supporter_trigger"() IS 'Implementation of trigger "autocreate_supporter" on table "opinion"';
jbe@0 1739 COMMENT ON TRIGGER "autocreate_supporter" ON "opinion" IS 'Opinions can only be added for supported initiatives. This trigger automatrically creates an entry in the "supporter" table, if not existent yet.';
jbe@0 1740
jbe@0 1741
jbe@0 1742
jbe@0 1743 ------------------------------------------
jbe@0 1744 -- Views and helper functions for views --
jbe@0 1745 ------------------------------------------
jbe@0 1746
jbe@5 1747
jbe@97 1748 CREATE VIEW "unit_delegation" AS
jbe@97 1749 SELECT
jbe@97 1750 "unit"."id" AS "unit_id",
jbe@97 1751 "delegation"."id",
jbe@97 1752 "delegation"."truster_id",
jbe@97 1753 "delegation"."trustee_id",
jbe@97 1754 "delegation"."scope"
jbe@97 1755 FROM "unit"
jbe@97 1756 JOIN "delegation"
jbe@97 1757 ON "delegation"."unit_id" = "unit"."id"
jbe@97 1758 JOIN "member"
jbe@97 1759 ON "delegation"."truster_id" = "member"."id"
jbe@97 1760 JOIN "privilege"
jbe@97 1761 ON "delegation"."unit_id" = "privilege"."unit_id"
jbe@97 1762 AND "delegation"."truster_id" = "privilege"."member_id"
jbe@97 1763 WHERE "member"."active" AND "privilege"."voting_right";
jbe@97 1764
jbe@97 1765 COMMENT ON VIEW "unit_delegation" IS 'Unit delegations where trusters are active and have voting right';
jbe@5 1766
jbe@5 1767
jbe@5 1768 CREATE VIEW "area_delegation" AS
jbe@70 1769 SELECT DISTINCT ON ("area"."id", "delegation"."truster_id")
jbe@70 1770 "area"."id" AS "area_id",
jbe@70 1771 "delegation"."id",
jbe@70 1772 "delegation"."truster_id",
jbe@70 1773 "delegation"."trustee_id",
jbe@70 1774 "delegation"."scope"
jbe@97 1775 FROM "area"
jbe@97 1776 JOIN "delegation"
jbe@97 1777 ON "delegation"."unit_id" = "area"."unit_id"
jbe@97 1778 OR "delegation"."area_id" = "area"."id"
jbe@97 1779 JOIN "member"
jbe@97 1780 ON "delegation"."truster_id" = "member"."id"
jbe@97 1781 JOIN "privilege"
jbe@97 1782 ON "area"."unit_id" = "privilege"."unit_id"
jbe@97 1783 AND "delegation"."truster_id" = "privilege"."member_id"
jbe@97 1784 WHERE "member"."active" AND "privilege"."voting_right"
jbe@70 1785 ORDER BY
jbe@70 1786 "area"."id",
jbe@70 1787 "delegation"."truster_id",
jbe@70 1788 "delegation"."scope" DESC;
jbe@70 1789
jbe@97 1790 COMMENT ON VIEW "area_delegation" IS 'Area delegations where trusters are active and have voting right';
jbe@5 1791
jbe@5 1792
jbe@5 1793 CREATE VIEW "issue_delegation" AS
jbe@70 1794 SELECT DISTINCT ON ("issue"."id", "delegation"."truster_id")
jbe@70 1795 "issue"."id" AS "issue_id",
jbe@70 1796 "delegation"."id",
jbe@70 1797 "delegation"."truster_id",
jbe@70 1798 "delegation"."trustee_id",
jbe@70 1799 "delegation"."scope"
jbe@97 1800 FROM "issue"
jbe@97 1801 JOIN "area"
jbe@97 1802 ON "area"."id" = "issue"."area_id"
jbe@97 1803 JOIN "delegation"
jbe@97 1804 ON "delegation"."unit_id" = "area"."unit_id"
jbe@97 1805 OR "delegation"."area_id" = "area"."id"
jbe@97 1806 OR "delegation"."issue_id" = "issue"."id"
jbe@97 1807 JOIN "member"
jbe@97 1808 ON "delegation"."truster_id" = "member"."id"
jbe@97 1809 JOIN "privilege"
jbe@97 1810 ON "area"."unit_id" = "privilege"."unit_id"
jbe@97 1811 AND "delegation"."truster_id" = "privilege"."member_id"
jbe@97 1812 WHERE "member"."active" AND "privilege"."voting_right"
jbe@70 1813 ORDER BY
jbe@70 1814 "issue"."id",
jbe@70 1815 "delegation"."truster_id",
jbe@70 1816 "delegation"."scope" DESC;
jbe@70 1817
jbe@97 1818 COMMENT ON VIEW "issue_delegation" IS 'Issue delegations where trusters are active and have voting right';
jbe@5 1819
jbe@5 1820
jbe@5 1821 CREATE FUNCTION "membership_weight_with_skipping"
jbe@5 1822 ( "area_id_p" "area"."id"%TYPE,
jbe@5 1823 "member_id_p" "member"."id"%TYPE,
jbe@5 1824 "skip_member_ids_p" INT4[] ) -- "member"."id"%TYPE[]
jbe@5 1825 RETURNS INT4
jbe@5 1826 LANGUAGE 'plpgsql' STABLE AS $$
jbe@5 1827 DECLARE
jbe@5 1828 "sum_v" INT4;
jbe@5 1829 "delegation_row" "area_delegation"%ROWTYPE;
jbe@5 1830 BEGIN
jbe@5 1831 "sum_v" := 1;
jbe@5 1832 FOR "delegation_row" IN
jbe@5 1833 SELECT "area_delegation".*
jbe@5 1834 FROM "area_delegation" LEFT JOIN "membership"
jbe@5 1835 ON "membership"."area_id" = "area_id_p"
jbe@5 1836 AND "membership"."member_id" = "area_delegation"."truster_id"
jbe@5 1837 WHERE "area_delegation"."area_id" = "area_id_p"
jbe@5 1838 AND "area_delegation"."trustee_id" = "member_id_p"
jbe@5 1839 AND "membership"."member_id" ISNULL
jbe@5 1840 LOOP
jbe@5 1841 IF NOT
jbe@5 1842 "skip_member_ids_p" @> ARRAY["delegation_row"."truster_id"]
jbe@5 1843 THEN
jbe@5 1844 "sum_v" := "sum_v" + "membership_weight_with_skipping"(
jbe@5 1845 "area_id_p",
jbe@5 1846 "delegation_row"."truster_id",
jbe@5 1847 "skip_member_ids_p" || "delegation_row"."truster_id"
jbe@5 1848 );
jbe@5 1849 END IF;
jbe@5 1850 END LOOP;
jbe@5 1851 RETURN "sum_v";
jbe@5 1852 END;
jbe@5 1853 $$;
jbe@5 1854
jbe@8 1855 COMMENT ON FUNCTION "membership_weight_with_skipping"
jbe@8 1856 ( "area"."id"%TYPE,
jbe@8 1857 "member"."id"%TYPE,
jbe@8 1858 INT4[] )
jbe@8 1859 IS 'Helper function for "membership_weight" function';
jbe@8 1860
jbe@8 1861
jbe@5 1862 CREATE FUNCTION "membership_weight"
jbe@5 1863 ( "area_id_p" "area"."id"%TYPE,
jbe@5 1864 "member_id_p" "member"."id"%TYPE ) -- "member"."id"%TYPE[]
jbe@5 1865 RETURNS INT4
jbe@5 1866 LANGUAGE 'plpgsql' STABLE AS $$
jbe@5 1867 BEGIN
jbe@5 1868 RETURN "membership_weight_with_skipping"(
jbe@5 1869 "area_id_p",
jbe@5 1870 "member_id_p",
jbe@5 1871 ARRAY["member_id_p"]
jbe@5 1872 );
jbe@5 1873 END;
jbe@5 1874 $$;
jbe@5 1875
jbe@8 1876 COMMENT ON FUNCTION "membership_weight"
jbe@8 1877 ( "area"."id"%TYPE,
jbe@8 1878 "member"."id"%TYPE )
jbe@8 1879 IS 'Calculates the potential voting weight of a member in a given area';
jbe@8 1880
jbe@5 1881
jbe@4 1882 CREATE VIEW "member_count_view" AS
jbe@5 1883 SELECT count(1) AS "total_count" FROM "member" WHERE "active";
jbe@4 1884
jbe@4 1885 COMMENT ON VIEW "member_count_view" IS 'View used to update "member_count" table';
jbe@4 1886
jbe@4 1887
jbe@97 1888 CREATE VIEW "unit_member_count" AS
jbe@97 1889 SELECT
jbe@97 1890 "unit"."id" AS "unit_id",
jbe@248 1891 count("member"."id") AS "member_count"
jbe@97 1892 FROM "unit"
jbe@97 1893 LEFT JOIN "privilege"
jbe@97 1894 ON "privilege"."unit_id" = "unit"."id"
jbe@97 1895 AND "privilege"."voting_right"
jbe@97 1896 LEFT JOIN "member"
jbe@97 1897 ON "member"."id" = "privilege"."member_id"
jbe@97 1898 AND "member"."active"
jbe@97 1899 GROUP BY "unit"."id";
jbe@97 1900
jbe@97 1901 COMMENT ON VIEW "unit_member_count" IS 'View used to update "member_count" column of "unit" table';
jbe@97 1902
jbe@97 1903
jbe@4 1904 CREATE VIEW "area_member_count" AS
jbe@5 1905 SELECT
jbe@5 1906 "area"."id" AS "area_id",
jbe@5 1907 count("member"."id") AS "direct_member_count",
jbe@5 1908 coalesce(
jbe@5 1909 sum(
jbe@5 1910 CASE WHEN "member"."id" NOTNULL THEN
jbe@5 1911 "membership_weight"("area"."id", "member"."id")
jbe@5 1912 ELSE 0 END
jbe@5 1913 )
jbe@169 1914 ) AS "member_weight"
jbe@4 1915 FROM "area"
jbe@4 1916 LEFT JOIN "membership"
jbe@4 1917 ON "area"."id" = "membership"."area_id"
jbe@97 1918 LEFT JOIN "privilege"
jbe@97 1919 ON "privilege"."unit_id" = "area"."unit_id"
jbe@97 1920 AND "privilege"."member_id" = "membership"."member_id"
jbe@97 1921 AND "privilege"."voting_right"
jbe@4 1922 LEFT JOIN "member"
jbe@97 1923 ON "member"."id" = "privilege"."member_id" -- NOTE: no membership here!
jbe@4 1924 AND "member"."active"
jbe@4 1925 GROUP BY "area"."id";
jbe@4 1926
jbe@169 1927 COMMENT ON VIEW "area_member_count" IS 'View used to update "direct_member_count" and "member_weight" columns of table "area"';
jbe@4 1928
jbe@4 1929
jbe@9 1930 CREATE VIEW "opening_draft" AS
jbe@9 1931 SELECT "draft".* FROM (
jbe@9 1932 SELECT
jbe@9 1933 "initiative"."id" AS "initiative_id",
jbe@9 1934 min("draft"."id") AS "draft_id"
jbe@9 1935 FROM "initiative" JOIN "draft"
jbe@9 1936 ON "initiative"."id" = "draft"."initiative_id"
jbe@9 1937 GROUP BY "initiative"."id"
jbe@9 1938 ) AS "subquery"
jbe@9 1939 JOIN "draft" ON "subquery"."draft_id" = "draft"."id";
jbe@9 1940
jbe@9 1941 COMMENT ON VIEW "opening_draft" IS 'First drafts of all initiatives';
jbe@9 1942
jbe@9 1943
jbe@0 1944 CREATE VIEW "current_draft" AS
jbe@0 1945 SELECT "draft".* FROM (
jbe@0 1946 SELECT
jbe@0 1947 "initiative"."id" AS "initiative_id",
jbe@0 1948 max("draft"."id") AS "draft_id"
jbe@0 1949 FROM "initiative" JOIN "draft"
jbe@0 1950 ON "initiative"."id" = "draft"."initiative_id"
jbe@0 1951 GROUP BY "initiative"."id"
jbe@0 1952 ) AS "subquery"
jbe@0 1953 JOIN "draft" ON "subquery"."draft_id" = "draft"."id";
jbe@0 1954
jbe@0 1955 COMMENT ON VIEW "current_draft" IS 'All latest drafts for each initiative';
jbe@0 1956
jbe@0 1957
jbe@0 1958 CREATE VIEW "critical_opinion" AS
jbe@0 1959 SELECT * FROM "opinion"
jbe@0 1960 WHERE ("degree" = 2 AND "fulfilled" = FALSE)
jbe@0 1961 OR ("degree" = -2 AND "fulfilled" = TRUE);
jbe@0 1962
jbe@0 1963 COMMENT ON VIEW "critical_opinion" IS 'Opinions currently causing dissatisfaction';
jbe@0 1964
jbe@0 1965
jbe@126 1966 CREATE VIEW "battle_participant" AS
jbe@126 1967 SELECT "initiative"."id", "initiative"."issue_id"
jbe@126 1968 FROM "issue" JOIN "initiative"
jbe@126 1969 ON "issue"."id" = "initiative"."issue_id"
jbe@126 1970 WHERE "initiative"."admitted"
jbe@126 1971 UNION ALL
jbe@126 1972 SELECT NULL, "id" AS "issue_id"
jbe@126 1973 FROM "issue";
jbe@126 1974
jbe@126 1975 COMMENT ON VIEW "battle_participant" IS 'Helper view for "battle_view" containing admitted initiatives plus virtual "status-quo" initiative denoted by NULL reference';
jbe@126 1976
jbe@126 1977
jbe@61 1978 CREATE VIEW "battle_view" AS
jbe@0 1979 SELECT
jbe@0 1980 "issue"."id" AS "issue_id",
jbe@10 1981 "winning_initiative"."id" AS "winning_initiative_id",
jbe@10 1982 "losing_initiative"."id" AS "losing_initiative_id",
jbe@0 1983 sum(
jbe@0 1984 CASE WHEN
jbe@0 1985 coalesce("better_vote"."grade", 0) >
jbe@0 1986 coalesce("worse_vote"."grade", 0)
jbe@0 1987 THEN "direct_voter"."weight" ELSE 0 END
jbe@0 1988 ) AS "count"
jbe@0 1989 FROM "issue"
jbe@0 1990 LEFT JOIN "direct_voter"
jbe@0 1991 ON "issue"."id" = "direct_voter"."issue_id"
jbe@126 1992 JOIN "battle_participant" AS "winning_initiative"
jbe@10 1993 ON "issue"."id" = "winning_initiative"."issue_id"
jbe@126 1994 JOIN "battle_participant" AS "losing_initiative"
jbe@10 1995 ON "issue"."id" = "losing_initiative"."issue_id"
jbe@0 1996 LEFT JOIN "vote" AS "better_vote"
jbe@10 1997 ON "direct_voter"."member_id" = "better_vote"."member_id"
jbe@10 1998 AND "winning_initiative"."id" = "better_vote"."initiative_id"
jbe@0 1999 LEFT JOIN "vote" AS "worse_vote"
jbe@10 2000 ON "direct_voter"."member_id" = "worse_vote"."member_id"
jbe@10 2001 AND "losing_initiative"."id" = "worse_vote"."initiative_id"
jbe@61 2002 WHERE "issue"."closed" NOTNULL
jbe@61 2003 AND "issue"."cleaned" ISNULL
jbe@126 2004 AND (
jbe@126 2005 "winning_initiative"."id" != "losing_initiative"."id" OR
jbe@126 2006 ( ("winning_initiative"."id" NOTNULL AND "losing_initiative"."id" ISNULL) OR
jbe@126 2007 ("winning_initiative"."id" ISNULL AND "losing_initiative"."id" NOTNULL) ) )
jbe@0 2008 GROUP BY
jbe@0 2009 "issue"."id",
jbe@10 2010 "winning_initiative"."id",
jbe@10 2011 "losing_initiative"."id";
jbe@0 2012
jbe@126 2013 COMMENT ON VIEW "battle_view" IS 'Number of members preferring one initiative (or status-quo) to another initiative (or status-quo); Used to fill "battle" table';
jbe@1 2014
jbe@1 2015
jbe@235 2016 CREATE VIEW "expired_session" AS
jbe@235 2017 SELECT * FROM "session" WHERE now() > "expiry";
jbe@235 2018
jbe@235 2019 CREATE RULE "delete" AS ON DELETE TO "expired_session" DO INSTEAD
jbe@235 2020 DELETE FROM "session" WHERE "ident" = OLD."ident";
jbe@235 2021
jbe@235 2022 COMMENT ON VIEW "expired_session" IS 'View containing all expired sessions where DELETE is possible';
jbe@235 2023 COMMENT ON RULE "delete" ON "expired_session" IS 'Rule allowing DELETE on rows in "expired_session" view, i.e. DELETE FROM "expired_session"';
jbe@235 2024
jbe@235 2025
jbe@0 2026 CREATE VIEW "open_issue" AS
jbe@0 2027 SELECT * FROM "issue" WHERE "closed" ISNULL;
jbe@0 2028
jbe@0 2029 COMMENT ON VIEW "open_issue" IS 'All open issues';
jbe@0 2030
jbe@0 2031
jbe@0 2032 CREATE VIEW "issue_with_ranks_missing" AS
jbe@0 2033 SELECT * FROM "issue"
jbe@3 2034 WHERE "fully_frozen" NOTNULL
jbe@0 2035 AND "closed" NOTNULL
jbe@0 2036 AND "ranks_available" = FALSE;
jbe@0 2037
jbe@0 2038 COMMENT ON VIEW "issue_with_ranks_missing" IS 'Issues where voting was finished, but no ranks have been calculated yet';
jbe@0 2039
jbe@0 2040
jbe@9 2041 CREATE VIEW "member_contingent" AS
jbe@9 2042 SELECT
jbe@9 2043 "member"."id" AS "member_id",
jbe@9 2044 "contingent"."time_frame",
jbe@9 2045 CASE WHEN "contingent"."text_entry_limit" NOTNULL THEN
jbe@9 2046 (
jbe@9 2047 SELECT count(1) FROM "draft"
jbe@9 2048 WHERE "draft"."author_id" = "member"."id"
jbe@9 2049 AND "draft"."created" > now() - "contingent"."time_frame"
jbe@9 2050 ) + (
jbe@9 2051 SELECT count(1) FROM "suggestion"
jbe@9 2052 WHERE "suggestion"."author_id" = "member"."id"
jbe@9 2053 AND "suggestion"."created" > now() - "contingent"."time_frame"
jbe@9 2054 )
jbe@9 2055 ELSE NULL END AS "text_entry_count",
jbe@9 2056 "contingent"."text_entry_limit",
jbe@9 2057 CASE WHEN "contingent"."initiative_limit" NOTNULL THEN (
jbe@9 2058 SELECT count(1) FROM "opening_draft"
jbe@9 2059 WHERE "opening_draft"."author_id" = "member"."id"
jbe@9 2060 AND "opening_draft"."created" > now() - "contingent"."time_frame"
jbe@9 2061 ) ELSE NULL END AS "initiative_count",
jbe@9 2062 "contingent"."initiative_limit"
jbe@9 2063 FROM "member" CROSS JOIN "contingent";
jbe@9 2064
jbe@9 2065 COMMENT ON VIEW "member_contingent" IS 'Actual counts of text entries and initiatives are calculated per member for each limit in the "contingent" table.';
jbe@9 2066
jbe@9 2067 COMMENT ON COLUMN "member_contingent"."text_entry_count" IS 'Only calculated when "text_entry_limit" is not null in the same row';
jbe@9 2068 COMMENT ON COLUMN "member_contingent"."initiative_count" IS 'Only calculated when "initiative_limit" is not null in the same row';
jbe@9 2069
jbe@9 2070
jbe@9 2071 CREATE VIEW "member_contingent_left" AS
jbe@9 2072 SELECT
jbe@9 2073 "member_id",
jbe@9 2074 max("text_entry_limit" - "text_entry_count") AS "text_entries_left",
jbe@9 2075 max("initiative_limit" - "initiative_count") AS "initiatives_left"
jbe@9 2076 FROM "member_contingent" GROUP BY "member_id";
jbe@9 2077
jbe@9 2078 COMMENT ON VIEW "member_contingent_left" IS 'Amount of text entries or initiatives which can be posted now instantly by a member. This view should be used by a frontend to determine, if the contingent for posting is exhausted.';
jbe@9 2079
jbe@9 2080
jbe@113 2081 CREATE VIEW "event_seen_by_member" AS
jbe@113 2082 SELECT
jbe@113 2083 "member"."id" AS "seen_by_member_id",
jbe@113 2084 CASE WHEN "event"."state" IN (
jbe@113 2085 'voting',
jbe@113 2086 'finished_without_winner',
jbe@113 2087 'finished_with_winner'
jbe@113 2088 ) THEN
jbe@113 2089 'voting'::"notify_level"
jbe@113 2090 ELSE
jbe@113 2091 CASE WHEN "event"."state" IN (
jbe@113 2092 'verification',
jbe@113 2093 'canceled_after_revocation_during_verification',
jbe@113 2094 'canceled_no_initiative_admitted'
jbe@113 2095 ) THEN
jbe@113 2096 'verification'::"notify_level"
jbe@113 2097 ELSE
jbe@113 2098 CASE WHEN "event"."state" IN (
jbe@113 2099 'discussion',
jbe@113 2100 'canceled_after_revocation_during_discussion'
jbe@113 2101 ) THEN
jbe@113 2102 'discussion'::"notify_level"
jbe@113 2103 ELSE
jbe@113 2104 'all'::"notify_level"
jbe@113 2105 END
jbe@113 2106 END
jbe@113 2107 END AS "notify_level",
jbe@113 2108 "event".*
jbe@113 2109 FROM "member" CROSS JOIN "event"
jbe@113 2110 LEFT JOIN "issue"
jbe@113 2111 ON "event"."issue_id" = "issue"."id"
jbe@113 2112 LEFT JOIN "membership"
jbe@113 2113 ON "member"."id" = "membership"."member_id"
jbe@113 2114 AND "issue"."area_id" = "membership"."area_id"
jbe@113 2115 LEFT JOIN "interest"
jbe@113 2116 ON "member"."id" = "interest"."member_id"
jbe@113 2117 AND "event"."issue_id" = "interest"."issue_id"
jbe@113 2118 LEFT JOIN "supporter"
jbe@113 2119 ON "member"."id" = "supporter"."member_id"
jbe@113 2120 AND "event"."initiative_id" = "supporter"."initiative_id"
jbe@113 2121 LEFT JOIN "ignored_member"
jbe@113 2122 ON "member"."id" = "ignored_member"."member_id"
jbe@113 2123 AND "event"."member_id" = "ignored_member"."other_member_id"
jbe@113 2124 LEFT JOIN "ignored_initiative"
jbe@113 2125 ON "member"."id" = "ignored_initiative"."member_id"
jbe@113 2126 AND "event"."initiative_id" = "ignored_initiative"."initiative_id"
jbe@113 2127 WHERE (
jbe@113 2128 "supporter"."member_id" NOTNULL OR
jbe@113 2129 "interest"."member_id" NOTNULL OR
jbe@113 2130 ( "membership"."member_id" NOTNULL AND
jbe@113 2131 "event"."event" IN (
jbe@113 2132 'issue_state_changed',
jbe@113 2133 'initiative_created_in_new_issue',
jbe@113 2134 'initiative_created_in_existing_issue',
jbe@113 2135 'initiative_revoked' ) ) )
jbe@113 2136 AND "ignored_member"."member_id" ISNULL
jbe@113 2137 AND "ignored_initiative"."member_id" ISNULL;
jbe@113 2138
jbe@222 2139 COMMENT ON VIEW "event_seen_by_member" IS 'Events as seen by a member, depending on its memberships, interests and support, but ignoring members "notify_level"';
jbe@222 2140
jbe@222 2141
jbe@222 2142 CREATE VIEW "selected_event_seen_by_member" AS
jbe@113 2143 SELECT
jbe@113 2144 "member"."id" AS "seen_by_member_id",
jbe@222 2145 CASE WHEN "event"."state" IN (
jbe@222 2146 'voting',
jbe@222 2147 'finished_without_winner',
jbe@222 2148 'finished_with_winner'
jbe@222 2149 ) THEN
jbe@222 2150 'voting'::"notify_level"
jbe@222 2151 ELSE
jbe@222 2152 CASE WHEN "event"."state" IN (
jbe@222 2153 'verification',
jbe@222 2154 'canceled_after_revocation_during_verification',
jbe@222 2155 'canceled_no_initiative_admitted'
jbe@222 2156 ) THEN
jbe@222 2157 'verification'::"notify_level"
jbe@222 2158 ELSE
jbe@222 2159 CASE WHEN "event"."state" IN (
jbe@222 2160 'discussion',
jbe@222 2161 'canceled_after_revocation_during_discussion'
jbe@222 2162 ) THEN
jbe@222 2163 'discussion'::"notify_level"
jbe@222 2164 ELSE
jbe@222 2165 'all'::"notify_level"
jbe@222 2166 END
jbe@222 2167 END
jbe@222 2168 END AS "notify_level",
jbe@113 2169 "event".*
jbe@113 2170 FROM "member" CROSS JOIN "event"
jbe@113 2171 LEFT JOIN "issue"
jbe@113 2172 ON "event"."issue_id" = "issue"."id"
jbe@113 2173 LEFT JOIN "membership"
jbe@113 2174 ON "member"."id" = "membership"."member_id"
jbe@113 2175 AND "issue"."area_id" = "membership"."area_id"
jbe@113 2176 LEFT JOIN "interest"
jbe@113 2177 ON "member"."id" = "interest"."member_id"
jbe@113 2178 AND "event"."issue_id" = "interest"."issue_id"
jbe@113 2179 LEFT JOIN "supporter"
jbe@113 2180 ON "member"."id" = "supporter"."member_id"
jbe@113 2181 AND "event"."initiative_id" = "supporter"."initiative_id"
jbe@113 2182 LEFT JOIN "ignored_member"
jbe@113 2183 ON "member"."id" = "ignored_member"."member_id"
jbe@113 2184 AND "event"."member_id" = "ignored_member"."other_member_id"
jbe@113 2185 LEFT JOIN "ignored_initiative"
jbe@113 2186 ON "member"."id" = "ignored_initiative"."member_id"
jbe@113 2187 AND "event"."initiative_id" = "ignored_initiative"."initiative_id"
jbe@113 2188 WHERE (
jbe@113 2189 ( "member"."notify_level" >= 'all' ) OR
jbe@113 2190 ( "member"."notify_level" >= 'voting' AND
jbe@113 2191 "event"."state" IN (
jbe@113 2192 'voting',
jbe@113 2193 'finished_without_winner',
jbe@113 2194 'finished_with_winner' ) ) OR
jbe@113 2195 ( "member"."notify_level" >= 'verification' AND
jbe@113 2196 "event"."state" IN (
jbe@113 2197 'verification',
jbe@113 2198 'canceled_after_revocation_during_verification',
jbe@113 2199 'canceled_no_initiative_admitted' ) ) OR
jbe@113 2200 ( "member"."notify_level" >= 'discussion' AND
jbe@113 2201 "event"."state" IN (
jbe@113 2202 'discussion',
jbe@113 2203 'canceled_after_revocation_during_discussion' ) ) )
jbe@113 2204 AND (
jbe@113 2205 "supporter"."member_id" NOTNULL OR
jbe@113 2206 "interest"."member_id" NOTNULL OR
jbe@113 2207 ( "membership"."member_id" NOTNULL AND
jbe@113 2208 "event"."event" IN (
jbe@113 2209 'issue_state_changed',
jbe@113 2210 'initiative_created_in_new_issue',
jbe@113 2211 'initiative_created_in_existing_issue',
jbe@113 2212 'initiative_revoked' ) ) )
jbe@113 2213 AND "ignored_member"."member_id" ISNULL
jbe@113 2214 AND "ignored_initiative"."member_id" ISNULL;
jbe@113 2215
jbe@222 2216 COMMENT ON VIEW "selected_event_seen_by_member" IS 'Events as seen by a member, depending on its memberships, interests, support and members "notify_level"';
jbe@113 2217
jbe@113 2218
jbe@16 2219 CREATE TYPE "timeline_event" AS ENUM (
jbe@16 2220 'issue_created',
jbe@16 2221 'issue_canceled',
jbe@16 2222 'issue_accepted',
jbe@16 2223 'issue_half_frozen',
jbe@16 2224 'issue_finished_without_voting',
jbe@16 2225 'issue_voting_started',
jbe@16 2226 'issue_finished_after_voting',
jbe@16 2227 'initiative_created',
jbe@16 2228 'initiative_revoked',
jbe@16 2229 'draft_created',
jbe@16 2230 'suggestion_created');
jbe@16 2231
jbe@112 2232 COMMENT ON TYPE "timeline_event" IS 'Types of event in timeline tables (DEPRECATED)';
jbe@16 2233
jbe@16 2234
jbe@16 2235 CREATE VIEW "timeline_issue" AS
jbe@16 2236 SELECT
jbe@16 2237 "created" AS "occurrence",
jbe@16 2238 'issue_created'::"timeline_event" AS "event",
jbe@16 2239 "id" AS "issue_id"
jbe@16 2240 FROM "issue"
jbe@16 2241 UNION ALL
jbe@16 2242 SELECT
jbe@16 2243 "closed" AS "occurrence",
jbe@16 2244 'issue_canceled'::"timeline_event" AS "event",
jbe@16 2245 "id" AS "issue_id"
jbe@16 2246 FROM "issue" WHERE "closed" NOTNULL AND "fully_frozen" ISNULL
jbe@16 2247 UNION ALL
jbe@16 2248 SELECT
jbe@16 2249 "accepted" AS "occurrence",
jbe@16 2250 'issue_accepted'::"timeline_event" AS "event",
jbe@16 2251 "id" AS "issue_id"
jbe@16 2252 FROM "issue" WHERE "accepted" NOTNULL
jbe@16 2253 UNION ALL
jbe@16 2254 SELECT
jbe@16 2255 "half_frozen" AS "occurrence",
jbe@16 2256 'issue_half_frozen'::"timeline_event" AS "event",
jbe@16 2257 "id" AS "issue_id"
jbe@16 2258 FROM "issue" WHERE "half_frozen" NOTNULL
jbe@16 2259 UNION ALL
jbe@16 2260 SELECT
jbe@16 2261 "fully_frozen" AS "occurrence",
jbe@16 2262 'issue_voting_started'::"timeline_event" AS "event",
jbe@16 2263 "id" AS "issue_id"
jbe@16 2264 FROM "issue"
jbe@17 2265 WHERE "fully_frozen" NOTNULL
jbe@17 2266 AND ("closed" ISNULL OR "closed" != "fully_frozen")
jbe@16 2267 UNION ALL
jbe@16 2268 SELECT
jbe@16 2269 "closed" AS "occurrence",
jbe@16 2270 CASE WHEN "fully_frozen" = "closed" THEN
jbe@16 2271 'issue_finished_without_voting'::"timeline_event"
jbe@16 2272 ELSE
jbe@16 2273 'issue_finished_after_voting'::"timeline_event"
jbe@16 2274 END AS "event",
jbe@16 2275 "id" AS "issue_id"
jbe@16 2276 FROM "issue" WHERE "closed" NOTNULL AND "fully_frozen" NOTNULL;
jbe@16 2277
jbe@112 2278 COMMENT ON VIEW "timeline_issue" IS 'Helper view for "timeline" view (DEPRECATED)';
jbe@16 2279
jbe@16 2280
jbe@16 2281 CREATE VIEW "timeline_initiative" AS
jbe@16 2282 SELECT
jbe@16 2283 "created" AS "occurrence",
jbe@16 2284 'initiative_created'::"timeline_event" AS "event",
jbe@16 2285 "id" AS "initiative_id"
jbe@16 2286 FROM "initiative"
jbe@16 2287 UNION ALL
jbe@16 2288 SELECT
jbe@16 2289 "revoked" AS "occurrence",
jbe@16 2290 'initiative_revoked'::"timeline_event" AS "event",
jbe@16 2291 "id" AS "initiative_id"
jbe@16 2292 FROM "initiative" WHERE "revoked" NOTNULL;
jbe@16 2293
jbe@112 2294 COMMENT ON VIEW "timeline_initiative" IS 'Helper view for "timeline" view (DEPRECATED)';
jbe@16 2295
jbe@16 2296
jbe@16 2297 CREATE VIEW "timeline_draft" AS
jbe@16 2298 SELECT
jbe@16 2299 "created" AS "occurrence",
jbe@16 2300 'draft_created'::"timeline_event" AS "event",
jbe@16 2301 "id" AS "draft_id"
jbe@16 2302 FROM "draft";
jbe@16 2303
jbe@112 2304 COMMENT ON VIEW "timeline_draft" IS 'Helper view for "timeline" view (DEPRECATED)';
jbe@16 2305
jbe@16 2306
jbe@16 2307 CREATE VIEW "timeline_suggestion" AS
jbe@16 2308 SELECT
jbe@16 2309 "created" AS "occurrence",
jbe@16 2310 'suggestion_created'::"timeline_event" AS "event",
jbe@16 2311 "id" AS "suggestion_id"
jbe@16 2312 FROM "suggestion";
jbe@16 2313
jbe@112 2314 COMMENT ON VIEW "timeline_suggestion" IS 'Helper view for "timeline" view (DEPRECATED)';
jbe@16 2315
jbe@16 2316
jbe@16 2317 CREATE VIEW "timeline" AS
jbe@16 2318 SELECT
jbe@16 2319 "occurrence",
jbe@16 2320 "event",
jbe@16 2321 "issue_id",
jbe@16 2322 NULL AS "initiative_id",
jbe@16 2323 NULL::INT8 AS "draft_id", -- TODO: Why do we need a type-cast here? Is this due to 32 bit architecture?
jbe@16 2324 NULL::INT8 AS "suggestion_id"
jbe@16 2325 FROM "timeline_issue"
jbe@16 2326 UNION ALL
jbe@16 2327 SELECT
jbe@16 2328 "occurrence",
jbe@16 2329 "event",
jbe@16 2330 NULL AS "issue_id",
jbe@16 2331 "initiative_id",
jbe@16 2332 NULL AS "draft_id",
jbe@16 2333 NULL AS "suggestion_id"
jbe@16 2334 FROM "timeline_initiative"
jbe@16 2335 UNION ALL
jbe@16 2336 SELECT
jbe@16 2337 "occurrence",
jbe@16 2338 "event",
jbe@16 2339 NULL AS "issue_id",
jbe@16 2340 NULL AS "initiative_id",
jbe@16 2341 "draft_id",
jbe@16 2342 NULL AS "suggestion_id"
jbe@16 2343 FROM "timeline_draft"
jbe@16 2344 UNION ALL
jbe@16 2345 SELECT
jbe@16 2346 "occurrence",
jbe@16 2347 "event",
jbe@16 2348 NULL AS "issue_id",
jbe@16 2349 NULL AS "initiative_id",
jbe@16 2350 NULL AS "draft_id",
jbe@16 2351 "suggestion_id"
jbe@16 2352 FROM "timeline_suggestion";
jbe@16 2353
jbe@112 2354 COMMENT ON VIEW "timeline" IS 'Aggregation of different events in the system (DEPRECATED)';
jbe@16 2355
jbe@16 2356
jbe@0 2357
jbe@242 2358 ------------------------------------------------------
jbe@242 2359 -- Row set returning function for delegation chains --
jbe@242 2360 ------------------------------------------------------
jbe@5 2361
jbe@5 2362
jbe@5 2363 CREATE TYPE "delegation_chain_loop_tag" AS ENUM
jbe@5 2364 ('first', 'intermediate', 'last', 'repetition');
jbe@5 2365
jbe@5 2366 COMMENT ON TYPE "delegation_chain_loop_tag" IS 'Type for loop tags in "delegation_chain_row" type';
jbe@5 2367
jbe@5 2368
jbe@5 2369 CREATE TYPE "delegation_chain_row" AS (
jbe@5 2370 "index" INT4,
jbe@5 2371 "member_id" INT4,
jbe@97 2372 "member_valid" BOOLEAN,
jbe@5 2373 "participation" BOOLEAN,
jbe@5 2374 "overridden" BOOLEAN,
jbe@5 2375 "scope_in" "delegation_scope",
jbe@5 2376 "scope_out" "delegation_scope",
jbe@86 2377 "disabled_out" BOOLEAN,
jbe@5 2378 "loop" "delegation_chain_loop_tag" );
jbe@5 2379
jbe@243 2380 COMMENT ON TYPE "delegation_chain_row" IS 'Type of rows returned by "delegation_chain" function';
jbe@5 2381
jbe@5 2382 COMMENT ON COLUMN "delegation_chain_row"."index" IS 'Index starting with 0 and counting up';
jbe@5 2383 COMMENT ON COLUMN "delegation_chain_row"."participation" IS 'In case of delegation chains for issues: interest, for areas: membership, for global delegation chains: always null';
jbe@5 2384 COMMENT ON COLUMN "delegation_chain_row"."overridden" IS 'True, if an entry with lower index has "participation" set to true';
jbe@5 2385 COMMENT ON COLUMN "delegation_chain_row"."scope_in" IS 'Scope of used incoming delegation';
jbe@5 2386 COMMENT ON COLUMN "delegation_chain_row"."scope_out" IS 'Scope of used outgoing delegation';
jbe@86 2387 COMMENT ON COLUMN "delegation_chain_row"."disabled_out" IS 'Outgoing delegation is explicitly disabled by a delegation with trustee_id set to NULL';
jbe@5 2388 COMMENT ON COLUMN "delegation_chain_row"."loop" IS 'Not null, if member is part of a loop, see "delegation_chain_loop_tag" type';
jbe@5 2389
jbe@5 2390
jbe@242 2391 CREATE FUNCTION "delegation_chain_for_closed_issue"
jbe@242 2392 ( "member_id_p" "member"."id"%TYPE,
jbe@242 2393 "issue_id_p" "issue"."id"%TYPE )
jbe@242 2394 RETURNS SETOF "delegation_chain_row"
jbe@242 2395 LANGUAGE 'plpgsql' STABLE AS $$
jbe@242 2396 DECLARE
jbe@242 2397 "output_row" "delegation_chain_row";
jbe@242 2398 "direct_voter_row" "direct_voter"%ROWTYPE;
jbe@242 2399 "delegating_voter_row" "delegating_voter"%ROWTYPE;
jbe@242 2400 BEGIN
jbe@242 2401 "output_row"."index" := 0;
jbe@242 2402 "output_row"."member_id" := "member_id_p";
jbe@242 2403 "output_row"."member_valid" := TRUE;
jbe@242 2404 "output_row"."participation" := FALSE;
jbe@242 2405 "output_row"."overridden" := FALSE;
jbe@242 2406 "output_row"."disabled_out" := FALSE;
jbe@242 2407 LOOP
jbe@242 2408 SELECT INTO "direct_voter_row" * FROM "direct_voter"
jbe@242 2409 WHERE "issue_id" = "issue_id_p"
jbe@242 2410 AND "member_id" = "output_row"."member_id";
jbe@242 2411 IF "direct_voter_row"."member_id" NOTNULL THEN
jbe@242 2412 "output_row"."participation" := TRUE;
jbe@242 2413 "output_row"."scope_out" := NULL;
jbe@242 2414 "output_row"."disabled_out" := NULL;
jbe@242 2415 RETURN NEXT "output_row";
jbe@242 2416 RETURN;
jbe@242 2417 END IF;
jbe@242 2418 SELECT INTO "delegating_voter_row" * FROM "delegating_voter"
jbe@242 2419 WHERE "issue_id" = "issue_id_p"
jbe@242 2420 AND "member_id" = "output_row"."member_id";
jbe@242 2421 IF "delegating_voter_row"."member_id" ISNULL THEN
jbe@242 2422 RETURN;
jbe@242 2423 END IF;
jbe@242 2424 "output_row"."scope_out" := "delegating_voter_row"."scope";
jbe@242 2425 RETURN NEXT "output_row";
jbe@242 2426 "output_row"."member_id" := "delegating_voter_row"."delegate_member_ids"[1];
jbe@242 2427 "output_row"."scope_in" := "output_row"."scope_out";
jbe@242 2428 END LOOP;
jbe@242 2429 END;
jbe@242 2430 $$;
jbe@242 2431
jbe@242 2432 COMMENT ON FUNCTION "delegation_chain_for_closed_issue"
jbe@242 2433 ( "member"."id"%TYPE,
jbe@242 2434 "member"."id"%TYPE )
jbe@242 2435 IS 'Helper function for "delegation_chain" function, handling the special case of closed issues after voting';
jbe@242 2436
jbe@242 2437
jbe@5 2438 CREATE FUNCTION "delegation_chain"
jbe@5 2439 ( "member_id_p" "member"."id"%TYPE,
jbe@97 2440 "unit_id_p" "unit"."id"%TYPE,
jbe@5 2441 "area_id_p" "area"."id"%TYPE,
jbe@5 2442 "issue_id_p" "issue"."id"%TYPE,
jbe@242 2443 "simulate_trustee_id_p" "member"."id"%TYPE DEFAULT NULL )
jbe@5 2444 RETURNS SETOF "delegation_chain_row"
jbe@5 2445 LANGUAGE 'plpgsql' STABLE AS $$
jbe@5 2446 DECLARE
jbe@97 2447 "scope_v" "delegation_scope";
jbe@97 2448 "unit_id_v" "unit"."id"%TYPE;
jbe@97 2449 "area_id_v" "area"."id"%TYPE;
jbe@241 2450 "issue_row" "issue"%ROWTYPE;
jbe@5 2451 "visited_member_ids" INT4[]; -- "member"."id"%TYPE[]
jbe@5 2452 "loop_member_id_v" "member"."id"%TYPE;
jbe@5 2453 "output_row" "delegation_chain_row";
jbe@5 2454 "output_rows" "delegation_chain_row"[];
jbe@5 2455 "delegation_row" "delegation"%ROWTYPE;
jbe@5 2456 "row_count" INT4;
jbe@5 2457 "i" INT4;
jbe@5 2458 "loop_v" BOOLEAN;
jbe@5 2459 BEGIN
jbe@97 2460 IF
jbe@97 2461 "unit_id_p" NOTNULL AND
jbe@97 2462 "area_id_p" ISNULL AND
jbe@97 2463 "issue_id_p" ISNULL
jbe@97 2464 THEN
jbe@97 2465 "scope_v" := 'unit';
jbe@97 2466 "unit_id_v" := "unit_id_p";
jbe@97 2467 ELSIF
jbe@97 2468 "unit_id_p" ISNULL AND
jbe@97 2469 "area_id_p" NOTNULL AND
jbe@97 2470 "issue_id_p" ISNULL
jbe@97 2471 THEN
jbe@97 2472 "scope_v" := 'area';
jbe@97 2473 "area_id_v" := "area_id_p";
jbe@97 2474 SELECT "unit_id" INTO "unit_id_v"
jbe@97 2475 FROM "area" WHERE "id" = "area_id_v";
jbe@97 2476 ELSIF
jbe@97 2477 "unit_id_p" ISNULL AND
jbe@97 2478 "area_id_p" ISNULL AND
jbe@97 2479 "issue_id_p" NOTNULL
jbe@97 2480 THEN
jbe@242 2481 SELECT INTO "issue_row" * FROM "issue" WHERE "id" = "issue_id_p";
jbe@242 2482 IF "issue_row"."id" ISNULL THEN
jbe@242 2483 RETURN;
jbe@242 2484 END IF;
jbe@242 2485 IF "issue_row"."closed" NOTNULL THEN
jbe@242 2486 IF "simulate_trustee_id_p" NOTNULL THEN
jbe@242 2487 RAISE EXCEPTION 'Tried to simulate delegation chain for closed issue.';
jbe@242 2488 END IF;
jbe@242 2489 FOR "output_row" IN
jbe@242 2490 SELECT * FROM
jbe@242 2491 "delegation_chain_for_closed_issue"("member_id_p", "issue_id_p")
jbe@242 2492 LOOP
jbe@242 2493 RETURN NEXT "output_row";
jbe@242 2494 END LOOP;
jbe@242 2495 RETURN;
jbe@242 2496 END IF;
jbe@97 2497 "scope_v" := 'issue';
jbe@97 2498 SELECT "area_id" INTO "area_id_v"
jbe@97 2499 FROM "issue" WHERE "id" = "issue_id_p";
jbe@97 2500 SELECT "unit_id" INTO "unit_id_v"
jbe@97 2501 FROM "area" WHERE "id" = "area_id_v";
jbe@97 2502 ELSE
jbe@97 2503 RAISE EXCEPTION 'Exactly one of unit_id_p, area_id_p, or issue_id_p must be NOTNULL.';
jbe@97 2504 END IF;
jbe@5 2505 "visited_member_ids" := '{}';
jbe@5 2506 "loop_member_id_v" := NULL;
jbe@5 2507 "output_rows" := '{}';
jbe@5 2508 "output_row"."index" := 0;
jbe@5 2509 "output_row"."member_id" := "member_id_p";
jbe@97 2510 "output_row"."member_valid" := TRUE;
jbe@5 2511 "output_row"."participation" := FALSE;
jbe@5 2512 "output_row"."overridden" := FALSE;
jbe@86 2513 "output_row"."disabled_out" := FALSE;
jbe@5 2514 "output_row"."scope_out" := NULL;
jbe@5 2515 LOOP
jbe@5 2516 IF "visited_member_ids" @> ARRAY["output_row"."member_id"] THEN
jbe@5 2517 "loop_member_id_v" := "output_row"."member_id";
jbe@5 2518 ELSE
jbe@5 2519 "visited_member_ids" :=
jbe@5 2520 "visited_member_ids" || "output_row"."member_id";
jbe@5 2521 END IF;
jbe@241 2522 IF "output_row"."participation" ISNULL THEN
jbe@241 2523 "output_row"."overridden" := NULL;
jbe@241 2524 ELSIF "output_row"."participation" THEN
jbe@5 2525 "output_row"."overridden" := TRUE;
jbe@5 2526 END IF;
jbe@5 2527 "output_row"."scope_in" := "output_row"."scope_out";
jbe@5 2528 IF EXISTS (
jbe@97 2529 SELECT NULL FROM "member" JOIN "privilege"
jbe@97 2530 ON "privilege"."member_id" = "member"."id"
jbe@97 2531 AND "privilege"."unit_id" = "unit_id_v"
jbe@97 2532 WHERE "id" = "output_row"."member_id"
jbe@97 2533 AND "member"."active" AND "privilege"."voting_right"
jbe@5 2534 ) THEN
jbe@97 2535 IF "scope_v" = 'unit' THEN
jbe@5 2536 SELECT * INTO "delegation_row" FROM "delegation"
jbe@5 2537 WHERE "truster_id" = "output_row"."member_id"
jbe@97 2538 AND "unit_id" = "unit_id_v";
jbe@97 2539 ELSIF "scope_v" = 'area' THEN
jbe@5 2540 "output_row"."participation" := EXISTS (
jbe@5 2541 SELECT NULL FROM "membership"
jbe@5 2542 WHERE "area_id" = "area_id_p"
jbe@5 2543 AND "member_id" = "output_row"."member_id"
jbe@5 2544 );
jbe@5 2545 SELECT * INTO "delegation_row" FROM "delegation"
jbe@5 2546 WHERE "truster_id" = "output_row"."member_id"
jbe@97 2547 AND (
jbe@97 2548 "unit_id" = "unit_id_v" OR
jbe@97 2549 "area_id" = "area_id_v"
jbe@97 2550 )
jbe@10 2551 ORDER BY "scope" DESC;
jbe@97 2552 ELSIF "scope_v" = 'issue' THEN
jbe@241 2553 IF "issue_row"."fully_frozen" ISNULL THEN
jbe@241 2554 "output_row"."participation" := EXISTS (
jbe@241 2555 SELECT NULL FROM "interest"
jbe@241 2556 WHERE "issue_id" = "issue_id_p"
jbe@241 2557 AND "member_id" = "output_row"."member_id"
jbe@241 2558 );
jbe@241 2559 ELSE
jbe@241 2560 IF "output_row"."member_id" = "member_id_p" THEN
jbe@241 2561 "output_row"."participation" := EXISTS (
jbe@241 2562 SELECT NULL FROM "direct_voter"
jbe@241 2563 WHERE "issue_id" = "issue_id_p"
jbe@241 2564 AND "member_id" = "output_row"."member_id"
jbe@241 2565 );
jbe@241 2566 ELSE
jbe@241 2567 "output_row"."participation" := NULL;
jbe@241 2568 END IF;
jbe@241 2569 END IF;
jbe@5 2570 SELECT * INTO "delegation_row" FROM "delegation"
jbe@5 2571 WHERE "truster_id" = "output_row"."member_id"
jbe@97 2572 AND (
jbe@97 2573 "unit_id" = "unit_id_v" OR
jbe@97 2574 "area_id" = "area_id_v" OR
jbe@10 2575 "issue_id" = "issue_id_p"
jbe@10 2576 )
jbe@10 2577 ORDER BY "scope" DESC;
jbe@5 2578 END IF;
jbe@5 2579 ELSE
jbe@97 2580 "output_row"."member_valid" := FALSE;
jbe@5 2581 "output_row"."participation" := FALSE;
jbe@5 2582 "output_row"."scope_out" := NULL;
jbe@5 2583 "delegation_row" := ROW(NULL);
jbe@5 2584 END IF;
jbe@5 2585 IF
jbe@5 2586 "output_row"."member_id" = "member_id_p" AND
jbe@5 2587 "simulate_trustee_id_p" NOTNULL
jbe@5 2588 THEN
jbe@97 2589 "output_row"."scope_out" := "scope_v";
jbe@5 2590 "output_rows" := "output_rows" || "output_row";
jbe@5 2591 "output_row"."member_id" := "simulate_trustee_id_p";
jbe@5 2592 ELSIF "delegation_row"."trustee_id" NOTNULL THEN
jbe@10 2593 "output_row"."scope_out" := "delegation_row"."scope";
jbe@5 2594 "output_rows" := "output_rows" || "output_row";
jbe@5 2595 "output_row"."member_id" := "delegation_row"."trustee_id";
jbe@86 2596 ELSIF "delegation_row"."scope" NOTNULL THEN
jbe@86 2597 "output_row"."scope_out" := "delegation_row"."scope";
jbe@86 2598 "output_row"."disabled_out" := TRUE;
jbe@86 2599 "output_rows" := "output_rows" || "output_row";
jbe@86 2600 EXIT;
jbe@5 2601 ELSE
jbe@5 2602 "output_row"."scope_out" := NULL;
jbe@5 2603 "output_rows" := "output_rows" || "output_row";
jbe@5 2604 EXIT;
jbe@5 2605 END IF;
jbe@5 2606 EXIT WHEN "loop_member_id_v" NOTNULL;
jbe@5 2607 "output_row"."index" := "output_row"."index" + 1;
jbe@5 2608 END LOOP;
jbe@5 2609 "row_count" := array_upper("output_rows", 1);
jbe@5 2610 "i" := 1;
jbe@5 2611 "loop_v" := FALSE;
jbe@5 2612 LOOP
jbe@5 2613 "output_row" := "output_rows"["i"];
jbe@98 2614 EXIT WHEN "output_row" ISNULL; -- NOTE: ISNULL and NOT ... NOTNULL produce different results!
jbe@5 2615 IF "loop_v" THEN
jbe@5 2616 IF "i" + 1 = "row_count" THEN
jbe@5 2617 "output_row"."loop" := 'last';
jbe@5 2618 ELSIF "i" = "row_count" THEN
jbe@5 2619 "output_row"."loop" := 'repetition';
jbe@5 2620 ELSE
jbe@5 2621 "output_row"."loop" := 'intermediate';
jbe@5 2622 END IF;
jbe@5 2623 ELSIF "output_row"."member_id" = "loop_member_id_v" THEN
jbe@5 2624 "output_row"."loop" := 'first';
jbe@5 2625 "loop_v" := TRUE;
jbe@5 2626 END IF;
jbe@97 2627 IF "scope_v" = 'unit' THEN
jbe@5 2628 "output_row"."participation" := NULL;
jbe@5 2629 END IF;
jbe@5 2630 RETURN NEXT "output_row";
jbe@5 2631 "i" := "i" + 1;
jbe@5 2632 END LOOP;
jbe@5 2633 RETURN;
jbe@5 2634 END;
jbe@5 2635 $$;
jbe@5 2636
jbe@5 2637 COMMENT ON FUNCTION "delegation_chain"
jbe@5 2638 ( "member"."id"%TYPE,
jbe@97 2639 "unit"."id"%TYPE,
jbe@5 2640 "area"."id"%TYPE,
jbe@5 2641 "issue"."id"%TYPE,
jbe@5 2642 "member"."id"%TYPE )
jbe@242 2643 IS 'Shows a delegation chain for unit, area, or issue; See "delegation_chain_row" type for more information';
jbe@242 2644
jbe@242 2645
jbe@242 2646
jbe@242 2647 ---------------------------------------------------------
jbe@242 2648 -- Single row returning function for delegation chains --
jbe@242 2649 ---------------------------------------------------------
jbe@242 2650
jbe@242 2651
jbe@242 2652 CREATE TYPE "delegation_info_loop_type" AS ENUM
jbe@242 2653 ('own', 'first', 'first_ellipsis', 'other', 'other_ellipsis');
jbe@240 2654
jbe@243 2655 COMMENT ON TYPE "delegation_info_loop_type" IS 'Type of "delegation_loop" in "delegation_info_type"; ''own'' means loop to self, ''first'' means loop to first trustee, ''first_ellipsis'' means loop to ellipsis after first trustee, ''other'' means loop to other trustee, ''other_ellipsis'' means loop to ellipsis after other trustee''';
jbe@243 2656
jbe@243 2657
jbe@240 2658 CREATE TYPE "delegation_info_type" AS (
jbe@242 2659 "own_participation" BOOLEAN,
jbe@242 2660 "own_delegation_scope" "delegation_scope",
jbe@242 2661 "first_trustee_id" INT4,
jbe@240 2662 "first_trustee_participation" BOOLEAN,
jbe@242 2663 "first_trustee_ellipsis" BOOLEAN,
jbe@242 2664 "other_trustee_id" INT4,
jbe@240 2665 "other_trustee_participation" BOOLEAN,
jbe@242 2666 "other_trustee_ellipsis" BOOLEAN,
jbe@253 2667 "delegation_loop" "delegation_info_loop_type",
jbe@253 2668 "participating_member_id" INT4 );
jbe@240 2669
jbe@243 2670 COMMENT ON TYPE "delegation_info_type" IS 'Type of result returned by "delegation_info" function; For meaning of "participation" check comment on "delegation_chain_row" type';
jbe@243 2671
jbe@243 2672 COMMENT ON COLUMN "delegation_info_type"."own_participation" IS 'Member is directly participating';
jbe@243 2673 COMMENT ON COLUMN "delegation_info_type"."own_delegation_scope" IS 'Delegation scope of member';
jbe@243 2674 COMMENT ON COLUMN "delegation_info_type"."first_trustee_id" IS 'Direct trustee of member';
jbe@243 2675 COMMENT ON COLUMN "delegation_info_type"."first_trustee_participation" IS 'Direct trustee of member is participating';
jbe@243 2676 COMMENT ON COLUMN "delegation_info_type"."first_trustee_ellipsis" IS 'Ellipsis in delegation chain after "first_trustee"';
jbe@243 2677 COMMENT ON COLUMN "delegation_info_type"."other_trustee_id" IS 'Another relevant trustee (due to participation)';
jbe@243 2678 COMMENT ON COLUMN "delegation_info_type"."other_trustee_participation" IS 'Another trustee is participating (redundant field: if "other_trustee_id" is set, then "other_trustee_participation" is always TRUE, else "other_trustee_participation" is NULL)';
jbe@243 2679 COMMENT ON COLUMN "delegation_info_type"."other_trustee_ellipsis" IS 'Ellipsis in delegation chain after "other_trustee"';
jbe@243 2680 COMMENT ON COLUMN "delegation_info_type"."delegation_loop" IS 'Non-NULL value, if delegation chain contains a circle; See comment on "delegation_info_loop_type" for details';
jbe@253 2681 COMMENT ON COLUMN "delegation_info_type"."participating_member_id" IS 'First participating member in delegation chain';
jbe@243 2682
jbe@243 2683
jbe@240 2684 CREATE FUNCTION "delegation_info"
jbe@242 2685 ( "member_id_p" "member"."id"%TYPE,
jbe@242 2686 "unit_id_p" "unit"."id"%TYPE,
jbe@242 2687 "area_id_p" "area"."id"%TYPE,
jbe@242 2688 "issue_id_p" "issue"."id"%TYPE,
jbe@242 2689 "simulate_trustee_id_p" "member"."id"%TYPE DEFAULT NULL )
jbe@240 2690 RETURNS "delegation_info_type"
jbe@240 2691 LANGUAGE 'plpgsql' STABLE AS $$
jbe@240 2692 DECLARE
jbe@242 2693 "current_row" "delegation_chain_row";
jbe@242 2694 "result" "delegation_info_type";
jbe@240 2695 BEGIN
jbe@242 2696 "result"."own_participation" := FALSE;
jbe@242 2697 FOR "current_row" IN
jbe@242 2698 SELECT * FROM "delegation_chain"(
jbe@242 2699 "member_id_p",
jbe@242 2700 "unit_id_p", "area_id_p", "issue_id_p",
jbe@242 2701 "simulate_trustee_id_p")
jbe@242 2702 LOOP
jbe@253 2703 IF
jbe@253 2704 "result"."participating_member_id" ISNULL AND
jbe@253 2705 "current_row"."participation"
jbe@253 2706 THEN
jbe@253 2707 "result"."participating_member_id" := "current_row"."member_id";
jbe@253 2708 END IF;
jbe@242 2709 IF "current_row"."member_id" = "member_id_p" THEN
jbe@242 2710 "result"."own_participation" := "current_row"."participation";
jbe@242 2711 "result"."own_delegation_scope" := "current_row"."scope_out";
jbe@242 2712 IF "current_row"."loop" = 'first' THEN
jbe@242 2713 "result"."delegation_loop" := 'own';
jbe@242 2714 END IF;
jbe@242 2715 ELSIF
jbe@242 2716 "current_row"."member_valid" AND
jbe@242 2717 ( "current_row"."loop" ISNULL OR
jbe@242 2718 "current_row"."loop" != 'repetition' )
jbe@242 2719 THEN
jbe@242 2720 IF "result"."first_trustee_id" ISNULL THEN
jbe@242 2721 "result"."first_trustee_id" := "current_row"."member_id";
jbe@242 2722 "result"."first_trustee_participation" := "current_row"."participation";
jbe@242 2723 "result"."first_trustee_ellipsis" := FALSE;
jbe@242 2724 IF "current_row"."loop" = 'first' THEN
jbe@242 2725 "result"."delegation_loop" := 'first';
jbe@242 2726 END IF;
jbe@242 2727 ELSIF "result"."other_trustee_id" ISNULL THEN
jbe@247 2728 IF "current_row"."participation" AND NOT "current_row"."overridden" THEN
jbe@242 2729 "result"."other_trustee_id" := "current_row"."member_id";
jbe@242 2730 "result"."other_trustee_participation" := TRUE;
jbe@242 2731 "result"."other_trustee_ellipsis" := FALSE;
jbe@242 2732 IF "current_row"."loop" = 'first' THEN
jbe@242 2733 "result"."delegation_loop" := 'other';
jbe@240 2734 END IF;
jbe@240 2735 ELSE
jbe@242 2736 "result"."first_trustee_ellipsis" := TRUE;
jbe@242 2737 IF "current_row"."loop" = 'first' THEN
jbe@242 2738 "result"."delegation_loop" := 'first_ellipsis';
jbe@242 2739 END IF;
jbe@242 2740 END IF;
jbe@242 2741 ELSE
jbe@242 2742 "result"."other_trustee_ellipsis" := TRUE;
jbe@242 2743 IF "current_row"."loop" = 'first' THEN
jbe@242 2744 "result"."delegation_loop" := 'other_ellipsis';
jbe@240 2745 END IF;
jbe@240 2746 END IF;
jbe@240 2747 END IF;
jbe@242 2748 END LOOP;
jbe@240 2749 RETURN "result";
jbe@240 2750 END;
jbe@240 2751 $$;
jbe@240 2752
jbe@243 2753 COMMENT ON FUNCTION "delegation_info"
jbe@243 2754 ( "member"."id"%TYPE,
jbe@243 2755 "unit"."id"%TYPE,
jbe@243 2756 "area"."id"%TYPE,
jbe@243 2757 "issue"."id"%TYPE,
jbe@243 2758 "member"."id"%TYPE )
jbe@243 2759 IS 'Notable information about a delegation chain for unit, area, or issue; See "delegation_info_type" for more information';
jbe@243 2760
jbe@240 2761
jbe@240 2762
jbe@240 2763 ------------------------------
jbe@0 2764 -- Comparison by vote count --
jbe@0 2765 ------------------------------
jbe@0 2766
jbe@0 2767 CREATE FUNCTION "vote_ratio"
jbe@0 2768 ( "positive_votes_p" "initiative"."positive_votes"%TYPE,
jbe@0 2769 "negative_votes_p" "initiative"."negative_votes"%TYPE )
jbe@0 2770 RETURNS FLOAT8
jbe@0 2771 LANGUAGE 'plpgsql' STABLE AS $$
jbe@0 2772 BEGIN
jbe@30 2773 IF "positive_votes_p" > 0 AND "negative_votes_p" > 0 THEN
jbe@30 2774 RETURN
jbe@30 2775 "positive_votes_p"::FLOAT8 /
jbe@30 2776 ("positive_votes_p" + "negative_votes_p")::FLOAT8;
jbe@30 2777 ELSIF "positive_votes_p" > 0 THEN
jbe@30 2778 RETURN "positive_votes_p";
jbe@30 2779 ELSIF "negative_votes_p" > 0 THEN
jbe@30 2780 RETURN 1 - "negative_votes_p";
jbe@0 2781 ELSE
jbe@0 2782 RETURN 0.5;
jbe@0 2783 END IF;
jbe@0 2784 END;
jbe@0 2785 $$;
jbe@0 2786
jbe@0 2787 COMMENT ON FUNCTION "vote_ratio"
jbe@0 2788 ( "initiative"."positive_votes"%TYPE,
jbe@0 2789 "initiative"."negative_votes"%TYPE )
jbe@30 2790 IS 'Returns a number, which can be used for comparison of initiatives based on count of approvals and disapprovals. Greater numbers indicate a better result. This function is NOT injective.';
jbe@0 2791
jbe@0 2792
jbe@0 2793
jbe@0 2794 ------------------------------------------------
jbe@0 2795 -- Locking for snapshots and voting procedure --
jbe@0 2796 ------------------------------------------------
jbe@0 2797
jbe@67 2798
jbe@67 2799 CREATE FUNCTION "share_row_lock_issue_trigger"()
jbe@67 2800 RETURNS TRIGGER
jbe@67 2801 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@67 2802 BEGIN
jbe@67 2803 IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
jbe@67 2804 PERFORM NULL FROM "issue" WHERE "id" = OLD."issue_id" FOR SHARE;
jbe@67 2805 END IF;
jbe@67 2806 IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
jbe@67 2807 PERFORM NULL FROM "issue" WHERE "id" = NEW."issue_id" FOR SHARE;
jbe@67 2808 RETURN NEW;
jbe@67 2809 ELSE
jbe@67 2810 RETURN OLD;
jbe@67 2811 END IF;
jbe@67 2812 END;
jbe@67 2813 $$;
jbe@67 2814
jbe@67 2815 COMMENT ON FUNCTION "share_row_lock_issue_trigger"() IS 'Implementation of triggers "share_row_lock_issue" on multiple tables';
jbe@67 2816
jbe@67 2817
jbe@67 2818 CREATE FUNCTION "share_row_lock_issue_via_initiative_trigger"()
jbe@67 2819 RETURNS TRIGGER
jbe@0 2820 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 2821 BEGIN
jbe@67 2822 IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
jbe@67 2823 PERFORM NULL FROM "issue"
jbe@67 2824 JOIN "initiative" ON "issue"."id" = "initiative"."issue_id"
jbe@67 2825 WHERE "initiative"."id" = OLD."initiative_id"
jbe@67 2826 FOR SHARE OF "issue";
jbe@67 2827 END IF;
jbe@67 2828 IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
jbe@67 2829 PERFORM NULL FROM "issue"
jbe@67 2830 JOIN "initiative" ON "issue"."id" = "initiative"."issue_id"
jbe@67 2831 WHERE "initiative"."id" = NEW."initiative_id"
jbe@67 2832 FOR SHARE OF "issue";
jbe@67 2833 RETURN NEW;
jbe@67 2834 ELSE
jbe@67 2835 RETURN OLD;
jbe@67 2836 END IF;
jbe@67 2837 END;
jbe@67 2838 $$;
jbe@67 2839
jbe@67 2840 COMMENT ON FUNCTION "share_row_lock_issue_trigger"() IS 'Implementation of trigger "share_row_lock_issue_via_initiative" on table "opinion"';
jbe@67 2841
jbe@67 2842
jbe@67 2843 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2844 BEFORE INSERT OR UPDATE OR DELETE ON "initiative"
jbe@67 2845 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2846 "share_row_lock_issue_trigger"();
jbe@67 2847
jbe@67 2848 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2849 BEFORE INSERT OR UPDATE OR DELETE ON "interest"
jbe@67 2850 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2851 "share_row_lock_issue_trigger"();
jbe@67 2852
jbe@67 2853 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2854 BEFORE INSERT OR UPDATE OR DELETE ON "supporter"
jbe@67 2855 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2856 "share_row_lock_issue_trigger"();
jbe@67 2857
jbe@67 2858 CREATE TRIGGER "share_row_lock_issue_via_initiative"
jbe@67 2859 BEFORE INSERT OR UPDATE OR DELETE ON "opinion"
jbe@67 2860 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2861 "share_row_lock_issue_via_initiative_trigger"();
jbe@67 2862
jbe@67 2863 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2864 BEFORE INSERT OR UPDATE OR DELETE ON "direct_voter"
jbe@67 2865 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2866 "share_row_lock_issue_trigger"();
jbe@67 2867
jbe@67 2868 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2869 BEFORE INSERT OR UPDATE OR DELETE ON "delegating_voter"
jbe@67 2870 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2871 "share_row_lock_issue_trigger"();
jbe@67 2872
jbe@67 2873 CREATE TRIGGER "share_row_lock_issue"
jbe@67 2874 BEFORE INSERT OR UPDATE OR DELETE ON "vote"
jbe@67 2875 FOR EACH ROW EXECUTE PROCEDURE
jbe@67 2876 "share_row_lock_issue_trigger"();
jbe@67 2877
jbe@67 2878 COMMENT ON TRIGGER "share_row_lock_issue" ON "initiative" IS 'See "lock_issue" function';
jbe@67 2879 COMMENT ON TRIGGER "share_row_lock_issue" ON "interest" IS 'See "lock_issue" function';
jbe@67 2880 COMMENT ON TRIGGER "share_row_lock_issue" ON "supporter" IS 'See "lock_issue" function';
jbe@67 2881 COMMENT ON TRIGGER "share_row_lock_issue_via_initiative" ON "opinion" IS 'See "lock_issue" function';
jbe@67 2882 COMMENT ON TRIGGER "share_row_lock_issue" ON "direct_voter" IS 'See "lock_issue" function';
jbe@67 2883 COMMENT ON TRIGGER "share_row_lock_issue" ON "delegating_voter" IS 'See "lock_issue" function';
jbe@67 2884 COMMENT ON TRIGGER "share_row_lock_issue" ON "vote" IS 'See "lock_issue" function';
jbe@67 2885
jbe@67 2886
jbe@67 2887 CREATE FUNCTION "lock_issue"
jbe@67 2888 ( "issue_id_p" "issue"."id"%TYPE )
jbe@67 2889 RETURNS VOID
jbe@67 2890 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@67 2891 BEGIN
jbe@67 2892 LOCK TABLE "member" IN SHARE MODE;
jbe@97 2893 LOCK TABLE "privilege" IN SHARE MODE;
jbe@67 2894 LOCK TABLE "membership" IN SHARE MODE;
jbe@67 2895 LOCK TABLE "policy" IN SHARE MODE;
jbe@67 2896 PERFORM NULL FROM "issue" WHERE "id" = "issue_id_p" FOR UPDATE;
jbe@67 2897 -- NOTE: The row-level exclusive lock in combination with the
jbe@67 2898 -- share_row_lock_issue(_via_initiative)_trigger functions (which
jbe@67 2899 -- acquire a row-level share lock on the issue) ensure that no data
jbe@67 2900 -- is changed, which could affect calculation of snapshots or
jbe@67 2901 -- counting of votes. Table "delegation" must be table-level-locked,
jbe@67 2902 -- as it also contains issue- and global-scope delegations.
jbe@67 2903 LOCK TABLE "delegation" IN SHARE MODE;
jbe@0 2904 LOCK TABLE "direct_population_snapshot" IN EXCLUSIVE MODE;
jbe@0 2905 LOCK TABLE "delegating_population_snapshot" IN EXCLUSIVE MODE;
jbe@0 2906 LOCK TABLE "direct_interest_snapshot" IN EXCLUSIVE MODE;
jbe@0 2907 LOCK TABLE "delegating_interest_snapshot" IN EXCLUSIVE MODE;
jbe@0 2908 LOCK TABLE "direct_supporter_snapshot" IN EXCLUSIVE MODE;
jbe@0 2909 RETURN;
jbe@0 2910 END;
jbe@0 2911 $$;
jbe@0 2912
jbe@67 2913 COMMENT ON FUNCTION "lock_issue"
jbe@67 2914 ( "issue"."id"%TYPE )
jbe@67 2915 IS 'Locks the issue and all other data which is used for calculating snapshots or counting votes.';
jbe@0 2916
jbe@0 2917
jbe@0 2918
jbe@103 2919 ------------------------------------------------------------------------
jbe@103 2920 -- Regular tasks, except calculcation of snapshots and voting results --
jbe@103 2921 ------------------------------------------------------------------------
jbe@103 2922
jbe@184 2923 CREATE FUNCTION "check_activity"()
jbe@103 2924 RETURNS VOID
jbe@103 2925 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@104 2926 DECLARE
jbe@104 2927 "system_setting_row" "system_setting"%ROWTYPE;
jbe@103 2928 BEGIN
jbe@104 2929 SELECT * INTO "system_setting_row" FROM "system_setting";
jbe@103 2930 LOCK TABLE "member" IN SHARE ROW EXCLUSIVE MODE;
jbe@104 2931 IF "system_setting_row"."member_ttl" NOTNULL THEN
jbe@104 2932 UPDATE "member" SET "active" = FALSE
jbe@104 2933 WHERE "active" = TRUE
jbe@184 2934 AND "last_activity" < (now() - "system_setting_row"."member_ttl")::DATE;
jbe@104 2935 END IF;
jbe@103 2936 RETURN;
jbe@103 2937 END;
jbe@103 2938 $$;
jbe@103 2939
jbe@184 2940 COMMENT ON FUNCTION "check_activity"() IS 'Deactivates members when "last_activity" is older than "system_setting"."member_ttl".';
jbe@103 2941
jbe@4 2942
jbe@4 2943 CREATE FUNCTION "calculate_member_counts"()
jbe@4 2944 RETURNS VOID
jbe@4 2945 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@4 2946 BEGIN
jbe@67 2947 LOCK TABLE "member" IN SHARE MODE;
jbe@67 2948 LOCK TABLE "member_count" IN EXCLUSIVE MODE;
jbe@97 2949 LOCK TABLE "unit" IN EXCLUSIVE MODE;
jbe@67 2950 LOCK TABLE "area" IN EXCLUSIVE MODE;
jbe@97 2951 LOCK TABLE "privilege" IN SHARE MODE;
jbe@67 2952 LOCK TABLE "membership" IN SHARE MODE;
jbe@4 2953 DELETE FROM "member_count";
jbe@5 2954 INSERT INTO "member_count" ("total_count")
jbe@5 2955 SELECT "total_count" FROM "member_count_view";
jbe@97 2956 UPDATE "unit" SET "member_count" = "view"."member_count"
jbe@97 2957 FROM "unit_member_count" AS "view"
jbe@97 2958 WHERE "view"."unit_id" = "unit"."id";
jbe@5 2959 UPDATE "area" SET
jbe@5 2960 "direct_member_count" = "view"."direct_member_count",
jbe@169 2961 "member_weight" = "view"."member_weight"
jbe@5 2962 FROM "area_member_count" AS "view"
jbe@5 2963 WHERE "view"."area_id" = "area"."id";
jbe@4 2964 RETURN;
jbe@4 2965 END;
jbe@4 2966 $$;
jbe@4 2967
jbe@4 2968 COMMENT ON FUNCTION "calculate_member_counts"() IS 'Updates "member_count" table and "member_count" column of table "area" by materializing data from views "member_count_view" and "area_member_count"';
jbe@4 2969
jbe@4 2970
jbe@4 2971
jbe@0 2972 ------------------------------
jbe@0 2973 -- Calculation of snapshots --
jbe@0 2974 ------------------------------
jbe@0 2975
jbe@0 2976 CREATE FUNCTION "weight_of_added_delegations_for_population_snapshot"
jbe@0 2977 ( "issue_id_p" "issue"."id"%TYPE,
jbe@0 2978 "member_id_p" "member"."id"%TYPE,
jbe@0 2979 "delegate_member_ids_p" "delegating_population_snapshot"."delegate_member_ids"%TYPE )
jbe@0 2980 RETURNS "direct_population_snapshot"."weight"%TYPE
jbe@0 2981 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 2982 DECLARE
jbe@0 2983 "issue_delegation_row" "issue_delegation"%ROWTYPE;
jbe@0 2984 "delegate_member_ids_v" "delegating_population_snapshot"."delegate_member_ids"%TYPE;
jbe@0 2985 "weight_v" INT4;
jbe@8 2986 "sub_weight_v" INT4;
jbe@0 2987 BEGIN
jbe@0 2988 "weight_v" := 0;
jbe@0 2989 FOR "issue_delegation_row" IN
jbe@0 2990 SELECT * FROM "issue_delegation"
jbe@0 2991 WHERE "trustee_id" = "member_id_p"
jbe@0 2992 AND "issue_id" = "issue_id_p"
jbe@0 2993 LOOP
jbe@0 2994 IF NOT EXISTS (
jbe@0 2995 SELECT NULL FROM "direct_population_snapshot"
jbe@0 2996 WHERE "issue_id" = "issue_id_p"
jbe@0 2997 AND "event" = 'periodic'
jbe@0 2998 AND "member_id" = "issue_delegation_row"."truster_id"
jbe@0 2999 ) AND NOT EXISTS (
jbe@0 3000 SELECT NULL FROM "delegating_population_snapshot"
jbe@0 3001 WHERE "issue_id" = "issue_id_p"
jbe@0 3002 AND "event" = 'periodic'
jbe@0 3003 AND "member_id" = "issue_delegation_row"."truster_id"
jbe@0 3004 ) THEN
jbe@0 3005 "delegate_member_ids_v" :=
jbe@0 3006 "member_id_p" || "delegate_member_ids_p";
jbe@10 3007 INSERT INTO "delegating_population_snapshot" (
jbe@10 3008 "issue_id",
jbe@10 3009 "event",
jbe@10 3010 "member_id",
jbe@10 3011 "scope",
jbe@10 3012 "delegate_member_ids"
jbe@10 3013 ) VALUES (
jbe@0 3014 "issue_id_p",
jbe@0 3015 'periodic',
jbe@0 3016 "issue_delegation_row"."truster_id",
jbe@10 3017 "issue_delegation_row"."scope",
jbe@0 3018 "delegate_member_ids_v"
jbe@0 3019 );
jbe@8 3020 "sub_weight_v" := 1 +
jbe@0 3021 "weight_of_added_delegations_for_population_snapshot"(
jbe@0 3022 "issue_id_p",
jbe@0 3023 "issue_delegation_row"."truster_id",
jbe@0 3024 "delegate_member_ids_v"
jbe@0 3025 );
jbe@8 3026 UPDATE "delegating_population_snapshot"
jbe@8 3027 SET "weight" = "sub_weight_v"
jbe@8 3028 WHERE "issue_id" = "issue_id_p"
jbe@8 3029 AND "event" = 'periodic'
jbe@8 3030 AND "member_id" = "issue_delegation_row"."truster_id";
jbe@8 3031 "weight_v" := "weight_v" + "sub_weight_v";
jbe@0 3032 END IF;
jbe@0 3033 END LOOP;
jbe@0 3034 RETURN "weight_v";
jbe@0 3035 END;
jbe@0 3036 $$;
jbe@0 3037
jbe@0 3038 COMMENT ON FUNCTION "weight_of_added_delegations_for_population_snapshot"
jbe@0 3039 ( "issue"."id"%TYPE,
jbe@0 3040 "member"."id"%TYPE,
jbe@0 3041 "delegating_population_snapshot"."delegate_member_ids"%TYPE )
jbe@0 3042 IS 'Helper function for "create_population_snapshot" function';
jbe@0 3043
jbe@0 3044
jbe@0 3045 CREATE FUNCTION "create_population_snapshot"
jbe@0 3046 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3047 RETURNS VOID
jbe@0 3048 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3049 DECLARE
jbe@0 3050 "member_id_v" "member"."id"%TYPE;
jbe@0 3051 BEGIN
jbe@0 3052 DELETE FROM "direct_population_snapshot"
jbe@0 3053 WHERE "issue_id" = "issue_id_p"
jbe@0 3054 AND "event" = 'periodic';
jbe@0 3055 DELETE FROM "delegating_population_snapshot"
jbe@0 3056 WHERE "issue_id" = "issue_id_p"
jbe@0 3057 AND "event" = 'periodic';
jbe@0 3058 INSERT INTO "direct_population_snapshot"
jbe@54 3059 ("issue_id", "event", "member_id")
jbe@54 3060 SELECT
jbe@54 3061 "issue_id_p" AS "issue_id",
jbe@54 3062 'periodic'::"snapshot_event" AS "event",
jbe@54 3063 "member"."id" AS "member_id"
jbe@54 3064 FROM "issue"
jbe@54 3065 JOIN "area" ON "issue"."area_id" = "area"."id"
jbe@54 3066 JOIN "membership" ON "area"."id" = "membership"."area_id"
jbe@54 3067 JOIN "member" ON "membership"."member_id" = "member"."id"
jbe@97 3068 JOIN "privilege"
jbe@97 3069 ON "privilege"."unit_id" = "area"."unit_id"
jbe@97 3070 AND "privilege"."member_id" = "member"."id"
jbe@54 3071 WHERE "issue"."id" = "issue_id_p"
jbe@97 3072 AND "member"."active" AND "privilege"."voting_right"
jbe@54 3073 UNION
jbe@54 3074 SELECT
jbe@54 3075 "issue_id_p" AS "issue_id",
jbe@54 3076 'periodic'::"snapshot_event" AS "event",
jbe@54 3077 "member"."id" AS "member_id"
jbe@97 3078 FROM "issue"
jbe@97 3079 JOIN "area" ON "issue"."area_id" = "area"."id"
jbe@97 3080 JOIN "interest" ON "issue"."id" = "interest"."issue_id"
jbe@97 3081 JOIN "member" ON "interest"."member_id" = "member"."id"
jbe@97 3082 JOIN "privilege"
jbe@97 3083 ON "privilege"."unit_id" = "area"."unit_id"
jbe@97 3084 AND "privilege"."member_id" = "member"."id"
jbe@97 3085 WHERE "issue"."id" = "issue_id_p"
jbe@97 3086 AND "member"."active" AND "privilege"."voting_right";
jbe@0 3087 FOR "member_id_v" IN
jbe@0 3088 SELECT "member_id" FROM "direct_population_snapshot"
jbe@0 3089 WHERE "issue_id" = "issue_id_p"
jbe@0 3090 AND "event" = 'periodic'
jbe@0 3091 LOOP
jbe@0 3092 UPDATE "direct_population_snapshot" SET
jbe@0 3093 "weight" = 1 +
jbe@0 3094 "weight_of_added_delegations_for_population_snapshot"(
jbe@0 3095 "issue_id_p",
jbe@0 3096 "member_id_v",
jbe@0 3097 '{}'
jbe@0 3098 )
jbe@0 3099 WHERE "issue_id" = "issue_id_p"
jbe@0 3100 AND "event" = 'periodic'
jbe@0 3101 AND "member_id" = "member_id_v";
jbe@0 3102 END LOOP;
jbe@0 3103 RETURN;
jbe@0 3104 END;
jbe@0 3105 $$;
jbe@0 3106
jbe@0 3107 COMMENT ON FUNCTION "create_population_snapshot"
jbe@67 3108 ( "issue"."id"%TYPE )
jbe@0 3109 IS 'This function creates a new ''periodic'' population snapshot for the given issue. It does neither lock any tables, nor updates precalculated values in other tables.';
jbe@0 3110
jbe@0 3111
jbe@0 3112 CREATE FUNCTION "weight_of_added_delegations_for_interest_snapshot"
jbe@0 3113 ( "issue_id_p" "issue"."id"%TYPE,
jbe@0 3114 "member_id_p" "member"."id"%TYPE,
jbe@0 3115 "delegate_member_ids_p" "delegating_interest_snapshot"."delegate_member_ids"%TYPE )
jbe@0 3116 RETURNS "direct_interest_snapshot"."weight"%TYPE
jbe@0 3117 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3118 DECLARE
jbe@0 3119 "issue_delegation_row" "issue_delegation"%ROWTYPE;
jbe@0 3120 "delegate_member_ids_v" "delegating_interest_snapshot"."delegate_member_ids"%TYPE;
jbe@0 3121 "weight_v" INT4;
jbe@8 3122 "sub_weight_v" INT4;
jbe@0 3123 BEGIN
jbe@0 3124 "weight_v" := 0;
jbe@0 3125 FOR "issue_delegation_row" IN
jbe@0 3126 SELECT * FROM "issue_delegation"
jbe@0 3127 WHERE "trustee_id" = "member_id_p"
jbe@0 3128 AND "issue_id" = "issue_id_p"
jbe@0 3129 LOOP
jbe@0 3130 IF NOT EXISTS (
jbe@0 3131 SELECT NULL FROM "direct_interest_snapshot"
jbe@0 3132 WHERE "issue_id" = "issue_id_p"
jbe@0 3133 AND "event" = 'periodic'
jbe@0 3134 AND "member_id" = "issue_delegation_row"."truster_id"
jbe@0 3135 ) AND NOT EXISTS (
jbe@0 3136 SELECT NULL FROM "delegating_interest_snapshot"
jbe@0 3137 WHERE "issue_id" = "issue_id_p"
jbe@0 3138 AND "event" = 'periodic'
jbe@0 3139 AND "member_id" = "issue_delegation_row"."truster_id"
jbe@0 3140 ) THEN
jbe@0 3141 "delegate_member_ids_v" :=
jbe@0 3142 "member_id_p" || "delegate_member_ids_p";
jbe@10 3143 INSERT INTO "delegating_interest_snapshot" (
jbe@10 3144 "issue_id",
jbe@10 3145 "event",
jbe@10 3146 "member_id",
jbe@10 3147 "scope",
jbe@10 3148 "delegate_member_ids"
jbe@10 3149 ) VALUES (
jbe@0 3150 "issue_id_p",
jbe@0 3151 'periodic',
jbe@0 3152 "issue_delegation_row"."truster_id",
jbe@10 3153 "issue_delegation_row"."scope",
jbe@0 3154 "delegate_member_ids_v"
jbe@0 3155 );
jbe@8 3156 "sub_weight_v" := 1 +
jbe@0 3157 "weight_of_added_delegations_for_interest_snapshot"(
jbe@0 3158 "issue_id_p",
jbe@0 3159 "issue_delegation_row"."truster_id",
jbe@0 3160 "delegate_member_ids_v"
jbe@0 3161 );
jbe@8 3162 UPDATE "delegating_interest_snapshot"
jbe@8 3163 SET "weight" = "sub_weight_v"
jbe@8 3164 WHERE "issue_id" = "issue_id_p"
jbe@8 3165 AND "event" = 'periodic'
jbe@8 3166 AND "member_id" = "issue_delegation_row"."truster_id";
jbe@8 3167 "weight_v" := "weight_v" + "sub_weight_v";
jbe@0 3168 END IF;
jbe@0 3169 END LOOP;
jbe@0 3170 RETURN "weight_v";
jbe@0 3171 END;
jbe@0 3172 $$;
jbe@0 3173
jbe@0 3174 COMMENT ON FUNCTION "weight_of_added_delegations_for_interest_snapshot"
jbe@0 3175 ( "issue"."id"%TYPE,
jbe@0 3176 "member"."id"%TYPE,
jbe@0 3177 "delegating_interest_snapshot"."delegate_member_ids"%TYPE )
jbe@0 3178 IS 'Helper function for "create_interest_snapshot" function';
jbe@0 3179
jbe@0 3180
jbe@0 3181 CREATE FUNCTION "create_interest_snapshot"
jbe@0 3182 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3183 RETURNS VOID
jbe@0 3184 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3185 DECLARE
jbe@0 3186 "member_id_v" "member"."id"%TYPE;
jbe@0 3187 BEGIN
jbe@0 3188 DELETE FROM "direct_interest_snapshot"
jbe@0 3189 WHERE "issue_id" = "issue_id_p"
jbe@0 3190 AND "event" = 'periodic';
jbe@0 3191 DELETE FROM "delegating_interest_snapshot"
jbe@0 3192 WHERE "issue_id" = "issue_id_p"
jbe@0 3193 AND "event" = 'periodic';
jbe@0 3194 DELETE FROM "direct_supporter_snapshot"
jbe@0 3195 WHERE "issue_id" = "issue_id_p"
jbe@0 3196 AND "event" = 'periodic';
jbe@0 3197 INSERT INTO "direct_interest_snapshot"
jbe@144 3198 ("issue_id", "event", "member_id")
jbe@0 3199 SELECT
jbe@0 3200 "issue_id_p" AS "issue_id",
jbe@0 3201 'periodic' AS "event",
jbe@144 3202 "member"."id" AS "member_id"
jbe@97 3203 FROM "issue"
jbe@97 3204 JOIN "area" ON "issue"."area_id" = "area"."id"
jbe@97 3205 JOIN "interest" ON "issue"."id" = "interest"."issue_id"
jbe@97 3206 JOIN "member" ON "interest"."member_id" = "member"."id"
jbe@97 3207 JOIN "privilege"
jbe@97 3208 ON "privilege"."unit_id" = "area"."unit_id"
jbe@97 3209 AND "privilege"."member_id" = "member"."id"
jbe@97 3210 WHERE "issue"."id" = "issue_id_p"
jbe@97 3211 AND "member"."active" AND "privilege"."voting_right";
jbe@0 3212 FOR "member_id_v" IN
jbe@0 3213 SELECT "member_id" FROM "direct_interest_snapshot"
jbe@0 3214 WHERE "issue_id" = "issue_id_p"
jbe@0 3215 AND "event" = 'periodic'
jbe@0 3216 LOOP
jbe@0 3217 UPDATE "direct_interest_snapshot" SET
jbe@0 3218 "weight" = 1 +
jbe@0 3219 "weight_of_added_delegations_for_interest_snapshot"(
jbe@0 3220 "issue_id_p",
jbe@0 3221 "member_id_v",
jbe@0 3222 '{}'
jbe@0 3223 )
jbe@0 3224 WHERE "issue_id" = "issue_id_p"
jbe@0 3225 AND "event" = 'periodic'
jbe@0 3226 AND "member_id" = "member_id_v";
jbe@0 3227 END LOOP;
jbe@0 3228 INSERT INTO "direct_supporter_snapshot"
jbe@0 3229 ( "issue_id", "initiative_id", "event", "member_id",
jbe@204 3230 "draft_id", "informed", "satisfied" )
jbe@0 3231 SELECT
jbe@96 3232 "issue_id_p" AS "issue_id",
jbe@96 3233 "initiative"."id" AS "initiative_id",
jbe@96 3234 'periodic' AS "event",
jbe@96 3235 "supporter"."member_id" AS "member_id",
jbe@204 3236 "supporter"."draft_id" AS "draft_id",
jbe@0 3237 "supporter"."draft_id" = "current_draft"."id" AS "informed",
jbe@0 3238 NOT EXISTS (
jbe@0 3239 SELECT NULL FROM "critical_opinion"
jbe@0 3240 WHERE "initiative_id" = "initiative"."id"
jbe@96 3241 AND "member_id" = "supporter"."member_id"
jbe@0 3242 ) AS "satisfied"
jbe@96 3243 FROM "initiative"
jbe@96 3244 JOIN "supporter"
jbe@0 3245 ON "supporter"."initiative_id" = "initiative"."id"
jbe@0 3246 JOIN "current_draft"
jbe@0 3247 ON "initiative"."id" = "current_draft"."initiative_id"
jbe@0 3248 JOIN "direct_interest_snapshot"
jbe@96 3249 ON "supporter"."member_id" = "direct_interest_snapshot"."member_id"
jbe@0 3250 AND "initiative"."issue_id" = "direct_interest_snapshot"."issue_id"
jbe@3 3251 AND "event" = 'periodic'
jbe@96 3252 WHERE "initiative"."issue_id" = "issue_id_p";
jbe@0 3253 RETURN;
jbe@0 3254 END;
jbe@0 3255 $$;
jbe@0 3256
jbe@0 3257 COMMENT ON FUNCTION "create_interest_snapshot"
jbe@0 3258 ( "issue"."id"%TYPE )
jbe@0 3259 IS 'This function creates a new ''periodic'' interest/supporter snapshot for the given issue. It does neither lock any tables, nor updates precalculated values in other tables.';
jbe@0 3260
jbe@0 3261
jbe@0 3262 CREATE FUNCTION "create_snapshot"
jbe@0 3263 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3264 RETURNS VOID
jbe@0 3265 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3266 DECLARE
jbe@0 3267 "initiative_id_v" "initiative"."id"%TYPE;
jbe@0 3268 "suggestion_id_v" "suggestion"."id"%TYPE;
jbe@0 3269 BEGIN
jbe@67 3270 PERFORM "lock_issue"("issue_id_p");
jbe@0 3271 PERFORM "create_population_snapshot"("issue_id_p");
jbe@0 3272 PERFORM "create_interest_snapshot"("issue_id_p");
jbe@0 3273 UPDATE "issue" SET
jbe@8 3274 "snapshot" = now(),
jbe@8 3275 "latest_snapshot_event" = 'periodic',
jbe@0 3276 "population" = (
jbe@0 3277 SELECT coalesce(sum("weight"), 0)
jbe@0 3278 FROM "direct_population_snapshot"
jbe@0 3279 WHERE "issue_id" = "issue_id_p"
jbe@0 3280 AND "event" = 'periodic'
jbe@0 3281 )
jbe@0 3282 WHERE "id" = "issue_id_p";
jbe@0 3283 FOR "initiative_id_v" IN
jbe@0 3284 SELECT "id" FROM "initiative" WHERE "issue_id" = "issue_id_p"
jbe@0 3285 LOOP
jbe@0 3286 UPDATE "initiative" SET
jbe@0 3287 "supporter_count" = (
jbe@0 3288 SELECT coalesce(sum("di"."weight"), 0)
jbe@0 3289 FROM "direct_interest_snapshot" AS "di"
jbe@0 3290 JOIN "direct_supporter_snapshot" AS "ds"
jbe@0 3291 ON "di"."member_id" = "ds"."member_id"
jbe@0 3292 WHERE "di"."issue_id" = "issue_id_p"
jbe@0 3293 AND "di"."event" = 'periodic'
jbe@0 3294 AND "ds"."initiative_id" = "initiative_id_v"
jbe@0 3295 AND "ds"."event" = 'periodic'
jbe@0 3296 ),
jbe@0 3297 "informed_supporter_count" = (
jbe@0 3298 SELECT coalesce(sum("di"."weight"), 0)
jbe@0 3299 FROM "direct_interest_snapshot" AS "di"
jbe@0 3300 JOIN "direct_supporter_snapshot" AS "ds"
jbe@0 3301 ON "di"."member_id" = "ds"."member_id"
jbe@0 3302 WHERE "di"."issue_id" = "issue_id_p"
jbe@0 3303 AND "di"."event" = 'periodic'
jbe@0 3304 AND "ds"."initiative_id" = "initiative_id_v"
jbe@0 3305 AND "ds"."event" = 'periodic'
jbe@0 3306 AND "ds"."informed"
jbe@0 3307 ),
jbe@0 3308 "satisfied_supporter_count" = (
jbe@0 3309 SELECT coalesce(sum("di"."weight"), 0)
jbe@0 3310 FROM "direct_interest_snapshot" AS "di"
jbe@0 3311 JOIN "direct_supporter_snapshot" AS "ds"
jbe@0 3312 ON "di"."member_id" = "ds"."member_id"
jbe@0 3313 WHERE "di"."issue_id" = "issue_id_p"
jbe@0 3314 AND "di"."event" = 'periodic'
jbe@0 3315 AND "ds"."initiative_id" = "initiative_id_v"
jbe@0 3316 AND "ds"."event" = 'periodic'
jbe@0 3317 AND "ds"."satisfied"
jbe@0 3318 ),
jbe@0 3319 "satisfied_informed_supporter_count" = (
jbe@0 3320 SELECT coalesce(sum("di"."weight"), 0)
jbe@0 3321 FROM "direct_interest_snapshot" AS "di"
jbe@0 3322 JOIN "direct_supporter_snapshot" AS "ds"
jbe@0 3323 ON "di"."member_id" = "ds"."member_id"
jbe@0 3324 WHERE "di"."issue_id" = "issue_id_p"
jbe@0 3325 AND "di"."event" = 'periodic'
jbe@0 3326 AND "ds"."initiative_id" = "initiative_id_v"
jbe@0 3327 AND "ds"."event" = 'periodic'
jbe@0 3328 AND "ds"."informed"
jbe@0 3329 AND "ds"."satisfied"
jbe@0 3330 )
jbe@0 3331 WHERE "id" = "initiative_id_v";
jbe@0 3332 FOR "suggestion_id_v" IN
jbe@0 3333 SELECT "id" FROM "suggestion"
jbe@0 3334 WHERE "initiative_id" = "initiative_id_v"
jbe@0 3335 LOOP
jbe@0 3336 UPDATE "suggestion" SET
jbe@0 3337 "minus2_unfulfilled_count" = (
jbe@0 3338 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3339 FROM "issue" CROSS JOIN "opinion"
jbe@36 3340 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3341 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3342 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3343 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3344 WHERE "issue"."id" = "issue_id_p"
jbe@36 3345 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3346 AND "opinion"."degree" = -2
jbe@0 3347 AND "opinion"."fulfilled" = FALSE
jbe@0 3348 ),
jbe@0 3349 "minus2_fulfilled_count" = (
jbe@0 3350 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3351 FROM "issue" CROSS JOIN "opinion"
jbe@36 3352 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3353 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3354 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3355 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3356 WHERE "issue"."id" = "issue_id_p"
jbe@36 3357 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3358 AND "opinion"."degree" = -2
jbe@0 3359 AND "opinion"."fulfilled" = TRUE
jbe@0 3360 ),
jbe@0 3361 "minus1_unfulfilled_count" = (
jbe@0 3362 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3363 FROM "issue" CROSS JOIN "opinion"
jbe@36 3364 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3365 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3366 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3367 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3368 WHERE "issue"."id" = "issue_id_p"
jbe@36 3369 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3370 AND "opinion"."degree" = -1
jbe@0 3371 AND "opinion"."fulfilled" = FALSE
jbe@0 3372 ),
jbe@0 3373 "minus1_fulfilled_count" = (
jbe@0 3374 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3375 FROM "issue" CROSS JOIN "opinion"
jbe@36 3376 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3377 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3378 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3379 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3380 WHERE "issue"."id" = "issue_id_p"
jbe@36 3381 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3382 AND "opinion"."degree" = -1
jbe@0 3383 AND "opinion"."fulfilled" = TRUE
jbe@0 3384 ),
jbe@0 3385 "plus1_unfulfilled_count" = (
jbe@0 3386 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3387 FROM "issue" CROSS JOIN "opinion"
jbe@36 3388 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3389 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3390 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3391 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3392 WHERE "issue"."id" = "issue_id_p"
jbe@36 3393 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3394 AND "opinion"."degree" = 1
jbe@0 3395 AND "opinion"."fulfilled" = FALSE
jbe@0 3396 ),
jbe@0 3397 "plus1_fulfilled_count" = (
jbe@0 3398 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3399 FROM "issue" CROSS JOIN "opinion"
jbe@36 3400 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3401 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3402 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3403 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3404 WHERE "issue"."id" = "issue_id_p"
jbe@36 3405 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3406 AND "opinion"."degree" = 1
jbe@0 3407 AND "opinion"."fulfilled" = TRUE
jbe@0 3408 ),
jbe@0 3409 "plus2_unfulfilled_count" = (
jbe@0 3410 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3411 FROM "issue" CROSS JOIN "opinion"
jbe@36 3412 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3413 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3414 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3415 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3416 WHERE "issue"."id" = "issue_id_p"
jbe@36 3417 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3418 AND "opinion"."degree" = 2
jbe@0 3419 AND "opinion"."fulfilled" = FALSE
jbe@0 3420 ),
jbe@0 3421 "plus2_fulfilled_count" = (
jbe@0 3422 SELECT coalesce(sum("snapshot"."weight"), 0)
jbe@36 3423 FROM "issue" CROSS JOIN "opinion"
jbe@36 3424 JOIN "direct_interest_snapshot" AS "snapshot"
jbe@36 3425 ON "snapshot"."issue_id" = "issue"."id"
jbe@36 3426 AND "snapshot"."event" = "issue"."latest_snapshot_event"
jbe@36 3427 AND "snapshot"."member_id" = "opinion"."member_id"
jbe@36 3428 WHERE "issue"."id" = "issue_id_p"
jbe@36 3429 AND "opinion"."suggestion_id" = "suggestion_id_v"
jbe@0 3430 AND "opinion"."degree" = 2
jbe@0 3431 AND "opinion"."fulfilled" = TRUE
jbe@0 3432 )
jbe@0 3433 WHERE "suggestion"."id" = "suggestion_id_v";
jbe@0 3434 END LOOP;
jbe@0 3435 END LOOP;
jbe@0 3436 RETURN;
jbe@0 3437 END;
jbe@0 3438 $$;
jbe@0 3439
jbe@0 3440 COMMENT ON FUNCTION "create_snapshot"
jbe@0 3441 ( "issue"."id"%TYPE )
jbe@0 3442 IS 'This function creates a complete new ''periodic'' snapshot of population, interest and support for the given issue. All involved tables are locked, and after completion precalculated values in the source tables are updated.';
jbe@0 3443
jbe@0 3444
jbe@0 3445 CREATE FUNCTION "set_snapshot_event"
jbe@0 3446 ( "issue_id_p" "issue"."id"%TYPE,
jbe@0 3447 "event_p" "snapshot_event" )
jbe@0 3448 RETURNS VOID
jbe@0 3449 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@21 3450 DECLARE
jbe@21 3451 "event_v" "issue"."latest_snapshot_event"%TYPE;
jbe@0 3452 BEGIN
jbe@21 3453 SELECT "latest_snapshot_event" INTO "event_v" FROM "issue"
jbe@21 3454 WHERE "id" = "issue_id_p" FOR UPDATE;
jbe@8 3455 UPDATE "issue" SET "latest_snapshot_event" = "event_p"
jbe@8 3456 WHERE "id" = "issue_id_p";
jbe@3 3457 UPDATE "direct_population_snapshot" SET "event" = "event_p"
jbe@21 3458 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
jbe@3 3459 UPDATE "delegating_population_snapshot" SET "event" = "event_p"
jbe@21 3460 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
jbe@3 3461 UPDATE "direct_interest_snapshot" SET "event" = "event_p"
jbe@21 3462 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
jbe@3 3463 UPDATE "delegating_interest_snapshot" SET "event" = "event_p"
jbe@21 3464 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
jbe@3 3465 UPDATE "direct_supporter_snapshot" SET "event" = "event_p"
jbe@21 3466 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
jbe@0 3467 RETURN;
jbe@0 3468 END;
jbe@0 3469 $$;
jbe@0 3470
jbe@0 3471 COMMENT ON FUNCTION "set_snapshot_event"
jbe@0 3472 ( "issue"."id"%TYPE,
jbe@0 3473 "snapshot_event" )
jbe@0 3474 IS 'Change "event" attribute of the previous ''periodic'' snapshot';
jbe@0 3475
jbe@0 3476
jbe@0 3477
jbe@0 3478 ---------------------
jbe@0 3479 -- Freezing issues --
jbe@0 3480 ---------------------
jbe@0 3481
jbe@0 3482 CREATE FUNCTION "freeze_after_snapshot"
jbe@0 3483 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3484 RETURNS VOID
jbe@0 3485 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3486 DECLARE
jbe@0 3487 "issue_row" "issue"%ROWTYPE;
jbe@0 3488 "policy_row" "policy"%ROWTYPE;
jbe@0 3489 "initiative_row" "initiative"%ROWTYPE;
jbe@0 3490 BEGIN
jbe@0 3491 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
jbe@0 3492 SELECT * INTO "policy_row"
jbe@0 3493 FROM "policy" WHERE "id" = "issue_row"."policy_id";
jbe@21 3494 PERFORM "set_snapshot_event"("issue_id_p", 'full_freeze');
jbe@0 3495 FOR "initiative_row" IN
jbe@15 3496 SELECT * FROM "initiative"
jbe@15 3497 WHERE "issue_id" = "issue_id_p" AND "revoked" ISNULL
jbe@0 3498 LOOP
jbe@0 3499 IF
jbe@0 3500 "initiative_row"."satisfied_supporter_count" > 0 AND
jbe@0 3501 "initiative_row"."satisfied_supporter_count" *
jbe@0 3502 "policy_row"."initiative_quorum_den" >=
jbe@0 3503 "issue_row"."population" * "policy_row"."initiative_quorum_num"
jbe@0 3504 THEN
jbe@0 3505 UPDATE "initiative" SET "admitted" = TRUE
jbe@0 3506 WHERE "id" = "initiative_row"."id";
jbe@0 3507 ELSE
jbe@0 3508 UPDATE "initiative" SET "admitted" = FALSE
jbe@0 3509 WHERE "id" = "initiative_row"."id";
jbe@0 3510 END IF;
jbe@0 3511 END LOOP;
jbe@113 3512 IF EXISTS (
jbe@9 3513 SELECT NULL FROM "initiative"
jbe@9 3514 WHERE "issue_id" = "issue_id_p" AND "admitted" = TRUE
jbe@9 3515 ) THEN
jbe@113 3516 UPDATE "issue" SET
jbe@113 3517 "state" = 'voting',
jbe@113 3518 "accepted" = coalesce("accepted", now()),
jbe@113 3519 "half_frozen" = coalesce("half_frozen", now()),
jbe@113 3520 "fully_frozen" = now()
jbe@113 3521 WHERE "id" = "issue_id_p";
jbe@113 3522 ELSE
jbe@113 3523 UPDATE "issue" SET
jbe@121 3524 "state" = 'canceled_no_initiative_admitted',
jbe@121 3525 "accepted" = coalesce("accepted", now()),
jbe@121 3526 "half_frozen" = coalesce("half_frozen", now()),
jbe@121 3527 "fully_frozen" = now(),
jbe@121 3528 "closed" = now(),
jbe@121 3529 "ranks_available" = TRUE
jbe@113 3530 WHERE "id" = "issue_id_p";
jbe@113 3531 -- NOTE: The following DELETE statements have effect only when
jbe@113 3532 -- issue state has been manipulated
jbe@113 3533 DELETE FROM "direct_voter" WHERE "issue_id" = "issue_id_p";
jbe@113 3534 DELETE FROM "delegating_voter" WHERE "issue_id" = "issue_id_p";
jbe@113 3535 DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
jbe@9 3536 END IF;
jbe@0 3537 RETURN;
jbe@0 3538 END;
jbe@0 3539 $$;
jbe@0 3540
jbe@0 3541 COMMENT ON FUNCTION "freeze_after_snapshot"
jbe@0 3542 ( "issue"."id"%TYPE )
jbe@9 3543 IS 'This function freezes an issue (fully) and starts voting, but must only be called when "create_snapshot" was called in the same transaction.';
jbe@0 3544
jbe@0 3545
jbe@0 3546 CREATE FUNCTION "manual_freeze"("issue_id_p" "issue"."id"%TYPE)
jbe@0 3547 RETURNS VOID
jbe@0 3548 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3549 DECLARE
jbe@0 3550 "issue_row" "issue"%ROWTYPE;
jbe@0 3551 BEGIN
jbe@0 3552 PERFORM "create_snapshot"("issue_id_p");
jbe@0 3553 PERFORM "freeze_after_snapshot"("issue_id_p");
jbe@0 3554 RETURN;
jbe@0 3555 END;
jbe@0 3556 $$;
jbe@0 3557
jbe@55 3558 COMMENT ON FUNCTION "manual_freeze"
jbe@0 3559 ( "issue"."id"%TYPE )
jbe@3 3560 IS 'Freeze an issue manually (fully) and start voting';
jbe@0 3561
jbe@0 3562
jbe@0 3563
jbe@0 3564 -----------------------
jbe@0 3565 -- Counting of votes --
jbe@0 3566 -----------------------
jbe@0 3567
jbe@0 3568
jbe@5 3569 CREATE FUNCTION "weight_of_added_vote_delegations"
jbe@0 3570 ( "issue_id_p" "issue"."id"%TYPE,
jbe@0 3571 "member_id_p" "member"."id"%TYPE,
jbe@0 3572 "delegate_member_ids_p" "delegating_voter"."delegate_member_ids"%TYPE )
jbe@0 3573 RETURNS "direct_voter"."weight"%TYPE
jbe@0 3574 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3575 DECLARE
jbe@0 3576 "issue_delegation_row" "issue_delegation"%ROWTYPE;
jbe@0 3577 "delegate_member_ids_v" "delegating_voter"."delegate_member_ids"%TYPE;
jbe@0 3578 "weight_v" INT4;
jbe@8 3579 "sub_weight_v" INT4;
jbe@0 3580 BEGIN
jbe@0 3581 "weight_v" := 0;
jbe@0 3582 FOR "issue_delegation_row" IN
jbe@0 3583 SELECT * FROM "issue_delegation"
jbe@0 3584 WHERE "trustee_id" = "member_id_p"
jbe@0 3585 AND "issue_id" = "issue_id_p"
jbe@0 3586 LOOP
jbe@0 3587 IF NOT EXISTS (
jbe@0 3588 SELECT NULL FROM "direct_voter"
jbe@0 3589 WHERE "member_id" = "issue_delegation_row"."truster_id"
jbe@0 3590 AND "issue_id" = "issue_id_p"
jbe@0 3591 ) AND NOT EXISTS (
jbe@0 3592 SELECT NULL FROM "delegating_voter"
jbe@0 3593 WHERE "member_id" = "issue_delegation_row"."truster_id"
jbe@0 3594 AND "issue_id" = "issue_id_p"
jbe@0 3595 ) THEN
jbe@0 3596 "delegate_member_ids_v" :=
jbe@0 3597 "member_id_p" || "delegate_member_ids_p";
jbe@10 3598 INSERT INTO "delegating_voter" (
jbe@10 3599 "issue_id",
jbe@10 3600 "member_id",
jbe@10 3601 "scope",
jbe@10 3602 "delegate_member_ids"
jbe@10 3603 ) VALUES (
jbe@5 3604 "issue_id_p",
jbe@5 3605 "issue_delegation_row"."truster_id",
jbe@10 3606 "issue_delegation_row"."scope",
jbe@5 3607 "delegate_member_ids_v"
jbe@5 3608 );
jbe@8 3609 "sub_weight_v" := 1 +
jbe@8 3610 "weight_of_added_vote_delegations"(
jbe@8 3611 "issue_id_p",
jbe@8 3612 "issue_delegation_row"."truster_id",
jbe@8 3613 "delegate_member_ids_v"
jbe@8 3614 );
jbe@8 3615 UPDATE "delegating_voter"
jbe@8 3616 SET "weight" = "sub_weight_v"
jbe@8 3617 WHERE "issue_id" = "issue_id_p"
jbe@8 3618 AND "member_id" = "issue_delegation_row"."truster_id";
jbe@8 3619 "weight_v" := "weight_v" + "sub_weight_v";
jbe@0 3620 END IF;
jbe@0 3621 END LOOP;
jbe@0 3622 RETURN "weight_v";
jbe@0 3623 END;
jbe@0 3624 $$;
jbe@0 3625
jbe@5 3626 COMMENT ON FUNCTION "weight_of_added_vote_delegations"
jbe@0 3627 ( "issue"."id"%TYPE,
jbe@0 3628 "member"."id"%TYPE,
jbe@0 3629 "delegating_voter"."delegate_member_ids"%TYPE )
jbe@0 3630 IS 'Helper function for "add_vote_delegations" function';
jbe@0 3631
jbe@0 3632
jbe@0 3633 CREATE FUNCTION "add_vote_delegations"
jbe@0 3634 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3635 RETURNS VOID
jbe@0 3636 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3637 DECLARE
jbe@0 3638 "member_id_v" "member"."id"%TYPE;
jbe@0 3639 BEGIN
jbe@0 3640 FOR "member_id_v" IN
jbe@0 3641 SELECT "member_id" FROM "direct_voter"
jbe@0 3642 WHERE "issue_id" = "issue_id_p"
jbe@0 3643 LOOP
jbe@0 3644 UPDATE "direct_voter" SET
jbe@5 3645 "weight" = "weight" + "weight_of_added_vote_delegations"(
jbe@0 3646 "issue_id_p",
jbe@0 3647 "member_id_v",
jbe@0 3648 '{}'
jbe@0 3649 )
jbe@0 3650 WHERE "member_id" = "member_id_v"
jbe@0 3651 AND "issue_id" = "issue_id_p";
jbe@0 3652 END LOOP;
jbe@0 3653 RETURN;
jbe@0 3654 END;
jbe@0 3655 $$;
jbe@0 3656
jbe@0 3657 COMMENT ON FUNCTION "add_vote_delegations"
jbe@0 3658 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 3659 IS 'Helper function for "close_voting" function';
jbe@0 3660
jbe@0 3661
jbe@0 3662 CREATE FUNCTION "close_voting"("issue_id_p" "issue"."id"%TYPE)
jbe@0 3663 RETURNS VOID
jbe@0 3664 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3665 DECLARE
jbe@97 3666 "area_id_v" "area"."id"%TYPE;
jbe@97 3667 "unit_id_v" "unit"."id"%TYPE;
jbe@0 3668 "member_id_v" "member"."id"%TYPE;
jbe@0 3669 BEGIN
jbe@67 3670 PERFORM "lock_issue"("issue_id_p");
jbe@129 3671 SELECT "area_id" INTO "area_id_v" FROM "issue" WHERE "id" = "issue_id_p";
jbe@129 3672 SELECT "unit_id" INTO "unit_id_v" FROM "area" WHERE "id" = "area_id_v";
jbe@169 3673 -- delete delegating votes (in cases of manual reset of issue state):
jbe@0 3674 DELETE FROM "delegating_voter"
jbe@0 3675 WHERE "issue_id" = "issue_id_p";
jbe@169 3676 -- delete votes from non-privileged voters:
jbe@97 3677 DELETE FROM "direct_voter"
jbe@97 3678 USING (
jbe@97 3679 SELECT
jbe@97 3680 "direct_voter"."member_id"
jbe@97 3681 FROM "direct_voter"
jbe@97 3682 JOIN "member" ON "direct_voter"."member_id" = "member"."id"
jbe@97 3683 LEFT JOIN "privilege"
jbe@97 3684 ON "privilege"."unit_id" = "unit_id_v"
jbe@97 3685 AND "privilege"."member_id" = "direct_voter"."member_id"
jbe@97 3686 WHERE "direct_voter"."issue_id" = "issue_id_p" AND (
jbe@97 3687 "member"."active" = FALSE OR
jbe@97 3688 "privilege"."voting_right" ISNULL OR
jbe@97 3689 "privilege"."voting_right" = FALSE
jbe@97 3690 )
jbe@97 3691 ) AS "subquery"
jbe@97 3692 WHERE "direct_voter"."issue_id" = "issue_id_p"
jbe@97 3693 AND "direct_voter"."member_id" = "subquery"."member_id";
jbe@169 3694 -- consider delegations:
jbe@0 3695 UPDATE "direct_voter" SET "weight" = 1
jbe@0 3696 WHERE "issue_id" = "issue_id_p";
jbe@0 3697 PERFORM "add_vote_delegations"("issue_id_p");
jbe@137 3698 -- set voter count and mark issue as being calculated:
jbe@4 3699 UPDATE "issue" SET
jbe@111 3700 "state" = 'calculation',
jbe@61 3701 "closed" = now(),
jbe@4 3702 "voter_count" = (
jbe@4 3703 SELECT coalesce(sum("weight"), 0)
jbe@4 3704 FROM "direct_voter" WHERE "issue_id" = "issue_id_p"
jbe@6 3705 )
jbe@6 3706 WHERE "id" = "issue_id_p";
jbe@137 3707 -- materialize battle_view:
jbe@61 3708 -- NOTE: "closed" column of issue must be set at this point
jbe@61 3709 DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
jbe@61 3710 INSERT INTO "battle" (
jbe@61 3711 "issue_id",
jbe@61 3712 "winning_initiative_id", "losing_initiative_id",
jbe@61 3713 "count"
jbe@61 3714 ) SELECT
jbe@61 3715 "issue_id",
jbe@61 3716 "winning_initiative_id", "losing_initiative_id",
jbe@61 3717 "count"
jbe@61 3718 FROM "battle_view" WHERE "issue_id" = "issue_id_p";
jbe@155 3719 -- copy "positive_votes" and "negative_votes" from "battle" table:
jbe@155 3720 UPDATE "initiative" SET
jbe@155 3721 "positive_votes" = "battle_win"."count",
jbe@155 3722 "negative_votes" = "battle_lose"."count"
jbe@155 3723 FROM "battle" AS "battle_win", "battle" AS "battle_lose"
jbe@155 3724 WHERE
jbe@155 3725 "battle_win"."issue_id" = "issue_id_p" AND
jbe@155 3726 "battle_win"."winning_initiative_id" = "initiative"."id" AND
jbe@155 3727 "battle_win"."losing_initiative_id" ISNULL AND
jbe@155 3728 "battle_lose"."issue_id" = "issue_id_p" AND
jbe@155 3729 "battle_lose"."losing_initiative_id" = "initiative"."id" AND
jbe@155 3730 "battle_lose"."winning_initiative_id" ISNULL;
jbe@0 3731 END;
jbe@0 3732 $$;
jbe@0 3733
jbe@0 3734 COMMENT ON FUNCTION "close_voting"
jbe@0 3735 ( "issue"."id"%TYPE )
jbe@0 3736 IS 'Closes the voting on an issue, and calculates positive and negative votes for each initiative; The ranking is not calculated yet, to keep the (locking) transaction short.';
jbe@0 3737
jbe@0 3738
jbe@30 3739 CREATE FUNCTION "defeat_strength"
jbe@30 3740 ( "positive_votes_p" INT4, "negative_votes_p" INT4 )
jbe@30 3741 RETURNS INT8
jbe@30 3742 LANGUAGE 'plpgsql' IMMUTABLE AS $$
jbe@30 3743 BEGIN
jbe@30 3744 IF "positive_votes_p" > "negative_votes_p" THEN
jbe@30 3745 RETURN ("positive_votes_p"::INT8 << 31) - "negative_votes_p"::INT8;
jbe@30 3746 ELSIF "positive_votes_p" = "negative_votes_p" THEN
jbe@30 3747 RETURN 0;
jbe@30 3748 ELSE
jbe@30 3749 RETURN -1;
jbe@30 3750 END IF;
jbe@30 3751 END;
jbe@30 3752 $$;
jbe@30 3753
jbe@30 3754 COMMENT ON FUNCTION "defeat_strength"(INT4, INT4) IS 'Calculates defeat strength (INT8!) of a pairwise defeat primarily by the absolute number of votes for the winner and secondarily by the absolute number of votes for the loser';
jbe@30 3755
jbe@30 3756
jbe@0 3757 CREATE FUNCTION "calculate_ranks"("issue_id_p" "issue"."id"%TYPE)
jbe@0 3758 RETURNS VOID
jbe@0 3759 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 3760 DECLARE
jbe@155 3761 "issue_row" "issue"%ROWTYPE;
jbe@155 3762 "policy_row" "policy"%ROWTYPE;
jbe@134 3763 "dimension_v" INTEGER;
jbe@134 3764 "vote_matrix" INT4[][]; -- absolute votes
jbe@134 3765 "matrix" INT8[][]; -- defeat strength / best paths
jbe@134 3766 "i" INTEGER;
jbe@134 3767 "j" INTEGER;
jbe@134 3768 "k" INTEGER;
jbe@134 3769 "battle_row" "battle"%ROWTYPE;
jbe@134 3770 "rank_ary" INT4[];
jbe@134 3771 "rank_v" INT4;
jbe@134 3772 "done_v" INTEGER;
jbe@134 3773 "winners_ary" INTEGER[];
jbe@134 3774 "initiative_id_v" "initiative"."id"%TYPE;
jbe@0 3775 BEGIN
jbe@155 3776 SELECT * INTO "issue_row"
jbe@155 3777 FROM "issue" WHERE "id" = "issue_id_p"
jbe@155 3778 FOR UPDATE;
jbe@155 3779 SELECT * INTO "policy_row"
jbe@155 3780 FROM "policy" WHERE "id" = "issue_row"."policy_id";
jbe@126 3781 SELECT count(1) INTO "dimension_v"
jbe@126 3782 FROM "battle_participant" WHERE "issue_id" = "issue_id_p";
jbe@170 3783 -- Create "vote_matrix" with absolute number of votes in pairwise
jbe@170 3784 -- comparison:
jbe@170 3785 "vote_matrix" := array_fill(NULL::INT4, ARRAY["dimension_v", "dimension_v"]);
jbe@170 3786 "i" := 1;
jbe@170 3787 "j" := 2;
jbe@170 3788 FOR "battle_row" IN
jbe@170 3789 SELECT * FROM "battle" WHERE "issue_id" = "issue_id_p"
jbe@170 3790 ORDER BY
jbe@170 3791 "winning_initiative_id" NULLS LAST,
jbe@170 3792 "losing_initiative_id" NULLS LAST
jbe@170 3793 LOOP
jbe@170 3794 "vote_matrix"["i"]["j"] := "battle_row"."count";
jbe@170 3795 IF "j" = "dimension_v" THEN
jbe@170 3796 "i" := "i" + 1;
jbe@170 3797 "j" := 1;
jbe@170 3798 ELSE
jbe@170 3799 "j" := "j" + 1;
jbe@170 3800 IF "j" = "i" THEN
jbe@170 3801 "j" := "j" + 1;
jbe@170 3802 END IF;
jbe@170 3803 END IF;
jbe@170 3804 END LOOP;
jbe@170 3805 IF "i" != "dimension_v" OR "j" != "dimension_v" + 1 THEN
jbe@170 3806 RAISE EXCEPTION 'Wrong battle count (should not happen)';
jbe@170 3807 END IF;
jbe@170 3808 -- Store defeat strengths in "matrix" using "defeat_strength"
jbe@170 3809 -- function:
jbe@170 3810 "matrix" := array_fill(NULL::INT8, ARRAY["dimension_v", "dimension_v"]);
jbe@170 3811 "i" := 1;
jbe@170 3812 LOOP
jbe@170 3813 "j" := 1;
jbe@0 3814 LOOP
jbe@170 3815 IF "i" != "j" THEN
jbe@170 3816 "matrix"["i"]["j"] := "defeat_strength"(
jbe@170 3817 "vote_matrix"["i"]["j"],
jbe@170 3818 "vote_matrix"["j"]["i"]
jbe@170 3819 );
jbe@0 3820 END IF;
jbe@170 3821 EXIT WHEN "j" = "dimension_v";
jbe@170 3822 "j" := "j" + 1;
jbe@0 3823 END LOOP;
jbe@170 3824 EXIT WHEN "i" = "dimension_v";
jbe@170 3825 "i" := "i" + 1;
jbe@170 3826 END LOOP;
jbe@170 3827 -- Find best paths:
jbe@170 3828 "i" := 1;
jbe@170 3829 LOOP
jbe@170 3830 "j" := 1;
jbe@170 3831 LOOP
jbe@170 3832 IF "i" != "j" THEN
jbe@170 3833 "k" := 1;
jbe@170 3834 LOOP
jbe@170 3835 IF "i" != "k" AND "j" != "k" THEN
jbe@170 3836 IF "matrix"["j"]["i"] < "matrix"["i"]["k"] THEN
jbe@170 3837 IF "matrix"["j"]["i"] > "matrix"["j"]["k"] THEN
jbe@170 3838 "matrix"["j"]["k"] := "matrix"["j"]["i"];
jbe@170 3839 END IF;
jbe@170 3840 ELSE
jbe@170 3841 IF "matrix"["i"]["k"] > "matrix"["j"]["k"] THEN
jbe@170 3842 "matrix"["j"]["k"] := "matrix"["i"]["k"];
jbe@170 3843 END IF;
jbe@170 3844 END IF;
jbe@170 3845 END IF;
jbe@170 3846 EXIT WHEN "k" = "dimension_v";
jbe@170 3847 "k" := "k" + 1;
jbe@170 3848 END LOOP;
jbe@170 3849 END IF;
jbe@170 3850 EXIT WHEN "j" = "dimension_v";
jbe@170 3851 "j" := "j" + 1;
jbe@170 3852 END LOOP;
jbe@170 3853 EXIT WHEN "i" = "dimension_v";
jbe@170 3854 "i" := "i" + 1;
jbe@170 3855 END LOOP;
jbe@170 3856 -- Determine order of winners:
jbe@170 3857 "rank_ary" := array_fill(NULL::INT4, ARRAY["dimension_v"]);
jbe@170 3858 "rank_v" := 1;
jbe@170 3859 "done_v" := 0;
jbe@170 3860 LOOP
jbe@170 3861 "winners_ary" := '{}';
jbe@0 3862 "i" := 1;
jbe@0 3863 LOOP
jbe@170 3864 IF "rank_ary"["i"] ISNULL THEN
jbe@170 3865 "j" := 1;
jbe@170 3866 LOOP
jbe@170 3867 IF
jbe@170 3868 "i" != "j" AND
jbe@170 3869 "rank_ary"["j"] ISNULL AND
jbe@170 3870 "matrix"["j"]["i"] > "matrix"["i"]["j"]
jbe@170 3871 THEN
jbe@170 3872 -- someone else is better
jbe@170 3873 EXIT;
jbe@170 3874 END IF;
jbe@170 3875 IF "j" = "dimension_v" THEN
jbe@170 3876 -- noone is better
jbe@170 3877 "winners_ary" := "winners_ary" || "i";
jbe@170 3878 EXIT;
jbe@170 3879 END IF;
jbe@170 3880 "j" := "j" + 1;
jbe@170 3881 END LOOP;
jbe@170 3882 END IF;
jbe@30 3883 EXIT WHEN "i" = "dimension_v";
jbe@0 3884 "i" := "i" + 1;
jbe@0 3885 END LOOP;
jbe@0 3886 "i" := 1;
jbe@0 3887 LOOP
jbe@170 3888 "rank_ary"["winners_ary"["i"]] := "rank_v";
jbe@170 3889 "done_v" := "done_v" + 1;
jbe@170 3890 EXIT WHEN "i" = array_upper("winners_ary", 1);
jbe@0 3891 "i" := "i" + 1;
jbe@0 3892 END LOOP;
jbe@170 3893 EXIT WHEN "done_v" = "dimension_v";
jbe@170 3894 "rank_v" := "rank_v" + 1;
jbe@170 3895 END LOOP;
jbe@170 3896 -- write preliminary results:
jbe@170 3897 "i" := 1;
jbe@170 3898 FOR "initiative_id_v" IN
jbe@170 3899 SELECT "id" FROM "initiative"
jbe@170 3900 WHERE "issue_id" = "issue_id_p" AND "admitted"
jbe@170 3901 ORDER BY "id"
jbe@170 3902 LOOP
jbe@170 3903 UPDATE "initiative" SET
jbe@170 3904 "direct_majority" =
jbe@170 3905 CASE WHEN "policy_row"."direct_majority_strict" THEN
jbe@170 3906 "positive_votes" * "policy_row"."direct_majority_den" >
jbe@170 3907 "policy_row"."direct_majority_num" * ("positive_votes"+"negative_votes")
jbe@170 3908 ELSE
jbe@170 3909 "positive_votes" * "policy_row"."direct_majority_den" >=
jbe@170 3910 "policy_row"."direct_majority_num" * ("positive_votes"+"negative_votes")
jbe@170 3911 END
jbe@170 3912 AND "positive_votes" >= "policy_row"."direct_majority_positive"
jbe@170 3913 AND "issue_row"."voter_count"-"negative_votes" >=
jbe@170 3914 "policy_row"."direct_majority_non_negative",
jbe@170 3915 "indirect_majority" =
jbe@170 3916 CASE WHEN "policy_row"."indirect_majority_strict" THEN
jbe@170 3917 "positive_votes" * "policy_row"."indirect_majority_den" >
jbe@170 3918 "policy_row"."indirect_majority_num" * ("positive_votes"+"negative_votes")
jbe@170 3919 ELSE
jbe@170 3920 "positive_votes" * "policy_row"."indirect_majority_den" >=
jbe@170 3921 "policy_row"."indirect_majority_num" * ("positive_votes"+"negative_votes")
jbe@170 3922 END
jbe@170 3923 AND "positive_votes" >= "policy_row"."indirect_majority_positive"
jbe@170 3924 AND "issue_row"."voter_count"-"negative_votes" >=
jbe@170 3925 "policy_row"."indirect_majority_non_negative",
jbe@171 3926 "schulze_rank" = "rank_ary"["i"],
jbe@170 3927 "better_than_status_quo" = "rank_ary"["i"] < "rank_ary"["dimension_v"],
jbe@170 3928 "worse_than_status_quo" = "rank_ary"["i"] > "rank_ary"["dimension_v"],
jbe@170 3929 "multistage_majority" = "rank_ary"["i"] >= "rank_ary"["dimension_v"],
jbe@172 3930 "reverse_beat_path" = "matrix"["dimension_v"]["i"] >= 0,
jbe@216 3931 "eligible" = FALSE,
jbe@250 3932 "winner" = FALSE,
jbe@250 3933 "rank" = NULL -- NOTE: in cases of manual reset of issue state
jbe@170 3934 WHERE "id" = "initiative_id_v";
jbe@170 3935 "i" := "i" + 1;
jbe@170 3936 END LOOP;
jbe@170 3937 IF "i" != "dimension_v" THEN
jbe@170 3938 RAISE EXCEPTION 'Wrong winner count (should not happen)';
jbe@0 3939 END IF;
jbe@170 3940 -- take indirect majorities into account:
jbe@170 3941 LOOP
jbe@170 3942 UPDATE "initiative" SET "indirect_majority" = TRUE
jbe@139 3943 FROM (
jbe@170 3944 SELECT "new_initiative"."id" AS "initiative_id"
jbe@170 3945 FROM "initiative" "old_initiative"
jbe@170 3946 JOIN "initiative" "new_initiative"
jbe@170 3947 ON "new_initiative"."issue_id" = "issue_id_p"
jbe@170 3948 AND "new_initiative"."indirect_majority" = FALSE
jbe@139 3949 JOIN "battle" "battle_win"
jbe@139 3950 ON "battle_win"."issue_id" = "issue_id_p"
jbe@170 3951 AND "battle_win"."winning_initiative_id" = "new_initiative"."id"
jbe@170 3952 AND "battle_win"."losing_initiative_id" = "old_initiative"."id"
jbe@139 3953 JOIN "battle" "battle_lose"
jbe@139 3954 ON "battle_lose"."issue_id" = "issue_id_p"
jbe@170 3955 AND "battle_lose"."losing_initiative_id" = "new_initiative"."id"
jbe@170 3956 AND "battle_lose"."winning_initiative_id" = "old_initiative"."id"
jbe@170 3957 WHERE "old_initiative"."issue_id" = "issue_id_p"
jbe@170 3958 AND "old_initiative"."indirect_majority" = TRUE
jbe@170 3959 AND CASE WHEN "policy_row"."indirect_majority_strict" THEN
jbe@170 3960 "battle_win"."count" * "policy_row"."indirect_majority_den" >
jbe@170 3961 "policy_row"."indirect_majority_num" *
jbe@170 3962 ("battle_win"."count"+"battle_lose"."count")
jbe@170 3963 ELSE
jbe@170 3964 "battle_win"."count" * "policy_row"."indirect_majority_den" >=
jbe@170 3965 "policy_row"."indirect_majority_num" *
jbe@170 3966 ("battle_win"."count"+"battle_lose"."count")
jbe@170 3967 END
jbe@170 3968 AND "battle_win"."count" >= "policy_row"."indirect_majority_positive"
jbe@170 3969 AND "issue_row"."voter_count"-"battle_lose"."count" >=
jbe@170 3970 "policy_row"."indirect_majority_non_negative"
jbe@139 3971 ) AS "subquery"
jbe@139 3972 WHERE "id" = "subquery"."initiative_id";
jbe@170 3973 EXIT WHEN NOT FOUND;
jbe@170 3974 END LOOP;
jbe@170 3975 -- set "multistage_majority" for remaining matching initiatives:
jbe@216 3976 UPDATE "initiative" SET "multistage_majority" = TRUE
jbe@170 3977 FROM (
jbe@170 3978 SELECT "losing_initiative"."id" AS "initiative_id"
jbe@170 3979 FROM "initiative" "losing_initiative"
jbe@170 3980 JOIN "initiative" "winning_initiative"
jbe@170 3981 ON "winning_initiative"."issue_id" = "issue_id_p"
jbe@170 3982 AND "winning_initiative"."admitted"
jbe@170 3983 JOIN "battle" "battle_win"
jbe@170 3984 ON "battle_win"."issue_id" = "issue_id_p"
jbe@170 3985 AND "battle_win"."winning_initiative_id" = "winning_initiative"."id"
jbe@170 3986 AND "battle_win"."losing_initiative_id" = "losing_initiative"."id"
jbe@170 3987 JOIN "battle" "battle_lose"
jbe@170 3988 ON "battle_lose"."issue_id" = "issue_id_p"
jbe@170 3989 AND "battle_lose"."losing_initiative_id" = "winning_initiative"."id"
jbe@170 3990 AND "battle_lose"."winning_initiative_id" = "losing_initiative"."id"
jbe@170 3991 WHERE "losing_initiative"."issue_id" = "issue_id_p"
jbe@170 3992 AND "losing_initiative"."admitted"
jbe@170 3993 AND "winning_initiative"."schulze_rank" <
jbe@170 3994 "losing_initiative"."schulze_rank"
jbe@170 3995 AND "battle_win"."count" > "battle_lose"."count"
jbe@170 3996 AND (
jbe@170 3997 "battle_win"."count" > "winning_initiative"."positive_votes" OR
jbe@170 3998 "battle_lose"."count" < "losing_initiative"."negative_votes" )
jbe@170 3999 ) AS "subquery"
jbe@170 4000 WHERE "id" = "subquery"."initiative_id";
jbe@170 4001 -- mark eligible initiatives:
jbe@170 4002 UPDATE "initiative" SET "eligible" = TRUE
jbe@171 4003 WHERE "issue_id" = "issue_id_p"
jbe@171 4004 AND "initiative"."direct_majority"
jbe@171 4005 AND "initiative"."indirect_majority"
jbe@171 4006 AND "initiative"."better_than_status_quo"
jbe@171 4007 AND (
jbe@171 4008 "policy_row"."no_multistage_majority" = FALSE OR
jbe@171 4009 "initiative"."multistage_majority" = FALSE )
jbe@171 4010 AND (
jbe@171 4011 "policy_row"."no_reverse_beat_path" = FALSE OR
jbe@171 4012 "initiative"."reverse_beat_path" = FALSE );
jbe@170 4013 -- mark final winner:
jbe@170 4014 UPDATE "initiative" SET "winner" = TRUE
jbe@170 4015 FROM (
jbe@170 4016 SELECT "id" AS "initiative_id"
jbe@170 4017 FROM "initiative"
jbe@170 4018 WHERE "issue_id" = "issue_id_p" AND "eligible"
jbe@217 4019 ORDER BY
jbe@217 4020 "schulze_rank",
jbe@217 4021 "vote_ratio"("positive_votes", "negative_votes"),
jbe@217 4022 "id"
jbe@170 4023 LIMIT 1
jbe@170 4024 ) AS "subquery"
jbe@170 4025 WHERE "id" = "subquery"."initiative_id";
jbe@173 4026 -- write (final) ranks:
jbe@173 4027 "rank_v" := 1;
jbe@173 4028 FOR "initiative_id_v" IN
jbe@173 4029 SELECT "id"
jbe@173 4030 FROM "initiative"
jbe@173 4031 WHERE "issue_id" = "issue_id_p" AND "admitted"
jbe@174 4032 ORDER BY
jbe@174 4033 "winner" DESC,
jbe@217 4034 "eligible" DESC,
jbe@174 4035 "schulze_rank",
jbe@217 4036 "vote_ratio"("positive_votes", "negative_votes"),
jbe@174 4037 "id"
jbe@173 4038 LOOP
jbe@173 4039 UPDATE "initiative" SET "rank" = "rank_v"
jbe@173 4040 WHERE "id" = "initiative_id_v";
jbe@173 4041 "rank_v" := "rank_v" + 1;
jbe@173 4042 END LOOP;
jbe@170 4043 -- set schulze rank of status quo and mark issue as finished:
jbe@111 4044 UPDATE "issue" SET
jbe@170 4045 "status_quo_schulze_rank" = "rank_ary"["dimension_v"],
jbe@111 4046 "state" =
jbe@139 4047 CASE WHEN EXISTS (
jbe@139 4048 SELECT NULL FROM "initiative"
jbe@139 4049 WHERE "issue_id" = "issue_id_p" AND "winner"
jbe@139 4050 ) THEN
jbe@139 4051 'finished_with_winner'::"issue_state"
jbe@139 4052 ELSE
jbe@121 4053 'finished_without_winner'::"issue_state"
jbe@111 4054 END,
jbe@111 4055 "ranks_available" = TRUE
jbe@0 4056 WHERE "id" = "issue_id_p";
jbe@0 4057 RETURN;
jbe@0 4058 END;
jbe@0 4059 $$;
jbe@0 4060
jbe@0 4061 COMMENT ON FUNCTION "calculate_ranks"
jbe@0 4062 ( "issue"."id"%TYPE )
jbe@0 4063 IS 'Determine ranking (Votes have to be counted first)';
jbe@0 4064
jbe@0 4065
jbe@0 4066
jbe@0 4067 -----------------------------
jbe@0 4068 -- Automatic state changes --
jbe@0 4069 -----------------------------
jbe@0 4070
jbe@0 4071
jbe@0 4072 CREATE FUNCTION "check_issue"
jbe@0 4073 ( "issue_id_p" "issue"."id"%TYPE )
jbe@0 4074 RETURNS VOID
jbe@0 4075 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 4076 DECLARE
jbe@0 4077 "issue_row" "issue"%ROWTYPE;
jbe@0 4078 "policy_row" "policy"%ROWTYPE;
jbe@0 4079 BEGIN
jbe@67 4080 PERFORM "lock_issue"("issue_id_p");
jbe@0 4081 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
jbe@24 4082 -- only process open issues:
jbe@0 4083 IF "issue_row"."closed" ISNULL THEN
jbe@0 4084 SELECT * INTO "policy_row" FROM "policy"
jbe@0 4085 WHERE "id" = "issue_row"."policy_id";
jbe@24 4086 -- create a snapshot, unless issue is already fully frozen:
jbe@3 4087 IF "issue_row"."fully_frozen" ISNULL THEN
jbe@0 4088 PERFORM "create_snapshot"("issue_id_p");
jbe@0 4089 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
jbe@0 4090 END IF;
jbe@24 4091 -- eventually close or accept issues, which have not been accepted:
jbe@0 4092 IF "issue_row"."accepted" ISNULL THEN
jbe@0 4093 IF EXISTS (
jbe@0 4094 SELECT NULL FROM "initiative"
jbe@0 4095 WHERE "issue_id" = "issue_id_p"
jbe@0 4096 AND "supporter_count" > 0
jbe@0 4097 AND "supporter_count" * "policy_row"."issue_quorum_den"
jbe@0 4098 >= "issue_row"."population" * "policy_row"."issue_quorum_num"
jbe@0 4099 ) THEN
jbe@24 4100 -- accept issues, if supporter count is high enough
jbe@3 4101 PERFORM "set_snapshot_event"("issue_id_p", 'end_of_admission');
jbe@111 4102 -- NOTE: "issue_row" used later
jbe@111 4103 "issue_row"."state" := 'discussion';
jbe@111 4104 "issue_row"."accepted" := now();
jbe@111 4105 UPDATE "issue" SET
jbe@111 4106 "state" = "issue_row"."state",
jbe@111 4107 "accepted" = "issue_row"."accepted"
jbe@0 4108 WHERE "id" = "issue_row"."id";
jbe@0 4109 ELSIF
jbe@22 4110 now() >= "issue_row"."created" + "issue_row"."admission_time"
jbe@0 4111 THEN
jbe@24 4112 -- close issues, if admission time has expired
jbe@0 4113 PERFORM "set_snapshot_event"("issue_id_p", 'end_of_admission');
jbe@111 4114 UPDATE "issue" SET
jbe@111 4115 "state" = 'canceled_issue_not_accepted',
jbe@111 4116 "closed" = now()
jbe@0 4117 WHERE "id" = "issue_row"."id";
jbe@0 4118 END IF;
jbe@0 4119 END IF;
jbe@24 4120 -- eventually half freeze issues:
jbe@0 4121 IF
jbe@24 4122 -- NOTE: issue can't be closed at this point, if it has been accepted
jbe@0 4123 "issue_row"."accepted" NOTNULL AND
jbe@3 4124 "issue_row"."half_frozen" ISNULL
jbe@0 4125 THEN
jbe@0 4126 IF
jbe@144 4127 now() >= "issue_row"."accepted" + "issue_row"."discussion_time"
jbe@0 4128 THEN
jbe@21 4129 PERFORM "set_snapshot_event"("issue_id_p", 'half_freeze');
jbe@111 4130 -- NOTE: "issue_row" used later
jbe@111 4131 "issue_row"."state" := 'verification';
jbe@111 4132 "issue_row"."half_frozen" := now();
jbe@111 4133 UPDATE "issue" SET
jbe@111 4134 "state" = "issue_row"."state",
jbe@111 4135 "half_frozen" = "issue_row"."half_frozen"
jbe@3 4136 WHERE "id" = "issue_row"."id";
jbe@0 4137 END IF;
jbe@0 4138 END IF;
jbe@24 4139 -- close issues after some time, if all initiatives have been revoked:
jbe@24 4140 IF
jbe@24 4141 "issue_row"."closed" ISNULL AND
jbe@24 4142 NOT EXISTS (
jbe@24 4143 -- all initiatives are revoked
jbe@24 4144 SELECT NULL FROM "initiative"
jbe@24 4145 WHERE "issue_id" = "issue_id_p" AND "revoked" ISNULL
jbe@24 4146 ) AND (
jbe@111 4147 -- and issue has not been accepted yet
jbe@111 4148 "issue_row"."accepted" ISNULL OR
jbe@24 4149 NOT EXISTS (
jbe@111 4150 -- or no initiatives have been revoked lately
jbe@24 4151 SELECT NULL FROM "initiative"
jbe@24 4152 WHERE "issue_id" = "issue_id_p"
jbe@24 4153 AND now() < "revoked" + "issue_row"."verification_time"
jbe@24 4154 ) OR (
jbe@24 4155 -- or verification time has elapsed
jbe@24 4156 "issue_row"."half_frozen" NOTNULL AND
jbe@24 4157 "issue_row"."fully_frozen" ISNULL AND
jbe@24 4158 now() >= "issue_row"."half_frozen" + "issue_row"."verification_time"
jbe@24 4159 )
jbe@24 4160 )
jbe@24 4161 THEN
jbe@111 4162 -- NOTE: "issue_row" used later
jbe@113 4163 IF "issue_row"."accepted" ISNULL THEN
jbe@113 4164 "issue_row"."state" := 'canceled_revoked_before_accepted';
jbe@113 4165 ELSIF "issue_row"."half_frozen" ISNULL THEN
jbe@113 4166 "issue_row"."state" := 'canceled_after_revocation_during_discussion';
jbe@113 4167 ELSE
jbe@113 4168 "issue_row"."state" := 'canceled_after_revocation_during_verification';
jbe@113 4169 END IF;
jbe@111 4170 "issue_row"."closed" := now();
jbe@111 4171 UPDATE "issue" SET
jbe@111 4172 "state" = "issue_row"."state",
jbe@111 4173 "closed" = "issue_row"."closed"
jbe@24 4174 WHERE "id" = "issue_row"."id";
jbe@24 4175 END IF;
jbe@24 4176 -- fully freeze issue after verification time:
jbe@0 4177 IF
jbe@3 4178 "issue_row"."half_frozen" NOTNULL AND
jbe@3 4179 "issue_row"."fully_frozen" ISNULL AND
jbe@24 4180 "issue_row"."closed" ISNULL AND
jbe@22 4181 now() >= "issue_row"."half_frozen" + "issue_row"."verification_time"
jbe@3 4182 THEN
jbe@3 4183 PERFORM "freeze_after_snapshot"("issue_id_p");
jbe@24 4184 -- NOTE: "issue" might change, thus "issue_row" has to be updated below
jbe@3 4185 END IF;
jbe@9 4186 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
jbe@24 4187 -- close issue by calling close_voting(...) after voting time:
jbe@3 4188 IF
jbe@9 4189 "issue_row"."closed" ISNULL AND
jbe@3 4190 "issue_row"."fully_frozen" NOTNULL AND
jbe@22 4191 now() >= "issue_row"."fully_frozen" + "issue_row"."voting_time"
jbe@0 4192 THEN
jbe@0 4193 PERFORM "close_voting"("issue_id_p");
jbe@111 4194 -- calculate ranks will not consume much time and can be done now
jbe@111 4195 PERFORM "calculate_ranks"("issue_id_p");
jbe@0 4196 END IF;
jbe@0 4197 END IF;
jbe@0 4198 RETURN;
jbe@0 4199 END;
jbe@0 4200 $$;
jbe@0 4201
jbe@0 4202 COMMENT ON FUNCTION "check_issue"
jbe@0 4203 ( "issue"."id"%TYPE )
jbe@0 4204 IS 'Precalculate supporter counts etc. for a given issue, and check, if status change is required; At end of voting the ranking is not calculated by this function, but must be calculated in a seperate transaction using the "calculate_ranks" function.';
jbe@0 4205
jbe@0 4206
jbe@0 4207 CREATE FUNCTION "check_everything"()
jbe@0 4208 RETURNS VOID
jbe@0 4209 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@0 4210 DECLARE
jbe@0 4211 "issue_id_v" "issue"."id"%TYPE;
jbe@0 4212 BEGIN
jbe@235 4213 DELETE FROM "expired_session";
jbe@184 4214 PERFORM "check_activity"();
jbe@4 4215 PERFORM "calculate_member_counts"();
jbe@4 4216 FOR "issue_id_v" IN SELECT "id" FROM "open_issue" LOOP
jbe@0 4217 PERFORM "check_issue"("issue_id_v");
jbe@0 4218 END LOOP;
jbe@4 4219 FOR "issue_id_v" IN SELECT "id" FROM "issue_with_ranks_missing" LOOP
jbe@0 4220 PERFORM "calculate_ranks"("issue_id_v");
jbe@0 4221 END LOOP;
jbe@0 4222 RETURN;
jbe@0 4223 END;
jbe@0 4224 $$;
jbe@0 4225
jbe@103 4226 COMMENT ON FUNCTION "check_everything"() IS 'Amongst other regular tasks this function performs "check_issue" for every open issue, and if possible, automatically calculates ranks. Use this function only for development and debugging purposes, as long transactions with exclusive locking may result. In productive environments you should call the lf_update program instead.';
jbe@0 4227
jbe@0 4228
jbe@0 4229
jbe@59 4230 ----------------------
jbe@59 4231 -- Deletion of data --
jbe@59 4232 ----------------------
jbe@59 4233
jbe@59 4234
jbe@59 4235 CREATE FUNCTION "clean_issue"("issue_id_p" "issue"."id"%TYPE)
jbe@59 4236 RETURNS VOID
jbe@59 4237 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@59 4238 DECLARE
jbe@59 4239 "issue_row" "issue"%ROWTYPE;
jbe@59 4240 BEGIN
jbe@59 4241 SELECT * INTO "issue_row"
jbe@59 4242 FROM "issue" WHERE "id" = "issue_id_p"
jbe@59 4243 FOR UPDATE;
jbe@59 4244 IF "issue_row"."cleaned" ISNULL THEN
jbe@59 4245 UPDATE "issue" SET
jbe@152 4246 "state" = 'voting',
jbe@152 4247 "closed" = NULL,
jbe@59 4248 "ranks_available" = FALSE
jbe@59 4249 WHERE "id" = "issue_id_p";
jbe@163 4250 DELETE FROM "issue_comment"
jbe@163 4251 WHERE "issue_id" = "issue_id_p";
jbe@163 4252 DELETE FROM "voting_comment"
jbe@163 4253 WHERE "issue_id" = "issue_id_p";
jbe@59 4254 DELETE FROM "delegating_voter"
jbe@59 4255 WHERE "issue_id" = "issue_id_p";
jbe@59 4256 DELETE FROM "direct_voter"
jbe@59 4257 WHERE "issue_id" = "issue_id_p";
jbe@59 4258 DELETE FROM "delegating_interest_snapshot"
jbe@59 4259 WHERE "issue_id" = "issue_id_p";
jbe@59 4260 DELETE FROM "direct_interest_snapshot"
jbe@59 4261 WHERE "issue_id" = "issue_id_p";
jbe@59 4262 DELETE FROM "delegating_population_snapshot"
jbe@59 4263 WHERE "issue_id" = "issue_id_p";
jbe@59 4264 DELETE FROM "direct_population_snapshot"
jbe@59 4265 WHERE "issue_id" = "issue_id_p";
jbe@113 4266 DELETE FROM "non_voter"
jbe@94 4267 WHERE "issue_id" = "issue_id_p";
jbe@59 4268 DELETE FROM "delegation"
jbe@59 4269 WHERE "issue_id" = "issue_id_p";
jbe@59 4270 DELETE FROM "supporter"
jbe@59 4271 WHERE "issue_id" = "issue_id_p";
jbe@59 4272 UPDATE "issue" SET
jbe@152 4273 "state" = "issue_row"."state",
jbe@59 4274 "closed" = "issue_row"."closed",
jbe@59 4275 "ranks_available" = "issue_row"."ranks_available",
jbe@59 4276 "cleaned" = now()
jbe@59 4277 WHERE "id" = "issue_id_p";
jbe@59 4278 END IF;
jbe@59 4279 RETURN;
jbe@59 4280 END;
jbe@59 4281 $$;
jbe@59 4282
jbe@59 4283 COMMENT ON FUNCTION "clean_issue"("issue"."id"%TYPE) IS 'Delete discussion data and votes belonging to an issue';
jbe@8 4284
jbe@8 4285
jbe@54 4286 CREATE FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE)
jbe@8 4287 RETURNS VOID
jbe@8 4288 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@8 4289 BEGIN
jbe@9 4290 UPDATE "member" SET
jbe@57 4291 "last_login" = NULL,
jbe@45 4292 "login" = NULL,
jbe@11 4293 "password" = NULL,
jbe@101 4294 "locked" = TRUE,
jbe@54 4295 "active" = FALSE,
jbe@11 4296 "notify_email" = NULL,
jbe@11 4297 "notify_email_unconfirmed" = NULL,
jbe@11 4298 "notify_email_secret" = NULL,
jbe@11 4299 "notify_email_secret_expiry" = NULL,
jbe@57 4300 "notify_email_lock_expiry" = NULL,
jbe@11 4301 "password_reset_secret" = NULL,
jbe@11 4302 "password_reset_secret_expiry" = NULL,
jbe@11 4303 "organizational_unit" = NULL,
jbe@11 4304 "internal_posts" = NULL,
jbe@11 4305 "realname" = NULL,
jbe@11 4306 "birthday" = NULL,
jbe@11 4307 "address" = NULL,
jbe@11 4308 "email" = NULL,
jbe@11 4309 "xmpp_address" = NULL,
jbe@11 4310 "website" = NULL,
jbe@11 4311 "phone" = NULL,
jbe@11 4312 "mobile_phone" = NULL,
jbe@11 4313 "profession" = NULL,
jbe@11 4314 "external_memberships" = NULL,
jbe@11 4315 "external_posts" = NULL,
jbe@45 4316 "statement" = NULL
jbe@45 4317 WHERE "id" = "member_id_p";
jbe@11 4318 -- "text_search_data" is updated by triggers
jbe@45 4319 DELETE FROM "setting" WHERE "member_id" = "member_id_p";
jbe@45 4320 DELETE FROM "setting_map" WHERE "member_id" = "member_id_p";
jbe@45 4321 DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
jbe@45 4322 DELETE FROM "member_image" WHERE "member_id" = "member_id_p";
jbe@45 4323 DELETE FROM "contact" WHERE "member_id" = "member_id_p";
jbe@113 4324 DELETE FROM "ignored_member" WHERE "member_id" = "member_id_p";
jbe@235 4325 DELETE FROM "session" WHERE "member_id" = "member_id_p";
jbe@45 4326 DELETE FROM "area_setting" WHERE "member_id" = "member_id_p";
jbe@45 4327 DELETE FROM "issue_setting" WHERE "member_id" = "member_id_p";
jbe@113 4328 DELETE FROM "ignored_initiative" WHERE "member_id" = "member_id_p";
jbe@45 4329 DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
jbe@45 4330 DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
jbe@54 4331 DELETE FROM "membership" WHERE "member_id" = "member_id_p";
jbe@54 4332 DELETE FROM "delegation" WHERE "truster_id" = "member_id_p";
jbe@113 4333 DELETE FROM "non_voter" WHERE "member_id" = "member_id_p";
jbe@57 4334 DELETE FROM "direct_voter" USING "issue"
jbe@57 4335 WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@57 4336 AND "issue"."closed" ISNULL
jbe@57 4337 AND "member_id" = "member_id_p";
jbe@45 4338 RETURN;
jbe@45 4339 END;
jbe@45 4340 $$;
jbe@45 4341
jbe@57 4342 COMMENT ON FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE) IS 'Deactivate member and clear certain settings and data of this member (data protection)';
jbe@45 4343
jbe@45 4344
jbe@45 4345 CREATE FUNCTION "delete_private_data"()
jbe@45 4346 RETURNS VOID
jbe@45 4347 LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@45 4348 BEGIN
jbe@226 4349 DELETE FROM "member" WHERE "activated" ISNULL;
jbe@50 4350 UPDATE "member" SET
jbe@206 4351 "invite_code" = NULL,
jbe@232 4352 "invite_code_expiry" = NULL,
jbe@228 4353 "admin_comment" = NULL,
jbe@57 4354 "last_login" = NULL,
jbe@50 4355 "login" = NULL,
jbe@50 4356 "password" = NULL,
jbe@238 4357 "lang" = NULL,
jbe@50 4358 "notify_email" = NULL,
jbe@50 4359 "notify_email_unconfirmed" = NULL,
jbe@50 4360 "notify_email_secret" = NULL,
jbe@50 4361 "notify_email_secret_expiry" = NULL,
jbe@57 4362 "notify_email_lock_expiry" = NULL,
jbe@238 4363 "notify_level" = NULL,
jbe@50 4364 "password_reset_secret" = NULL,
jbe@50 4365 "password_reset_secret_expiry" = NULL,
jbe@50 4366 "organizational_unit" = NULL,
jbe@50 4367 "internal_posts" = NULL,
jbe@50 4368 "realname" = NULL,
jbe@50 4369 "birthday" = NULL,
jbe@50 4370 "address" = NULL,
jbe@50 4371 "email" = NULL,
jbe@50 4372 "xmpp_address" = NULL,
jbe@50 4373 "website" = NULL,
jbe@50 4374 "phone" = NULL,
jbe@50 4375 "mobile_phone" = NULL,
jbe@50 4376 "profession" = NULL,
jbe@50 4377 "external_memberships" = NULL,
jbe@50 4378 "external_posts" = NULL,
jbe@238 4379 "formatting_engine" = NULL,
jbe@50 4380 "statement" = NULL;
jbe@50 4381 -- "text_search_data" is updated by triggers
jbe@50 4382 DELETE FROM "setting";
jbe@50 4383 DELETE FROM "setting_map";
jbe@50 4384 DELETE FROM "member_relation_setting";
jbe@50 4385 DELETE FROM "member_image";
jbe@50 4386 DELETE FROM "contact";
jbe@113 4387 DELETE FROM "ignored_member";
jbe@235 4388 DELETE FROM "session";
jbe@50 4389 DELETE FROM "area_setting";
jbe@50 4390 DELETE FROM "issue_setting";
jbe@113 4391 DELETE FROM "ignored_initiative";
jbe@50 4392 DELETE FROM "initiative_setting";
jbe@50 4393 DELETE FROM "suggestion_setting";
jbe@113 4394 DELETE FROM "non_voter";
jbe@8 4395 DELETE FROM "direct_voter" USING "issue"
jbe@8 4396 WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@8 4397 AND "issue"."closed" ISNULL;
jbe@8 4398 RETURN;
jbe@8 4399 END;
jbe@8 4400 $$;
jbe@8 4401
jbe@103 4402 COMMENT ON FUNCTION "delete_private_data"() IS 'Used by lf_export script. DO NOT USE on productive database, but only on a copy! This function deletes all data which should not be publicly available, and can be used to create a database dump for publication.';
jbe@8 4403
jbe@8 4404
jbe@8 4405
jbe@0 4406 COMMIT;

Impressum / About Us