52 lines
2.5 KiB
SQL
52 lines
2.5 KiB
SQL
-- create table for categories
|
|
create table kandimat_data.category
|
|
(
|
|
row_id serial primary key,
|
|
title character varying(300) UNIQUE NOT NULL check ( title <> '' ),
|
|
description character varying(15000)
|
|
);
|
|
grant select on table kandimat_data.category to kandimat_person;
|
|
-- the following line is only necessary as long as the kandimat should be publicly accessible
|
|
grant select on table kandimat_data.category to kandimat_anonymous;
|
|
grant insert, update, delete on table kandimat_data.category to kandimat_editor;
|
|
grant usage on sequence kandimat_data.category_row_id_seq to kandimat_editor;
|
|
|
|
-- create table for questions
|
|
create table kandimat_data.question
|
|
(
|
|
row_id serial primary key,
|
|
category_row_id integer REFERENCES kandimat_data.category (row_id) ON UPDATE CASCADE ON DELETE SET NULL,
|
|
title character varying(3000) UNIQUE NOT NULL check ( title <> '' ),
|
|
description character varying(15000)
|
|
);
|
|
grant select on table kandimat_data.question to kandimat_person;
|
|
-- the following line is only necessary as long as the kandimat should be publicly accessible
|
|
grant select on table kandimat_data.question to kandimat_anonymous;
|
|
grant insert, update, delete on table kandimat_data.question to kandimat_editor;
|
|
grant usage on sequence kandimat_data.question_row_id_seq to kandimat_editor;
|
|
|
|
-- create table for answers
|
|
create table kandimat_data.answer
|
|
(
|
|
question_row_id integer REFERENCES kandimat_data.question (row_id) ON UPDATE CASCADE ON DELETE CASCADE,
|
|
person_row_id integer REFERENCES kandimat_data.person (row_id) ON UPDATE CASCADE ON DELETE CASCADE,
|
|
position integer NOT NULL check (position between 0 and 3),
|
|
text character varying(15000),
|
|
created_at timestamp default now(),
|
|
primary key (question_row_id, person_row_id)
|
|
);
|
|
grant select on table kandimat_data.answer to kandimat_person;
|
|
-- the following line is only necessary as long as the kandimat should be publicly accessible
|
|
grant select on table kandimat_data.answer to kandimat_anonymous;
|
|
grant insert, update, delete on table kandimat_data.answer to kandimat_candidate;
|
|
|
|
alter table kandimat_data.answer
|
|
enable row level security;
|
|
create policy change_answer on kandimat_data.answer to kandimat_candidate
|
|
using (person_row_id = nullif(current_setting('jwt.claims.person_row_id', true), '')::integer);
|
|
create policy select_answer
|
|
on kandimat_data.answer
|
|
for select
|
|
to kandimat_anonymous, kandimat_person -- maybe change to kandimat_person only in the future
|
|
using (true);
|