From e3daf160359c3a21e4cf9dc1fd53aac41aba6c04 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 18:03:04 +0200 Subject: [PATCH 1/7] feature #3: create table article --- resources/sql/migrations/0000-init.down.sql | 4 +++ resources/sql/migrations/0000-init.up.sql | 33 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 resources/sql/migrations/0000-init.down.sql create mode 100644 resources/sql/migrations/0000-init.up.sql diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql new file mode 100644 index 0000000..683caae --- /dev/null +++ b/resources/sql/migrations/0000-init.down.sql @@ -0,0 +1,4 @@ +drop trigger generate_version_number_trigger on article; +drop table article; +drop function generate_version_number(regclass, uuid); +drop function set_version_number(); \ No newline at end of file diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql new file mode 100644 index 0000000..6607a36 --- /dev/null +++ b/resources/sql/migrations/0000-init.up.sql @@ -0,0 +1,33 @@ +create or replace function generate_version_number(tablename regclass, version_id uuid) returns int + language plpgsql as +$$ +begin + return random(); -- TODO +end; +$$; + +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); +end; +$$; + +create table article +( + id uuid default uuid_generate_v4() not null, + version_id uuid default uuid_generate_v4() not null, + version_number int not null, + title text not null, + annonymous boolean default false not null, + content text not null, + description text, + tags varchar(32)[] default '{}' not null, + created_at timestamptz default now() not null +); + +CREATE TRIGGER generate_version_number_trigger + BEFORE INSERT + ON article +EXECUTE PROCEDURE set_version_number(); From 5d6f9e613ce5e6320594d54505e3473f7e83e3b2 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 18:47:17 +0200 Subject: [PATCH 2/7] feature #3: create table constitution --- resources/sql/migrations/0000-init.down.sql | 4 +++- resources/sql/migrations/0000-init.up.sql | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql index 683caae..5682408 100644 --- a/resources/sql/migrations/0000-init.down.sql +++ b/resources/sql/migrations/0000-init.down.sql @@ -1,4 +1,6 @@ drop trigger generate_version_number_trigger on article; drop table article; drop function generate_version_number(regclass, uuid); -drop function set_version_number(); \ No newline at end of file +drop function set_version_number(); +drop trigger generate_version_number_trigger on constitution; +drop table constitution; \ No newline at end of file diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql index 6607a36..0f7702f 100644 --- a/resources/sql/migrations/0000-init.up.sql +++ b/resources/sql/migrations/0000-init.up.sql @@ -17,17 +17,32 @@ $$; create table article ( id uuid default uuid_generate_v4() not null, + created_at timestamptz default now() not null, version_id uuid default uuid_generate_v4() not null, version_number int not null, title text not null, annonymous boolean default false not null, content text not null, description text, - tags varchar(32)[] default '{}' not null, - created_at timestamptz default now() not null + tags varchar(32)[] default '{}' not null ); 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, + created_at timestamptz default now() not null, + 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(); \ No newline at end of file From 8c05072907243e0a25759ec985b920ce90702d3f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 19:21:43 +0200 Subject: [PATCH 3/7] feature #3: link table constitution and article --- resources/sql/migrations/0000-init.down.sql | 18 ++++--- resources/sql/migrations/0000-init.up.sql | 52 +++++++++++++++++---- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql index 5682408..5e7b55d 100644 --- a/resources/sql/migrations/0000-init.down.sql +++ b/resources/sql/migrations/0000-init.down.sql @@ -1,6 +1,12 @@ -drop trigger generate_version_number_trigger on article; -drop table article; -drop function generate_version_number(regclass, uuid); -drop function set_version_number(); -drop trigger generate_version_number_trigger on constitution; -drop table constitution; \ No newline at end of file +-- Article & Contitution +drop trigger if exists set_constitution_link_trigger on article_on_title; +drop table if exists article_in_title; +drop table if exists title; +drop function if exists set_constitution_link(); + +drop trigger if exists generate_version_number_trigger on article; +drop table if exists article; +drop function if exists generate_version_number(regclass, uuid); +drop trigger if exists generate_version_number_trigger on constitution; +drop table if exists constitution; +drop function if exists set_version_number(); diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql index 0f7702f..547a2d0 100644 --- a/resources/sql/migrations/0000-init.up.sql +++ b/resources/sql/migrations/0000-init.up.sql @@ -16,7 +16,7 @@ $$; create table article ( - id uuid default uuid_generate_v4() not null, + id uuid default uuid_generate_v4() not null primary key, created_at timestamptz default now() not null, version_id uuid default uuid_generate_v4() not null, version_number int not null, @@ -34,15 +34,51 @@ EXECUTE PROCEDURE set_version_number(); create table constitution ( - id uuid default uuid_generate_v4() not null, - created_at timestamptz default now() not null, - version_id uuid default uuid_generate_v4() not null, - version_number int not null, - title text not null, - annonymous boolean default false not null + id uuid default uuid_generate_v4() not null primary key, + created_at timestamptz default now() not null, + 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(); \ No newline at end of file +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, + name text not null, + rank int not null, + constitution_id uuid not null references constitution (id) +); + +create table article_in_title +( + id uuid default uuid_generate_v4() not null primary key, + created_at timestamptz default now() not null, + rank int not null, + title_id uuid not null references title (id), + article_id uuid not null references article (id), + constitution_id uuid not null references constitution (id) +); + +create or replace function set_constitution_link() returns trigger + language plpgsql as +$$ +begin + new.constitution_id = ( + select t.constitution_id + from title as t + where t.id = new.title_id + ); +end; +$$; + +CREATE TRIGGER set_constitution_link_trigger + BEFORE INSERT + ON article_in_title +EXECUTE PROCEDURE set_constitution_link(); \ No newline at end of file From dfae83884eb017c10126aa8720593b98b503cc59 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 20:28:15 +0200 Subject: [PATCH 4/7] feature #3: create table user & citizen and add created_by column to other tables --- resources/sql/migrations/0000-init.down.sql | 9 +++ resources/sql/migrations/0000-init.up.sql | 74 ++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql index 5e7b55d..8e59c40 100644 --- a/resources/sql/migrations/0000-init.down.sql +++ b/resources/sql/migrations/0000-init.down.sql @@ -1,4 +1,5 @@ -- Article & Contitution +drop table if exists article_relations; drop trigger if exists set_constitution_link_trigger on article_on_title; drop table if exists article_in_title; drop table if exists title; @@ -10,3 +11,11 @@ drop function if exists generate_version_number(regclass, uuid); drop trigger if exists generate_version_number_trigger on constitution; drop table if exists constitution; drop function if exists set_version_number(); + +-- User +drop table if exists moderator; +drop table if exists citizen_in_workgroup; +drop table if exists workgroup; +drop table if exists citizen; +drop table if exists "user"; +drop type if exists public."name"; diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql index 547a2d0..f2b9266 100644 --- a/resources/sql/migrations/0000-init.up.sql +++ b/resources/sql/migrations/0000-init.up.sql @@ -1,3 +1,62 @@ +-- Users + +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, + blocked_at timestamptz default null null, + username varchar(64) not null, + password varchar(258) not null +); + +create type "name" as ( + first_name text, + last_name text, + civility text + ); + +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) +); + +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), + name varchar(128) not null, + description text not null, + annonymous boolean default false not null, + logo text null, + owner_id uuid not null references citizen (id) +); + +create table citizen_in_workgroup +( + citizen_id uuid not null references citizen (id), + workgroup uuid not null references workgroup (id), + created_at timestamptz default now() not null, + primary key (citizen_id, workgroup) +); + +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, + assigned_period tstzrange[] default '{}' not null, + user_id uuid not null references "user" (id) +); + +-- Article & Contitution + create or replace function generate_version_number(tablename regclass, version_id uuid) returns int language plpgsql as $$ @@ -18,6 +77,7 @@ 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), version_id uuid default uuid_generate_v4() not null, version_number int not null, title text not null, @@ -36,6 +96,7 @@ 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), version_id uuid default uuid_generate_v4() not null, version_number int not null, title text not null, @@ -51,6 +112,7 @@ 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, rank int not null, constitution_id uuid not null references constitution (id) @@ -60,6 +122,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), rank int not null, title_id uuid not null references title (id), article_id uuid not null references article (id), @@ -81,4 +144,13 @@ $$; CREATE TRIGGER set_constitution_link_trigger BEFORE INSERT ON article_in_title -EXECUTE PROCEDURE set_constitution_link(); \ No newline at end of file +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), + primary key (source_id, target_id) +); From 899bae4a1dd4a10cb191c8d9e72ff98e16e26fad Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 22:24:59 +0200 Subject: [PATCH 5/7] feature #3: create extra table like comments and votes --- .idea/codeStyles/Project.xml | 6 + resources/sql/migrations/0000-init.down.sql | 18 ++ resources/sql/migrations/0000-init.up.sql | 176 ++++++++++++++++---- 3 files changed, 167 insertions(+), 33 deletions(-) diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 1bec35e..2dee5fc 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -3,6 +3,12 @@ + + diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql index 8e59c40..b2e68a7 100644 --- a/resources/sql/migrations/0000-init.down.sql +++ b/resources/sql/migrations/0000-init.down.sql @@ -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; diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql index f2b9266..50225ca 100644 --- a/resources/sql/migrations/0000-init.up.sql +++ b/resources/sql/migrations/0000-init.up.sql @@ -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); From 0f42d985c5c7b9020985d0c2311816e5c580636e Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 22:30:24 +0200 Subject: [PATCH 6/7] feature #3: create stats tables --- resources/sql/migrations/0000-init.down.sql | 3 +++ resources/sql/migrations/0000-init.up.sql | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init.down.sql index b2e68a7..544b18d 100644 --- a/resources/sql/migrations/0000-init.down.sql +++ b/resources/sql/migrations/0000-init.down.sql @@ -1,3 +1,6 @@ +-- Stats +drop table if exists resource_view; + -- Extra resources drop table if exists follow_article; drop table if exists follow_constitution; diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init.up.sql index 50225ca..8ec5264 100644 --- a/resources/sql/migrations/0000-init.up.sql +++ b/resources/sql/migrations/0000-init.up.sql @@ -264,3 +264,13 @@ create table vote_for_comment_on_constitution foreign key (target_id) references comment_on_constitution (id), foreign key (target_id) references citizen (id) ) inherits (vote); + +-- Stats +create table resource_view +( + id uuid default uuid_generate_v4() not null primary key, + type regclass not null, + created_at timestamptz default now() not null, + created_by_id uuid null references citizen (id), + ip cidr null +); \ No newline at end of file From b19eb7f5fe26f78c23d7d6d75bf6413706f011bf Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 23:58:53 +0200 Subject: [PATCH 7/7] feature #3: rename sql files --- .../migrations/{0000-init.down.sql => 0000-init_schema.down.sql} | 0 .../sql/migrations/{0000-init.up.sql => 0000-init_schema.up.sql} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename resources/sql/migrations/{0000-init.down.sql => 0000-init_schema.down.sql} (100%) rename resources/sql/migrations/{0000-init.up.sql => 0000-init_schema.up.sql} (100%) diff --git a/resources/sql/migrations/0000-init.down.sql b/resources/sql/migrations/0000-init_schema.down.sql similarity index 100% rename from resources/sql/migrations/0000-init.down.sql rename to resources/sql/migrations/0000-init_schema.down.sql diff --git a/resources/sql/migrations/0000-init.up.sql b/resources/sql/migrations/0000-init_schema.up.sql similarity index 100% rename from resources/sql/migrations/0000-init.up.sql rename to resources/sql/migrations/0000-init_schema.up.sql