liquid_feedback_core

view core.sql @ 91:8387388f1f7c

Comments for columns of table "ignored_issue" changed
author jbe
date Sun Oct 31 01:49:49 2010 +0200 (2010-10-31)
parents 1934f9b4f803
children 425bb591bff8
line source
2 CREATE LANGUAGE plpgsql; -- Triggers are implemented in PL/pgSQL
4 -- NOTE: In PostgreSQL every UNIQUE constraint implies creation of an index
6 BEGIN;
8 CREATE VIEW "liquid_feedback_version" AS
9 SELECT * FROM (VALUES ('1.3.0', 1, 3, 0))
10 AS "subquery"("string", "major", "minor", "revision");
14 ----------------------
15 -- Full text search --
16 ----------------------
19 CREATE FUNCTION "text_search_query"("query_text_p" TEXT)
20 RETURNS TSQUERY
21 LANGUAGE 'plpgsql' IMMUTABLE AS $$
22 BEGIN
23 RETURN plainto_tsquery('pg_catalog.simple', "query_text_p");
24 END;
25 $$;
27 COMMENT ON FUNCTION "text_search_query"(TEXT) IS 'Usage: WHERE "text_search_data" @@ "text_search_query"(''<user query>'')';
30 CREATE FUNCTION "highlight"
31 ( "body_p" TEXT,
32 "query_text_p" TEXT )
33 RETURNS TEXT
34 LANGUAGE 'plpgsql' IMMUTABLE AS $$
35 BEGIN
36 RETURN ts_headline(
37 'pg_catalog.simple',
38 replace(replace("body_p", e'\\', e'\\\\'), '*', e'\\*'),
39 "text_search_query"("query_text_p"),
40 'StartSel=* StopSel=* HighlightAll=TRUE' );
41 END;
42 $$;
44 COMMENT ON FUNCTION "highlight"
45 ( "body_p" TEXT,
46 "query_text_p" TEXT )
47 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.';
51 -------------------------
52 -- Tables and indicies --
53 -------------------------
56 CREATE TABLE "member" (
57 "id" SERIAL4 PRIMARY KEY,
58 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
59 "last_login" TIMESTAMPTZ,
60 "login" TEXT UNIQUE,
61 "password" TEXT,
62 "active" BOOLEAN NOT NULL DEFAULT TRUE,
63 "admin" BOOLEAN NOT NULL DEFAULT FALSE,
64 "notify_email" TEXT,
65 "notify_email_unconfirmed" TEXT,
66 "notify_email_secret" TEXT UNIQUE,
67 "notify_email_secret_expiry" TIMESTAMPTZ,
68 "notify_email_lock_expiry" TIMESTAMPTZ,
69 "password_reset_secret" TEXT UNIQUE,
70 "password_reset_secret_expiry" TIMESTAMPTZ,
71 "name" TEXT NOT NULL UNIQUE,
72 "identification" TEXT UNIQUE,
73 "organizational_unit" TEXT,
74 "internal_posts" TEXT,
75 "realname" TEXT,
76 "birthday" DATE,
77 "address" TEXT,
78 "email" TEXT,
79 "xmpp_address" TEXT,
80 "website" TEXT,
81 "phone" TEXT,
82 "mobile_phone" TEXT,
83 "profession" TEXT,
84 "external_memberships" TEXT,
85 "external_posts" TEXT,
86 "statement" TEXT,
87 "text_search_data" TSVECTOR );
88 CREATE INDEX "member_active_idx" ON "member" ("active");
89 CREATE INDEX "member_text_search_data_idx" ON "member" USING gin ("text_search_data");
90 CREATE TRIGGER "update_text_search_data"
91 BEFORE INSERT OR UPDATE ON "member"
92 FOR EACH ROW EXECUTE PROCEDURE
93 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
94 "name", "identification", "organizational_unit", "internal_posts",
95 "realname", "external_memberships", "external_posts", "statement" );
97 COMMENT ON TABLE "member" IS 'Users of the system, e.g. members of an organization';
99 COMMENT ON COLUMN "member"."login" IS 'Login name';
100 COMMENT ON COLUMN "member"."password" IS 'Password (preferably as crypto-hash, depending on the frontend or access layer)';
101 COMMENT ON COLUMN "member"."active" IS 'Inactive members can not login and their supports/votes are not counted by the system.';
102 COMMENT ON COLUMN "member"."admin" IS 'TRUE for admins, which can administrate other users and setup policies and areas';
103 COMMENT ON COLUMN "member"."notify_email" IS 'Email address where notifications of the system are sent to';
104 COMMENT ON COLUMN "member"."notify_email_unconfirmed" IS 'Unconfirmed email address provided by the member to be copied into "notify_email" field after verification';
105 COMMENT ON COLUMN "member"."notify_email_secret" IS 'Secret sent to the address in "notify_email_unconformed"';
106 COMMENT ON COLUMN "member"."notify_email_secret_expiry" IS 'Expiry date/time for "notify_email_secret"';
107 COMMENT ON COLUMN "member"."notify_email_lock_expiry" IS 'Date/time until no further email confirmation mails may be sent (abuse protection)';
108 COMMENT ON COLUMN "member"."name" IS 'Distinct name of the member';
109 COMMENT ON COLUMN "member"."identification" IS 'Optional identification number or code of the member';
110 COMMENT ON COLUMN "member"."organizational_unit" IS 'Branch or division of the organization the member belongs to';
111 COMMENT ON COLUMN "member"."internal_posts" IS 'Posts (offices) of the member inside the organization';
112 COMMENT ON COLUMN "member"."realname" IS 'Real name of the member, may be identical with "name"';
113 COMMENT ON COLUMN "member"."email" IS 'Published email address of the member; not used for system notifications';
114 COMMENT ON COLUMN "member"."external_memberships" IS 'Other organizations the member is involved in';
115 COMMENT ON COLUMN "member"."external_posts" IS 'Posts (offices) outside the organization';
116 COMMENT ON COLUMN "member"."statement" IS 'Freely chosen text of the member for his homepage within the system';
119 CREATE TABLE "member_history" (
120 "id" SERIAL8 PRIMARY KEY,
121 "member_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
122 "until" TIMESTAMPTZ NOT NULL DEFAULT now(),
123 "active" BOOLEAN NOT NULL,
124 "name" TEXT NOT NULL );
125 CREATE INDEX "member_history_member_id_idx" ON "member_history" ("member_id");
127 COMMENT ON TABLE "member_history" IS 'Filled by trigger; keeps information about old names and active flag of members';
129 COMMENT ON COLUMN "member_history"."id" IS 'Primary key, which can be used to sort entries correctly (and time warp resistant)';
130 COMMENT ON COLUMN "member_history"."until" IS 'Timestamp until the data was valid';
133 CREATE TABLE "invite_code" (
134 "code" TEXT PRIMARY KEY,
135 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
136 "used" TIMESTAMPTZ,
137 "member_id" INT4 UNIQUE REFERENCES "member" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
138 "comment" TEXT,
139 CONSTRAINT "only_used_codes_may_refer_to_member" CHECK ("used" NOTNULL OR "member_id" ISNULL) );
141 COMMENT ON TABLE "invite_code" IS 'Invite codes can be used once to create a new member account.';
143 COMMENT ON COLUMN "invite_code"."code" IS 'Secret code';
144 COMMENT ON COLUMN "invite_code"."created" IS 'Time of creation of the secret code';
145 COMMENT ON COLUMN "invite_code"."used" IS 'NULL, if not used yet, otherwise tells when this code was used to create a member account';
146 COMMENT ON COLUMN "invite_code"."member_id" IS 'References the member whose account was created with this code';
147 COMMENT ON COLUMN "invite_code"."comment" IS 'Comment on the code, which is to be used for administrative reasons only';
150 CREATE TABLE "setting" (
151 PRIMARY KEY ("member_id", "key"),
152 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
153 "key" TEXT NOT NULL,
154 "value" TEXT NOT NULL );
155 CREATE INDEX "setting_key_idx" ON "setting" ("key");
157 COMMENT ON TABLE "setting" IS 'Place to store a frontend specific setting for members as a string';
159 COMMENT ON COLUMN "setting"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
162 CREATE TABLE "setting_map" (
163 PRIMARY KEY ("member_id", "key", "subkey"),
164 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
165 "key" TEXT NOT NULL,
166 "subkey" TEXT NOT NULL,
167 "value" TEXT NOT NULL );
168 CREATE INDEX "setting_map_key_idx" ON "setting_map" ("key");
170 COMMENT ON TABLE "setting_map" IS 'Place to store a frontend specific setting for members as a map of key value pairs';
172 COMMENT ON COLUMN "setting_map"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
173 COMMENT ON COLUMN "setting_map"."subkey" IS 'Key of a map entry';
174 COMMENT ON COLUMN "setting_map"."value" IS 'Value of a map entry';
177 CREATE TABLE "member_relation_setting" (
178 PRIMARY KEY ("member_id", "key", "other_member_id"),
179 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
180 "key" TEXT NOT NULL,
181 "other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
182 "value" TEXT NOT NULL );
184 COMMENT ON TABLE "member_relation_setting" IS 'Place to store a frontend specific setting related to relations between members as a string';
187 CREATE TYPE "member_image_type" AS ENUM ('photo', 'avatar');
189 COMMENT ON TYPE "member_image_type" IS 'Types of images for a member';
192 CREATE TABLE "member_image" (
193 PRIMARY KEY ("member_id", "image_type", "scaled"),
194 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
195 "image_type" "member_image_type",
196 "scaled" BOOLEAN,
197 "content_type" TEXT,
198 "data" BYTEA NOT NULL );
200 COMMENT ON TABLE "member_image" IS 'Images of members';
202 COMMENT ON COLUMN "member_image"."scaled" IS 'FALSE for original image, TRUE for scaled version of the image';
205 CREATE TABLE "member_count" (
206 "calculated" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
207 "total_count" INT4 NOT NULL );
209 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';
211 COMMENT ON COLUMN "member_count"."calculated" IS 'timestamp indicating when the total member count and area member counts were calculated';
212 COMMENT ON COLUMN "member_count"."total_count" IS 'Total count of active(!) members';
215 CREATE TABLE "contact" (
216 PRIMARY KEY ("member_id", "other_member_id"),
217 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
218 "other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
219 "public" BOOLEAN NOT NULL DEFAULT FALSE,
220 CONSTRAINT "cant_save_yourself_as_contact"
221 CHECK ("member_id" != "other_member_id") );
223 COMMENT ON TABLE "contact" IS 'Contact lists';
225 COMMENT ON COLUMN "contact"."member_id" IS 'Member having the contact list';
226 COMMENT ON COLUMN "contact"."other_member_id" IS 'Member referenced in the contact list';
227 COMMENT ON COLUMN "contact"."public" IS 'TRUE = display contact publically';
230 CREATE TABLE "session" (
231 "ident" TEXT PRIMARY KEY,
232 "additional_secret" TEXT,
233 "expiry" TIMESTAMPTZ NOT NULL DEFAULT now() + '24 hours',
234 "member_id" INT8 REFERENCES "member" ("id") ON DELETE SET NULL,
235 "lang" TEXT );
236 CREATE INDEX "session_expiry_idx" ON "session" ("expiry");
238 COMMENT ON TABLE "session" IS 'Sessions, i.e. for a web-frontend';
240 COMMENT ON COLUMN "session"."ident" IS 'Secret session identifier (i.e. random string)';
241 COMMENT ON COLUMN "session"."additional_secret" IS 'Additional field to store a secret, which can be used against CSRF attacks';
242 COMMENT ON COLUMN "session"."member_id" IS 'Reference to member, who is logged in';
243 COMMENT ON COLUMN "session"."lang" IS 'Language code of the selected language';
246 CREATE TABLE "policy" (
247 "id" SERIAL4 PRIMARY KEY,
248 "index" INT4 NOT NULL,
249 "active" BOOLEAN NOT NULL DEFAULT TRUE,
250 "name" TEXT NOT NULL UNIQUE,
251 "description" TEXT NOT NULL DEFAULT '',
252 "admission_time" INTERVAL NOT NULL,
253 "discussion_time" INTERVAL NOT NULL,
254 "verification_time" INTERVAL NOT NULL,
255 "voting_time" INTERVAL NOT NULL,
256 "issue_quorum_num" INT4 NOT NULL,
257 "issue_quorum_den" INT4 NOT NULL,
258 "initiative_quorum_num" INT4 NOT NULL,
259 "initiative_quorum_den" INT4 NOT NULL,
260 "majority_num" INT4 NOT NULL DEFAULT 1,
261 "majority_den" INT4 NOT NULL DEFAULT 2,
262 "majority_strict" BOOLEAN NOT NULL DEFAULT TRUE );
263 CREATE INDEX "policy_active_idx" ON "policy" ("active");
265 COMMENT ON TABLE "policy" IS 'Policies for a particular proceeding type (timelimits, quorum)';
267 COMMENT ON COLUMN "policy"."index" IS 'Determines the order in listings';
268 COMMENT ON COLUMN "policy"."active" IS 'TRUE = policy can be used for new issues';
269 COMMENT ON COLUMN "policy"."admission_time" IS 'Maximum time an issue stays open without being "accepted"';
270 COMMENT ON COLUMN "policy"."discussion_time" IS 'Regular time until an issue is "half_frozen" after being "accepted"';
271 COMMENT ON COLUMN "policy"."verification_time" IS 'Regular time until an issue is "fully_frozen" after being "half_frozen"';
272 COMMENT ON COLUMN "policy"."voting_time" IS 'Time after an issue is "fully_frozen" but not "closed"';
273 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"';
274 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"';
275 COMMENT ON COLUMN "policy"."initiative_quorum_num" IS 'Numerator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
276 COMMENT ON COLUMN "policy"."initiative_quorum_den" IS 'Denominator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
277 COMMENT ON COLUMN "policy"."majority_num" IS 'Numerator of fraction of majority to be reached during voting by an initiative to be aggreed upon';
278 COMMENT ON COLUMN "policy"."majority_den" IS 'Denominator of fraction of majority to be reached during voting by an initiative to be aggreed upon';
279 COMMENT ON COLUMN "policy"."majority_strict" IS 'If TRUE, then the majority must be strictly greater than "majority_num"/"majority_den", otherwise it may also be equal.';
282 CREATE TABLE "area" (
283 "id" SERIAL4 PRIMARY KEY,
284 "active" BOOLEAN NOT NULL DEFAULT TRUE,
285 "name" TEXT NOT NULL,
286 "description" TEXT NOT NULL DEFAULT '',
287 "direct_member_count" INT4,
288 "member_weight" INT4,
289 "autoreject_weight" INT4,
290 "text_search_data" TSVECTOR );
291 CREATE INDEX "area_active_idx" ON "area" ("active");
292 CREATE INDEX "area_text_search_data_idx" ON "area" USING gin ("text_search_data");
293 CREATE TRIGGER "update_text_search_data"
294 BEFORE INSERT OR UPDATE ON "area"
295 FOR EACH ROW EXECUTE PROCEDURE
296 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
297 "name", "description" );
299 COMMENT ON TABLE "area" IS 'Subject areas';
301 COMMENT ON COLUMN "area"."active" IS 'TRUE means new issues can be created in this area';
302 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"';
303 COMMENT ON COLUMN "area"."member_weight" IS 'Same as "direct_member_count" but respecting delegations';
304 COMMENT ON COLUMN "area"."autoreject_weight" IS 'Sum of weight of members using the autoreject feature';
307 CREATE TABLE "area_setting" (
308 PRIMARY KEY ("member_id", "key", "area_id"),
309 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
310 "key" TEXT NOT NULL,
311 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
312 "value" TEXT NOT NULL );
314 COMMENT ON TABLE "area_setting" IS 'Place for frontend to store area specific settings of members as strings';
317 CREATE TABLE "allowed_policy" (
318 PRIMARY KEY ("area_id", "policy_id"),
319 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
320 "policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
321 "default_policy" BOOLEAN NOT NULL DEFAULT FALSE );
322 CREATE UNIQUE INDEX "allowed_policy_one_default_per_area_idx" ON "allowed_policy" ("area_id") WHERE "default_policy";
324 COMMENT ON TABLE "allowed_policy" IS 'Selects which policies can be used in each area';
326 COMMENT ON COLUMN "allowed_policy"."default_policy" IS 'One policy per area can be set as default.';
329 CREATE TYPE "snapshot_event" AS ENUM ('periodic', 'end_of_admission', 'half_freeze', 'full_freeze');
331 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';
334 CREATE TABLE "issue" (
335 "id" SERIAL4 PRIMARY KEY,
336 "area_id" INT4 NOT NULL REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
337 "policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
338 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
339 "accepted" TIMESTAMPTZ,
340 "half_frozen" TIMESTAMPTZ,
341 "fully_frozen" TIMESTAMPTZ,
342 "closed" TIMESTAMPTZ,
343 "ranks_available" BOOLEAN NOT NULL DEFAULT FALSE,
344 "cleaned" TIMESTAMPTZ,
345 "admission_time" INTERVAL NOT NULL,
346 "discussion_time" INTERVAL NOT NULL,
347 "verification_time" INTERVAL NOT NULL,
348 "voting_time" INTERVAL NOT NULL,
349 "snapshot" TIMESTAMPTZ,
350 "latest_snapshot_event" "snapshot_event",
351 "population" INT4,
352 "vote_now" INT4,
353 "vote_later" INT4,
354 "voter_count" INT4,
355 CONSTRAINT "valid_state" CHECK (
356 ("accepted" ISNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
357 ("accepted" ISNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
358 ("accepted" NOTNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
359 ("accepted" NOTNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
360 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
361 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
362 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" ISNULL AND "ranks_available" = FALSE) OR
363 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" NOTNULL AND "ranks_available" = FALSE) OR
364 ("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL AND "closed" NOTNULL AND "ranks_available" = TRUE) ),
365 CONSTRAINT "state_change_order" CHECK (
366 "created" <= "accepted" AND
367 "accepted" <= "half_frozen" AND
368 "half_frozen" <= "fully_frozen" AND
369 "fully_frozen" <= "closed" ),
370 CONSTRAINT "only_closed_issues_may_be_cleaned" CHECK (
371 "cleaned" ISNULL OR "closed" NOTNULL ),
372 CONSTRAINT "last_snapshot_on_full_freeze"
373 CHECK ("snapshot" = "fully_frozen"), -- NOTE: snapshot can be set, while frozen is NULL yet
374 CONSTRAINT "freeze_requires_snapshot"
375 CHECK ("fully_frozen" ISNULL OR "snapshot" NOTNULL),
376 CONSTRAINT "set_both_or_none_of_snapshot_and_latest_snapshot_event"
377 CHECK ("snapshot" NOTNULL = "latest_snapshot_event" NOTNULL) );
378 CREATE INDEX "issue_area_id_idx" ON "issue" ("area_id");
379 CREATE INDEX "issue_policy_id_idx" ON "issue" ("policy_id");
380 CREATE INDEX "issue_created_idx" ON "issue" ("created");
381 CREATE INDEX "issue_accepted_idx" ON "issue" ("accepted");
382 CREATE INDEX "issue_half_frozen_idx" ON "issue" ("half_frozen");
383 CREATE INDEX "issue_fully_frozen_idx" ON "issue" ("fully_frozen");
384 CREATE INDEX "issue_closed_idx" ON "issue" ("closed");
385 CREATE INDEX "issue_created_idx_open" ON "issue" ("created") WHERE "closed" ISNULL;
386 CREATE INDEX "issue_closed_idx_canceled" ON "issue" ("closed") WHERE "fully_frozen" ISNULL;
388 COMMENT ON TABLE "issue" IS 'Groups of initiatives';
390 COMMENT ON COLUMN "issue"."accepted" IS 'Point in time, when one initiative of issue reached the "issue_quorum"';
391 COMMENT ON COLUMN "issue"."half_frozen" IS 'Point in time, when "discussion_time" has elapsed, or members voted for voting; 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.';
392 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.';
393 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.';
394 COMMENT ON COLUMN "issue"."ranks_available" IS 'TRUE = ranks have been calculated';
395 COMMENT ON COLUMN "issue"."cleaned" IS 'Point in time, when discussion data and votes had been deleted';
396 COMMENT ON COLUMN "issue"."admission_time" IS 'Copied from "policy" table at creation of issue';
397 COMMENT ON COLUMN "issue"."discussion_time" IS 'Copied from "policy" table at creation of issue';
398 COMMENT ON COLUMN "issue"."verification_time" IS 'Copied from "policy" table at creation of issue';
399 COMMENT ON COLUMN "issue"."voting_time" IS 'Copied from "policy" table at creation of issue';
400 COMMENT ON COLUMN "issue"."snapshot" IS 'Point in time, when snapshot tables have been updated and "population", "vote_now", "vote_later" and *_count values were precalculated';
401 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';
402 COMMENT ON COLUMN "issue"."population" IS 'Sum of "weight" column in table "direct_population_snapshot"';
403 COMMENT ON COLUMN "issue"."vote_now" IS 'Number of votes in favor of voting now, as calculated from table "direct_interest_snapshot"';
404 COMMENT ON COLUMN "issue"."vote_later" IS 'Number of votes against voting now, as calculated from table "direct_interest_snapshot"';
405 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';
408 CREATE TABLE "issue_setting" (
409 PRIMARY KEY ("member_id", "key", "issue_id"),
410 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
411 "key" TEXT NOT NULL,
412 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
413 "value" TEXT NOT NULL );
415 COMMENT ON TABLE "issue_setting" IS 'Place for frontend to store issue specific settings of members as strings';
418 CREATE TABLE "initiative" (
419 UNIQUE ("issue_id", "id"), -- index needed for foreign-key on table "vote"
420 "issue_id" INT4 NOT NULL REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
421 "id" SERIAL4 PRIMARY KEY,
422 "name" TEXT NOT NULL,
423 "discussion_url" TEXT,
424 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
425 "revoked" TIMESTAMPTZ,
426 "suggested_initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
427 "admitted" BOOLEAN,
428 "supporter_count" INT4,
429 "informed_supporter_count" INT4,
430 "satisfied_supporter_count" INT4,
431 "satisfied_informed_supporter_count" INT4,
432 "positive_votes" INT4,
433 "negative_votes" INT4,
434 "agreed" BOOLEAN,
435 "rank" INT4,
436 "text_search_data" TSVECTOR,
437 CONSTRAINT "non_revoked_initiatives_cant_suggest_other"
438 CHECK ("revoked" NOTNULL OR "suggested_initiative_id" ISNULL),
439 CONSTRAINT "revoked_initiatives_cant_be_admitted"
440 CHECK ("revoked" ISNULL OR "admitted" ISNULL),
441 CONSTRAINT "non_admitted_initiatives_cant_contain_voting_results"
442 CHECK (("admitted" NOTNULL AND "admitted" = TRUE) OR ("positive_votes" ISNULL AND "negative_votes" ISNULL AND "agreed" ISNULL)),
443 CONSTRAINT "all_or_none_of_positive_votes_negative_votes_and_agreed_must_be_null"
444 CHECK ("positive_votes" NOTNULL = "negative_votes" NOTNULL AND "positive_votes" NOTNULL = "agreed" NOTNULL),
445 CONSTRAINT "non_agreed_initiatives_cant_get_a_rank"
446 CHECK (("agreed" NOTNULL AND "agreed" = TRUE) OR "rank" ISNULL) );
447 CREATE INDEX "initiative_created_idx" ON "initiative" ("created");
448 CREATE INDEX "initiative_revoked_idx" ON "initiative" ("revoked");
449 CREATE INDEX "initiative_text_search_data_idx" ON "initiative" USING gin ("text_search_data");
450 CREATE TRIGGER "update_text_search_data"
451 BEFORE INSERT OR UPDATE ON "initiative"
452 FOR EACH ROW EXECUTE PROCEDURE
453 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
454 "name", "discussion_url");
456 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.';
458 COMMENT ON COLUMN "initiative"."discussion_url" IS 'URL pointing to a discussion platform for this initiative';
459 COMMENT ON COLUMN "initiative"."revoked" IS 'Point in time, when one initiator decided to revoke the initiative';
460 COMMENT ON COLUMN "initiative"."admitted" IS 'TRUE, if initiative reaches the "initiative_quorum" when freezing the issue';
461 COMMENT ON COLUMN "initiative"."supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
462 COMMENT ON COLUMN "initiative"."informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
463 COMMENT ON COLUMN "initiative"."satisfied_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
464 COMMENT ON COLUMN "initiative"."satisfied_informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
465 COMMENT ON COLUMN "initiative"."positive_votes" IS 'Calculated from table "direct_voter"';
466 COMMENT ON COLUMN "initiative"."negative_votes" IS 'Calculated from table "direct_voter"';
467 COMMENT ON COLUMN "initiative"."agreed" IS 'TRUE, if "positive_votes"/("positive_votes"+"negative_votes") is strictly greater or greater-equal than "majority_num"/"majority_den"';
468 COMMENT ON COLUMN "initiative"."rank" IS 'Rank of approved initiatives (winner is 1), calculated from table "direct_voter"';
471 CREATE TABLE "battle" (
472 PRIMARY KEY ("issue_id", "winning_initiative_id", "losing_initiative_id"),
473 "issue_id" INT4,
474 "winning_initiative_id" INT4,
475 FOREIGN KEY ("issue_id", "winning_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
476 "losing_initiative_id" INT4,
477 FOREIGN KEY ("issue_id", "losing_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
478 "count" INT4 NOT NULL);
480 COMMENT ON TABLE "battle" IS 'Number of members preferring one initiative to another; Filled by "battle_view" when closing an issue';
483 CREATE TABLE "initiative_setting" (
484 PRIMARY KEY ("member_id", "key", "initiative_id"),
485 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
486 "key" TEXT NOT NULL,
487 "initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
488 "value" TEXT NOT NULL );
490 COMMENT ON TABLE "initiative_setting" IS 'Place for frontend to store initiative specific settings of members as strings';
493 CREATE TABLE "draft" (
494 UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "supporter"
495 "initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
496 "id" SERIAL8 PRIMARY KEY,
497 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
498 "author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
499 "formatting_engine" TEXT,
500 "content" TEXT NOT NULL,
501 "text_search_data" TSVECTOR );
502 CREATE INDEX "draft_created_idx" ON "draft" ("created");
503 CREATE INDEX "draft_author_id_created_idx" ON "draft" ("author_id", "created");
504 CREATE INDEX "draft_text_search_data_idx" ON "draft" USING gin ("text_search_data");
505 CREATE TRIGGER "update_text_search_data"
506 BEFORE INSERT OR UPDATE ON "draft"
507 FOR EACH ROW EXECUTE PROCEDURE
508 tsvector_update_trigger('text_search_data', 'pg_catalog.simple', "content");
510 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.';
512 COMMENT ON COLUMN "draft"."formatting_engine" IS 'Allows different formatting engines (i.e. wiki formats) to be used';
513 COMMENT ON COLUMN "draft"."content" IS 'Text of the draft in a format depending on the field "formatting_engine"';
516 CREATE TABLE "rendered_draft" (
517 PRIMARY KEY ("draft_id", "format"),
518 "draft_id" INT8 REFERENCES "draft" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
519 "format" TEXT,
520 "content" TEXT NOT NULL );
522 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)';
525 CREATE TABLE "suggestion" (
526 UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "opinion"
527 "initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
528 "id" SERIAL8 PRIMARY KEY,
529 "created" TIMESTAMPTZ NOT NULL DEFAULT now(),
530 "author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
531 "name" TEXT NOT NULL,
532 "description" TEXT NOT NULL DEFAULT '',
533 "text_search_data" TSVECTOR,
534 "minus2_unfulfilled_count" INT4,
535 "minus2_fulfilled_count" INT4,
536 "minus1_unfulfilled_count" INT4,
537 "minus1_fulfilled_count" INT4,
538 "plus1_unfulfilled_count" INT4,
539 "plus1_fulfilled_count" INT4,
540 "plus2_unfulfilled_count" INT4,
541 "plus2_fulfilled_count" INT4 );
542 CREATE INDEX "suggestion_created_idx" ON "suggestion" ("created");
543 CREATE INDEX "suggestion_author_id_created_idx" ON "suggestion" ("author_id", "created");
544 CREATE INDEX "suggestion_text_search_data_idx" ON "suggestion" USING gin ("text_search_data");
545 CREATE TRIGGER "update_text_search_data"
546 BEFORE INSERT OR UPDATE ON "suggestion"
547 FOR EACH ROW EXECUTE PROCEDURE
548 tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
549 "name", "description");
551 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';
553 COMMENT ON COLUMN "suggestion"."minus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
554 COMMENT ON COLUMN "suggestion"."minus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
555 COMMENT ON COLUMN "suggestion"."minus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
556 COMMENT ON COLUMN "suggestion"."minus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
557 COMMENT ON COLUMN "suggestion"."plus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
558 COMMENT ON COLUMN "suggestion"."plus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
559 COMMENT ON COLUMN "suggestion"."plus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
560 COMMENT ON COLUMN "suggestion"."plus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
563 CREATE TABLE "suggestion_setting" (
564 PRIMARY KEY ("member_id", "key", "suggestion_id"),
565 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
566 "key" TEXT NOT NULL,
567 "suggestion_id" INT8 REFERENCES "suggestion" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
568 "value" TEXT NOT NULL );
570 COMMENT ON TABLE "suggestion_setting" IS 'Place for frontend to store suggestion specific settings of members as strings';
573 CREATE TABLE "membership" (
574 PRIMARY KEY ("area_id", "member_id"),
575 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
576 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
577 "autoreject" BOOLEAN NOT NULL DEFAULT FALSE );
578 CREATE INDEX "membership_member_id_idx" ON "membership" ("member_id");
580 COMMENT ON TABLE "membership" IS 'Interest of members in topic areas';
582 COMMENT ON COLUMN "membership"."autoreject" IS 'TRUE = member votes against all initiatives, if he is neither direct_ or delegating_voter; Entries in the "interest" table can override this setting.';
585 CREATE TABLE "interest" (
586 PRIMARY KEY ("issue_id", "member_id"),
587 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
588 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
589 "autoreject" BOOLEAN,
590 "voting_requested" BOOLEAN );
591 CREATE INDEX "interest_member_id_idx" ON "interest" ("member_id");
593 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.';
595 COMMENT ON COLUMN "interest"."autoreject" IS 'TRUE = member votes against all initiatives in case of not explicitly taking part in the voting procedure';
596 COMMENT ON COLUMN "interest"."voting_requested" IS 'TRUE = member wants to vote now, FALSE = member wants to vote later, NULL = policy rules should apply';
599 CREATE TABLE "ignored_issue" (
600 PRIMARY KEY ("issue_id", "member_id"),
601 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
602 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
603 "new" BOOLEAN NOT NULL DEFAULT FALSE,
604 "accepted" BOOLEAN NOT NULL DEFAULT FALSE,
605 "half_frozen" BOOLEAN NOT NULL DEFAULT FALSE,
606 "fully_frozen" BOOLEAN NOT NULL DEFAULT FALSE );
607 CREATE INDEX "ignored_issue_member_id_idx" ON "ignored_issue" ("member_id");
609 COMMENT ON TABLE "ignored_issue" IS 'Table to store member specific options to ignore issues in selected states';
611 COMMENT ON COLUMN "ignored_issue"."new" IS 'Apply when issue is neither closed nor accepted';
612 COMMENT ON COLUMN "ignored_issue"."accepted" IS 'Apply when issue is accepted but not (half_)frozen or closed';
613 COMMENT ON COLUMN "ignored_issue"."half_frozen" IS 'Apply when issue is half_frozen but not fully_frozen or closed';
614 COMMENT ON COLUMN "ignored_issue"."fully_frozen" IS 'Apply when issue is fully_frozen (in voting) and not closed';
617 CREATE TABLE "initiator" (
618 PRIMARY KEY ("initiative_id", "member_id"),
619 "initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
620 "member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
621 "accepted" BOOLEAN );
622 CREATE INDEX "initiator_member_id_idx" ON "initiator" ("member_id");
624 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.';
626 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.';
629 CREATE TABLE "supporter" (
630 "issue_id" INT4 NOT NULL,
631 PRIMARY KEY ("initiative_id", "member_id"),
632 "initiative_id" INT4,
633 "member_id" INT4,
634 "draft_id" INT8 NOT NULL,
635 FOREIGN KEY ("issue_id", "member_id") REFERENCES "interest" ("issue_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE,
636 FOREIGN KEY ("initiative_id", "draft_id") REFERENCES "draft" ("initiative_id", "id") ON DELETE CASCADE ON UPDATE CASCADE );
637 CREATE INDEX "supporter_member_id_idx" ON "supporter" ("member_id");
639 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.';
641 COMMENT ON COLUMN "supporter"."draft_id" IS 'Latest seen draft, defaults to current draft of the initiative (implemented by trigger "default_for_draft_id")';
644 CREATE TABLE "opinion" (
645 "initiative_id" INT4 NOT NULL,
646 PRIMARY KEY ("suggestion_id", "member_id"),
647 "suggestion_id" INT8,
648 "member_id" INT4,
649 "degree" INT2 NOT NULL CHECK ("degree" >= -2 AND "degree" <= 2 AND "degree" != 0),
650 "fulfilled" BOOLEAN NOT NULL DEFAULT FALSE,
651 FOREIGN KEY ("initiative_id", "suggestion_id") REFERENCES "suggestion" ("initiative_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
652 FOREIGN KEY ("initiative_id", "member_id") REFERENCES "supporter" ("initiative_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
653 CREATE INDEX "opinion_member_id_initiative_id_idx" ON "opinion" ("member_id", "initiative_id");
655 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.';
657 COMMENT ON COLUMN "opinion"."degree" IS '2 = fulfillment required for support; 1 = fulfillment desired; -1 = fulfillment unwanted; -2 = fulfillment cancels support';
660 CREATE TYPE "delegation_scope" AS ENUM ('global', 'area', 'issue');
662 COMMENT ON TYPE "delegation_scope" IS 'Scope for delegations: ''global'', ''area'', or ''issue'' (order is relevant)';
665 CREATE TABLE "delegation" (
666 "id" SERIAL8 PRIMARY KEY,
667 "truster_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
668 "trustee_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
669 "scope" "delegation_scope" NOT NULL,
670 "area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
671 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
672 CONSTRAINT "cant_delegate_to_yourself" CHECK ("truster_id" != "trustee_id"),
673 CONSTRAINT "no_global_delegation_to_null"
674 CHECK ("trustee_id" NOTNULL OR "scope" != 'global'),
675 CONSTRAINT "area_id_and_issue_id_set_according_to_scope" CHECK (
676 ("scope" = 'global' AND "area_id" ISNULL AND "issue_id" ISNULL ) OR
677 ("scope" = 'area' AND "area_id" NOTNULL AND "issue_id" ISNULL ) OR
678 ("scope" = 'issue' AND "area_id" ISNULL AND "issue_id" NOTNULL) ),
679 UNIQUE ("area_id", "truster_id"),
680 UNIQUE ("issue_id", "truster_id") );
681 CREATE UNIQUE INDEX "delegation_global_truster_id_unique_idx"
682 ON "delegation" ("truster_id") WHERE "scope" = 'global';
683 CREATE INDEX "delegation_truster_id_idx" ON "delegation" ("truster_id");
684 CREATE INDEX "delegation_trustee_id_idx" ON "delegation" ("trustee_id");
686 COMMENT ON TABLE "delegation" IS 'Delegation of vote-weight to other members';
688 COMMENT ON COLUMN "delegation"."area_id" IS 'Reference to area, if delegation is area-wide, otherwise NULL';
689 COMMENT ON COLUMN "delegation"."issue_id" IS 'Reference to issue, if delegation is issue-wide, otherwise NULL';
692 CREATE TABLE "direct_population_snapshot" (
693 PRIMARY KEY ("issue_id", "event", "member_id"),
694 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
695 "event" "snapshot_event",
696 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
697 "weight" INT4 );
698 CREATE INDEX "direct_population_snapshot_member_id_idx" ON "direct_population_snapshot" ("member_id");
700 COMMENT ON TABLE "direct_population_snapshot" IS 'Snapshot of active members having either a "membership" in the "area" or an "interest" in the "issue"';
702 COMMENT ON COLUMN "direct_population_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
703 COMMENT ON COLUMN "direct_population_snapshot"."weight" IS 'Weight of member (1 or higher) according to "delegating_population_snapshot"';
706 CREATE TABLE "delegating_population_snapshot" (
707 PRIMARY KEY ("issue_id", "event", "member_id"),
708 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
709 "event" "snapshot_event",
710 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
711 "weight" INT4,
712 "scope" "delegation_scope" NOT NULL,
713 "delegate_member_ids" INT4[] NOT NULL );
714 CREATE INDEX "delegating_population_snapshot_member_id_idx" ON "delegating_population_snapshot" ("member_id");
716 COMMENT ON TABLE "direct_population_snapshot" IS 'Delegations increasing the weight of entries in the "direct_population_snapshot" table';
718 COMMENT ON COLUMN "delegating_population_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
719 COMMENT ON COLUMN "delegating_population_snapshot"."member_id" IS 'Delegating member';
720 COMMENT ON COLUMN "delegating_population_snapshot"."weight" IS 'Intermediate weight';
721 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"';
724 CREATE TABLE "direct_interest_snapshot" (
725 PRIMARY KEY ("issue_id", "event", "member_id"),
726 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
727 "event" "snapshot_event",
728 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
729 "weight" INT4,
730 "voting_requested" BOOLEAN );
731 CREATE INDEX "direct_interest_snapshot_member_id_idx" ON "direct_interest_snapshot" ("member_id");
733 COMMENT ON TABLE "direct_interest_snapshot" IS 'Snapshot of active members having an "interest" in the "issue"';
735 COMMENT ON COLUMN "direct_interest_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
736 COMMENT ON COLUMN "direct_interest_snapshot"."weight" IS 'Weight of member (1 or higher) according to "delegating_interest_snapshot"';
737 COMMENT ON COLUMN "direct_interest_snapshot"."voting_requested" IS 'Copied from column "voting_requested" of table "interest"';
740 CREATE TABLE "delegating_interest_snapshot" (
741 PRIMARY KEY ("issue_id", "event", "member_id"),
742 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
743 "event" "snapshot_event",
744 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
745 "weight" INT4,
746 "scope" "delegation_scope" NOT NULL,
747 "delegate_member_ids" INT4[] NOT NULL );
748 CREATE INDEX "delegating_interest_snapshot_member_id_idx" ON "delegating_interest_snapshot" ("member_id");
750 COMMENT ON TABLE "delegating_interest_snapshot" IS 'Delegations increasing the weight of entries in the "direct_interest_snapshot" table';
752 COMMENT ON COLUMN "delegating_interest_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
753 COMMENT ON COLUMN "delegating_interest_snapshot"."member_id" IS 'Delegating member';
754 COMMENT ON COLUMN "delegating_interest_snapshot"."weight" IS 'Intermediate weight';
755 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"';
758 CREATE TABLE "direct_supporter_snapshot" (
759 "issue_id" INT4 NOT NULL,
760 PRIMARY KEY ("initiative_id", "event", "member_id"),
761 "initiative_id" INT4,
762 "event" "snapshot_event",
763 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
764 "informed" BOOLEAN NOT NULL,
765 "satisfied" BOOLEAN NOT NULL,
766 FOREIGN KEY ("issue_id", "initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
767 FOREIGN KEY ("issue_id", "event", "member_id") REFERENCES "direct_interest_snapshot" ("issue_id", "event", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
768 CREATE INDEX "direct_supporter_snapshot_member_id_idx" ON "direct_supporter_snapshot" ("member_id");
770 COMMENT ON TABLE "direct_supporter_snapshot" IS 'Snapshot of supporters of initiatives (weight is stored in "direct_interest_snapshot")';
772 COMMENT ON COLUMN "direct_supporter_snapshot"."event" IS 'Reason for snapshot, see "snapshot_event" type for details';
773 COMMENT ON COLUMN "direct_supporter_snapshot"."informed" IS 'Supporter has seen the latest draft of the initiative';
774 COMMENT ON COLUMN "direct_supporter_snapshot"."satisfied" IS 'Supporter has no "critical_opinion"s';
777 CREATE TABLE "direct_voter" (
778 PRIMARY KEY ("issue_id", "member_id"),
779 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
780 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
781 "weight" INT4,
782 "autoreject" BOOLEAN NOT NULL DEFAULT FALSE );
783 CREATE INDEX "direct_voter_member_id_idx" ON "direct_voter" ("member_id");
785 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.';
787 COMMENT ON COLUMN "direct_voter"."weight" IS 'Weight of member (1 or higher) according to "delegating_voter" table';
788 COMMENT ON COLUMN "direct_voter"."autoreject" IS 'Votes were inserted due to "autoreject" feature';
791 CREATE TABLE "delegating_voter" (
792 PRIMARY KEY ("issue_id", "member_id"),
793 "issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
794 "member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
795 "weight" INT4,
796 "scope" "delegation_scope" NOT NULL,
797 "delegate_member_ids" INT4[] NOT NULL );
798 CREATE INDEX "delegating_voter_member_id_idx" ON "delegating_voter" ("member_id");
800 COMMENT ON TABLE "delegating_voter" IS 'Delegations increasing the weight of entries in the "direct_voter" table';
802 COMMENT ON COLUMN "delegating_voter"."member_id" IS 'Delegating member';
803 COMMENT ON COLUMN "delegating_voter"."weight" IS 'Intermediate weight';
804 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"';
807 CREATE TABLE "vote" (
808 "issue_id" INT4 NOT NULL,
809 PRIMARY KEY ("initiative_id", "member_id"),
810 "initiative_id" INT4,
811 "member_id" INT4,
812 "grade" INT4,
813 FOREIGN KEY ("issue_id", "initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
814 FOREIGN KEY ("issue_id", "member_id") REFERENCES "direct_voter" ("issue_id", "member_id") ON DELETE CASCADE ON UPDATE CASCADE );
815 CREATE INDEX "vote_member_id_idx" ON "vote" ("member_id");
817 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.';
819 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.';
822 CREATE TABLE "contingent" (
823 "time_frame" INTERVAL PRIMARY KEY,
824 "text_entry_limit" INT4,
825 "initiative_limit" INT4 );
827 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.';
829 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';
830 COMMENT ON COLUMN "contingent"."initiative_limit" IS 'Number of new initiatives to be opened by each member within a given time frame';
834 --------------------------------
835 -- Writing of history entries --
836 --------------------------------
838 CREATE FUNCTION "write_member_history_trigger"()
839 RETURNS TRIGGER
840 LANGUAGE 'plpgsql' VOLATILE AS $$
841 BEGIN
842 IF
843 NEW."active" != OLD."active" OR
844 NEW."name" != OLD."name"
845 THEN
846 INSERT INTO "member_history"
847 ("member_id", "active", "name")
848 VALUES (NEW."id", OLD."active", OLD."name");
849 END IF;
850 RETURN NULL;
851 END;
852 $$;
854 CREATE TRIGGER "write_member_history"
855 AFTER UPDATE ON "member" FOR EACH ROW EXECUTE PROCEDURE
856 "write_member_history_trigger"();
858 COMMENT ON FUNCTION "write_member_history_trigger"() IS 'Implementation of trigger "write_member_history" on table "member"';
859 COMMENT ON TRIGGER "write_member_history" ON "member" IS 'When changing certain fields of a member, create a history entry in "member_history" table';
863 ----------------------------
864 -- Additional constraints --
865 ----------------------------
868 CREATE FUNCTION "issue_requires_first_initiative_trigger"()
869 RETURNS TRIGGER
870 LANGUAGE 'plpgsql' VOLATILE AS $$
871 BEGIN
872 IF NOT EXISTS (
873 SELECT NULL FROM "initiative" WHERE "issue_id" = NEW."id"
874 ) THEN
875 --RAISE 'Cannot create issue without an initial initiative.' USING
876 -- ERRCODE = 'integrity_constraint_violation',
877 -- HINT = 'Create issue, initiative, and draft within the same transaction.';
878 RAISE EXCEPTION 'Cannot create issue without an initial initiative.';
879 END IF;
880 RETURN NULL;
881 END;
882 $$;
884 CREATE CONSTRAINT TRIGGER "issue_requires_first_initiative"
885 AFTER INSERT OR UPDATE ON "issue" DEFERRABLE INITIALLY DEFERRED
886 FOR EACH ROW EXECUTE PROCEDURE
887 "issue_requires_first_initiative_trigger"();
889 COMMENT ON FUNCTION "issue_requires_first_initiative_trigger"() IS 'Implementation of trigger "issue_requires_first_initiative" on table "issue"';
890 COMMENT ON TRIGGER "issue_requires_first_initiative" ON "issue" IS 'Ensure that new issues have at least one initiative';
893 CREATE FUNCTION "last_initiative_deletes_issue_trigger"()
894 RETURNS TRIGGER
895 LANGUAGE 'plpgsql' VOLATILE AS $$
896 DECLARE
897 "reference_lost" BOOLEAN;
898 BEGIN
899 IF TG_OP = 'DELETE' THEN
900 "reference_lost" := TRUE;
901 ELSE
902 "reference_lost" := NEW."issue_id" != OLD."issue_id";
903 END IF;
904 IF
905 "reference_lost" AND NOT EXISTS (
906 SELECT NULL FROM "initiative" WHERE "issue_id" = OLD."issue_id"
907 )
908 THEN
909 DELETE FROM "issue" WHERE "id" = OLD."issue_id";
910 END IF;
911 RETURN NULL;
912 END;
913 $$;
915 CREATE CONSTRAINT TRIGGER "last_initiative_deletes_issue"
916 AFTER UPDATE OR DELETE ON "initiative" DEFERRABLE INITIALLY DEFERRED
917 FOR EACH ROW EXECUTE PROCEDURE
918 "last_initiative_deletes_issue_trigger"();
920 COMMENT ON FUNCTION "last_initiative_deletes_issue_trigger"() IS 'Implementation of trigger "last_initiative_deletes_issue" on table "initiative"';
921 COMMENT ON TRIGGER "last_initiative_deletes_issue" ON "initiative" IS 'Removing the last initiative of an issue deletes the issue';
924 CREATE FUNCTION "initiative_requires_first_draft_trigger"()
925 RETURNS TRIGGER
926 LANGUAGE 'plpgsql' VOLATILE AS $$
927 BEGIN
928 IF NOT EXISTS (
929 SELECT NULL FROM "draft" WHERE "initiative_id" = NEW."id"
930 ) THEN
931 --RAISE 'Cannot create initiative without an initial draft.' USING
932 -- ERRCODE = 'integrity_constraint_violation',
933 -- HINT = 'Create issue, initiative and draft within the same transaction.';
934 RAISE EXCEPTION 'Cannot create initiative without an initial draft.';
935 END IF;
936 RETURN NULL;
937 END;
938 $$;
940 CREATE CONSTRAINT TRIGGER "initiative_requires_first_draft"
941 AFTER INSERT OR UPDATE ON "initiative" DEFERRABLE INITIALLY DEFERRED
942 FOR EACH ROW EXECUTE PROCEDURE
943 "initiative_requires_first_draft_trigger"();
945 COMMENT ON FUNCTION "initiative_requires_first_draft_trigger"() IS 'Implementation of trigger "initiative_requires_first_draft" on table "initiative"';
946 COMMENT ON TRIGGER "initiative_requires_first_draft" ON "initiative" IS 'Ensure that new initiatives have at least one draft';
949 CREATE FUNCTION "last_draft_deletes_initiative_trigger"()
950 RETURNS TRIGGER
951 LANGUAGE 'plpgsql' VOLATILE AS $$
952 DECLARE
953 "reference_lost" BOOLEAN;
954 BEGIN
955 IF TG_OP = 'DELETE' THEN
956 "reference_lost" := TRUE;
957 ELSE
958 "reference_lost" := NEW."initiative_id" != OLD."initiative_id";
959 END IF;
960 IF
961 "reference_lost" AND NOT EXISTS (
962 SELECT NULL FROM "draft" WHERE "initiative_id" = OLD."initiative_id"
963 )
964 THEN
965 DELETE FROM "initiative" WHERE "id" = OLD."initiative_id";
966 END IF;
967 RETURN NULL;
968 END;
969 $$;
971 CREATE CONSTRAINT TRIGGER "last_draft_deletes_initiative"
972 AFTER UPDATE OR DELETE ON "draft" DEFERRABLE INITIALLY DEFERRED
973 FOR EACH ROW EXECUTE PROCEDURE
974 "last_draft_deletes_initiative_trigger"();
976 COMMENT ON FUNCTION "last_draft_deletes_initiative_trigger"() IS 'Implementation of trigger "last_draft_deletes_initiative" on table "draft"';
977 COMMENT ON TRIGGER "last_draft_deletes_initiative" ON "draft" IS 'Removing the last draft of an initiative deletes the initiative';
980 CREATE FUNCTION "suggestion_requires_first_opinion_trigger"()
981 RETURNS TRIGGER
982 LANGUAGE 'plpgsql' VOLATILE AS $$
983 BEGIN
984 IF NOT EXISTS (
985 SELECT NULL FROM "opinion" WHERE "suggestion_id" = NEW."id"
986 ) THEN
987 RAISE EXCEPTION 'Cannot create a suggestion without an opinion.';
988 END IF;
989 RETURN NULL;
990 END;
991 $$;
993 CREATE CONSTRAINT TRIGGER "suggestion_requires_first_opinion"
994 AFTER INSERT OR UPDATE ON "suggestion" DEFERRABLE INITIALLY DEFERRED
995 FOR EACH ROW EXECUTE PROCEDURE
996 "suggestion_requires_first_opinion_trigger"();
998 COMMENT ON FUNCTION "suggestion_requires_first_opinion_trigger"() IS 'Implementation of trigger "suggestion_requires_first_opinion" on table "suggestion"';
999 COMMENT ON TRIGGER "suggestion_requires_first_opinion" ON "suggestion" IS 'Ensure that new suggestions have at least one opinion';
1002 CREATE FUNCTION "last_opinion_deletes_suggestion_trigger"()
1003 RETURNS TRIGGER
1004 LANGUAGE 'plpgsql' VOLATILE AS $$
1005 DECLARE
1006 "reference_lost" BOOLEAN;
1007 BEGIN
1008 IF TG_OP = 'DELETE' THEN
1009 "reference_lost" := TRUE;
1010 ELSE
1011 "reference_lost" := NEW."suggestion_id" != OLD."suggestion_id";
1012 END IF;
1013 IF
1014 "reference_lost" AND NOT EXISTS (
1015 SELECT NULL FROM "opinion" WHERE "suggestion_id" = OLD."suggestion_id"
1017 THEN
1018 DELETE FROM "suggestion" WHERE "id" = OLD."suggestion_id";
1019 END IF;
1020 RETURN NULL;
1021 END;
1022 $$;
1024 CREATE CONSTRAINT TRIGGER "last_opinion_deletes_suggestion"
1025 AFTER UPDATE OR DELETE ON "opinion" DEFERRABLE INITIALLY DEFERRED
1026 FOR EACH ROW EXECUTE PROCEDURE
1027 "last_opinion_deletes_suggestion_trigger"();
1029 COMMENT ON FUNCTION "last_opinion_deletes_suggestion_trigger"() IS 'Implementation of trigger "last_opinion_deletes_suggestion" on table "opinion"';
1030 COMMENT ON TRIGGER "last_opinion_deletes_suggestion" ON "opinion" IS 'Removing the last opinion of a suggestion deletes the suggestion';
1034 ---------------------------------------------------------------
1035 -- Ensure that votes are not modified when issues are frozen --
1036 ---------------------------------------------------------------
1038 -- NOTE: Frontends should ensure this anyway, but in case of programming
1039 -- errors the following triggers ensure data integrity.
1042 CREATE FUNCTION "forbid_changes_on_closed_issue_trigger"()
1043 RETURNS TRIGGER
1044 LANGUAGE 'plpgsql' VOLATILE AS $$
1045 DECLARE
1046 "issue_id_v" "issue"."id"%TYPE;
1047 "issue_row" "issue"%ROWTYPE;
1048 BEGIN
1049 IF TG_OP = 'DELETE' THEN
1050 "issue_id_v" := OLD."issue_id";
1051 ELSE
1052 "issue_id_v" := NEW."issue_id";
1053 END IF;
1054 SELECT INTO "issue_row" * FROM "issue"
1055 WHERE "id" = "issue_id_v" FOR SHARE;
1056 IF "issue_row"."closed" NOTNULL THEN
1057 RAISE EXCEPTION 'Tried to modify data belonging to a closed issue.';
1058 END IF;
1059 RETURN NULL;
1060 END;
1061 $$;
1063 CREATE TRIGGER "forbid_changes_on_closed_issue"
1064 AFTER INSERT OR UPDATE OR DELETE ON "direct_voter"
1065 FOR EACH ROW EXECUTE PROCEDURE
1066 "forbid_changes_on_closed_issue_trigger"();
1068 CREATE TRIGGER "forbid_changes_on_closed_issue"
1069 AFTER INSERT OR UPDATE OR DELETE ON "delegating_voter"
1070 FOR EACH ROW EXECUTE PROCEDURE
1071 "forbid_changes_on_closed_issue_trigger"();
1073 CREATE TRIGGER "forbid_changes_on_closed_issue"
1074 AFTER INSERT OR UPDATE OR DELETE ON "vote"
1075 FOR EACH ROW EXECUTE PROCEDURE
1076 "forbid_changes_on_closed_issue_trigger"();
1078 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"';
1079 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';
1080 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';
1081 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';
1085 --------------------------------------------------------------------
1086 -- Auto-retrieval of fields only needed for referential integrity --
1087 --------------------------------------------------------------------
1090 CREATE FUNCTION "autofill_issue_id_trigger"()
1091 RETURNS TRIGGER
1092 LANGUAGE 'plpgsql' VOLATILE AS $$
1093 BEGIN
1094 IF NEW."issue_id" ISNULL THEN
1095 SELECT "issue_id" INTO NEW."issue_id"
1096 FROM "initiative" WHERE "id" = NEW."initiative_id";
1097 END IF;
1098 RETURN NEW;
1099 END;
1100 $$;
1102 CREATE TRIGGER "autofill_issue_id" BEFORE INSERT ON "supporter"
1103 FOR EACH ROW EXECUTE PROCEDURE "autofill_issue_id_trigger"();
1105 CREATE TRIGGER "autofill_issue_id" BEFORE INSERT ON "vote"
1106 FOR EACH ROW EXECUTE PROCEDURE "autofill_issue_id_trigger"();
1108 COMMENT ON FUNCTION "autofill_issue_id_trigger"() IS 'Implementation of triggers "autofill_issue_id" on tables "supporter" and "vote"';
1109 COMMENT ON TRIGGER "autofill_issue_id" ON "supporter" IS 'Set "issue_id" field automatically, if NULL';
1110 COMMENT ON TRIGGER "autofill_issue_id" ON "vote" IS 'Set "issue_id" field automatically, if NULL';
1113 CREATE FUNCTION "autofill_initiative_id_trigger"()
1114 RETURNS TRIGGER
1115 LANGUAGE 'plpgsql' VOLATILE AS $$
1116 BEGIN
1117 IF NEW."initiative_id" ISNULL THEN
1118 SELECT "initiative_id" INTO NEW."initiative_id"
1119 FROM "suggestion" WHERE "id" = NEW."suggestion_id";
1120 END IF;
1121 RETURN NEW;
1122 END;
1123 $$;
1125 CREATE TRIGGER "autofill_initiative_id" BEFORE INSERT ON "opinion"
1126 FOR EACH ROW EXECUTE PROCEDURE "autofill_initiative_id_trigger"();
1128 COMMENT ON FUNCTION "autofill_initiative_id_trigger"() IS 'Implementation of trigger "autofill_initiative_id" on table "opinion"';
1129 COMMENT ON TRIGGER "autofill_initiative_id" ON "opinion" IS 'Set "initiative_id" field automatically, if NULL';
1133 -----------------------------------------------------
1134 -- Automatic calculation of certain default values --
1135 -----------------------------------------------------
1138 CREATE FUNCTION "copy_timings_trigger"()
1139 RETURNS TRIGGER
1140 LANGUAGE 'plpgsql' VOLATILE AS $$
1141 DECLARE
1142 "policy_row" "policy"%ROWTYPE;
1143 BEGIN
1144 SELECT * INTO "policy_row" FROM "policy"
1145 WHERE "id" = NEW."policy_id";
1146 IF NEW."admission_time" ISNULL THEN
1147 NEW."admission_time" := "policy_row"."admission_time";
1148 END IF;
1149 IF NEW."discussion_time" ISNULL THEN
1150 NEW."discussion_time" := "policy_row"."discussion_time";
1151 END IF;
1152 IF NEW."verification_time" ISNULL THEN
1153 NEW."verification_time" := "policy_row"."verification_time";
1154 END IF;
1155 IF NEW."voting_time" ISNULL THEN
1156 NEW."voting_time" := "policy_row"."voting_time";
1157 END IF;
1158 RETURN NEW;
1159 END;
1160 $$;
1162 CREATE TRIGGER "copy_timings" BEFORE INSERT OR UPDATE ON "issue"
1163 FOR EACH ROW EXECUTE PROCEDURE "copy_timings_trigger"();
1165 COMMENT ON FUNCTION "copy_timings_trigger"() IS 'Implementation of trigger "copy_timings" on table "issue"';
1166 COMMENT ON TRIGGER "copy_timings" ON "issue" IS 'If timing fields are NULL, copy values from policy.';
1169 CREATE FUNCTION "supporter_default_for_draft_id_trigger"()
1170 RETURNS TRIGGER
1171 LANGUAGE 'plpgsql' VOLATILE AS $$
1172 BEGIN
1173 IF NEW."draft_id" ISNULL THEN
1174 SELECT "id" INTO NEW."draft_id" FROM "current_draft"
1175 WHERE "initiative_id" = NEW."initiative_id";
1176 END IF;
1177 RETURN NEW;
1178 END;
1179 $$;
1181 CREATE TRIGGER "default_for_draft_id" BEFORE INSERT OR UPDATE ON "supporter"
1182 FOR EACH ROW EXECUTE PROCEDURE "supporter_default_for_draft_id_trigger"();
1184 COMMENT ON FUNCTION "supporter_default_for_draft_id_trigger"() IS 'Implementation of trigger "default_for_draft" on table "supporter"';
1185 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';
1189 ----------------------------------------
1190 -- Automatic creation of dependencies --
1191 ----------------------------------------
1194 CREATE FUNCTION "autocreate_interest_trigger"()
1195 RETURNS TRIGGER
1196 LANGUAGE 'plpgsql' VOLATILE AS $$
1197 BEGIN
1198 IF NOT EXISTS (
1199 SELECT NULL FROM "initiative" JOIN "interest"
1200 ON "initiative"."issue_id" = "interest"."issue_id"
1201 WHERE "initiative"."id" = NEW."initiative_id"
1202 AND "interest"."member_id" = NEW."member_id"
1203 ) THEN
1204 BEGIN
1205 INSERT INTO "interest" ("issue_id", "member_id")
1206 SELECT "issue_id", NEW."member_id"
1207 FROM "initiative" WHERE "id" = NEW."initiative_id";
1208 EXCEPTION WHEN unique_violation THEN END;
1209 END IF;
1210 RETURN NEW;
1211 END;
1212 $$;
1214 CREATE TRIGGER "autocreate_interest" BEFORE INSERT ON "supporter"
1215 FOR EACH ROW EXECUTE PROCEDURE "autocreate_interest_trigger"();
1217 COMMENT ON FUNCTION "autocreate_interest_trigger"() IS 'Implementation of trigger "autocreate_interest" on table "supporter"';
1218 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';
1221 CREATE FUNCTION "autocreate_supporter_trigger"()
1222 RETURNS TRIGGER
1223 LANGUAGE 'plpgsql' VOLATILE AS $$
1224 BEGIN
1225 IF NOT EXISTS (
1226 SELECT NULL FROM "suggestion" JOIN "supporter"
1227 ON "suggestion"."initiative_id" = "supporter"."initiative_id"
1228 WHERE "suggestion"."id" = NEW."suggestion_id"
1229 AND "supporter"."member_id" = NEW."member_id"
1230 ) THEN
1231 BEGIN
1232 INSERT INTO "supporter" ("initiative_id", "member_id")
1233 SELECT "initiative_id", NEW."member_id"
1234 FROM "suggestion" WHERE "id" = NEW."suggestion_id";
1235 EXCEPTION WHEN unique_violation THEN END;
1236 END IF;
1237 RETURN NEW;
1238 END;
1239 $$;
1241 CREATE TRIGGER "autocreate_supporter" BEFORE INSERT ON "opinion"
1242 FOR EACH ROW EXECUTE PROCEDURE "autocreate_supporter_trigger"();
1244 COMMENT ON FUNCTION "autocreate_supporter_trigger"() IS 'Implementation of trigger "autocreate_supporter" on table "opinion"';
1245 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.';
1249 ------------------------------------------
1250 -- Views and helper functions for views --
1251 ------------------------------------------
1254 CREATE VIEW "active_delegation" AS
1255 SELECT "delegation".* FROM "delegation"
1256 JOIN "member" ON "delegation"."truster_id" = "member"."id"
1257 WHERE "member"."active" = TRUE;
1259 COMMENT ON VIEW "active_delegation" IS 'Helper view for views "global_delegation", "area_delegation" and "issue_delegation": Contains delegations where the truster_id refers to an active member and includes those delegations where trustee_id is NULL';
1262 CREATE VIEW "global_delegation" AS
1263 SELECT "id", "truster_id", "trustee_id"
1264 FROM "active_delegation" WHERE "scope" = 'global';
1266 COMMENT ON VIEW "global_delegation" IS 'Global delegations from active members';
1269 CREATE VIEW "area_delegation" AS
1270 SELECT DISTINCT ON ("area"."id", "delegation"."truster_id")
1271 "area"."id" AS "area_id",
1272 "delegation"."id",
1273 "delegation"."truster_id",
1274 "delegation"."trustee_id",
1275 "delegation"."scope"
1276 FROM "area" JOIN "active_delegation" AS "delegation"
1277 ON "delegation"."scope" = 'global'
1278 OR "delegation"."area_id" = "area"."id"
1279 ORDER BY
1280 "area"."id",
1281 "delegation"."truster_id",
1282 "delegation"."scope" DESC;
1284 COMMENT ON VIEW "area_delegation" IS 'Resulting area delegations from active members; can include rows with trustee_id set to NULL';
1287 CREATE VIEW "issue_delegation" AS
1288 SELECT DISTINCT ON ("issue"."id", "delegation"."truster_id")
1289 "issue"."id" AS "issue_id",
1290 "delegation"."id",
1291 "delegation"."truster_id",
1292 "delegation"."trustee_id",
1293 "delegation"."scope"
1294 FROM "issue" JOIN "active_delegation" AS "delegation"
1295 ON "delegation"."scope" = 'global'
1296 OR "delegation"."area_id" = "issue"."area_id"
1297 OR "delegation"."issue_id" = "issue"."id"
1298 ORDER BY
1299 "issue"."id",
1300 "delegation"."truster_id",
1301 "delegation"."scope" DESC;
1303 COMMENT ON VIEW "issue_delegation" IS 'Resulting issue delegations from active members; can include rows with trustee_id set to NULL';
1306 CREATE FUNCTION "membership_weight_with_skipping"
1307 ( "area_id_p" "area"."id"%TYPE,
1308 "member_id_p" "member"."id"%TYPE,
1309 "skip_member_ids_p" INT4[] ) -- "member"."id"%TYPE[]
1310 RETURNS INT4
1311 LANGUAGE 'plpgsql' STABLE AS $$
1312 DECLARE
1313 "sum_v" INT4;
1314 "delegation_row" "area_delegation"%ROWTYPE;
1315 BEGIN
1316 "sum_v" := 1;
1317 FOR "delegation_row" IN
1318 SELECT "area_delegation".*
1319 FROM "area_delegation" LEFT JOIN "membership"
1320 ON "membership"."area_id" = "area_id_p"
1321 AND "membership"."member_id" = "area_delegation"."truster_id"
1322 WHERE "area_delegation"."area_id" = "area_id_p"
1323 AND "area_delegation"."trustee_id" = "member_id_p"
1324 AND "membership"."member_id" ISNULL
1325 LOOP
1326 IF NOT
1327 "skip_member_ids_p" @> ARRAY["delegation_row"."truster_id"]
1328 THEN
1329 "sum_v" := "sum_v" + "membership_weight_with_skipping"(
1330 "area_id_p",
1331 "delegation_row"."truster_id",
1332 "skip_member_ids_p" || "delegation_row"."truster_id"
1333 );
1334 END IF;
1335 END LOOP;
1336 RETURN "sum_v";
1337 END;
1338 $$;
1340 COMMENT ON FUNCTION "membership_weight_with_skipping"
1341 ( "area"."id"%TYPE,
1342 "member"."id"%TYPE,
1343 INT4[] )
1344 IS 'Helper function for "membership_weight" function';
1347 CREATE FUNCTION "membership_weight"
1348 ( "area_id_p" "area"."id"%TYPE,
1349 "member_id_p" "member"."id"%TYPE ) -- "member"."id"%TYPE[]
1350 RETURNS INT4
1351 LANGUAGE 'plpgsql' STABLE AS $$
1352 BEGIN
1353 RETURN "membership_weight_with_skipping"(
1354 "area_id_p",
1355 "member_id_p",
1356 ARRAY["member_id_p"]
1357 );
1358 END;
1359 $$;
1361 COMMENT ON FUNCTION "membership_weight"
1362 ( "area"."id"%TYPE,
1363 "member"."id"%TYPE )
1364 IS 'Calculates the potential voting weight of a member in a given area';
1367 CREATE VIEW "member_count_view" AS
1368 SELECT count(1) AS "total_count" FROM "member" WHERE "active";
1370 COMMENT ON VIEW "member_count_view" IS 'View used to update "member_count" table';
1373 CREATE VIEW "area_member_count" AS
1374 SELECT
1375 "area"."id" AS "area_id",
1376 count("member"."id") AS "direct_member_count",
1377 coalesce(
1378 sum(
1379 CASE WHEN "member"."id" NOTNULL THEN
1380 "membership_weight"("area"."id", "member"."id")
1381 ELSE 0 END
1383 ) AS "member_weight",
1384 coalesce(
1385 sum(
1386 CASE WHEN "member"."id" NOTNULL AND "membership"."autoreject" THEN
1387 "membership_weight"("area"."id", "member"."id")
1388 ELSE 0 END
1390 ) AS "autoreject_weight"
1391 FROM "area"
1392 LEFT JOIN "membership"
1393 ON "area"."id" = "membership"."area_id"
1394 LEFT JOIN "member"
1395 ON "membership"."member_id" = "member"."id"
1396 AND "member"."active"
1397 GROUP BY "area"."id";
1399 COMMENT ON VIEW "area_member_count" IS 'View used to update "member_count" column of table "area"';
1402 CREATE VIEW "opening_draft" AS
1403 SELECT "draft".* FROM (
1404 SELECT
1405 "initiative"."id" AS "initiative_id",
1406 min("draft"."id") AS "draft_id"
1407 FROM "initiative" JOIN "draft"
1408 ON "initiative"."id" = "draft"."initiative_id"
1409 GROUP BY "initiative"."id"
1410 ) AS "subquery"
1411 JOIN "draft" ON "subquery"."draft_id" = "draft"."id";
1413 COMMENT ON VIEW "opening_draft" IS 'First drafts of all initiatives';
1416 CREATE VIEW "current_draft" AS
1417 SELECT "draft".* FROM (
1418 SELECT
1419 "initiative"."id" AS "initiative_id",
1420 max("draft"."id") AS "draft_id"
1421 FROM "initiative" JOIN "draft"
1422 ON "initiative"."id" = "draft"."initiative_id"
1423 GROUP BY "initiative"."id"
1424 ) AS "subquery"
1425 JOIN "draft" ON "subquery"."draft_id" = "draft"."id";
1427 COMMENT ON VIEW "current_draft" IS 'All latest drafts for each initiative';
1430 CREATE VIEW "critical_opinion" AS
1431 SELECT * FROM "opinion"
1432 WHERE ("degree" = 2 AND "fulfilled" = FALSE)
1433 OR ("degree" = -2 AND "fulfilled" = TRUE);
1435 COMMENT ON VIEW "critical_opinion" IS 'Opinions currently causing dissatisfaction';
1438 CREATE VIEW "battle_view" AS
1439 SELECT
1440 "issue"."id" AS "issue_id",
1441 "winning_initiative"."id" AS "winning_initiative_id",
1442 "losing_initiative"."id" AS "losing_initiative_id",
1443 sum(
1444 CASE WHEN
1445 coalesce("better_vote"."grade", 0) >
1446 coalesce("worse_vote"."grade", 0)
1447 THEN "direct_voter"."weight" ELSE 0 END
1448 ) AS "count"
1449 FROM "issue"
1450 LEFT JOIN "direct_voter"
1451 ON "issue"."id" = "direct_voter"."issue_id"
1452 JOIN "initiative" AS "winning_initiative"
1453 ON "issue"."id" = "winning_initiative"."issue_id"
1454 AND "winning_initiative"."agreed"
1455 JOIN "initiative" AS "losing_initiative"
1456 ON "issue"."id" = "losing_initiative"."issue_id"
1457 AND "losing_initiative"."agreed"
1458 LEFT JOIN "vote" AS "better_vote"
1459 ON "direct_voter"."member_id" = "better_vote"."member_id"
1460 AND "winning_initiative"."id" = "better_vote"."initiative_id"
1461 LEFT JOIN "vote" AS "worse_vote"
1462 ON "direct_voter"."member_id" = "worse_vote"."member_id"
1463 AND "losing_initiative"."id" = "worse_vote"."initiative_id"
1464 WHERE "issue"."closed" NOTNULL
1465 AND "issue"."cleaned" ISNULL
1466 AND "winning_initiative"."id" != "losing_initiative"."id"
1467 GROUP BY
1468 "issue"."id",
1469 "winning_initiative"."id",
1470 "losing_initiative"."id";
1472 COMMENT ON VIEW "battle_view" IS 'Number of members preferring one initiative to another; Used to fill "battle" table';
1475 CREATE VIEW "expired_session" AS
1476 SELECT * FROM "session" WHERE now() > "expiry";
1478 CREATE RULE "delete" AS ON DELETE TO "expired_session" DO INSTEAD
1479 DELETE FROM "session" WHERE "ident" = OLD."ident";
1481 COMMENT ON VIEW "expired_session" IS 'View containing all expired sessions where DELETE is possible';
1482 COMMENT ON RULE "delete" ON "expired_session" IS 'Rule allowing DELETE on rows in "expired_session" view, i.e. DELETE FROM "expired_session"';
1485 CREATE VIEW "open_issue" AS
1486 SELECT * FROM "issue" WHERE "closed" ISNULL;
1488 COMMENT ON VIEW "open_issue" IS 'All open issues';
1491 CREATE VIEW "issue_with_ranks_missing" AS
1492 SELECT * FROM "issue"
1493 WHERE "fully_frozen" NOTNULL
1494 AND "closed" NOTNULL
1495 AND "ranks_available" = FALSE;
1497 COMMENT ON VIEW "issue_with_ranks_missing" IS 'Issues where voting was finished, but no ranks have been calculated yet';
1500 CREATE VIEW "member_contingent" AS
1501 SELECT
1502 "member"."id" AS "member_id",
1503 "contingent"."time_frame",
1504 CASE WHEN "contingent"."text_entry_limit" NOTNULL THEN
1506 SELECT count(1) FROM "draft"
1507 WHERE "draft"."author_id" = "member"."id"
1508 AND "draft"."created" > now() - "contingent"."time_frame"
1509 ) + (
1510 SELECT count(1) FROM "suggestion"
1511 WHERE "suggestion"."author_id" = "member"."id"
1512 AND "suggestion"."created" > now() - "contingent"."time_frame"
1514 ELSE NULL END AS "text_entry_count",
1515 "contingent"."text_entry_limit",
1516 CASE WHEN "contingent"."initiative_limit" NOTNULL THEN (
1517 SELECT count(1) FROM "opening_draft"
1518 WHERE "opening_draft"."author_id" = "member"."id"
1519 AND "opening_draft"."created" > now() - "contingent"."time_frame"
1520 ) ELSE NULL END AS "initiative_count",
1521 "contingent"."initiative_limit"
1522 FROM "member" CROSS JOIN "contingent";
1524 COMMENT ON VIEW "member_contingent" IS 'Actual counts of text entries and initiatives are calculated per member for each limit in the "contingent" table.';
1526 COMMENT ON COLUMN "member_contingent"."text_entry_count" IS 'Only calculated when "text_entry_limit" is not null in the same row';
1527 COMMENT ON COLUMN "member_contingent"."initiative_count" IS 'Only calculated when "initiative_limit" is not null in the same row';
1530 CREATE VIEW "member_contingent_left" AS
1531 SELECT
1532 "member_id",
1533 max("text_entry_limit" - "text_entry_count") AS "text_entries_left",
1534 max("initiative_limit" - "initiative_count") AS "initiatives_left"
1535 FROM "member_contingent" GROUP BY "member_id";
1537 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.';
1540 CREATE TYPE "timeline_event" AS ENUM (
1541 'issue_created',
1542 'issue_canceled',
1543 'issue_accepted',
1544 'issue_half_frozen',
1545 'issue_finished_without_voting',
1546 'issue_voting_started',
1547 'issue_finished_after_voting',
1548 'initiative_created',
1549 'initiative_revoked',
1550 'draft_created',
1551 'suggestion_created');
1553 COMMENT ON TYPE "timeline_event" IS 'Types of event in timeline tables';
1556 CREATE VIEW "timeline_issue" AS
1557 SELECT
1558 "created" AS "occurrence",
1559 'issue_created'::"timeline_event" AS "event",
1560 "id" AS "issue_id"
1561 FROM "issue"
1562 UNION ALL
1563 SELECT
1564 "closed" AS "occurrence",
1565 'issue_canceled'::"timeline_event" AS "event",
1566 "id" AS "issue_id"
1567 FROM "issue" WHERE "closed" NOTNULL AND "fully_frozen" ISNULL
1568 UNION ALL
1569 SELECT
1570 "accepted" AS "occurrence",
1571 'issue_accepted'::"timeline_event" AS "event",
1572 "id" AS "issue_id"
1573 FROM "issue" WHERE "accepted" NOTNULL
1574 UNION ALL
1575 SELECT
1576 "half_frozen" AS "occurrence",
1577 'issue_half_frozen'::"timeline_event" AS "event",
1578 "id" AS "issue_id"
1579 FROM "issue" WHERE "half_frozen" NOTNULL
1580 UNION ALL
1581 SELECT
1582 "fully_frozen" AS "occurrence",
1583 'issue_voting_started'::"timeline_event" AS "event",
1584 "id" AS "issue_id"
1585 FROM "issue"
1586 WHERE "fully_frozen" NOTNULL
1587 AND ("closed" ISNULL OR "closed" != "fully_frozen")
1588 UNION ALL
1589 SELECT
1590 "closed" AS "occurrence",
1591 CASE WHEN "fully_frozen" = "closed" THEN
1592 'issue_finished_without_voting'::"timeline_event"
1593 ELSE
1594 'issue_finished_after_voting'::"timeline_event"
1595 END AS "event",
1596 "id" AS "issue_id"
1597 FROM "issue" WHERE "closed" NOTNULL AND "fully_frozen" NOTNULL;
1599 COMMENT ON VIEW "timeline_issue" IS 'Helper view for "timeline" view';
1602 CREATE VIEW "timeline_initiative" AS
1603 SELECT
1604 "created" AS "occurrence",
1605 'initiative_created'::"timeline_event" AS "event",
1606 "id" AS "initiative_id"
1607 FROM "initiative"
1608 UNION ALL
1609 SELECT
1610 "revoked" AS "occurrence",
1611 'initiative_revoked'::"timeline_event" AS "event",
1612 "id" AS "initiative_id"
1613 FROM "initiative" WHERE "revoked" NOTNULL;
1615 COMMENT ON VIEW "timeline_initiative" IS 'Helper view for "timeline" view';
1618 CREATE VIEW "timeline_draft" AS
1619 SELECT
1620 "created" AS "occurrence",
1621 'draft_created'::"timeline_event" AS "event",
1622 "id" AS "draft_id"
1623 FROM "draft";
1625 COMMENT ON VIEW "timeline_draft" IS 'Helper view for "timeline" view';
1628 CREATE VIEW "timeline_suggestion" AS
1629 SELECT
1630 "created" AS "occurrence",
1631 'suggestion_created'::"timeline_event" AS "event",
1632 "id" AS "suggestion_id"
1633 FROM "suggestion";
1635 COMMENT ON VIEW "timeline_suggestion" IS 'Helper view for "timeline" view';
1638 CREATE VIEW "timeline" AS
1639 SELECT
1640 "occurrence",
1641 "event",
1642 "issue_id",
1643 NULL AS "initiative_id",
1644 NULL::INT8 AS "draft_id", -- TODO: Why do we need a type-cast here? Is this due to 32 bit architecture?
1645 NULL::INT8 AS "suggestion_id"
1646 FROM "timeline_issue"
1647 UNION ALL
1648 SELECT
1649 "occurrence",
1650 "event",
1651 NULL AS "issue_id",
1652 "initiative_id",
1653 NULL AS "draft_id",
1654 NULL AS "suggestion_id"
1655 FROM "timeline_initiative"
1656 UNION ALL
1657 SELECT
1658 "occurrence",
1659 "event",
1660 NULL AS "issue_id",
1661 NULL AS "initiative_id",
1662 "draft_id",
1663 NULL AS "suggestion_id"
1664 FROM "timeline_draft"
1665 UNION ALL
1666 SELECT
1667 "occurrence",
1668 "event",
1669 NULL AS "issue_id",
1670 NULL AS "initiative_id",
1671 NULL AS "draft_id",
1672 "suggestion_id"
1673 FROM "timeline_suggestion";
1675 COMMENT ON VIEW "timeline" IS 'Aggregation of different events in the system';
1679 --------------------------------------------------
1680 -- Set returning function for delegation chains --
1681 --------------------------------------------------
1684 CREATE TYPE "delegation_chain_loop_tag" AS ENUM
1685 ('first', 'intermediate', 'last', 'repetition');
1687 COMMENT ON TYPE "delegation_chain_loop_tag" IS 'Type for loop tags in "delegation_chain_row" type';
1690 CREATE TYPE "delegation_chain_row" AS (
1691 "index" INT4,
1692 "member_id" INT4,
1693 "member_active" BOOLEAN,
1694 "participation" BOOLEAN,
1695 "overridden" BOOLEAN,
1696 "scope_in" "delegation_scope",
1697 "scope_out" "delegation_scope",
1698 "disabled_out" BOOLEAN,
1699 "loop" "delegation_chain_loop_tag" );
1701 COMMENT ON TYPE "delegation_chain_row" IS 'Type of rows returned by "delegation_chain"(...) functions';
1703 COMMENT ON COLUMN "delegation_chain_row"."index" IS 'Index starting with 0 and counting up';
1704 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';
1705 COMMENT ON COLUMN "delegation_chain_row"."overridden" IS 'True, if an entry with lower index has "participation" set to true';
1706 COMMENT ON COLUMN "delegation_chain_row"."scope_in" IS 'Scope of used incoming delegation';
1707 COMMENT ON COLUMN "delegation_chain_row"."scope_out" IS 'Scope of used outgoing delegation';
1708 COMMENT ON COLUMN "delegation_chain_row"."disabled_out" IS 'Outgoing delegation is explicitly disabled by a delegation with trustee_id set to NULL';
1709 COMMENT ON COLUMN "delegation_chain_row"."loop" IS 'Not null, if member is part of a loop, see "delegation_chain_loop_tag" type';
1712 CREATE FUNCTION "delegation_chain"
1713 ( "member_id_p" "member"."id"%TYPE,
1714 "area_id_p" "area"."id"%TYPE,
1715 "issue_id_p" "issue"."id"%TYPE,
1716 "simulate_trustee_id_p" "member"."id"%TYPE )
1717 RETURNS SETOF "delegation_chain_row"
1718 LANGUAGE 'plpgsql' STABLE AS $$
1719 DECLARE
1720 "issue_row" "issue"%ROWTYPE;
1721 "visited_member_ids" INT4[]; -- "member"."id"%TYPE[]
1722 "loop_member_id_v" "member"."id"%TYPE;
1723 "output_row" "delegation_chain_row";
1724 "output_rows" "delegation_chain_row"[];
1725 "delegation_row" "delegation"%ROWTYPE;
1726 "row_count" INT4;
1727 "i" INT4;
1728 "loop_v" BOOLEAN;
1729 BEGIN
1730 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
1731 "visited_member_ids" := '{}';
1732 "loop_member_id_v" := NULL;
1733 "output_rows" := '{}';
1734 "output_row"."index" := 0;
1735 "output_row"."member_id" := "member_id_p";
1736 "output_row"."member_active" := TRUE;
1737 "output_row"."participation" := FALSE;
1738 "output_row"."overridden" := FALSE;
1739 "output_row"."disabled_out" := FALSE;
1740 "output_row"."scope_out" := NULL;
1741 LOOP
1742 IF "visited_member_ids" @> ARRAY["output_row"."member_id"] THEN
1743 "loop_member_id_v" := "output_row"."member_id";
1744 ELSE
1745 "visited_member_ids" :=
1746 "visited_member_ids" || "output_row"."member_id";
1747 END IF;
1748 IF "output_row"."participation" THEN
1749 "output_row"."overridden" := TRUE;
1750 END IF;
1751 "output_row"."scope_in" := "output_row"."scope_out";
1752 IF EXISTS (
1753 SELECT NULL FROM "member"
1754 WHERE "id" = "output_row"."member_id" AND "active"
1755 ) THEN
1756 IF "area_id_p" ISNULL AND "issue_id_p" ISNULL THEN
1757 SELECT * INTO "delegation_row" FROM "delegation"
1758 WHERE "truster_id" = "output_row"."member_id"
1759 AND "scope" = 'global';
1760 ELSIF "area_id_p" NOTNULL AND "issue_id_p" ISNULL THEN
1761 "output_row"."participation" := EXISTS (
1762 SELECT NULL FROM "membership"
1763 WHERE "area_id" = "area_id_p"
1764 AND "member_id" = "output_row"."member_id"
1765 );
1766 SELECT * INTO "delegation_row" FROM "delegation"
1767 WHERE "truster_id" = "output_row"."member_id"
1768 AND ("scope" = 'global' OR "area_id" = "area_id_p")
1769 ORDER BY "scope" DESC;
1770 ELSIF "area_id_p" ISNULL AND "issue_id_p" NOTNULL THEN
1771 "output_row"."participation" := EXISTS (
1772 SELECT NULL FROM "interest"
1773 WHERE "issue_id" = "issue_id_p"
1774 AND "member_id" = "output_row"."member_id"
1775 );
1776 SELECT * INTO "delegation_row" FROM "delegation"
1777 WHERE "truster_id" = "output_row"."member_id"
1778 AND ("scope" = 'global' OR
1779 "area_id" = "issue_row"."area_id" OR
1780 "issue_id" = "issue_id_p"
1782 ORDER BY "scope" DESC;
1783 ELSE
1784 RAISE EXCEPTION 'Either area_id or issue_id or both must be NULL.';
1785 END IF;
1786 ELSE
1787 "output_row"."member_active" := FALSE;
1788 "output_row"."participation" := FALSE;
1789 "output_row"."scope_out" := NULL;
1790 "delegation_row" := ROW(NULL);
1791 END IF;
1792 IF
1793 "output_row"."member_id" = "member_id_p" AND
1794 "simulate_trustee_id_p" NOTNULL
1795 THEN
1796 "output_row"."scope_out" := CASE
1797 WHEN "area_id_p" ISNULL AND "issue_id_p" ISNULL THEN 'global'
1798 WHEN "area_id_p" NOTNULL AND "issue_id_p" ISNULL THEN 'area'
1799 WHEN "area_id_p" ISNULL AND "issue_id_p" NOTNULL THEN 'issue'
1800 END;
1801 "output_rows" := "output_rows" || "output_row";
1802 "output_row"."member_id" := "simulate_trustee_id_p";
1803 ELSIF "delegation_row"."trustee_id" NOTNULL THEN
1804 "output_row"."scope_out" := "delegation_row"."scope";
1805 "output_rows" := "output_rows" || "output_row";
1806 "output_row"."member_id" := "delegation_row"."trustee_id";
1807 ELSIF "delegation_row"."scope" NOTNULL THEN
1808 "output_row"."scope_out" := "delegation_row"."scope";
1809 "output_row"."disabled_out" := TRUE;
1810 "output_rows" := "output_rows" || "output_row";
1811 EXIT;
1812 ELSE
1813 "output_row"."scope_out" := NULL;
1814 "output_rows" := "output_rows" || "output_row";
1815 EXIT;
1816 END IF;
1817 EXIT WHEN "loop_member_id_v" NOTNULL;
1818 "output_row"."index" := "output_row"."index" + 1;
1819 END LOOP;
1820 "row_count" := array_upper("output_rows", 1);
1821 "i" := 1;
1822 "loop_v" := FALSE;
1823 LOOP
1824 "output_row" := "output_rows"["i"];
1825 EXIT WHEN "output_row" ISNULL;
1826 IF "loop_v" THEN
1827 IF "i" + 1 = "row_count" THEN
1828 "output_row"."loop" := 'last';
1829 ELSIF "i" = "row_count" THEN
1830 "output_row"."loop" := 'repetition';
1831 ELSE
1832 "output_row"."loop" := 'intermediate';
1833 END IF;
1834 ELSIF "output_row"."member_id" = "loop_member_id_v" THEN
1835 "output_row"."loop" := 'first';
1836 "loop_v" := TRUE;
1837 END IF;
1838 IF "area_id_p" ISNULL AND "issue_id_p" ISNULL THEN
1839 "output_row"."participation" := NULL;
1840 END IF;
1841 RETURN NEXT "output_row";
1842 "i" := "i" + 1;
1843 END LOOP;
1844 RETURN;
1845 END;
1846 $$;
1848 COMMENT ON FUNCTION "delegation_chain"
1849 ( "member"."id"%TYPE,
1850 "area"."id"%TYPE,
1851 "issue"."id"%TYPE,
1852 "member"."id"%TYPE )
1853 IS 'Helper function for frontends to display delegation chains; Not part of internal voting logic';
1855 CREATE FUNCTION "delegation_chain"
1856 ( "member_id_p" "member"."id"%TYPE,
1857 "area_id_p" "area"."id"%TYPE,
1858 "issue_id_p" "issue"."id"%TYPE )
1859 RETURNS SETOF "delegation_chain_row"
1860 LANGUAGE 'plpgsql' STABLE AS $$
1861 DECLARE
1862 "result_row" "delegation_chain_row";
1863 BEGIN
1864 FOR "result_row" IN
1865 SELECT * FROM "delegation_chain"(
1866 "member_id_p", "area_id_p", "issue_id_p", NULL
1868 LOOP
1869 RETURN NEXT "result_row";
1870 END LOOP;
1871 RETURN;
1872 END;
1873 $$;
1875 COMMENT ON FUNCTION "delegation_chain"
1876 ( "member"."id"%TYPE,
1877 "area"."id"%TYPE,
1878 "issue"."id"%TYPE )
1879 IS 'Shortcut for "delegation_chain"(...) function where 4th parameter is null';
1883 ------------------------------
1884 -- Comparison by vote count --
1885 ------------------------------
1887 CREATE FUNCTION "vote_ratio"
1888 ( "positive_votes_p" "initiative"."positive_votes"%TYPE,
1889 "negative_votes_p" "initiative"."negative_votes"%TYPE )
1890 RETURNS FLOAT8
1891 LANGUAGE 'plpgsql' STABLE AS $$
1892 BEGIN
1893 IF "positive_votes_p" > 0 AND "negative_votes_p" > 0 THEN
1894 RETURN
1895 "positive_votes_p"::FLOAT8 /
1896 ("positive_votes_p" + "negative_votes_p")::FLOAT8;
1897 ELSIF "positive_votes_p" > 0 THEN
1898 RETURN "positive_votes_p";
1899 ELSIF "negative_votes_p" > 0 THEN
1900 RETURN 1 - "negative_votes_p";
1901 ELSE
1902 RETURN 0.5;
1903 END IF;
1904 END;
1905 $$;
1907 COMMENT ON FUNCTION "vote_ratio"
1908 ( "initiative"."positive_votes"%TYPE,
1909 "initiative"."negative_votes"%TYPE )
1910 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.';
1914 ------------------------------------------------
1915 -- Locking for snapshots and voting procedure --
1916 ------------------------------------------------
1919 CREATE FUNCTION "share_row_lock_issue_trigger"()
1920 RETURNS TRIGGER
1921 LANGUAGE 'plpgsql' VOLATILE AS $$
1922 BEGIN
1923 IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
1924 PERFORM NULL FROM "issue" WHERE "id" = OLD."issue_id" FOR SHARE;
1925 END IF;
1926 IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
1927 PERFORM NULL FROM "issue" WHERE "id" = NEW."issue_id" FOR SHARE;
1928 RETURN NEW;
1929 ELSE
1930 RETURN OLD;
1931 END IF;
1932 END;
1933 $$;
1935 COMMENT ON FUNCTION "share_row_lock_issue_trigger"() IS 'Implementation of triggers "share_row_lock_issue" on multiple tables';
1938 CREATE FUNCTION "share_row_lock_issue_via_initiative_trigger"()
1939 RETURNS TRIGGER
1940 LANGUAGE 'plpgsql' VOLATILE AS $$
1941 BEGIN
1942 IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
1943 PERFORM NULL FROM "issue"
1944 JOIN "initiative" ON "issue"."id" = "initiative"."issue_id"
1945 WHERE "initiative"."id" = OLD."initiative_id"
1946 FOR SHARE OF "issue";
1947 END IF;
1948 IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
1949 PERFORM NULL FROM "issue"
1950 JOIN "initiative" ON "issue"."id" = "initiative"."issue_id"
1951 WHERE "initiative"."id" = NEW."initiative_id"
1952 FOR SHARE OF "issue";
1953 RETURN NEW;
1954 ELSE
1955 RETURN OLD;
1956 END IF;
1957 END;
1958 $$;
1960 COMMENT ON FUNCTION "share_row_lock_issue_trigger"() IS 'Implementation of trigger "share_row_lock_issue_via_initiative" on table "opinion"';
1963 CREATE TRIGGER "share_row_lock_issue"
1964 BEFORE INSERT OR UPDATE OR DELETE ON "initiative"
1965 FOR EACH ROW EXECUTE PROCEDURE
1966 "share_row_lock_issue_trigger"();
1968 CREATE TRIGGER "share_row_lock_issue"
1969 BEFORE INSERT OR UPDATE OR DELETE ON "interest"
1970 FOR EACH ROW EXECUTE PROCEDURE
1971 "share_row_lock_issue_trigger"();
1973 CREATE TRIGGER "share_row_lock_issue"
1974 BEFORE INSERT OR UPDATE OR DELETE ON "supporter"
1975 FOR EACH ROW EXECUTE PROCEDURE
1976 "share_row_lock_issue_trigger"();
1978 CREATE TRIGGER "share_row_lock_issue_via_initiative"
1979 BEFORE INSERT OR UPDATE OR DELETE ON "opinion"
1980 FOR EACH ROW EXECUTE PROCEDURE
1981 "share_row_lock_issue_via_initiative_trigger"();
1983 CREATE TRIGGER "share_row_lock_issue"
1984 BEFORE INSERT OR UPDATE OR DELETE ON "direct_voter"
1985 FOR EACH ROW EXECUTE PROCEDURE
1986 "share_row_lock_issue_trigger"();
1988 CREATE TRIGGER "share_row_lock_issue"
1989 BEFORE INSERT OR UPDATE OR DELETE ON "delegating_voter"
1990 FOR EACH ROW EXECUTE PROCEDURE
1991 "share_row_lock_issue_trigger"();
1993 CREATE TRIGGER "share_row_lock_issue"
1994 BEFORE INSERT OR UPDATE OR DELETE ON "vote"
1995 FOR EACH ROW EXECUTE PROCEDURE
1996 "share_row_lock_issue_trigger"();
1998 COMMENT ON TRIGGER "share_row_lock_issue" ON "initiative" IS 'See "lock_issue" function';
1999 COMMENT ON TRIGGER "share_row_lock_issue" ON "interest" IS 'See "lock_issue" function';
2000 COMMENT ON TRIGGER "share_row_lock_issue" ON "supporter" IS 'See "lock_issue" function';
2001 COMMENT ON TRIGGER "share_row_lock_issue_via_initiative" ON "opinion" IS 'See "lock_issue" function';
2002 COMMENT ON TRIGGER "share_row_lock_issue" ON "direct_voter" IS 'See "lock_issue" function';
2003 COMMENT ON TRIGGER "share_row_lock_issue" ON "delegating_voter" IS 'See "lock_issue" function';
2004 COMMENT ON TRIGGER "share_row_lock_issue" ON "vote" IS 'See "lock_issue" function';
2007 CREATE FUNCTION "lock_issue"
2008 ( "issue_id_p" "issue"."id"%TYPE )
2009 RETURNS VOID
2010 LANGUAGE 'plpgsql' VOLATILE AS $$
2011 BEGIN
2012 LOCK TABLE "member" IN SHARE MODE;
2013 LOCK TABLE "membership" IN SHARE MODE;
2014 LOCK TABLE "policy" IN SHARE MODE;
2015 PERFORM NULL FROM "issue" WHERE "id" = "issue_id_p" FOR UPDATE;
2016 -- NOTE: The row-level exclusive lock in combination with the
2017 -- share_row_lock_issue(_via_initiative)_trigger functions (which
2018 -- acquire a row-level share lock on the issue) ensure that no data
2019 -- is changed, which could affect calculation of snapshots or
2020 -- counting of votes. Table "delegation" must be table-level-locked,
2021 -- as it also contains issue- and global-scope delegations.
2022 LOCK TABLE "delegation" IN SHARE MODE;
2023 LOCK TABLE "direct_population_snapshot" IN EXCLUSIVE MODE;
2024 LOCK TABLE "delegating_population_snapshot" IN EXCLUSIVE MODE;
2025 LOCK TABLE "direct_interest_snapshot" IN EXCLUSIVE MODE;
2026 LOCK TABLE "delegating_interest_snapshot" IN EXCLUSIVE MODE;
2027 LOCK TABLE "direct_supporter_snapshot" IN EXCLUSIVE MODE;
2028 RETURN;
2029 END;
2030 $$;
2032 COMMENT ON FUNCTION "lock_issue"
2033 ( "issue"."id"%TYPE )
2034 IS 'Locks the issue and all other data which is used for calculating snapshots or counting votes.';
2038 -------------------------------
2039 -- Materialize member counts --
2040 -------------------------------
2042 CREATE FUNCTION "calculate_member_counts"()
2043 RETURNS VOID
2044 LANGUAGE 'plpgsql' VOLATILE AS $$
2045 BEGIN
2046 LOCK TABLE "member" IN SHARE MODE;
2047 LOCK TABLE "member_count" IN EXCLUSIVE MODE;
2048 LOCK TABLE "area" IN EXCLUSIVE MODE;
2049 LOCK TABLE "membership" IN SHARE MODE;
2050 DELETE FROM "member_count";
2051 INSERT INTO "member_count" ("total_count")
2052 SELECT "total_count" FROM "member_count_view";
2053 UPDATE "area" SET
2054 "direct_member_count" = "view"."direct_member_count",
2055 "member_weight" = "view"."member_weight",
2056 "autoreject_weight" = "view"."autoreject_weight"
2057 FROM "area_member_count" AS "view"
2058 WHERE "view"."area_id" = "area"."id";
2059 RETURN;
2060 END;
2061 $$;
2063 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"';
2067 ------------------------------
2068 -- Calculation of snapshots --
2069 ------------------------------
2071 CREATE FUNCTION "weight_of_added_delegations_for_population_snapshot"
2072 ( "issue_id_p" "issue"."id"%TYPE,
2073 "member_id_p" "member"."id"%TYPE,
2074 "delegate_member_ids_p" "delegating_population_snapshot"."delegate_member_ids"%TYPE )
2075 RETURNS "direct_population_snapshot"."weight"%TYPE
2076 LANGUAGE 'plpgsql' VOLATILE AS $$
2077 DECLARE
2078 "issue_delegation_row" "issue_delegation"%ROWTYPE;
2079 "delegate_member_ids_v" "delegating_population_snapshot"."delegate_member_ids"%TYPE;
2080 "weight_v" INT4;
2081 "sub_weight_v" INT4;
2082 BEGIN
2083 "weight_v" := 0;
2084 FOR "issue_delegation_row" IN
2085 SELECT * FROM "issue_delegation"
2086 WHERE "trustee_id" = "member_id_p"
2087 AND "issue_id" = "issue_id_p"
2088 LOOP
2089 IF NOT EXISTS (
2090 SELECT NULL FROM "direct_population_snapshot"
2091 WHERE "issue_id" = "issue_id_p"
2092 AND "event" = 'periodic'
2093 AND "member_id" = "issue_delegation_row"."truster_id"
2094 ) AND NOT EXISTS (
2095 SELECT NULL FROM "delegating_population_snapshot"
2096 WHERE "issue_id" = "issue_id_p"
2097 AND "event" = 'periodic'
2098 AND "member_id" = "issue_delegation_row"."truster_id"
2099 ) THEN
2100 "delegate_member_ids_v" :=
2101 "member_id_p" || "delegate_member_ids_p";
2102 INSERT INTO "delegating_population_snapshot" (
2103 "issue_id",
2104 "event",
2105 "member_id",
2106 "scope",
2107 "delegate_member_ids"
2108 ) VALUES (
2109 "issue_id_p",
2110 'periodic',
2111 "issue_delegation_row"."truster_id",
2112 "issue_delegation_row"."scope",
2113 "delegate_member_ids_v"
2114 );
2115 "sub_weight_v" := 1 +
2116 "weight_of_added_delegations_for_population_snapshot"(
2117 "issue_id_p",
2118 "issue_delegation_row"."truster_id",
2119 "delegate_member_ids_v"
2120 );
2121 UPDATE "delegating_population_snapshot"
2122 SET "weight" = "sub_weight_v"
2123 WHERE "issue_id" = "issue_id_p"
2124 AND "event" = 'periodic'
2125 AND "member_id" = "issue_delegation_row"."truster_id";
2126 "weight_v" := "weight_v" + "sub_weight_v";
2127 END IF;
2128 END LOOP;
2129 RETURN "weight_v";
2130 END;
2131 $$;
2133 COMMENT ON FUNCTION "weight_of_added_delegations_for_population_snapshot"
2134 ( "issue"."id"%TYPE,
2135 "member"."id"%TYPE,
2136 "delegating_population_snapshot"."delegate_member_ids"%TYPE )
2137 IS 'Helper function for "create_population_snapshot" function';
2140 CREATE FUNCTION "create_population_snapshot"
2141 ( "issue_id_p" "issue"."id"%TYPE )
2142 RETURNS VOID
2143 LANGUAGE 'plpgsql' VOLATILE AS $$
2144 DECLARE
2145 "member_id_v" "member"."id"%TYPE;
2146 BEGIN
2147 DELETE FROM "direct_population_snapshot"
2148 WHERE "issue_id" = "issue_id_p"
2149 AND "event" = 'periodic';
2150 DELETE FROM "delegating_population_snapshot"
2151 WHERE "issue_id" = "issue_id_p"
2152 AND "event" = 'periodic';
2153 INSERT INTO "direct_population_snapshot"
2154 ("issue_id", "event", "member_id")
2155 SELECT
2156 "issue_id_p" AS "issue_id",
2157 'periodic'::"snapshot_event" AS "event",
2158 "member"."id" AS "member_id"
2159 FROM "issue"
2160 JOIN "area" ON "issue"."area_id" = "area"."id"
2161 JOIN "membership" ON "area"."id" = "membership"."area_id"
2162 JOIN "member" ON "membership"."member_id" = "member"."id"
2163 WHERE "issue"."id" = "issue_id_p"
2164 AND "member"."active"
2165 UNION
2166 SELECT
2167 "issue_id_p" AS "issue_id",
2168 'periodic'::"snapshot_event" AS "event",
2169 "member"."id" AS "member_id"
2170 FROM "interest" JOIN "member"
2171 ON "interest"."member_id" = "member"."id"
2172 WHERE "interest"."issue_id" = "issue_id_p"
2173 AND "member"."active";
2174 FOR "member_id_v" IN
2175 SELECT "member_id" FROM "direct_population_snapshot"
2176 WHERE "issue_id" = "issue_id_p"
2177 AND "event" = 'periodic'
2178 LOOP
2179 UPDATE "direct_population_snapshot" SET
2180 "weight" = 1 +
2181 "weight_of_added_delegations_for_population_snapshot"(
2182 "issue_id_p",
2183 "member_id_v",
2184 '{}'
2186 WHERE "issue_id" = "issue_id_p"
2187 AND "event" = 'periodic'
2188 AND "member_id" = "member_id_v";
2189 END LOOP;
2190 RETURN;
2191 END;
2192 $$;
2194 COMMENT ON FUNCTION "create_population_snapshot"
2195 ( "issue"."id"%TYPE )
2196 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.';
2199 CREATE FUNCTION "weight_of_added_delegations_for_interest_snapshot"
2200 ( "issue_id_p" "issue"."id"%TYPE,
2201 "member_id_p" "member"."id"%TYPE,
2202 "delegate_member_ids_p" "delegating_interest_snapshot"."delegate_member_ids"%TYPE )
2203 RETURNS "direct_interest_snapshot"."weight"%TYPE
2204 LANGUAGE 'plpgsql' VOLATILE AS $$
2205 DECLARE
2206 "issue_delegation_row" "issue_delegation"%ROWTYPE;
2207 "delegate_member_ids_v" "delegating_interest_snapshot"."delegate_member_ids"%TYPE;
2208 "weight_v" INT4;
2209 "sub_weight_v" INT4;
2210 BEGIN
2211 "weight_v" := 0;
2212 FOR "issue_delegation_row" IN
2213 SELECT * FROM "issue_delegation"
2214 WHERE "trustee_id" = "member_id_p"
2215 AND "issue_id" = "issue_id_p"
2216 LOOP
2217 IF NOT EXISTS (
2218 SELECT NULL FROM "direct_interest_snapshot"
2219 WHERE "issue_id" = "issue_id_p"
2220 AND "event" = 'periodic'
2221 AND "member_id" = "issue_delegation_row"."truster_id"
2222 ) AND NOT EXISTS (
2223 SELECT NULL FROM "delegating_interest_snapshot"
2224 WHERE "issue_id" = "issue_id_p"
2225 AND "event" = 'periodic'
2226 AND "member_id" = "issue_delegation_row"."truster_id"
2227 ) THEN
2228 "delegate_member_ids_v" :=
2229 "member_id_p" || "delegate_member_ids_p";
2230 INSERT INTO "delegating_interest_snapshot" (
2231 "issue_id",
2232 "event",
2233 "member_id",
2234 "scope",
2235 "delegate_member_ids"
2236 ) VALUES (
2237 "issue_id_p",
2238 'periodic',
2239 "issue_delegation_row"."truster_id",
2240 "issue_delegation_row"."scope",
2241 "delegate_member_ids_v"
2242 );
2243 "sub_weight_v" := 1 +
2244 "weight_of_added_delegations_for_interest_snapshot"(
2245 "issue_id_p",
2246 "issue_delegation_row"."truster_id",
2247 "delegate_member_ids_v"
2248 );
2249 UPDATE "delegating_interest_snapshot"
2250 SET "weight" = "sub_weight_v"
2251 WHERE "issue_id" = "issue_id_p"
2252 AND "event" = 'periodic'
2253 AND "member_id" = "issue_delegation_row"."truster_id";
2254 "weight_v" := "weight_v" + "sub_weight_v";
2255 END IF;
2256 END LOOP;
2257 RETURN "weight_v";
2258 END;
2259 $$;
2261 COMMENT ON FUNCTION "weight_of_added_delegations_for_interest_snapshot"
2262 ( "issue"."id"%TYPE,
2263 "member"."id"%TYPE,
2264 "delegating_interest_snapshot"."delegate_member_ids"%TYPE )
2265 IS 'Helper function for "create_interest_snapshot" function';
2268 CREATE FUNCTION "create_interest_snapshot"
2269 ( "issue_id_p" "issue"."id"%TYPE )
2270 RETURNS VOID
2271 LANGUAGE 'plpgsql' VOLATILE AS $$
2272 DECLARE
2273 "member_id_v" "member"."id"%TYPE;
2274 BEGIN
2275 DELETE FROM "direct_interest_snapshot"
2276 WHERE "issue_id" = "issue_id_p"
2277 AND "event" = 'periodic';
2278 DELETE FROM "delegating_interest_snapshot"
2279 WHERE "issue_id" = "issue_id_p"
2280 AND "event" = 'periodic';
2281 DELETE FROM "direct_supporter_snapshot"
2282 WHERE "issue_id" = "issue_id_p"
2283 AND "event" = 'periodic';
2284 INSERT INTO "direct_interest_snapshot"
2285 ("issue_id", "event", "member_id", "voting_requested")
2286 SELECT
2287 "issue_id_p" AS "issue_id",
2288 'periodic' AS "event",
2289 "member"."id" AS "member_id",
2290 "interest"."voting_requested"
2291 FROM "interest" JOIN "member"
2292 ON "interest"."member_id" = "member"."id"
2293 WHERE "interest"."issue_id" = "issue_id_p"
2294 AND "member"."active";
2295 FOR "member_id_v" IN
2296 SELECT "member_id" FROM "direct_interest_snapshot"
2297 WHERE "issue_id" = "issue_id_p"
2298 AND "event" = 'periodic'
2299 LOOP
2300 UPDATE "direct_interest_snapshot" SET
2301 "weight" = 1 +
2302 "weight_of_added_delegations_for_interest_snapshot"(
2303 "issue_id_p",
2304 "member_id_v",
2305 '{}'
2307 WHERE "issue_id" = "issue_id_p"
2308 AND "event" = 'periodic'
2309 AND "member_id" = "member_id_v";
2310 END LOOP;
2311 INSERT INTO "direct_supporter_snapshot"
2312 ( "issue_id", "initiative_id", "event", "member_id",
2313 "informed", "satisfied" )
2314 SELECT
2315 "issue_id_p" AS "issue_id",
2316 "initiative"."id" AS "initiative_id",
2317 'periodic' AS "event",
2318 "member"."id" AS "member_id",
2319 "supporter"."draft_id" = "current_draft"."id" AS "informed",
2320 NOT EXISTS (
2321 SELECT NULL FROM "critical_opinion"
2322 WHERE "initiative_id" = "initiative"."id"
2323 AND "member_id" = "member"."id"
2324 ) AS "satisfied"
2325 FROM "supporter"
2326 JOIN "member"
2327 ON "supporter"."member_id" = "member"."id"
2328 JOIN "initiative"
2329 ON "supporter"."initiative_id" = "initiative"."id"
2330 JOIN "current_draft"
2331 ON "initiative"."id" = "current_draft"."initiative_id"
2332 JOIN "direct_interest_snapshot"
2333 ON "member"."id" = "direct_interest_snapshot"."member_id"
2334 AND "initiative"."issue_id" = "direct_interest_snapshot"."issue_id"
2335 AND "event" = 'periodic'
2336 WHERE "member"."active"
2337 AND "initiative"."issue_id" = "issue_id_p";
2338 RETURN;
2339 END;
2340 $$;
2342 COMMENT ON FUNCTION "create_interest_snapshot"
2343 ( "issue"."id"%TYPE )
2344 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.';
2347 CREATE FUNCTION "create_snapshot"
2348 ( "issue_id_p" "issue"."id"%TYPE )
2349 RETURNS VOID
2350 LANGUAGE 'plpgsql' VOLATILE AS $$
2351 DECLARE
2352 "initiative_id_v" "initiative"."id"%TYPE;
2353 "suggestion_id_v" "suggestion"."id"%TYPE;
2354 BEGIN
2355 PERFORM "lock_issue"("issue_id_p");
2356 PERFORM "create_population_snapshot"("issue_id_p");
2357 PERFORM "create_interest_snapshot"("issue_id_p");
2358 UPDATE "issue" SET
2359 "snapshot" = now(),
2360 "latest_snapshot_event" = 'periodic',
2361 "population" = (
2362 SELECT coalesce(sum("weight"), 0)
2363 FROM "direct_population_snapshot"
2364 WHERE "issue_id" = "issue_id_p"
2365 AND "event" = 'periodic'
2366 ),
2367 "vote_now" = (
2368 SELECT coalesce(sum("weight"), 0)
2369 FROM "direct_interest_snapshot"
2370 WHERE "issue_id" = "issue_id_p"
2371 AND "event" = 'periodic'
2372 AND "voting_requested" = TRUE
2373 ),
2374 "vote_later" = (
2375 SELECT coalesce(sum("weight"), 0)
2376 FROM "direct_interest_snapshot"
2377 WHERE "issue_id" = "issue_id_p"
2378 AND "event" = 'periodic'
2379 AND "voting_requested" = FALSE
2381 WHERE "id" = "issue_id_p";
2382 FOR "initiative_id_v" IN
2383 SELECT "id" FROM "initiative" WHERE "issue_id" = "issue_id_p"
2384 LOOP
2385 UPDATE "initiative" SET
2386 "supporter_count" = (
2387 SELECT coalesce(sum("di"."weight"), 0)
2388 FROM "direct_interest_snapshot" AS "di"
2389 JOIN "direct_supporter_snapshot" AS "ds"
2390 ON "di"."member_id" = "ds"."member_id"
2391 WHERE "di"."issue_id" = "issue_id_p"
2392 AND "di"."event" = 'periodic'
2393 AND "ds"."initiative_id" = "initiative_id_v"
2394 AND "ds"."event" = 'periodic'
2395 ),
2396 "informed_supporter_count" = (
2397 SELECT coalesce(sum("di"."weight"), 0)
2398 FROM "direct_interest_snapshot" AS "di"
2399 JOIN "direct_supporter_snapshot" AS "ds"
2400 ON "di"."member_id" = "ds"."member_id"
2401 WHERE "di"."issue_id" = "issue_id_p"
2402 AND "di"."event" = 'periodic'
2403 AND "ds"."initiative_id" = "initiative_id_v"
2404 AND "ds"."event" = 'periodic'
2405 AND "ds"."informed"
2406 ),
2407 "satisfied_supporter_count" = (
2408 SELECT coalesce(sum("di"."weight"), 0)
2409 FROM "direct_interest_snapshot" AS "di"
2410 JOIN "direct_supporter_snapshot" AS "ds"
2411 ON "di"."member_id" = "ds"."member_id"
2412 WHERE "di"."issue_id" = "issue_id_p"
2413 AND "di"."event" = 'periodic'
2414 AND "ds"."initiative_id" = "initiative_id_v"
2415 AND "ds"."event" = 'periodic'
2416 AND "ds"."satisfied"
2417 ),
2418 "satisfied_informed_supporter_count" = (
2419 SELECT coalesce(sum("di"."weight"), 0)
2420 FROM "direct_interest_snapshot" AS "di"
2421 JOIN "direct_supporter_snapshot" AS "ds"
2422 ON "di"."member_id" = "ds"."member_id"
2423 WHERE "di"."issue_id" = "issue_id_p"
2424 AND "di"."event" = 'periodic'
2425 AND "ds"."initiative_id" = "initiative_id_v"
2426 AND "ds"."event" = 'periodic'
2427 AND "ds"."informed"
2428 AND "ds"."satisfied"
2430 WHERE "id" = "initiative_id_v";
2431 FOR "suggestion_id_v" IN
2432 SELECT "id" FROM "suggestion"
2433 WHERE "initiative_id" = "initiative_id_v"
2434 LOOP
2435 UPDATE "suggestion" SET
2436 "minus2_unfulfilled_count" = (
2437 SELECT coalesce(sum("snapshot"."weight"), 0)
2438 FROM "issue" CROSS JOIN "opinion"
2439 JOIN "direct_interest_snapshot" AS "snapshot"
2440 ON "snapshot"."issue_id" = "issue"."id"
2441 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2442 AND "snapshot"."member_id" = "opinion"."member_id"
2443 WHERE "issue"."id" = "issue_id_p"
2444 AND "opinion"."suggestion_id" = "suggestion_id_v"
2445 AND "opinion"."degree" = -2
2446 AND "opinion"."fulfilled" = FALSE
2447 ),
2448 "minus2_fulfilled_count" = (
2449 SELECT coalesce(sum("snapshot"."weight"), 0)
2450 FROM "issue" CROSS JOIN "opinion"
2451 JOIN "direct_interest_snapshot" AS "snapshot"
2452 ON "snapshot"."issue_id" = "issue"."id"
2453 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2454 AND "snapshot"."member_id" = "opinion"."member_id"
2455 WHERE "issue"."id" = "issue_id_p"
2456 AND "opinion"."suggestion_id" = "suggestion_id_v"
2457 AND "opinion"."degree" = -2
2458 AND "opinion"."fulfilled" = TRUE
2459 ),
2460 "minus1_unfulfilled_count" = (
2461 SELECT coalesce(sum("snapshot"."weight"), 0)
2462 FROM "issue" CROSS JOIN "opinion"
2463 JOIN "direct_interest_snapshot" AS "snapshot"
2464 ON "snapshot"."issue_id" = "issue"."id"
2465 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2466 AND "snapshot"."member_id" = "opinion"."member_id"
2467 WHERE "issue"."id" = "issue_id_p"
2468 AND "opinion"."suggestion_id" = "suggestion_id_v"
2469 AND "opinion"."degree" = -1
2470 AND "opinion"."fulfilled" = FALSE
2471 ),
2472 "minus1_fulfilled_count" = (
2473 SELECT coalesce(sum("snapshot"."weight"), 0)
2474 FROM "issue" CROSS JOIN "opinion"
2475 JOIN "direct_interest_snapshot" AS "snapshot"
2476 ON "snapshot"."issue_id" = "issue"."id"
2477 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2478 AND "snapshot"."member_id" = "opinion"."member_id"
2479 WHERE "issue"."id" = "issue_id_p"
2480 AND "opinion"."suggestion_id" = "suggestion_id_v"
2481 AND "opinion"."degree" = -1
2482 AND "opinion"."fulfilled" = TRUE
2483 ),
2484 "plus1_unfulfilled_count" = (
2485 SELECT coalesce(sum("snapshot"."weight"), 0)
2486 FROM "issue" CROSS JOIN "opinion"
2487 JOIN "direct_interest_snapshot" AS "snapshot"
2488 ON "snapshot"."issue_id" = "issue"."id"
2489 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2490 AND "snapshot"."member_id" = "opinion"."member_id"
2491 WHERE "issue"."id" = "issue_id_p"
2492 AND "opinion"."suggestion_id" = "suggestion_id_v"
2493 AND "opinion"."degree" = 1
2494 AND "opinion"."fulfilled" = FALSE
2495 ),
2496 "plus1_fulfilled_count" = (
2497 SELECT coalesce(sum("snapshot"."weight"), 0)
2498 FROM "issue" CROSS JOIN "opinion"
2499 JOIN "direct_interest_snapshot" AS "snapshot"
2500 ON "snapshot"."issue_id" = "issue"."id"
2501 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2502 AND "snapshot"."member_id" = "opinion"."member_id"
2503 WHERE "issue"."id" = "issue_id_p"
2504 AND "opinion"."suggestion_id" = "suggestion_id_v"
2505 AND "opinion"."degree" = 1
2506 AND "opinion"."fulfilled" = TRUE
2507 ),
2508 "plus2_unfulfilled_count" = (
2509 SELECT coalesce(sum("snapshot"."weight"), 0)
2510 FROM "issue" CROSS JOIN "opinion"
2511 JOIN "direct_interest_snapshot" AS "snapshot"
2512 ON "snapshot"."issue_id" = "issue"."id"
2513 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2514 AND "snapshot"."member_id" = "opinion"."member_id"
2515 WHERE "issue"."id" = "issue_id_p"
2516 AND "opinion"."suggestion_id" = "suggestion_id_v"
2517 AND "opinion"."degree" = 2
2518 AND "opinion"."fulfilled" = FALSE
2519 ),
2520 "plus2_fulfilled_count" = (
2521 SELECT coalesce(sum("snapshot"."weight"), 0)
2522 FROM "issue" CROSS JOIN "opinion"
2523 JOIN "direct_interest_snapshot" AS "snapshot"
2524 ON "snapshot"."issue_id" = "issue"."id"
2525 AND "snapshot"."event" = "issue"."latest_snapshot_event"
2526 AND "snapshot"."member_id" = "opinion"."member_id"
2527 WHERE "issue"."id" = "issue_id_p"
2528 AND "opinion"."suggestion_id" = "suggestion_id_v"
2529 AND "opinion"."degree" = 2
2530 AND "opinion"."fulfilled" = TRUE
2532 WHERE "suggestion"."id" = "suggestion_id_v";
2533 END LOOP;
2534 END LOOP;
2535 RETURN;
2536 END;
2537 $$;
2539 COMMENT ON FUNCTION "create_snapshot"
2540 ( "issue"."id"%TYPE )
2541 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.';
2544 CREATE FUNCTION "set_snapshot_event"
2545 ( "issue_id_p" "issue"."id"%TYPE,
2546 "event_p" "snapshot_event" )
2547 RETURNS VOID
2548 LANGUAGE 'plpgsql' VOLATILE AS $$
2549 DECLARE
2550 "event_v" "issue"."latest_snapshot_event"%TYPE;
2551 BEGIN
2552 SELECT "latest_snapshot_event" INTO "event_v" FROM "issue"
2553 WHERE "id" = "issue_id_p" FOR UPDATE;
2554 UPDATE "issue" SET "latest_snapshot_event" = "event_p"
2555 WHERE "id" = "issue_id_p";
2556 UPDATE "direct_population_snapshot" SET "event" = "event_p"
2557 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
2558 UPDATE "delegating_population_snapshot" SET "event" = "event_p"
2559 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
2560 UPDATE "direct_interest_snapshot" SET "event" = "event_p"
2561 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
2562 UPDATE "delegating_interest_snapshot" SET "event" = "event_p"
2563 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
2564 UPDATE "direct_supporter_snapshot" SET "event" = "event_p"
2565 WHERE "issue_id" = "issue_id_p" AND "event" = "event_v";
2566 RETURN;
2567 END;
2568 $$;
2570 COMMENT ON FUNCTION "set_snapshot_event"
2571 ( "issue"."id"%TYPE,
2572 "snapshot_event" )
2573 IS 'Change "event" attribute of the previous ''periodic'' snapshot';
2577 ---------------------
2578 -- Freezing issues --
2579 ---------------------
2581 CREATE FUNCTION "freeze_after_snapshot"
2582 ( "issue_id_p" "issue"."id"%TYPE )
2583 RETURNS VOID
2584 LANGUAGE 'plpgsql' VOLATILE AS $$
2585 DECLARE
2586 "issue_row" "issue"%ROWTYPE;
2587 "policy_row" "policy"%ROWTYPE;
2588 "initiative_row" "initiative"%ROWTYPE;
2589 BEGIN
2590 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
2591 SELECT * INTO "policy_row"
2592 FROM "policy" WHERE "id" = "issue_row"."policy_id";
2593 PERFORM "set_snapshot_event"("issue_id_p", 'full_freeze');
2594 UPDATE "issue" SET
2595 "accepted" = coalesce("accepted", now()),
2596 "half_frozen" = coalesce("half_frozen", now()),
2597 "fully_frozen" = now()
2598 WHERE "id" = "issue_id_p";
2599 FOR "initiative_row" IN
2600 SELECT * FROM "initiative"
2601 WHERE "issue_id" = "issue_id_p" AND "revoked" ISNULL
2602 LOOP
2603 IF
2604 "initiative_row"."satisfied_supporter_count" > 0 AND
2605 "initiative_row"."satisfied_supporter_count" *
2606 "policy_row"."initiative_quorum_den" >=
2607 "issue_row"."population" * "policy_row"."initiative_quorum_num"
2608 THEN
2609 UPDATE "initiative" SET "admitted" = TRUE
2610 WHERE "id" = "initiative_row"."id";
2611 ELSE
2612 UPDATE "initiative" SET "admitted" = FALSE
2613 WHERE "id" = "initiative_row"."id";
2614 END IF;
2615 END LOOP;
2616 IF NOT EXISTS (
2617 SELECT NULL FROM "initiative"
2618 WHERE "issue_id" = "issue_id_p" AND "admitted" = TRUE
2619 ) THEN
2620 PERFORM "close_voting"("issue_id_p");
2621 END IF;
2622 RETURN;
2623 END;
2624 $$;
2626 COMMENT ON FUNCTION "freeze_after_snapshot"
2627 ( "issue"."id"%TYPE )
2628 IS 'This function freezes an issue (fully) and starts voting, but must only be called when "create_snapshot" was called in the same transaction.';
2631 CREATE FUNCTION "manual_freeze"("issue_id_p" "issue"."id"%TYPE)
2632 RETURNS VOID
2633 LANGUAGE 'plpgsql' VOLATILE AS $$
2634 DECLARE
2635 "issue_row" "issue"%ROWTYPE;
2636 BEGIN
2637 PERFORM "create_snapshot"("issue_id_p");
2638 PERFORM "freeze_after_snapshot"("issue_id_p");
2639 RETURN;
2640 END;
2641 $$;
2643 COMMENT ON FUNCTION "manual_freeze"
2644 ( "issue"."id"%TYPE )
2645 IS 'Freeze an issue manually (fully) and start voting';
2649 -----------------------
2650 -- Counting of votes --
2651 -----------------------
2654 CREATE FUNCTION "weight_of_added_vote_delegations"
2655 ( "issue_id_p" "issue"."id"%TYPE,
2656 "member_id_p" "member"."id"%TYPE,
2657 "delegate_member_ids_p" "delegating_voter"."delegate_member_ids"%TYPE )
2658 RETURNS "direct_voter"."weight"%TYPE
2659 LANGUAGE 'plpgsql' VOLATILE AS $$
2660 DECLARE
2661 "issue_delegation_row" "issue_delegation"%ROWTYPE;
2662 "delegate_member_ids_v" "delegating_voter"."delegate_member_ids"%TYPE;
2663 "weight_v" INT4;
2664 "sub_weight_v" INT4;
2665 BEGIN
2666 "weight_v" := 0;
2667 FOR "issue_delegation_row" IN
2668 SELECT * FROM "issue_delegation"
2669 WHERE "trustee_id" = "member_id_p"
2670 AND "issue_id" = "issue_id_p"
2671 LOOP
2672 IF NOT EXISTS (
2673 SELECT NULL FROM "direct_voter"
2674 WHERE "member_id" = "issue_delegation_row"."truster_id"
2675 AND "issue_id" = "issue_id_p"
2676 ) AND NOT EXISTS (
2677 SELECT NULL FROM "delegating_voter"
2678 WHERE "member_id" = "issue_delegation_row"."truster_id"
2679 AND "issue_id" = "issue_id_p"
2680 ) THEN
2681 "delegate_member_ids_v" :=
2682 "member_id_p" || "delegate_member_ids_p";
2683 INSERT INTO "delegating_voter" (
2684 "issue_id",
2685 "member_id",
2686 "scope",
2687 "delegate_member_ids"
2688 ) VALUES (
2689 "issue_id_p",
2690 "issue_delegation_row"."truster_id",
2691 "issue_delegation_row"."scope",
2692 "delegate_member_ids_v"
2693 );
2694 "sub_weight_v" := 1 +
2695 "weight_of_added_vote_delegations"(
2696 "issue_id_p",
2697 "issue_delegation_row"."truster_id",
2698 "delegate_member_ids_v"
2699 );
2700 UPDATE "delegating_voter"
2701 SET "weight" = "sub_weight_v"
2702 WHERE "issue_id" = "issue_id_p"
2703 AND "member_id" = "issue_delegation_row"."truster_id";
2704 "weight_v" := "weight_v" + "sub_weight_v";
2705 END IF;
2706 END LOOP;
2707 RETURN "weight_v";
2708 END;
2709 $$;
2711 COMMENT ON FUNCTION "weight_of_added_vote_delegations"
2712 ( "issue"."id"%TYPE,
2713 "member"."id"%TYPE,
2714 "delegating_voter"."delegate_member_ids"%TYPE )
2715 IS 'Helper function for "add_vote_delegations" function';
2718 CREATE FUNCTION "add_vote_delegations"
2719 ( "issue_id_p" "issue"."id"%TYPE )
2720 RETURNS VOID
2721 LANGUAGE 'plpgsql' VOLATILE AS $$
2722 DECLARE
2723 "member_id_v" "member"."id"%TYPE;
2724 BEGIN
2725 FOR "member_id_v" IN
2726 SELECT "member_id" FROM "direct_voter"
2727 WHERE "issue_id" = "issue_id_p"
2728 LOOP
2729 UPDATE "direct_voter" SET
2730 "weight" = "weight" + "weight_of_added_vote_delegations"(
2731 "issue_id_p",
2732 "member_id_v",
2733 '{}'
2735 WHERE "member_id" = "member_id_v"
2736 AND "issue_id" = "issue_id_p";
2737 END LOOP;
2738 RETURN;
2739 END;
2740 $$;
2742 COMMENT ON FUNCTION "add_vote_delegations"
2743 ( "issue_id_p" "issue"."id"%TYPE )
2744 IS 'Helper function for "close_voting" function';
2747 CREATE FUNCTION "close_voting"("issue_id_p" "issue"."id"%TYPE)
2748 RETURNS VOID
2749 LANGUAGE 'plpgsql' VOLATILE AS $$
2750 DECLARE
2751 "issue_row" "issue"%ROWTYPE;
2752 "member_id_v" "member"."id"%TYPE;
2753 BEGIN
2754 PERFORM "lock_issue"("issue_id_p");
2755 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
2756 DELETE FROM "delegating_voter"
2757 WHERE "issue_id" = "issue_id_p";
2758 DELETE FROM "direct_voter"
2759 WHERE "issue_id" = "issue_id_p"
2760 AND "autoreject" = TRUE;
2761 DELETE FROM "direct_voter" USING "member"
2762 WHERE "direct_voter"."member_id" = "member"."id"
2763 AND "direct_voter"."issue_id" = "issue_id_p"
2764 AND "member"."active" = FALSE;
2765 UPDATE "direct_voter" SET "weight" = 1
2766 WHERE "issue_id" = "issue_id_p";
2767 PERFORM "add_vote_delegations"("issue_id_p");
2768 FOR "member_id_v" IN
2769 SELECT "interest"."member_id"
2770 FROM "interest"
2771 JOIN "member"
2772 ON "interest"."member_id" = "member"."id"
2773 LEFT JOIN "direct_voter"
2774 ON "interest"."member_id" = "direct_voter"."member_id"
2775 AND "interest"."issue_id" = "direct_voter"."issue_id"
2776 LEFT JOIN "delegating_voter"
2777 ON "interest"."member_id" = "delegating_voter"."member_id"
2778 AND "interest"."issue_id" = "delegating_voter"."issue_id"
2779 WHERE "interest"."issue_id" = "issue_id_p"
2780 AND "interest"."autoreject" = TRUE
2781 AND "member"."active"
2782 AND "direct_voter"."member_id" ISNULL
2783 AND "delegating_voter"."member_id" ISNULL
2784 UNION SELECT "membership"."member_id"
2785 FROM "membership"
2786 JOIN "member"
2787 ON "membership"."member_id" = "member"."id"
2788 LEFT JOIN "interest"
2789 ON "membership"."member_id" = "interest"."member_id"
2790 AND "interest"."issue_id" = "issue_id_p"
2791 LEFT JOIN "direct_voter"
2792 ON "membership"."member_id" = "direct_voter"."member_id"
2793 AND "direct_voter"."issue_id" = "issue_id_p"
2794 LEFT JOIN "delegating_voter"
2795 ON "membership"."member_id" = "delegating_voter"."member_id"
2796 AND "delegating_voter"."issue_id" = "issue_id_p"
2797 WHERE "membership"."area_id" = "issue_row"."area_id"
2798 AND "membership"."autoreject" = TRUE
2799 AND "member"."active"
2800 AND "interest"."autoreject" ISNULL
2801 AND "direct_voter"."member_id" ISNULL
2802 AND "delegating_voter"."member_id" ISNULL
2803 LOOP
2804 INSERT INTO "direct_voter"
2805 ("member_id", "issue_id", "weight", "autoreject") VALUES
2806 ("member_id_v", "issue_id_p", 1, TRUE);
2807 INSERT INTO "vote" (
2808 "member_id",
2809 "issue_id",
2810 "initiative_id",
2811 "grade"
2812 ) SELECT
2813 "member_id_v" AS "member_id",
2814 "issue_id_p" AS "issue_id",
2815 "id" AS "initiative_id",
2816 -1 AS "grade"
2817 FROM "initiative" WHERE "issue_id" = "issue_id_p";
2818 END LOOP;
2819 PERFORM "add_vote_delegations"("issue_id_p");
2820 UPDATE "issue" SET
2821 "closed" = now(),
2822 "voter_count" = (
2823 SELECT coalesce(sum("weight"), 0)
2824 FROM "direct_voter" WHERE "issue_id" = "issue_id_p"
2826 WHERE "id" = "issue_id_p";
2827 UPDATE "initiative" SET
2828 "positive_votes" = "vote_counts"."positive_votes",
2829 "negative_votes" = "vote_counts"."negative_votes",
2830 "agreed" = CASE WHEN "majority_strict" THEN
2831 "vote_counts"."positive_votes" * "majority_den" >
2832 "majority_num" *
2833 ("vote_counts"."positive_votes"+"vote_counts"."negative_votes")
2834 ELSE
2835 "vote_counts"."positive_votes" * "majority_den" >=
2836 "majority_num" *
2837 ("vote_counts"."positive_votes"+"vote_counts"."negative_votes")
2838 END
2839 FROM
2840 ( SELECT
2841 "initiative"."id" AS "initiative_id",
2842 coalesce(
2843 sum(
2844 CASE WHEN "grade" > 0 THEN "direct_voter"."weight" ELSE 0 END
2845 ),
2847 ) AS "positive_votes",
2848 coalesce(
2849 sum(
2850 CASE WHEN "grade" < 0 THEN "direct_voter"."weight" ELSE 0 END
2851 ),
2853 ) AS "negative_votes"
2854 FROM "initiative"
2855 JOIN "issue" ON "initiative"."issue_id" = "issue"."id"
2856 JOIN "policy" ON "issue"."policy_id" = "policy"."id"
2857 LEFT JOIN "direct_voter"
2858 ON "direct_voter"."issue_id" = "initiative"."issue_id"
2859 LEFT JOIN "vote"
2860 ON "vote"."initiative_id" = "initiative"."id"
2861 AND "vote"."member_id" = "direct_voter"."member_id"
2862 WHERE "initiative"."issue_id" = "issue_id_p"
2863 AND "initiative"."admitted" -- NOTE: NULL case is handled too
2864 GROUP BY "initiative"."id"
2865 ) AS "vote_counts",
2866 "issue",
2867 "policy"
2868 WHERE "vote_counts"."initiative_id" = "initiative"."id"
2869 AND "issue"."id" = "initiative"."issue_id"
2870 AND "policy"."id" = "issue"."policy_id";
2871 -- NOTE: "closed" column of issue must be set at this point
2872 DELETE FROM "battle" WHERE "issue_id" = "issue_id_p";
2873 INSERT INTO "battle" (
2874 "issue_id",
2875 "winning_initiative_id", "losing_initiative_id",
2876 "count"
2877 ) SELECT
2878 "issue_id",
2879 "winning_initiative_id", "losing_initiative_id",
2880 "count"
2881 FROM "battle_view" WHERE "issue_id" = "issue_id_p";
2882 END;
2883 $$;
2885 COMMENT ON FUNCTION "close_voting"
2886 ( "issue"."id"%TYPE )
2887 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.';
2890 CREATE FUNCTION "defeat_strength"
2891 ( "positive_votes_p" INT4, "negative_votes_p" INT4 )
2892 RETURNS INT8
2893 LANGUAGE 'plpgsql' IMMUTABLE AS $$
2894 BEGIN
2895 IF "positive_votes_p" > "negative_votes_p" THEN
2896 RETURN ("positive_votes_p"::INT8 << 31) - "negative_votes_p"::INT8;
2897 ELSIF "positive_votes_p" = "negative_votes_p" THEN
2898 RETURN 0;
2899 ELSE
2900 RETURN -1;
2901 END IF;
2902 END;
2903 $$;
2905 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';
2908 CREATE FUNCTION "array_init_string"("dim_p" INTEGER)
2909 RETURNS TEXT
2910 LANGUAGE 'plpgsql' IMMUTABLE AS $$
2911 DECLARE
2912 "i" INTEGER;
2913 "ary_text_v" TEXT;
2914 BEGIN
2915 IF "dim_p" >= 1 THEN
2916 "ary_text_v" := '{NULL';
2917 "i" := "dim_p";
2918 LOOP
2919 "i" := "i" - 1;
2920 EXIT WHEN "i" = 0;
2921 "ary_text_v" := "ary_text_v" || ',NULL';
2922 END LOOP;
2923 "ary_text_v" := "ary_text_v" || '}';
2924 RETURN "ary_text_v";
2925 ELSE
2926 RAISE EXCEPTION 'Dimension needs to be at least 1.';
2927 END IF;
2928 END;
2929 $$;
2931 COMMENT ON FUNCTION "array_init_string"(INTEGER) IS 'Needed for PostgreSQL < 8.4, due to missing "array_fill" function';
2934 CREATE FUNCTION "square_matrix_init_string"("dim_p" INTEGER)
2935 RETURNS TEXT
2936 LANGUAGE 'plpgsql' IMMUTABLE AS $$
2937 DECLARE
2938 "i" INTEGER;
2939 "row_text_v" TEXT;
2940 "ary_text_v" TEXT;
2941 BEGIN
2942 IF "dim_p" >= 1 THEN
2943 "row_text_v" := '{NULL';
2944 "i" := "dim_p";
2945 LOOP
2946 "i" := "i" - 1;
2947 EXIT WHEN "i" = 0;
2948 "row_text_v" := "row_text_v" || ',NULL';
2949 END LOOP;
2950 "row_text_v" := "row_text_v" || '}';
2951 "ary_text_v" := '{' || "row_text_v";
2952 "i" := "dim_p";
2953 LOOP
2954 "i" := "i" - 1;
2955 EXIT WHEN "i" = 0;
2956 "ary_text_v" := "ary_text_v" || ',' || "row_text_v";
2957 END LOOP;
2958 "ary_text_v" := "ary_text_v" || '}';
2959 RETURN "ary_text_v";
2960 ELSE
2961 RAISE EXCEPTION 'Dimension needs to be at least 1.';
2962 END IF;
2963 END;
2964 $$;
2966 COMMENT ON FUNCTION "square_matrix_init_string"(INTEGER) IS 'Needed for PostgreSQL < 8.4, due to missing "array_fill" function';
2969 CREATE FUNCTION "calculate_ranks"("issue_id_p" "issue"."id"%TYPE)
2970 RETURNS VOID
2971 LANGUAGE 'plpgsql' VOLATILE AS $$
2972 DECLARE
2973 "dimension_v" INTEGER;
2974 "vote_matrix" INT4[][]; -- absolute votes
2975 "matrix" INT8[][]; -- defeat strength / best paths
2976 "i" INTEGER;
2977 "j" INTEGER;
2978 "k" INTEGER;
2979 "battle_row" "battle"%ROWTYPE;
2980 "rank_ary" INT4[];
2981 "rank_v" INT4;
2982 "done_v" INTEGER;
2983 "winners_ary" INTEGER[];
2984 "initiative_id_v" "initiative"."id"%TYPE;
2985 BEGIN
2986 PERFORM NULL FROM "issue" WHERE "id" = "issue_id_p" FOR UPDATE;
2987 SELECT count(1) INTO "dimension_v" FROM "initiative"
2988 WHERE "issue_id" = "issue_id_p" AND "agreed";
2989 IF "dimension_v" = 1 THEN
2990 UPDATE "initiative" SET "rank" = 1
2991 WHERE "issue_id" = "issue_id_p" AND "agreed";
2992 ELSIF "dimension_v" > 1 THEN
2993 -- Create "vote_matrix" with absolute number of votes in pairwise
2994 -- comparison:
2995 "vote_matrix" := "square_matrix_init_string"("dimension_v"); -- TODO: replace by "array_fill" function (PostgreSQL 8.4)
2996 "i" := 1;
2997 "j" := 2;
2998 FOR "battle_row" IN
2999 SELECT * FROM "battle" WHERE "issue_id" = "issue_id_p"
3000 ORDER BY "winning_initiative_id", "losing_initiative_id"
3001 LOOP
3002 "vote_matrix"["i"]["j"] := "battle_row"."count";
3003 IF "j" = "dimension_v" THEN
3004 "i" := "i" + 1;
3005 "j" := 1;
3006 ELSE
3007 "j" := "j" + 1;
3008 IF "j" = "i" THEN
3009 "j" := "j" + 1;
3010 END IF;
3011 END IF;
3012 END LOOP;
3013 IF "i" != "dimension_v" OR "j" != "dimension_v" + 1 THEN
3014 RAISE EXCEPTION 'Wrong battle count (should not happen)';
3015 END IF;
3016 -- Store defeat strengths in "matrix" using "defeat_strength"
3017 -- function:
3018 "matrix" := "square_matrix_init_string"("dimension_v"); -- TODO: replace by "array_fill" function (PostgreSQL 8.4)
3019 "i" := 1;
3020 LOOP
3021 "j" := 1;
3022 LOOP
3023 IF "i" != "j" THEN
3024 "matrix"["i"]["j"] := "defeat_strength"(
3025 "vote_matrix"["i"]["j"],
3026 "vote_matrix"["j"]["i"]
3027 );
3028 END IF;
3029 EXIT WHEN "j" = "dimension_v";
3030 "j" := "j" + 1;
3031 END LOOP;
3032 EXIT WHEN "i" = "dimension_v";
3033 "i" := "i" + 1;
3034 END LOOP;
3035 -- Find best paths:
3036 "i" := 1;
3037 LOOP
3038 "j" := 1;
3039 LOOP
3040 IF "i" != "j" THEN
3041 "k" := 1;
3042 LOOP
3043 IF "i" != "k" AND "j" != "k" THEN
3044 IF "matrix"["j"]["i"] < "matrix"["i"]["k"] THEN
3045 IF "matrix"["j"]["i"] > "matrix"["j"]["k"] THEN
3046 "matrix"["j"]["k"] := "matrix"["j"]["i"];
3047 END IF;
3048 ELSE
3049 IF "matrix"["i"]["k"] > "matrix"["j"]["k"] THEN
3050 "matrix"["j"]["k"] := "matrix"["i"]["k"];
3051 END IF;
3052 END IF;
3053 END IF;
3054 EXIT WHEN "k" = "dimension_v";
3055 "k" := "k" + 1;
3056 END LOOP;
3057 END IF;
3058 EXIT WHEN "j" = "dimension_v";
3059 "j" := "j" + 1;
3060 END LOOP;
3061 EXIT WHEN "i" = "dimension_v";
3062 "i" := "i" + 1;
3063 END LOOP;
3064 -- Determine order of winners:
3065 "rank_ary" := "array_init_string"("dimension_v"); -- TODO: replace by "array_fill" function (PostgreSQL 8.4)
3066 "rank_v" := 1;
3067 "done_v" := 0;
3068 LOOP
3069 "winners_ary" := '{}';
3070 "i" := 1;
3071 LOOP
3072 IF "rank_ary"["i"] ISNULL THEN
3073 "j" := 1;
3074 LOOP
3075 IF
3076 "i" != "j" AND
3077 "rank_ary"["j"] ISNULL AND
3078 "matrix"["j"]["i"] > "matrix"["i"]["j"]
3079 THEN
3080 -- someone else is better
3081 EXIT;
3082 END IF;
3083 IF "j" = "dimension_v" THEN
3084 -- noone is better
3085 "winners_ary" := "winners_ary" || "i";
3086 EXIT;
3087 END IF;
3088 "j" := "j" + 1;
3089 END LOOP;
3090 END IF;
3091 EXIT WHEN "i" = "dimension_v";
3092 "i" := "i" + 1;
3093 END LOOP;
3094 "i" := 1;
3095 LOOP
3096 "rank_ary"["winners_ary"["i"]] := "rank_v";
3097 "done_v" := "done_v" + 1;
3098 EXIT WHEN "i" = array_upper("winners_ary", 1);
3099 "i" := "i" + 1;
3100 END LOOP;
3101 EXIT WHEN "done_v" = "dimension_v";
3102 "rank_v" := "rank_v" + 1;
3103 END LOOP;
3104 -- write preliminary ranks:
3105 "i" := 1;
3106 FOR "initiative_id_v" IN
3107 SELECT "id" FROM "initiative"
3108 WHERE "issue_id" = "issue_id_p" AND "agreed"
3109 ORDER BY "id"
3110 LOOP
3111 UPDATE "initiative" SET "rank" = "rank_ary"["i"]
3112 WHERE "id" = "initiative_id_v";
3113 "i" := "i" + 1;
3114 END LOOP;
3115 IF "i" != "dimension_v" + 1 THEN
3116 RAISE EXCEPTION 'Wrong winner count (should not happen)';
3117 END IF;
3118 -- straighten ranks (start counting with 1, no equal ranks):
3119 "rank_v" := 1;
3120 FOR "initiative_id_v" IN
3121 SELECT "id" FROM "initiative"
3122 WHERE "issue_id" = "issue_id_p" AND "rank" NOTNULL
3123 ORDER BY
3124 "rank",
3125 "vote_ratio"("positive_votes", "negative_votes") DESC,
3126 "id"
3127 LOOP
3128 UPDATE "initiative" SET "rank" = "rank_v"
3129 WHERE "id" = "initiative_id_v";
3130 "rank_v" := "rank_v" + 1;
3131 END LOOP;
3132 END IF;
3133 -- mark issue as finished
3134 UPDATE "issue" SET "ranks_available" = TRUE
3135 WHERE "id" = "issue_id_p";
3136 RETURN;
3137 END;
3138 $$;
3140 COMMENT ON FUNCTION "calculate_ranks"
3141 ( "issue"."id"%TYPE )
3142 IS 'Determine ranking (Votes have to be counted first)';
3146 -----------------------------
3147 -- Automatic state changes --
3148 -----------------------------
3151 CREATE FUNCTION "check_issue"
3152 ( "issue_id_p" "issue"."id"%TYPE )
3153 RETURNS VOID
3154 LANGUAGE 'plpgsql' VOLATILE AS $$
3155 DECLARE
3156 "issue_row" "issue"%ROWTYPE;
3157 "policy_row" "policy"%ROWTYPE;
3158 "voting_requested_v" BOOLEAN;
3159 BEGIN
3160 PERFORM "lock_issue"("issue_id_p");
3161 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
3162 -- only process open issues:
3163 IF "issue_row"."closed" ISNULL THEN
3164 SELECT * INTO "policy_row" FROM "policy"
3165 WHERE "id" = "issue_row"."policy_id";
3166 -- create a snapshot, unless issue is already fully frozen:
3167 IF "issue_row"."fully_frozen" ISNULL THEN
3168 PERFORM "create_snapshot"("issue_id_p");
3169 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
3170 END IF;
3171 -- eventually close or accept issues, which have not been accepted:
3172 IF "issue_row"."accepted" ISNULL THEN
3173 IF EXISTS (
3174 SELECT NULL FROM "initiative"
3175 WHERE "issue_id" = "issue_id_p"
3176 AND "supporter_count" > 0
3177 AND "supporter_count" * "policy_row"."issue_quorum_den"
3178 >= "issue_row"."population" * "policy_row"."issue_quorum_num"
3179 ) THEN
3180 -- accept issues, if supporter count is high enough
3181 PERFORM "set_snapshot_event"("issue_id_p", 'end_of_admission');
3182 "issue_row"."accepted" = now(); -- NOTE: "issue_row" used later
3183 UPDATE "issue" SET "accepted" = "issue_row"."accepted"
3184 WHERE "id" = "issue_row"."id";
3185 ELSIF
3186 now() >= "issue_row"."created" + "issue_row"."admission_time"
3187 THEN
3188 -- close issues, if admission time has expired
3189 PERFORM "set_snapshot_event"("issue_id_p", 'end_of_admission');
3190 UPDATE "issue" SET "closed" = now()
3191 WHERE "id" = "issue_row"."id";
3192 END IF;
3193 END IF;
3194 -- eventually half freeze issues:
3195 IF
3196 -- NOTE: issue can't be closed at this point, if it has been accepted
3197 "issue_row"."accepted" NOTNULL AND
3198 "issue_row"."half_frozen" ISNULL
3199 THEN
3200 SELECT
3201 CASE
3202 WHEN "vote_now" * 2 > "issue_row"."population" THEN
3203 TRUE
3204 WHEN "vote_later" * 2 > "issue_row"."population" THEN
3205 FALSE
3206 ELSE NULL
3207 END
3208 INTO "voting_requested_v"
3209 FROM "issue" WHERE "id" = "issue_id_p";
3210 IF
3211 "voting_requested_v" OR (
3212 "voting_requested_v" ISNULL AND
3213 now() >= "issue_row"."accepted" + "issue_row"."discussion_time"
3215 THEN
3216 PERFORM "set_snapshot_event"("issue_id_p", 'half_freeze');
3217 "issue_row"."half_frozen" = now(); -- NOTE: "issue_row" used later
3218 UPDATE "issue" SET "half_frozen" = "issue_row"."half_frozen"
3219 WHERE "id" = "issue_row"."id";
3220 END IF;
3221 END IF;
3222 -- close issues after some time, if all initiatives have been revoked:
3223 IF
3224 "issue_row"."closed" ISNULL AND
3225 NOT EXISTS (
3226 -- all initiatives are revoked
3227 SELECT NULL FROM "initiative"
3228 WHERE "issue_id" = "issue_id_p" AND "revoked" ISNULL
3229 ) AND (
3230 NOT EXISTS (
3231 -- and no initiatives have been revoked lately
3232 SELECT NULL FROM "initiative"
3233 WHERE "issue_id" = "issue_id_p"
3234 AND now() < "revoked" + "issue_row"."verification_time"
3235 ) OR (
3236 -- or verification time has elapsed
3237 "issue_row"."half_frozen" NOTNULL AND
3238 "issue_row"."fully_frozen" ISNULL AND
3239 now() >= "issue_row"."half_frozen" + "issue_row"."verification_time"
3242 THEN
3243 "issue_row"."closed" = now(); -- NOTE: "issue_row" used later
3244 UPDATE "issue" SET "closed" = "issue_row"."closed"
3245 WHERE "id" = "issue_row"."id";
3246 END IF;
3247 -- fully freeze issue after verification time:
3248 IF
3249 "issue_row"."half_frozen" NOTNULL AND
3250 "issue_row"."fully_frozen" ISNULL AND
3251 "issue_row"."closed" ISNULL AND
3252 now() >= "issue_row"."half_frozen" + "issue_row"."verification_time"
3253 THEN
3254 PERFORM "freeze_after_snapshot"("issue_id_p");
3255 -- NOTE: "issue" might change, thus "issue_row" has to be updated below
3256 END IF;
3257 SELECT * INTO "issue_row" FROM "issue" WHERE "id" = "issue_id_p";
3258 -- close issue by calling close_voting(...) after voting time:
3259 IF
3260 "issue_row"."closed" ISNULL AND
3261 "issue_row"."fully_frozen" NOTNULL AND
3262 now() >= "issue_row"."fully_frozen" + "issue_row"."voting_time"
3263 THEN
3264 PERFORM "close_voting"("issue_id_p");
3265 END IF;
3266 END IF;
3267 RETURN;
3268 END;
3269 $$;
3271 COMMENT ON FUNCTION "check_issue"
3272 ( "issue"."id"%TYPE )
3273 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.';
3276 CREATE FUNCTION "check_everything"()
3277 RETURNS VOID
3278 LANGUAGE 'plpgsql' VOLATILE AS $$
3279 DECLARE
3280 "issue_id_v" "issue"."id"%TYPE;
3281 BEGIN
3282 DELETE FROM "expired_session";
3283 PERFORM "calculate_member_counts"();
3284 FOR "issue_id_v" IN SELECT "id" FROM "open_issue" LOOP
3285 PERFORM "check_issue"("issue_id_v");
3286 END LOOP;
3287 FOR "issue_id_v" IN SELECT "id" FROM "issue_with_ranks_missing" LOOP
3288 PERFORM "calculate_ranks"("issue_id_v");
3289 END LOOP;
3290 RETURN;
3291 END;
3292 $$;
3294 COMMENT ON FUNCTION "check_everything"() IS 'Perform "check_issue" for every open issue, and if possible, automatically calculate ranks. Use this function only for development and debugging purposes, as long transactions with exclusive locking may result.';
3298 ----------------------
3299 -- Deletion of data --
3300 ----------------------
3303 CREATE FUNCTION "clean_issue"("issue_id_p" "issue"."id"%TYPE)
3304 RETURNS VOID
3305 LANGUAGE 'plpgsql' VOLATILE AS $$
3306 DECLARE
3307 "issue_row" "issue"%ROWTYPE;
3308 BEGIN
3309 SELECT * INTO "issue_row"
3310 FROM "issue" WHERE "id" = "issue_id_p"
3311 FOR UPDATE;
3312 IF "issue_row"."cleaned" ISNULL THEN
3313 UPDATE "issue" SET
3314 "closed" = NULL,
3315 "ranks_available" = FALSE
3316 WHERE "id" = "issue_id_p";
3317 DELETE FROM "delegating_voter"
3318 WHERE "issue_id" = "issue_id_p";
3319 DELETE FROM "direct_voter"
3320 WHERE "issue_id" = "issue_id_p";
3321 DELETE FROM "delegating_interest_snapshot"
3322 WHERE "issue_id" = "issue_id_p";
3323 DELETE FROM "direct_interest_snapshot"
3324 WHERE "issue_id" = "issue_id_p";
3325 DELETE FROM "delegating_population_snapshot"
3326 WHERE "issue_id" = "issue_id_p";
3327 DELETE FROM "direct_population_snapshot"
3328 WHERE "issue_id" = "issue_id_p";
3329 DELETE FROM "delegation"
3330 WHERE "issue_id" = "issue_id_p";
3331 DELETE FROM "supporter"
3332 WHERE "issue_id" = "issue_id_p";
3333 UPDATE "issue" SET
3334 "closed" = "issue_row"."closed",
3335 "ranks_available" = "issue_row"."ranks_available",
3336 "cleaned" = now()
3337 WHERE "id" = "issue_id_p";
3338 END IF;
3339 RETURN;
3340 END;
3341 $$;
3343 COMMENT ON FUNCTION "clean_issue"("issue"."id"%TYPE) IS 'Delete discussion data and votes belonging to an issue';
3346 CREATE FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE)
3347 RETURNS VOID
3348 LANGUAGE 'plpgsql' VOLATILE AS $$
3349 BEGIN
3350 UPDATE "member" SET
3351 "last_login" = NULL,
3352 "login" = NULL,
3353 "password" = NULL,
3354 "active" = FALSE,
3355 "notify_email" = NULL,
3356 "notify_email_unconfirmed" = NULL,
3357 "notify_email_secret" = NULL,
3358 "notify_email_secret_expiry" = NULL,
3359 "notify_email_lock_expiry" = NULL,
3360 "password_reset_secret" = NULL,
3361 "password_reset_secret_expiry" = NULL,
3362 "organizational_unit" = NULL,
3363 "internal_posts" = NULL,
3364 "realname" = NULL,
3365 "birthday" = NULL,
3366 "address" = NULL,
3367 "email" = NULL,
3368 "xmpp_address" = NULL,
3369 "website" = NULL,
3370 "phone" = NULL,
3371 "mobile_phone" = NULL,
3372 "profession" = NULL,
3373 "external_memberships" = NULL,
3374 "external_posts" = NULL,
3375 "statement" = NULL
3376 WHERE "id" = "member_id_p";
3377 -- "text_search_data" is updated by triggers
3378 DELETE FROM "setting" WHERE "member_id" = "member_id_p";
3379 DELETE FROM "setting_map" WHERE "member_id" = "member_id_p";
3380 DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
3381 DELETE FROM "member_image" WHERE "member_id" = "member_id_p";
3382 DELETE FROM "contact" WHERE "member_id" = "member_id_p";
3383 DELETE FROM "area_setting" WHERE "member_id" = "member_id_p";
3384 DELETE FROM "issue_setting" WHERE "member_id" = "member_id_p";
3385 DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
3386 DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
3387 DELETE FROM "membership" WHERE "member_id" = "member_id_p";
3388 DELETE FROM "delegation" WHERE "truster_id" = "member_id_p";
3389 DELETE FROM "ignored_voting" WHERE "member_id" = "member_id_p";
3390 DELETE FROM "direct_voter" USING "issue"
3391 WHERE "direct_voter"."issue_id" = "issue"."id"
3392 AND "issue"."closed" ISNULL
3393 AND "member_id" = "member_id_p";
3394 RETURN;
3395 END;
3396 $$;
3398 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)';
3401 CREATE FUNCTION "delete_private_data"()
3402 RETURNS VOID
3403 LANGUAGE 'plpgsql' VOLATILE AS $$
3404 BEGIN
3405 UPDATE "member" SET
3406 "last_login" = NULL,
3407 "login" = NULL,
3408 "password" = NULL,
3409 "notify_email" = NULL,
3410 "notify_email_unconfirmed" = NULL,
3411 "notify_email_secret" = NULL,
3412 "notify_email_secret_expiry" = NULL,
3413 "notify_email_lock_expiry" = NULL,
3414 "password_reset_secret" = NULL,
3415 "password_reset_secret_expiry" = NULL,
3416 "organizational_unit" = NULL,
3417 "internal_posts" = NULL,
3418 "realname" = NULL,
3419 "birthday" = NULL,
3420 "address" = NULL,
3421 "email" = NULL,
3422 "xmpp_address" = NULL,
3423 "website" = NULL,
3424 "phone" = NULL,
3425 "mobile_phone" = NULL,
3426 "profession" = NULL,
3427 "external_memberships" = NULL,
3428 "external_posts" = NULL,
3429 "statement" = NULL;
3430 -- "text_search_data" is updated by triggers
3431 DELETE FROM "invite_code";
3432 DELETE FROM "setting";
3433 DELETE FROM "setting_map";
3434 DELETE FROM "member_relation_setting";
3435 DELETE FROM "member_image";
3436 DELETE FROM "contact";
3437 DELETE FROM "session";
3438 DELETE FROM "area_setting";
3439 DELETE FROM "issue_setting";
3440 DELETE FROM "initiative_setting";
3441 DELETE FROM "suggestion_setting";
3442 DELETE FROM "ignored_voting";
3443 DELETE FROM "direct_voter" USING "issue"
3444 WHERE "direct_voter"."issue_id" = "issue"."id"
3445 AND "issue"."closed" ISNULL;
3446 RETURN;
3447 END;
3448 $$;
3450 COMMENT ON FUNCTION "delete_private_data"() IS '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.';
3454 COMMIT;

Impressum / About Us