diff --git a/resources/functions/follow/follow.sql b/resources/functions/follow/follow.sql index 3043f5d..a025d2d 100644 --- a/resources/functions/follow/follow.sql +++ b/resources/functions/follow/follow.sql @@ -1,19 +1,21 @@ -create or replace function follow(reference regclass, target_id uuid, citizen_id uuid) returns void +create or replace function follow(reference regclass, _target_id uuid, _citizen_id uuid) returns void language plpgsql as $$ -declare - _citizen_id alias for citizen_id; - _target_id alias for target_id; begin if reference = 'article'::regclass then insert into follow_article (citizen_id, target_id) - values (_citizen_id, _target_id); + values (_citizen_id, _target_id) + on conflict (citizen_id, target_id) do nothing; elseif reference = 'constitution'::regclass then insert into follow_constitution (citizen_id, target_id) - values (_citizen_id, _target_id); + values (_citizen_id, _target_id) + on conflict (citizen_id, target_id) do nothing; elseif reference = 'citizen'::regclass then insert into follow_citizen (citizen_id, target_id) - values (_citizen_id, _target_id); + values (_citizen_id, _target_id) + on conflict (citizen_id, target_id) do nothing; + else + raise exception '% no implemented', reference::text; end if; end; $$; diff --git a/resources/sql/migrations/0000-init_schema.up.sql b/resources/sql/migrations/0000-init_schema.up.sql index cee2c62..0642b6f 100644 --- a/resources/sql/migrations/0000-init_schema.up.sql +++ b/resources/sql/migrations/0000-init_schema.up.sql @@ -193,7 +193,8 @@ create table extra create table follow ( foreign key (citizen_id) references citizen (id), - primary key (id) + primary key (id), + unique (citizen_id, target_id) ) inherits (extra); create table follow_article @@ -201,7 +202,8 @@ create table follow_article target_reference regclass default 'article'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references article (id), - primary key (id) + primary key (id), + unique (citizen_id, target_id) ) inherits (follow); create table follow_constitution @@ -209,7 +211,8 @@ create table follow_constitution target_reference regclass default 'constitution'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references constitution (id), - primary key (id) + primary key (id), + unique (citizen_id, target_id) ) inherits (follow); create table follow_citizen @@ -217,7 +220,8 @@ create table follow_citizen target_reference regclass default 'citizen'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references citizen (id), - primary key (id) + primary key (id), + unique (citizen_id, target_id) ) inherits (follow); diff --git a/resources/tests/comment.sql b/resources/tests/comment.sql index 677874b..b972081 100644 --- a/resources/tests/comment.sql +++ b/resources/tests/comment.sql @@ -57,7 +57,7 @@ begin assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment'; assert (select count(*) = 1 from "comment" where content = 'edited'), 'edit comment must not insert new comment'; - -- delete article and context + -- delete comment and context delete from "comment"; delete from article; delete from citizen; diff --git a/resources/tests/follow.sql b/resources/tests/follow.sql index 2a8ba3d..6c84a38 100644 --- a/resources/tests/follow.sql +++ b/resources/tests/follow.sql @@ -38,13 +38,16 @@ begin _citizen_id2 := created_citizen2->>'id'; + perform follow('citizen'::regclass, _citizen_id, _citizen_id2); + assert (select count(*) > 0 from follow), 'follow must be inserted'; + perform follow('citizen'::regclass, _citizen_id, _citizen_id2); assert (select count(*) > 0 from follow), 'follow must be inserted'; perform unfollow('citizen'::regclass, _citizen_id, _citizen_id2); assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow'; - -- delete article and context + -- delete follow and context delete from citizen; delete from "user";