feature #3: create extra table like comments and votes
This commit is contained in:
@@ -1,3 +1,21 @@
|
||||
-- Extra resources
|
||||
drop table if exists follow_article;
|
||||
drop table if exists follow_constitution;
|
||||
drop table if exists follow_citizen;
|
||||
drop table if exists follow;
|
||||
|
||||
drop table if exists vote_for_article;
|
||||
drop table if exists vote_for_constitution;
|
||||
drop table if exists vote_for_comment_on_article;
|
||||
drop table if exists vote_for_comment_on_constitution;
|
||||
drop table if exists vote;
|
||||
|
||||
drop table if exists comment_on_article;
|
||||
drop table if exists comment_on_constitution;
|
||||
drop table if exists comment;
|
||||
|
||||
drop table if exists extra;
|
||||
|
||||
-- Article & Contitution
|
||||
drop table if exists article_relations;
|
||||
drop trigger if exists set_constitution_link_trigger on article_on_title;
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
-- Users
|
||||
create extension if not exists pgcrypto;
|
||||
-- select *
|
||||
-- from "user"
|
||||
-- where username = lower('nick@example.com')
|
||||
-- and password = crypt('12346', password);
|
||||
|
||||
create table "user"
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
updated_at timestamptz default now() not null,
|
||||
updated_at timestamptz default now() not null check ( updated_at >= created_at ),
|
||||
blocked_at timestamptz default null null,
|
||||
username varchar(64) not null,
|
||||
password varchar(258) not null
|
||||
username varchar(64) not null check ( username != '' and lower(username) = username) unique,
|
||||
password text not null check ( password != '' )
|
||||
);
|
||||
|
||||
create type "name" as (
|
||||
@@ -18,21 +23,23 @@ create type "name" as (
|
||||
|
||||
create table citizen
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
name "name" not null,
|
||||
birthday date not null,
|
||||
user_id uuid not null references "user" (id)
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
name "name" not null check ( name != '' ),
|
||||
birthday date not null,
|
||||
user_id uuid not null references "user" (id),
|
||||
vote_annonymous boolean default true not null,
|
||||
follow_annonymous boolean default true not null
|
||||
);
|
||||
|
||||
create table workgroup
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
updated_at timestamptz default now() not null,
|
||||
created_by_id uuid not null references "user" (id),
|
||||
updated_at timestamptz default now() not null check ( updated_at >= created_at ),
|
||||
created_by_id uuid not null references citizen (id),
|
||||
name varchar(128) not null,
|
||||
description text not null,
|
||||
description text null,
|
||||
annonymous boolean default false not null,
|
||||
logo text null,
|
||||
owner_id uuid not null references citizen (id)
|
||||
@@ -50,7 +57,7 @@ create table moderator
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
updated_at timestamptz default now() not null,
|
||||
updated_at timestamptz default now() not null check ( updated_at >= created_at ),
|
||||
assigned_period tstzrange[] default '{}' not null,
|
||||
user_id uuid not null references "user" (id)
|
||||
);
|
||||
@@ -69,7 +76,7 @@ create or replace function set_version_number() returns trigger
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
new.version_number = generate_version_number(TG_TABLE_NAME::regclass, new.version_id);
|
||||
new.version_number = generate_version_number(tg_table_name::regclass, new.version_id);
|
||||
end;
|
||||
$$;
|
||||
|
||||
@@ -77,43 +84,43 @@ create table article
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
created_by_id uuid not null references "user" (id),
|
||||
created_by_id uuid not null references citizen (id),
|
||||
version_id uuid default uuid_generate_v4() not null,
|
||||
version_number int not null,
|
||||
version_number int not null unique,
|
||||
title text not null,
|
||||
annonymous boolean default false not null,
|
||||
content text not null,
|
||||
content text not null check ( content != '' ),
|
||||
description text,
|
||||
tags varchar(32)[] default '{}' not null
|
||||
);
|
||||
|
||||
CREATE TRIGGER generate_version_number_trigger
|
||||
BEFORE INSERT
|
||||
ON article
|
||||
EXECUTE PROCEDURE set_version_number();
|
||||
create trigger generate_version_number_trigger
|
||||
before insert
|
||||
on article
|
||||
execute procedure set_version_number();
|
||||
|
||||
create table constitution
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
created_by_id uuid not null references "user" (id),
|
||||
created_by_id uuid not null references citizen (id),
|
||||
version_id uuid default uuid_generate_v4() not null,
|
||||
version_number int not null,
|
||||
title text not null,
|
||||
annonymous boolean default false not null
|
||||
);
|
||||
|
||||
CREATE TRIGGER generate_version_number_trigger
|
||||
BEFORE INSERT
|
||||
ON constitution
|
||||
EXECUTE PROCEDURE set_version_number();
|
||||
create trigger generate_version_number_trigger
|
||||
before insert
|
||||
on constitution
|
||||
execute procedure set_version_number();
|
||||
|
||||
create table title
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
created_by_id uuid not null references "user" (id),
|
||||
name text not null,
|
||||
created_by_id uuid not null references citizen (id),
|
||||
name text not null check ( name != '' ),
|
||||
rank int not null,
|
||||
constitution_id uuid not null references constitution (id)
|
||||
);
|
||||
@@ -122,7 +129,7 @@ create table article_in_title
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
created_by_id uuid not null references "user" (id),
|
||||
created_by_id uuid not null references citizen (id),
|
||||
rank int not null,
|
||||
title_id uuid not null references title (id),
|
||||
article_id uuid not null references article (id),
|
||||
@@ -141,16 +148,119 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER set_constitution_link_trigger
|
||||
BEFORE INSERT
|
||||
ON article_in_title
|
||||
EXECUTE PROCEDURE set_constitution_link();
|
||||
create trigger set_constitution_link_trigger
|
||||
before insert
|
||||
on article_in_title
|
||||
execute procedure set_constitution_link();
|
||||
|
||||
create table article_relations
|
||||
(
|
||||
source_id uuid references article,
|
||||
target_id uuid references article check ( source_id != target_id ),
|
||||
created_at timestamptz default now(),
|
||||
created_by_id uuid not null references "user" (id),
|
||||
created_by_id uuid not null references citizen (id),
|
||||
primary key (source_id, target_id)
|
||||
);
|
||||
|
||||
-- Extra resources
|
||||
|
||||
create table extra
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
citizen_id uuid not null references citizen (id),
|
||||
target_id uuid not null,
|
||||
target_reference regclass not null
|
||||
);
|
||||
|
||||
create table follow
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (extra);
|
||||
|
||||
create table follow_article
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
foreign key (target_id) references article (id),
|
||||
primary key (id)
|
||||
) inherits (follow);
|
||||
|
||||
create table follow_constitution
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
foreign key (target_id) references constitution (id),
|
||||
primary key (id)
|
||||
) inherits (follow);
|
||||
|
||||
create table follow_citizen
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
foreign key (target_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (follow);
|
||||
|
||||
|
||||
|
||||
create table comment
|
||||
(
|
||||
updated_at timestamptz default now() not null check ( updated_at >= created_at ),
|
||||
"content" text not null check ( content != '' ),
|
||||
parent_id uuid null references comment (id),
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (extra);
|
||||
|
||||
create table comment_on_article
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
foreign key (target_id) references article (id),
|
||||
foreign key (parent_id) references comment_on_article (id),
|
||||
primary key (id)
|
||||
) inherits (comment);
|
||||
|
||||
create table comment_on_constitution
|
||||
(
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
foreign key (target_id) references constitution (id),
|
||||
foreign key (parent_id) references comment_on_constitution (id),
|
||||
primary key (id)
|
||||
) inherits (comment);
|
||||
|
||||
|
||||
|
||||
create table vote
|
||||
(
|
||||
anonymous boolean default true not null,
|
||||
note int not null check ( note >= -1 and note <= 1 ),
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (extra);
|
||||
|
||||
create table vote_for_article
|
||||
(
|
||||
foreign key (target_id) references article (id),
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (vote);
|
||||
|
||||
create table vote_for_constitution
|
||||
(
|
||||
foreign key (target_id) references constitution (id),
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (vote);
|
||||
|
||||
create table vote_for_comment_on_article
|
||||
(
|
||||
foreign key (target_id) references comment_on_article (id),
|
||||
foreign key (citizen_id) references citizen (id),
|
||||
primary key (id)
|
||||
) inherits (vote);
|
||||
|
||||
create table vote_for_comment_on_constitution
|
||||
(
|
||||
primary key (id),
|
||||
foreign key (target_id) references comment_on_constitution (id),
|
||||
foreign key (target_id) references citizen (id)
|
||||
) inherits (vote);
|
||||
|
||||
Reference in New Issue
Block a user