-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.sql
More file actions
executable file
·5209 lines (4631 loc) · 241 KB
/
core.sql
File metadata and controls
executable file
·5209 lines (4631 loc) · 241 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Execute the following command manually for PostgreSQL prior version 9.0:
-- CREATE LANGUAGE plpgsql;
-- NOTE: In PostgreSQL every UNIQUE constraint implies creation of an index
BEGIN;
CREATE VIEW "liquid_feedback_version" AS
SELECT * FROM (VALUES ('3.0.3', 3, 0, 3))
AS "subquery"("string", "major", "minor", "revision");
----------------------
-- Full text search --
----------------------
CREATE FUNCTION "text_search_query"("query_text_p" TEXT)
RETURNS TSQUERY
LANGUAGE 'plpgsql' IMMUTABLE AS $$
BEGIN
RETURN plainto_tsquery('pg_catalog.simple', "query_text_p");
END;
$$;
COMMENT ON FUNCTION "text_search_query"(TEXT) IS 'Usage: WHERE "text_search_data" @@ "text_search_query"(''<user query>'')';
CREATE FUNCTION "highlight"
( "body_p" TEXT,
"query_text_p" TEXT )
RETURNS TEXT
LANGUAGE 'plpgsql' IMMUTABLE AS $$
BEGIN
RETURN ts_headline(
'pg_catalog.simple',
replace(replace("body_p", e'\\', e'\\\\'), '*', e'\\*'),
"text_search_query"("query_text_p"),
'StartSel=* StopSel=* HighlightAll=TRUE' );
END;
$$;
COMMENT ON FUNCTION "highlight"
( "body_p" TEXT,
"query_text_p" TEXT )
IS 'For a given user query this function encapsulates all matches with asterisks. Asterisks and backslashes being already present are preceeded with one extra backslash.';
-------------------------
-- Tables and indicies --
-------------------------
CREATE TABLE "temporary_transaction_data" (
PRIMARY KEY ("txid", "key"),
"txid" INT8 DEFAULT txid_current(),
"key" TEXT,
"value" TEXT NOT NULL );
COMMENT ON TABLE "temporary_transaction_data" IS 'Table to store temporary transaction data; shall be emptied before a transaction is committed';
COMMENT ON COLUMN "temporary_transaction_data"."txid" IS 'Value returned by function txid_current(); should be added to WHERE clause, when doing SELECT on this table, but ignored when doing DELETE on this table';
CREATE TABLE "system_setting" (
"member_ttl" INTERVAL );
CREATE UNIQUE INDEX "system_setting_singleton_idx" ON "system_setting" ((1));
COMMENT ON TABLE "system_setting" IS 'This table contains only one row with different settings in each column.';
COMMENT ON INDEX "system_setting_singleton_idx" IS 'This index ensures that "system_setting" only contains one row maximum.';
COMMENT ON COLUMN "system_setting"."member_ttl" IS 'Time after members get their "active" flag set to FALSE, if they do not show any activity.';
CREATE TABLE "contingent" (
PRIMARY KEY ("polling", "time_frame"),
"polling" BOOLEAN,
"time_frame" INTERVAL,
"text_entry_limit" INT4,
"initiative_limit" INT4 );
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.';
COMMENT ON COLUMN "contingent"."polling" IS 'Determines if settings are for creating initiatives and new drafts of initiatives with "polling" flag set';
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';
COMMENT ON COLUMN "contingent"."initiative_limit" IS 'Number of new initiatives to be opened by each member within a given time frame';
CREATE TYPE "notify_level" AS ENUM
('expert', 'none', 'voting', 'verification', 'discussion', 'all');
COMMENT ON TYPE "notify_level" IS 'Level of notification: ''expert'' = detailed settings in table ''notify'', ''none'' = no notifications, ''voting'' = notifications about finished issues and issues in voting, ''verification'' = notifications about finished issues, issues in voting and verification phase, ''discussion'' = notifications about everything except issues in admission phase, ''all'' = notifications about everything';
CREATE TABLE "member" (
"id" SERIAL4 PRIMARY KEY,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"invite_code" TEXT UNIQUE,
"invite_code_expiry" TIMESTAMPTZ,
"admin_comment" TEXT,
"activated" TIMESTAMPTZ,
"last_activity" DATE,
"last_login" TIMESTAMPTZ,
"last_delegation_check" TIMESTAMPTZ,
"login" TEXT UNIQUE,
"password" TEXT,
"locked" BOOLEAN NOT NULL DEFAULT FALSE,
"active" BOOLEAN NOT NULL DEFAULT FALSE,
"admin" BOOLEAN NOT NULL DEFAULT FALSE,
"lang" TEXT,
"notify_email" TEXT,
"notify_email_unconfirmed" TEXT,
"notify_email_secret" TEXT UNIQUE,
"notify_email_secret_expiry" TIMESTAMPTZ,
"notify_email_lock_expiry" TIMESTAMPTZ,
"notify_level" "notify_level",
"login_recovery_expiry" TIMESTAMPTZ,
"password_reset_secret" TEXT UNIQUE,
"password_reset_secret_expiry" TIMESTAMPTZ,
"name" TEXT UNIQUE,
"identification" TEXT UNIQUE,
"authentication" TEXT,
"organizational_unit" TEXT,
"internal_posts" TEXT,
"realname" TEXT,
"birthday" DATE,
"address" TEXT,
"email" TEXT,
"xmpp_address" TEXT,
"website" TEXT,
"phone" TEXT,
"mobile_phone" TEXT,
"profession" TEXT,
"external_memberships" TEXT,
"external_posts" TEXT,
"formatting_engine" TEXT,
"statement" TEXT,
"text_search_data" TSVECTOR,
CONSTRAINT "active_requires_activated_and_last_activity"
CHECK ("active" = FALSE OR ("activated" NOTNULL AND "last_activity" NOTNULL)),
CONSTRAINT "name_not_null_if_activated"
CHECK ("activated" ISNULL OR "name" NOTNULL) );
CREATE INDEX "member_active_idx" ON "member" ("active");
CREATE INDEX "member_text_search_data_idx" ON "member" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "member"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "identification", "organizational_unit", "internal_posts",
"realname", "external_memberships", "external_posts", "statement" );
COMMENT ON TABLE "member" IS 'Users of the system, e.g. members of an organization';
COMMENT ON COLUMN "member"."created" IS 'Creation of member record and/or invite code';
COMMENT ON COLUMN "member"."invite_code" IS 'Optional invite code, to allow a member to initialize his/her account the first time';
COMMENT ON COLUMN "member"."invite_code_expiry" IS 'Expiry data/time for "invite_code"';
COMMENT ON COLUMN "member"."admin_comment" IS 'Hidden comment for administrative purposes';
COMMENT ON COLUMN "member"."activated" IS 'Timestamp of first activation of account (i.e. usage of "invite_code"); required to be set for "active" members';
COMMENT ON COLUMN "member"."last_activity" IS 'Date of last activity of member; required to be set for "active" members';
COMMENT ON COLUMN "member"."last_login" IS 'Timestamp of last login';
COMMENT ON COLUMN "member"."last_delegation_check" IS 'Timestamp of last delegation check (i.e. confirmation of all unit and area delegations)';
COMMENT ON COLUMN "member"."login" IS 'Login name';
COMMENT ON COLUMN "member"."password" IS 'Password (preferably as crypto-hash, depending on the frontend or access layer)';
COMMENT ON COLUMN "member"."locked" IS 'Locked members can not log in.';
COMMENT ON COLUMN "member"."active" IS 'Memberships, support and votes are taken into account when corresponding members are marked as active. Automatically set to FALSE, if "last_activity" is older than "system_setting"."member_ttl".';
COMMENT ON COLUMN "member"."admin" IS 'TRUE for admins, which can administrate other users and setup policies and areas';
COMMENT ON COLUMN "member"."lang" IS 'Language code of the preferred language of the member';
COMMENT ON COLUMN "member"."notify_email" IS 'Email address where notifications of the system are sent to';
COMMENT ON COLUMN "member"."notify_email_unconfirmed" IS 'Unconfirmed email address provided by the member to be copied into "notify_email" field after verification';
COMMENT ON COLUMN "member"."notify_email_secret" IS 'Secret sent to the address in "notify_email_unconformed"';
COMMENT ON COLUMN "member"."notify_email_secret_expiry" IS 'Expiry date/time for "notify_email_secret"';
COMMENT ON COLUMN "member"."notify_email_lock_expiry" IS 'Date/time until no further email confirmation mails may be sent (abuse protection)';
COMMENT ON COLUMN "member"."notify_level" IS 'Selects which event notifications are to be sent to the "notify_email" mail address, may be NULL if member did not make any selection yet';
COMMENT ON COLUMN "member"."login_recovery_expiry" IS 'Date/time after which another login recovery attempt is allowed';
COMMENT ON COLUMN "member"."password_reset_secret" IS 'Secret string sent via e-mail for password recovery';
COMMENT ON COLUMN "member"."password_reset_secret_expiry" IS 'Date/time until the password recovery secret is valid, and date/time after which another password recovery attempt is allowed';
COMMENT ON COLUMN "member"."name" IS 'Distinct name of the member, may be NULL if account has not been activated yet';
COMMENT ON COLUMN "member"."identification" IS 'Optional identification number or code of the member';
COMMENT ON COLUMN "member"."authentication" IS 'Information about how this member was authenticated';
COMMENT ON COLUMN "member"."organizational_unit" IS 'Branch or division of the organization the member belongs to';
COMMENT ON COLUMN "member"."internal_posts" IS 'Posts (offices) of the member inside the organization';
COMMENT ON COLUMN "member"."realname" IS 'Real name of the member, may be identical with "name"';
COMMENT ON COLUMN "member"."email" IS 'Published email address of the member; not used for system notifications';
COMMENT ON COLUMN "member"."external_memberships" IS 'Other organizations the member is involved in';
COMMENT ON COLUMN "member"."external_posts" IS 'Posts (offices) outside the organization';
COMMENT ON COLUMN "member"."formatting_engine" IS 'Allows different formatting engines (i.e. wiki formats) to be used for "member"."statement"';
COMMENT ON COLUMN "member"."statement" IS 'Freely chosen text of the member for his/her profile';
CREATE TYPE "notify_interest" AS ENUM
('all', 'my_units', 'my_areas', 'interested', 'potentially', 'supported', 'initiated', 'voted');
CREATE TABLE "notify" (
"member_id" INT4 NOT NULL REFERENCES "member" ("id")
ON DELETE CASCADE ON UPDATE CASCADE,
"interest" "notify_interest" NOT NULL,
"initiative_created_in_new_issue" BOOLEAN NOT NULL DEFAULT FALSE,
"admission__initiative_created_in_existing_issue" BOOLEAN NOT NULL DEFAULT FALSE,
"admission__new_draft_created" BOOLEAN NOT NULL DEFAULT FALSE,
"admission__suggestion_created" BOOLEAN NOT NULL DEFAULT FALSE,
"admission__initiative_revoked" BOOLEAN NOT NULL DEFAULT FALSE,
"canceled_revoked_before_accepted" BOOLEAN NOT NULL DEFAULT FALSE,
"canceled_issue_not_accepted" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion__initiative_created_in_existing_issue" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion__new_draft_created" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion__suggestion_created" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion__argument_created" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion__initiative_revoked" BOOLEAN NOT NULL DEFAULT FALSE,
"canceled_after_revocation_during_discussion" BOOLEAN NOT NULL DEFAULT FALSE,
"verification" BOOLEAN NOT NULL DEFAULT FALSE,
"verification__initiative_created_in_existing_issue" BOOLEAN NOT NULL DEFAULT FALSE,
"verification__argument_created" BOOLEAN NOT NULL DEFAULT FALSE,
"verification__initiative_revoked" BOOLEAN NOT NULL DEFAULT FALSE,
"canceled_after_revocation_during_verification" BOOLEAN NOT NULL DEFAULT FALSE,
"canceled_no_initiative_admitted" BOOLEAN NOT NULL DEFAULT FALSE,
"voting" BOOLEAN NOT NULL DEFAULT FALSE,
"finished_with_winner" BOOLEAN NOT NULL DEFAULT FALSE,
"finished_without_winner" BOOLEAN NOT NULL DEFAULT FALSE );
CREATE UNIQUE INDEX notify_member_interest ON notify USING btree (member_id, interest);
COMMENT ON TABLE "notify" IS 'Member settings in export mode which notifications are to be sent; No entry if the member does not use the expert mode';
-- DEPRECARED API TABLES --
CREATE TYPE "application_access_level" AS ENUM
('member', 'full', 'pseudonymous', 'anonymous');
COMMENT ON TYPE "application_access_level" IS 'DEPRECATED, WILL BE REMOVED! Access privileges for applications using the API';
CREATE TABLE "member_application" (
"id" SERIAL8 PRIMARY KEY,
UNIQUE ("member_id", "name"),
"member_id" INT4 NOT NULL REFERENCES "member" ("id")
ON DELETE CASCADE ON UPDATE CASCADE,
"name" TEXT NOT NULL,
"comment" TEXT,
"access_level" "application_access_level" NOT NULL,
"key" TEXT NOT NULL UNIQUE,
"last_usage" TIMESTAMPTZ );
COMMENT ON TABLE "member_application" IS 'DEPRECATED, WILL BE REMOVED! Registered application being allowed to use the API';
-- END OF DEPRECARED API TABLES --
CREATE TABLE "member_history" (
"id" SERIAL8 PRIMARY KEY,
"member_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"until" TIMESTAMPTZ NOT NULL DEFAULT now(),
"active" BOOLEAN NOT NULL,
"name" TEXT NOT NULL );
CREATE INDEX "member_history_member_id_idx" ON "member_history" ("member_id");
COMMENT ON TABLE "member_history" IS 'Filled by trigger; keeps information about old names and active flag of members';
COMMENT ON COLUMN "member_history"."id" IS 'Primary key, which can be used to sort entries correctly (and time warp resistant)';
COMMENT ON COLUMN "member_history"."until" IS 'Timestamp until the data was valid';
CREATE TABLE "rendered_member_statement" (
PRIMARY KEY ("member_id", "format"),
"member_id" INT8 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"format" TEXT,
"content" TEXT NOT NULL );
COMMENT ON TABLE "rendered_member_statement" IS 'This table may be used by frontends to cache "rendered" member statements (e.g. HTML output generated from wiki text)';
CREATE TABLE "setting" (
PRIMARY KEY ("member_id", "key"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL );
CREATE INDEX "setting_key_idx" ON "setting" ("key");
COMMENT ON TABLE "setting" IS 'Place to store a frontend specific setting for members as a string';
COMMENT ON COLUMN "setting"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
CREATE TABLE "setting_map" (
PRIMARY KEY ("member_id", "key", "subkey"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"subkey" TEXT NOT NULL,
"value" TEXT NOT NULL );
CREATE INDEX "setting_map_key_idx" ON "setting_map" ("key");
COMMENT ON TABLE "setting_map" IS 'Place to store a frontend specific setting for members as a map of key value pairs';
COMMENT ON COLUMN "setting_map"."key" IS 'Name of the setting, preceded by a frontend specific prefix';
COMMENT ON COLUMN "setting_map"."subkey" IS 'Key of a map entry';
COMMENT ON COLUMN "setting_map"."value" IS 'Value of a map entry';
CREATE TABLE "member_relation_setting" (
PRIMARY KEY ("member_id", "key", "other_member_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "member_relation_setting" IS 'Place to store a frontend specific setting related to relations between members as a string';
CREATE TYPE "member_image_type" AS ENUM ('photo', 'avatar');
COMMENT ON TYPE "member_image_type" IS 'Types of images for a member';
CREATE TABLE "member_image" (
PRIMARY KEY ("member_id", "image_type", "scaled"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"image_type" "member_image_type",
"scaled" BOOLEAN,
"content_type" TEXT,
"data" BYTEA NOT NULL );
COMMENT ON TABLE "member_image" IS 'Images of members';
COMMENT ON COLUMN "member_image"."scaled" IS 'FALSE for original image, TRUE for scaled version of the image';
CREATE TABLE "member_count" (
"calculated" TIMESTAMPTZ NOT NULL DEFAULT now(),
"total_count" INT4 NOT NULL );
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';
COMMENT ON COLUMN "member_count"."calculated" IS 'timestamp indicating when the total member count and area member counts were calculated';
COMMENT ON COLUMN "member_count"."total_count" IS 'Total count of active(!) members';
CREATE TABLE "contact" (
PRIMARY KEY ("member_id", "other_member_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"public" BOOLEAN NOT NULL DEFAULT FALSE,
CONSTRAINT "cant_save_yourself_as_contact"
CHECK ("member_id" != "other_member_id") );
CREATE INDEX "contact_other_member_id_idx" ON "contact" ("other_member_id");
COMMENT ON TABLE "contact" IS 'Contact lists';
COMMENT ON COLUMN "contact"."member_id" IS 'Member having the contact list';
COMMENT ON COLUMN "contact"."other_member_id" IS 'Member referenced in the contact list';
COMMENT ON COLUMN "contact"."public" IS 'TRUE = display contact publically';
CREATE TABLE "ignored_member" (
PRIMARY KEY ("member_id", "other_member_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"other_member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
CREATE INDEX "ignored_member_other_member_id_idx" ON "ignored_member" ("other_member_id");
COMMENT ON TABLE "ignored_member" IS 'Possibility to filter other members';
COMMENT ON COLUMN "ignored_member"."member_id" IS 'Member ignoring someone';
COMMENT ON COLUMN "ignored_member"."other_member_id" IS 'Member being ignored';
CREATE TABLE "session" (
"ident" TEXT PRIMARY KEY,
"additional_secret" TEXT,
"expiry" TIMESTAMPTZ NOT NULL DEFAULT now() + '24 hours',
"member_id" INT8 REFERENCES "member" ("id") ON DELETE SET NULL,
"needs_delegation_check" BOOLEAN NOT NULL DEFAULT FALSE,
"lang" TEXT );
CREATE INDEX "session_expiry_idx" ON "session" ("expiry");
COMMENT ON TABLE "session" IS 'Sessions, i.e. for a web-frontend or API layer';
COMMENT ON COLUMN "session"."ident" IS 'Secret session identifier (i.e. random string)';
COMMENT ON COLUMN "session"."additional_secret" IS 'Additional field to store a secret, which can be used against CSRF attacks';
COMMENT ON COLUMN "session"."member_id" IS 'Reference to member, who is logged in';
COMMENT ON COLUMN "session"."needs_delegation_check" IS 'Set to TRUE, if member must perform a delegation check to proceed with login; see column "last_delegation_check" in "member" table';
COMMENT ON COLUMN "session"."lang" IS 'Language code of the selected language';
CREATE TYPE "defeat_strength" AS ENUM ('simple', 'tuple');
COMMENT ON TYPE "defeat_strength" IS 'How pairwise defeats are measured for the Schulze method: ''simple'' = only the number of winning votes, ''tuple'' = primarily the number of winning votes, secondarily the number of losing votes';
CREATE TYPE "tie_breaking" AS ENUM ('simple', 'variant1', 'variant2');
COMMENT ON TYPE "tie_breaking" IS 'Tie-breaker for the Schulze method: ''simple'' = only initiative ids are used, ''variant1'' = use initiative ids in variant 1 for tie breaking of the links (TBRL) and sequentially forbid shared links, ''variant2'' = use initiative ids in variant 2 for tie breaking of the links (TBRL) and sequentially forbid shared links';
CREATE TABLE "policy" (
"id" SERIAL4 PRIMARY KEY,
"index" INT4 NOT NULL,
"active" BOOLEAN NOT NULL DEFAULT TRUE,
"name" TEXT NOT NULL UNIQUE,
"description" TEXT NOT NULL DEFAULT '',
"polling" BOOLEAN NOT NULL DEFAULT FALSE,
"admission_time" INTERVAL,
"discussion_time" INTERVAL,
"verification_time" INTERVAL,
"voting_time" INTERVAL,
"issue_quorum_num" INT4,
"issue_quorum_den" INT4,
"initiative_quorum_num" INT4 NOT NULL,
"initiative_quorum_den" INT4 NOT NULL,
"defeat_strength" "defeat_strength" NOT NULL DEFAULT 'tuple',
"tie_breaking" "tie_breaking" NOT NULL DEFAULT 'variant1',
"direct_majority_num" INT4 NOT NULL DEFAULT 1,
"direct_majority_den" INT4 NOT NULL DEFAULT 2,
"direct_majority_strict" BOOLEAN NOT NULL DEFAULT TRUE,
"direct_majority_positive" INT4 NOT NULL DEFAULT 0,
"direct_majority_non_negative" INT4 NOT NULL DEFAULT 0,
"indirect_majority_num" INT4 NOT NULL DEFAULT 1,
"indirect_majority_den" INT4 NOT NULL DEFAULT 2,
"indirect_majority_strict" BOOLEAN NOT NULL DEFAULT TRUE,
"indirect_majority_positive" INT4 NOT NULL DEFAULT 0,
"indirect_majority_non_negative" INT4 NOT NULL DEFAULT 0,
"no_reverse_beat_path" BOOLEAN NOT NULL DEFAULT FALSE,
"no_multistage_majority" BOOLEAN NOT NULL DEFAULT FALSE,
CONSTRAINT "timing" CHECK (
( "polling" = FALSE AND
"admission_time" NOTNULL AND "discussion_time" NOTNULL AND
"verification_time" NOTNULL AND "voting_time" NOTNULL ) OR
( "polling" = TRUE AND
"admission_time" ISNULL AND "discussion_time" NOTNULL AND
"verification_time" NOTNULL AND "voting_time" NOTNULL ) OR
( "polling" = TRUE AND
"admission_time" ISNULL AND "discussion_time" ISNULL AND
"verification_time" ISNULL AND "voting_time" ISNULL ) ),
CONSTRAINT "issue_quorum_if_and_only_if_not_polling" CHECK (
"polling" = "issue_quorum_num" ISNULL AND
"polling" = "issue_quorum_den" ISNULL ),
CONSTRAINT "no_reverse_beat_path_requires_tuple_defeat_strength" CHECK (
"defeat_strength" = 'tuple'::"defeat_strength" OR
"no_reverse_beat_path" = FALSE ) );
CREATE INDEX "policy_active_idx" ON "policy" ("active");
COMMENT ON TABLE "policy" IS 'Policies for a particular proceeding type (timelimits, quorum)';
COMMENT ON COLUMN "policy"."index" IS 'Determines the order in listings';
COMMENT ON COLUMN "policy"."active" IS 'TRUE = policy can be used for new issues';
COMMENT ON COLUMN "policy"."polling" IS 'TRUE = special policy for non-user-generated issues without issue quorum, where certain initiatives (those having the "polling" flag set) do not need to pass the initiative quorum; "admission_time" MUST be set to NULL, the other timings may be set to NULL altogether, allowing individual timing for those issues';
COMMENT ON COLUMN "policy"."admission_time" IS 'Maximum duration of issue state ''admission''; Maximum time an issue stays open without being "accepted"';
COMMENT ON COLUMN "policy"."discussion_time" IS 'Duration of issue state ''discussion''; Regular time until an issue is "half_frozen" after being "accepted"';
COMMENT ON COLUMN "policy"."verification_time" IS 'Duration of issue state ''verification''; Regular time until an issue is "fully_frozen" (e.g. entering issue state ''voting'') after being "half_frozen"';
COMMENT ON COLUMN "policy"."voting_time" IS 'Duration of issue state ''voting''; Time after an issue is "fully_frozen" but not "closed" (duration of issue state ''voting'')';
COMMENT ON COLUMN "policy"."issue_quorum_num" IS 'Numerator of potential supporter quorum to be reached by one initiative of an issue to be "accepted" and enter issue state ''discussion''';
COMMENT ON COLUMN "policy"."issue_quorum_den" IS 'Denominator of potential supporter quorum to be reached by one initiative of an issue to be "accepted" and enter issue state ''discussion''';
COMMENT ON COLUMN "policy"."initiative_quorum_num" IS 'Numerator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
COMMENT ON COLUMN "policy"."initiative_quorum_den" IS 'Denominator of satisfied supporter quorum to be reached by an initiative to be "admitted" for voting';
COMMENT ON COLUMN "policy"."defeat_strength" IS 'How pairwise defeats are measured for the Schulze method; see type "defeat_strength"; ''tuple'' is the recommended setting';
COMMENT ON COLUMN "policy"."tie_breaking" IS 'Tie-breaker for the Schulze method; see type "tie_breaking"; ''variant1'' or ''variant2'' are recommended';
COMMENT ON COLUMN "policy"."direct_majority_num" IS 'Numerator of fraction of neccessary direct majority for initiatives to be attainable as winner';
COMMENT ON COLUMN "policy"."direct_majority_den" IS 'Denominator of fraction of neccessary direct majority for initaitives to be attainable as winner';
COMMENT ON COLUMN "policy"."direct_majority_strict" IS 'If TRUE, then the direct majority must be strictly greater than "direct_majority_num"/"direct_majority_den", otherwise it may also be equal.';
COMMENT ON COLUMN "policy"."direct_majority_positive" IS 'Absolute number of "positive_votes" neccessary for an initiative to be attainable as winner';
COMMENT ON COLUMN "policy"."direct_majority_non_negative" IS 'Absolute number of sum of "positive_votes" and abstentions neccessary for an initiative to be attainable as winner';
COMMENT ON COLUMN "policy"."indirect_majority_num" IS 'Numerator of fraction of neccessary indirect majority (through beat path) for initiatives to be attainable as winner';
COMMENT ON COLUMN "policy"."indirect_majority_den" IS 'Denominator of fraction of neccessary indirect majority (through beat path) for initiatives to be attainable as winner';
COMMENT ON COLUMN "policy"."indirect_majority_strict" IS 'If TRUE, then the indirect majority must be strictly greater than "indirect_majority_num"/"indirect_majority_den", otherwise it may also be equal.';
COMMENT ON COLUMN "policy"."indirect_majority_positive" IS 'Absolute number of votes in favor of the winner neccessary in a beat path to the status quo for an initaitive to be attainable as winner';
COMMENT ON COLUMN "policy"."indirect_majority_non_negative" IS 'Absolute number of sum of votes in favor and abstentions in a beat path to the status quo for an initiative to be attainable as winner';
COMMENT ON COLUMN "policy"."no_reverse_beat_path" IS 'EXPERIMENTAL FEATURE: Causes initiatives with "reverse_beat_path" flag to not be "eligible", thus disallowing them to be winner. See comment on column "initiative"."reverse_beat_path". This option ensures both that a winning initiative is never tied in a (weak) condorcet paradox with the status quo and a winning initiative always beats the status quo directly with a simple majority.';
COMMENT ON COLUMN "policy"."no_multistage_majority" IS 'EXPERIMENTAL FEATURE: Causes initiatives with "multistage_majority" flag to not be "eligible", thus disallowing them to be winner. See comment on column "initiative"."multistage_majority". This disqualifies initiatives which could cause an instable result. An instable result in this meaning is a result such that repeating the ballot with same preferences but with the winner of the first ballot as status quo would lead to a different winner in the second ballot. If there are no direct majorities required for the winner, or if in direct comparison only simple majorities are required and "no_reverse_beat_path" is true, then results are always stable and this flag does not have any effect on the winner (but still affects the "eligible" flag of an "initiative").';
CREATE TABLE "unit" (
"id" SERIAL4 PRIMARY KEY,
"parent_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"active" BOOLEAN NOT NULL DEFAULT TRUE,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL DEFAULT '',
"member_count" INT4,
"text_search_data" TSVECTOR );
CREATE INDEX "unit_root_idx" ON "unit" ("id") WHERE "parent_id" ISNULL;
CREATE INDEX "unit_parent_id_idx" ON "unit" ("parent_id");
CREATE INDEX "unit_active_idx" ON "unit" ("active");
CREATE INDEX "unit_text_search_data_idx" ON "unit" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "unit"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "description" );
COMMENT ON TABLE "unit" IS 'Organizational units organized as trees; Delegations are not inherited through these trees.';
COMMENT ON COLUMN "unit"."parent_id" IS 'Parent id of tree node; Multiple roots allowed';
COMMENT ON COLUMN "unit"."active" IS 'TRUE means new issues can be created in areas of this unit';
COMMENT ON COLUMN "unit"."member_count" IS 'Count of members as determined by column "voting_right" in table "privilege"';
CREATE TABLE "unit_setting" (
PRIMARY KEY ("member_id", "key", "unit_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"unit_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "unit_setting" IS 'Place for frontend to store unit specific settings of members as strings';
CREATE TABLE "area" (
"id" SERIAL4 PRIMARY KEY,
"unit_id" INT4 NOT NULL REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"active" BOOLEAN NOT NULL DEFAULT TRUE,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL DEFAULT '',
"direct_member_count" INT4,
"member_weight" INT4,
"text_search_data" TSVECTOR );
CREATE INDEX "area_unit_id_idx" ON "area" ("unit_id");
CREATE INDEX "area_active_idx" ON "area" ("active");
CREATE INDEX "area_text_search_data_idx" ON "area" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "area"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "description" );
COMMENT ON TABLE "area" IS 'Subject areas';
COMMENT ON COLUMN "area"."active" IS 'TRUE means new issues can be created in this area';
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"';
COMMENT ON COLUMN "area"."member_weight" IS 'Same as "direct_member_count" but respecting delegations';
CREATE TABLE "area_setting" (
PRIMARY KEY ("member_id", "key", "area_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "area_setting" IS 'Place for frontend to store area specific settings of members as strings';
CREATE TABLE "allowed_policy" (
PRIMARY KEY ("area_id", "policy_id"),
"area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"default_policy" BOOLEAN NOT NULL DEFAULT FALSE );
CREATE UNIQUE INDEX "allowed_policy_one_default_per_area_idx" ON "allowed_policy" ("area_id") WHERE "default_policy";
COMMENT ON TABLE "allowed_policy" IS 'Selects which policies can be used in each area';
COMMENT ON COLUMN "allowed_policy"."default_policy" IS 'One policy per area can be set as default.';
CREATE TYPE "snapshot_event" AS ENUM ('periodic', 'end_of_admission', 'half_freeze', 'full_freeze');
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';
CREATE TYPE "issue_state" AS ENUM (
'admission', 'discussion', 'verification', 'voting',
'canceled_by_admin',
'canceled_revoked_before_accepted',
'canceled_issue_not_accepted',
'canceled_after_revocation_during_discussion',
'canceled_after_revocation_during_verification',
'canceled_no_initiative_admitted',
'finished_without_winner', 'finished_with_winner');
COMMENT ON TYPE "issue_state" IS 'State of issues';
CREATE TABLE "issue" (
"id" SERIAL4 PRIMARY KEY,
"area_id" INT4 NOT NULL REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"policy_id" INT4 NOT NULL REFERENCES "policy" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
"admin_notice" TEXT,
"state" "issue_state" NOT NULL DEFAULT 'admission',
"phase_finished" TIMESTAMPTZ,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"accepted" TIMESTAMPTZ,
"half_frozen" TIMESTAMPTZ,
"fully_frozen" TIMESTAMPTZ,
"closed" TIMESTAMPTZ,
"cleaned" TIMESTAMPTZ,
"admission_time" INTERVAL,
"discussion_time" INTERVAL NOT NULL,
"verification_time" INTERVAL NOT NULL,
"voting_time" INTERVAL NOT NULL,
"snapshot" TIMESTAMPTZ,
"latest_snapshot_event" "snapshot_event",
"population" INT4,
"voter_count" INT4,
"direct_voter_count" INT4,
"status_quo_schulze_rank" INT4,
CONSTRAINT "admission_time_not_null_unless_instantly_accepted" CHECK (
"admission_time" NOTNULL OR ("accepted" NOTNULL AND "accepted" = "created") ),
CONSTRAINT "valid_state" CHECK (
(
("accepted" ISNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL ) OR
("accepted" NOTNULL AND "half_frozen" ISNULL AND "fully_frozen" ISNULL ) OR
("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL ) OR
("accepted" NOTNULL AND "half_frozen" NOTNULL AND "fully_frozen" NOTNULL)
) AND (
("state" = 'admission' AND "closed" ISNULL AND "accepted" ISNULL) OR
("state" = 'discussion' AND "closed" ISNULL AND "accepted" NOTNULL AND "half_frozen" ISNULL) OR
("state" = 'verification' AND "closed" ISNULL AND "half_frozen" NOTNULL AND "fully_frozen" ISNULL) OR
("state" = 'voting' AND "closed" ISNULL AND "fully_frozen" NOTNULL) OR
("state" = 'canceled_by_admin' AND "closed" NOTNULL) OR
("state" = 'canceled_revoked_before_accepted' AND "closed" NOTNULL AND "accepted" ISNULL) OR
("state" = 'canceled_issue_not_accepted' AND "closed" NOTNULL AND "accepted" ISNULL) OR
("state" = 'canceled_after_revocation_during_discussion' AND "closed" NOTNULL AND "half_frozen" ISNULL) OR
("state" = 'canceled_after_revocation_during_verification' AND "closed" NOTNULL AND "fully_frozen" ISNULL) OR
("state" = 'canceled_no_initiative_admitted' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "closed" = "fully_frozen") OR
("state" = 'finished_without_winner' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "closed" != "fully_frozen") OR
("state" = 'finished_with_winner' AND "closed" NOTNULL AND "fully_frozen" NOTNULL AND "closed" != "fully_frozen")
)),
CONSTRAINT "phase_finished_only_when_not_closed" CHECK (
"phase_finished" ISNULL OR "closed" ISNULL ),
CONSTRAINT "state_change_order" CHECK (
"created" <= "accepted" AND
"accepted" <= "half_frozen" AND
"half_frozen" <= "fully_frozen" AND
"fully_frozen" <= "closed" ),
CONSTRAINT "only_closed_issues_may_be_cleaned" CHECK (
"cleaned" ISNULL OR "closed" NOTNULL ),
CONSTRAINT "last_snapshot_on_full_freeze"
CHECK ("snapshot" = "fully_frozen"), -- NOTE: snapshot can be set, while frozen is NULL yet
CONSTRAINT "freeze_requires_snapshot"
CHECK ("fully_frozen" ISNULL OR "snapshot" NOTNULL),
CONSTRAINT "set_both_or_none_of_snapshot_and_latest_snapshot_event"
CHECK ("snapshot" NOTNULL = "latest_snapshot_event" NOTNULL) );
CREATE INDEX "issue_area_id_idx" ON "issue" ("area_id");
CREATE INDEX "issue_policy_id_idx" ON "issue" ("policy_id");
CREATE INDEX "issue_created_idx" ON "issue" ("created");
CREATE INDEX "issue_accepted_idx" ON "issue" ("accepted");
CREATE INDEX "issue_half_frozen_idx" ON "issue" ("half_frozen");
CREATE INDEX "issue_fully_frozen_idx" ON "issue" ("fully_frozen");
CREATE INDEX "issue_closed_idx" ON "issue" ("closed");
CREATE INDEX "issue_created_idx_open" ON "issue" ("created") WHERE "closed" ISNULL;
CREATE INDEX "issue_closed_idx_canceled" ON "issue" ("closed") WHERE "fully_frozen" ISNULL;
COMMENT ON TABLE "issue" IS 'Groups of initiatives';
COMMENT ON COLUMN "issue"."admin_notice" IS 'Public notice by admin to explain manual interventions, or to announce corrections';
COMMENT ON COLUMN "issue"."phase_finished" IS 'Set to a value NOTNULL, if the current phase has finished, but calculations are pending; No changes in this issue shall be made by the frontend or API when this value is set';
COMMENT ON COLUMN "issue"."accepted" IS 'Point in time, when one initiative of issue reached the "issue_quorum"';
COMMENT ON COLUMN "issue"."half_frozen" IS 'Point in time, when "discussion_time" has elapsed; Frontends must ensure that for half_frozen issues a) initiatives are not revoked, b) no new drafts are created, c) no initiators are added or removed.';
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.';
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.';
COMMENT ON COLUMN "issue"."cleaned" IS 'Point in time, when discussion data and votes had been deleted';
COMMENT ON COLUMN "issue"."admission_time" IS 'Copied from "policy" table at creation of issue';
COMMENT ON COLUMN "issue"."discussion_time" IS 'Copied from "policy" table at creation of issue';
COMMENT ON COLUMN "issue"."verification_time" IS 'Copied from "policy" table at creation of issue';
COMMENT ON COLUMN "issue"."voting_time" IS 'Copied from "policy" table at creation of issue';
COMMENT ON COLUMN "issue"."snapshot" IS 'Point in time, when snapshot tables have been updated and "population" and *_count values were precalculated';
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';
COMMENT ON COLUMN "issue"."population" IS 'Sum of "weight" column in table "direct_population_snapshot"';
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';
COMMENT ON COLUMN "issue"."direct_voter_count" IS 'Total number of direct voters';
COMMENT ON COLUMN "issue"."status_quo_schulze_rank" IS 'Schulze rank of status quo, as calculated by "calculate_ranks" function';
CREATE TABLE "issue_order_in_admission_state" (
"id" INT8 PRIMARY KEY, --REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"order_in_area" INT4,
"order_in_unit" INT4 );
COMMENT ON TABLE "issue_order_in_admission_state" IS 'Ordering information for issues that are not stored in the "issue" table to avoid locking of multiple issues at once; Filled/updated by "lf_update_issue_order"';
COMMENT ON COLUMN "issue_order_in_admission_state"."id" IS 'References "issue" ("id") but has no referential integrity trigger associated, due to performance/locking issues';
COMMENT ON COLUMN "issue_order_in_admission_state"."order_in_area" IS 'Order of issues in admission state within a single area; NULL values sort last';
COMMENT ON COLUMN "issue_order_in_admission_state"."order_in_unit" IS 'Order of issues in admission state within all areas of a unit; NULL values sort last';
CREATE TABLE "issue_setting" (
PRIMARY KEY ("member_id", "key", "issue_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "issue_setting" IS 'Place for frontend to store issue specific settings of members as strings';
CREATE TABLE "initiative" (
UNIQUE ("issue_id", "id"), -- index needed for foreign-key on table "vote"
"issue_id" INT4 NOT NULL REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"id" SERIAL4 PRIMARY KEY,
"name" TEXT NOT NULL,
"polling" BOOLEAN NOT NULL DEFAULT FALSE,
"discussion_url" TEXT,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"revoked" TIMESTAMPTZ,
"revoked_by_member_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
"suggested_initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"admitted" BOOLEAN,
"supporter_count" INT4,
"direct_supporter_count" INT4,
"informed_supporter_count" INT4,
"satisfied_supporter_count" INT4,
"satisfied_direct_supporter_count" INT4,
"satisfied_informed_supporter_count" INT4,
"harmonic_weight" NUMERIC(12, 3),
"final_suggestion_order_calculated" BOOLEAN NOT NULL DEFAULT FALSE,
"first_preference_votes" INT4,
"positive_votes" INT4,
"negative_votes" INT4,
"positive_direct_votes" INT4,
"negative_direct_votes" INT4,
"direct_majority" BOOLEAN,
"indirect_majority" BOOLEAN,
"schulze_rank" INT4,
"better_than_status_quo" BOOLEAN,
"worse_than_status_quo" BOOLEAN,
"reverse_beat_path" BOOLEAN,
"multistage_majority" BOOLEAN,
"eligible" BOOLEAN,
"winner" BOOLEAN,
"rank" INT4,
"text_search_data" TSVECTOR,
CONSTRAINT "all_or_none_of_revoked_and_revoked_by_member_id_must_be_null"
CHECK ("revoked" NOTNULL = "revoked_by_member_id" NOTNULL),
CONSTRAINT "non_revoked_initiatives_cant_suggest_other"
CHECK ("revoked" NOTNULL OR "suggested_initiative_id" ISNULL),
CONSTRAINT "revoked_initiatives_cant_be_admitted"
CHECK ("revoked" ISNULL OR "admitted" ISNULL),
CONSTRAINT "non_admitted_initiatives_cant_contain_voting_results" CHECK (
( "admitted" NOTNULL AND "admitted" = TRUE ) OR
( "first_preference_votes" ISNULL AND
"positive_votes" ISNULL AND "negative_votes" ISNULL AND
"positive_direct_votes" ISNULL AND "negative_direct_votes" ISNULL AND
"direct_majority" ISNULL AND "indirect_majority" ISNULL AND
"schulze_rank" ISNULL AND
"better_than_status_quo" ISNULL AND "worse_than_status_quo" ISNULL AND
"reverse_beat_path" ISNULL AND "multistage_majority" ISNULL AND
"eligible" ISNULL AND "winner" ISNULL AND "rank" ISNULL ) ),
CONSTRAINT "better_excludes_worse" CHECK (NOT ("better_than_status_quo" AND "worse_than_status_quo")),
CONSTRAINT "minimum_requirement_to_be_eligible" CHECK (
"eligible" = FALSE OR
("direct_majority" AND "indirect_majority" AND "better_than_status_quo") ),
CONSTRAINT "winner_must_be_eligible" CHECK ("winner"=FALSE OR "eligible"=TRUE),
CONSTRAINT "winner_must_have_first_rank" CHECK ("winner"=FALSE OR "rank"=1),
CONSTRAINT "eligible_at_first_rank_is_winner" CHECK ("eligible"=FALSE OR "rank"!=1 OR "winner"=TRUE),
CONSTRAINT "unique_rank_per_issue" UNIQUE ("issue_id", "rank") );
CREATE INDEX "initiative_created_idx" ON "initiative" ("created");
CREATE INDEX "initiative_revoked_idx" ON "initiative" ("revoked");
CREATE INDEX "initiative_text_search_data_idx" ON "initiative" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "initiative"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "discussion_url");
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.';
COMMENT ON COLUMN "initiative"."polling" IS 'Initiative does not need to pass the initiative quorum (see "policy"."polling")';
COMMENT ON COLUMN "initiative"."discussion_url" IS 'URL pointing to a discussion platform for this initiative';
COMMENT ON COLUMN "initiative"."revoked" IS 'Point in time, when one initiator decided to revoke the initiative';
COMMENT ON COLUMN "initiative"."revoked_by_member_id" IS 'Member, who decided to revoke the initiative';
COMMENT ON COLUMN "initiative"."admitted" IS 'TRUE, if initiative reaches the "initiative_quorum" when freezing the issue';
COMMENT ON COLUMN "initiative"."supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."direct_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."satisfied_direct_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."satisfied_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."satisfied_informed_supporter_count" IS 'Calculated from table "direct_supporter_snapshot"';
COMMENT ON COLUMN "initiative"."harmonic_weight" IS 'Indicates the relevancy of the initiative, calculated from the potential supporters weighted with the harmonic series to avoid a large number of clones affecting other initiative''s sorting positions too much; shall be used as secondary sorting key after "admitted" as primary sorting key';
COMMENT ON COLUMN "initiative"."final_suggestion_order_calculated" IS 'Set to TRUE, when "proportional_order" of suggestions has been calculated the last time';
COMMENT ON COLUMN "initiative"."first_preference_votes" IS 'Number of direct and delegating voters who ranked this initiative as their first choice';
COMMENT ON COLUMN "initiative"."positive_votes" IS 'Number of direct and delegating voters who ranked this initiative better than the status quo';
COMMENT ON COLUMN "initiative"."negative_votes" IS 'Number of direct and delegating voters who ranked this initiative worse than the status quo';
COMMENT ON COLUMN "initiative"."positive_direct_votes" IS 'Calculated from table "direct_voter"';
COMMENT ON COLUMN "initiative"."negative_direct_votes" IS 'Calculated from table "direct_voter"';
COMMENT ON COLUMN "initiative"."direct_majority" IS 'TRUE, if "positive_votes"/("positive_votes"+"negative_votes") is strictly greater or greater-equal than "direct_majority_num"/"direct_majority_den", and "positive_votes" is greater-equal than "direct_majority_positive", and ("positive_votes"+abstentions) is greater-equal than "direct_majority_non_negative"';
COMMENT ON COLUMN "initiative"."indirect_majority" IS 'Same as "direct_majority", but also considering indirect beat paths';
COMMENT ON COLUMN "initiative"."schulze_rank" IS 'Schulze-Ranking';
COMMENT ON COLUMN "initiative"."better_than_status_quo" IS 'TRUE, if initiative has a schulze-ranking better than the status quo';
COMMENT ON COLUMN "initiative"."worse_than_status_quo" IS 'TRUE, if initiative has a schulze-ranking worse than the status quo (DEPRECATED, since schulze-ranking is unique per issue; use "better_than_status_quo"=FALSE)';
COMMENT ON COLUMN "initiative"."reverse_beat_path" IS 'TRUE, if there is a beat path (may include ties) from this initiative to the status quo; set to NULL if "policy"."defeat_strength" is set to ''simple''';
COMMENT ON COLUMN "initiative"."multistage_majority" IS 'TRUE, if either (a) this initiative has no better rank than the status quo, or (b) there exists a better ranked initiative X, which directly beats this initiative, and either more voters prefer X to this initiative than voters preferring X to the status quo or less voters prefer this initiative to X than voters preferring the status quo to X';
COMMENT ON COLUMN "initiative"."eligible" IS 'Initiative has a "direct_majority" and an "indirect_majority", is "better_than_status_quo" and depending on selected policy the initiative has no "reverse_beat_path" or "multistage_majority"';
COMMENT ON COLUMN "initiative"."winner" IS 'Winner is the "eligible" initiative with best "schulze_rank"';
COMMENT ON COLUMN "initiative"."rank" IS 'Unique ranking for all "admitted" initiatives per issue; lower rank is better; a winner always has rank 1, but rank 1 does not imply that an initiative is winner; initiatives with "direct_majority" AND "indirect_majority" always have a better (lower) rank than other initiatives';
CREATE TABLE "battle" (
"issue_id" INT4 NOT NULL,
"winning_initiative_id" INT4,
FOREIGN KEY ("issue_id", "winning_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
"losing_initiative_id" INT4,
FOREIGN KEY ("issue_id", "losing_initiative_id") REFERENCES "initiative" ("issue_id", "id") ON DELETE CASCADE ON UPDATE CASCADE,
"count" INT4 NOT NULL,
"direct_count" INT4, -- allow NULL for compatibility with existing records
CONSTRAINT "initiative_ids_not_equal" CHECK (
"winning_initiative_id" != "losing_initiative_id" OR
( ("winning_initiative_id" NOTNULL AND "losing_initiative_id" ISNULL) OR
("winning_initiative_id" ISNULL AND "losing_initiative_id" NOTNULL) ) ) );
CREATE UNIQUE INDEX "battle_winning_losing_idx" ON "battle" ("issue_id", "winning_initiative_id", "losing_initiative_id");
CREATE UNIQUE INDEX "battle_winning_null_idx" ON "battle" ("issue_id", "winning_initiative_id") WHERE "losing_initiative_id" ISNULL;
CREATE UNIQUE INDEX "battle_null_losing_idx" ON "battle" ("issue_id", "losing_initiative_id") WHERE "winning_initiative_id" ISNULL;
COMMENT ON TABLE "battle" IS 'Number of members preferring one initiative to another; Filled by "battle_view" when closing an issue; NULL as initiative_id denotes virtual "status-quo" initiative';
CREATE TABLE "ignored_initiative" (
PRIMARY KEY ("initiative_id", "member_id"),
"initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
CREATE INDEX "ignored_initiative_member_id_idx" ON "ignored_initiative" ("member_id");
COMMENT ON TABLE "ignored_initiative" IS 'Possibility to filter initiatives';
CREATE TABLE "initiative_setting" (
PRIMARY KEY ("member_id", "key", "initiative_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "initiative_setting" IS 'Place for frontend to store initiative specific settings of members as strings';
CREATE TABLE "draft" (
UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "supporter"
"initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"id" SERIAL8 PRIMARY KEY,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
"formatting_engine" TEXT,
"content" TEXT NOT NULL,
"text_search_data" TSVECTOR );
CREATE INDEX "draft_created_idx" ON "draft" ("created");
CREATE INDEX "draft_author_id_created_idx" ON "draft" ("author_id", "created");
CREATE INDEX "draft_text_search_data_idx" ON "draft" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "draft"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple', "content");
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.';
COMMENT ON COLUMN "draft"."formatting_engine" IS 'Allows different formatting engines (i.e. wiki formats) to be used';
COMMENT ON COLUMN "draft"."content" IS 'Text of the draft in a format depending on the field "formatting_engine"';
CREATE TABLE "rendered_draft" (
PRIMARY KEY ("draft_id", "format"),
"draft_id" INT8 REFERENCES "draft" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"format" TEXT,
"content" TEXT NOT NULL );
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)';
CREATE TABLE "suggestion" (
UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "opinion"
"initiative_id" INT4 NOT NULL REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"id" SERIAL8 PRIMARY KEY,
"draft_id" INT8 NOT NULL,
FOREIGN KEY ("initiative_id", "draft_id") REFERENCES "draft" ("initiative_id", "id") ON DELETE NO ACTION ON UPDATE CASCADE,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"author_id" INT4 NOT NULL REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
"name" TEXT NOT NULL,
"formatting_engine" TEXT,
"content" TEXT NOT NULL DEFAULT '',
"text_search_data" TSVECTOR,
"minus2_unfulfilled_count" INT4,
"minus2_fulfilled_count" INT4,
"minus1_unfulfilled_count" INT4,
"minus1_fulfilled_count" INT4,
"plus1_unfulfilled_count" INT4,
"plus1_fulfilled_count" INT4,
"plus2_unfulfilled_count" INT4,
"plus2_fulfilled_count" INT4,
"proportional_order" INT4 );
CREATE INDEX "suggestion_created_idx" ON "suggestion" ("created");
CREATE INDEX "suggestion_author_id_created_idx" ON "suggestion" ("author_id", "created");
CREATE INDEX "suggestion_text_search_data_idx" ON "suggestion" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "suggestion"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "content");
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';
COMMENT ON COLUMN "suggestion"."draft_id" IS 'Draft, which the author has seen when composing the suggestion; should always be set by a frontend, but defaults to current draft of the initiative (implemented by trigger "default_for_draft_id")';
COMMENT ON COLUMN "suggestion"."minus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."minus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."minus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."minus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."plus1_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."plus1_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."plus2_unfulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."plus2_fulfilled_count" IS 'Calculated from table "direct_supporter_snapshot", not requiring informed supporters';
COMMENT ON COLUMN "suggestion"."proportional_order" IS 'To be used for sorting suggestions within an initiative; NULL values sort last; updated by "lf_update_suggestion_order"';
CREATE TABLE "rendered_suggestion" (
PRIMARY KEY ("suggestion_id", "format"),
"suggestion_id" INT8 REFERENCES "suggestion" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"format" TEXT,
"content" TEXT NOT NULL );
COMMENT ON TABLE "rendered_suggestion" IS 'This table may be used by frontends to cache "rendered" suggestions (e.g. HTML output generated from wiki text)';
CREATE TABLE "suggestion_setting" (
PRIMARY KEY ("member_id", "key", "suggestion_id"),
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"key" TEXT NOT NULL,
"suggestion_id" INT8 REFERENCES "suggestion" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"value" TEXT NOT NULL );
COMMENT ON TABLE "suggestion_setting" IS 'Place for frontend to store suggestion specific settings of members as strings';
CREATE TYPE side AS ENUM ('pro', 'contra');
CREATE TABLE "argument" (
UNIQUE ("initiative_id", "id"), -- index needed for foreign-key on table "rating"
"initiative_id" INT4 REFERENCES "initiative" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"id" SERIAL8 PRIMARY KEY,
--"parent_id" SERIAL8,
"created" TIMESTAMPTZ NOT NULL DEFAULT now(),
"author_id" INT4 REFERENCES "member" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
"name" TEXT NOT NULL,
"formatting_engine" TEXT,
"content" TEXT NOT NULL DEFAULT '',
"text_search_data" TSVECTOR,
"side" side NOT NULL,
"minus_count" INT4 NOT NULL DEFAULT 0,
"plus_count" INT4 NOT NULL DEFAULT 0 );
CREATE INDEX "argument_created_idx" ON "argument" ("created");
CREATE INDEX "argument_author_id_created_idx" ON "argument" ("author_id", "created");
CREATE INDEX "argument_text_search_data_idx" ON "argument" USING gin ("text_search_data");
CREATE TRIGGER "update_text_search_data"
BEFORE INSERT OR UPDATE ON "argument"
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger('text_search_data', 'pg_catalog.simple',
"name", "content");
COMMENT ON TABLE "argument" IS 'Arguments to initiatives';
COMMENT ON COLUMN "argument"."minus_count" IS 'Number of negative ratings; delegations are not considered';
COMMENT ON COLUMN "argument"."plus_count" IS 'Number of positive ratings; delegations are not considered';
CREATE TABLE "rendered_argument" (
PRIMARY KEY ("argument_id", "format"),
"argument_id" INT8 NOT NULL REFERENCES "argument" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"format" TEXT,
"content" TEXT NOT NULL );
COMMENT ON TABLE "rendered_argument" IS 'This table may be used by frontends to cache "rendered" arguments (e.g. HTML output generated from wiki text)';
CREATE TABLE "privilege" (
PRIMARY KEY ("unit_id", "member_id"),
"unit_id" INT4 REFERENCES "unit" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"admin_manager" BOOLEAN NOT NULL DEFAULT FALSE,
"unit_manager" BOOLEAN NOT NULL DEFAULT FALSE,
"area_manager" BOOLEAN NOT NULL DEFAULT FALSE,
"member_manager" BOOLEAN NOT NULL DEFAULT FALSE,
"initiative_right" BOOLEAN NOT NULL DEFAULT TRUE,
"voting_right" BOOLEAN NOT NULL DEFAULT TRUE,
"polling_right" BOOLEAN NOT NULL DEFAULT FALSE );
COMMENT ON TABLE "privilege" IS 'Members rights related to each unit';
COMMENT ON COLUMN "privilege"."admin_manager" IS 'Grant/revoke any privileges to/from other members';
COMMENT ON COLUMN "privilege"."unit_manager" IS 'Create and disable sub units';
COMMENT ON COLUMN "privilege"."area_manager" IS 'Create and disable areas and set area parameters';
COMMENT ON COLUMN "privilege"."member_manager" IS 'Adding/removing members from the unit, granting or revoking "initiative_right" and "voting_right"';
COMMENT ON COLUMN "privilege"."initiative_right" IS 'Right to create an initiative';
COMMENT ON COLUMN "privilege"."voting_right" IS 'Right to support initiatives, create and rate suggestions, and to vote';
COMMENT ON COLUMN "privilege"."polling_right" IS 'Right to create issues with policies having the "policy"."polling" flag set, and to add initiatives having the "initiative"."polling" flag set to those issues';
CREATE TABLE "membership" (
PRIMARY KEY ("area_id", "member_id"),
"area_id" INT4 REFERENCES "area" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
CREATE INDEX "membership_member_id_idx" ON "membership" ("member_id");
COMMENT ON TABLE "membership" IS 'Interest of members in topic areas';
CREATE TABLE "interest" (
PRIMARY KEY ("issue_id", "member_id"),
"issue_id" INT4 REFERENCES "issue" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"member_id" INT4 REFERENCES "member" ("id") ON DELETE CASCADE ON UPDATE CASCADE );
CREATE INDEX "interest_member_id_idx" ON "interest" ("member_id");
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.';
CREATE TABLE "initiator" (