From dfae83884eb017c10126aa8720593b98b503cc59 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 25 Jul 2019 20:28:15 +0200 Subject: [PATCH] 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) +);