diff --git a/src/main/resources/sql/functions/comment/comment.sql b/src/main/resources/sql/functions/comment/comment.sql index bbd9676..a5b45b6 100644 --- a/src/main/resources/sql/functions/comment/comment.sql +++ b/src/main/resources/sql/functions/comment/comment.sql @@ -17,7 +17,7 @@ begin values (_citizen_id, _target_id, _content, _parent_id) returning comment_on_constitution.id into _id; else - raise exception '% no implemented', reference::text; + raise exception 'comment with target as "%", is no implemented', reference::text; end if; end; $$; diff --git a/src/main/resources/sql/migrations/0000-init_schema.up.sql b/src/main/resources/sql/migrations/0000-init_schema.up.sql index b082a72..d4462c3 100644 --- a/src/main/resources/sql/migrations/0000-init_schema.up.sql +++ b/src/main/resources/sql/migrations/0000-init_schema.up.sql @@ -167,6 +167,7 @@ $$; create trigger set_constitution_link_trigger before insert on article_in_title + for each row execute procedure set_constitution_link(); create table article_relations @@ -228,13 +229,41 @@ create table follow_citizen create table comment ( - updated_at timestamptz default now() not null check ( updated_at >= created_at ), - "content" text not null check ( content != '' and length(content) < 4096), - parent_id uuid null references comment (id), + updated_at timestamptz default now() not null check ( updated_at >= created_at ), + "content" text not null check ( content != '' and length(content) < 4096), + parent_id uuid references comment (id), + parents_ids uuid[], foreign key (citizen_id) references citizen (id), primary key (id) ) inherits (extra); +create index comment_parents_ids_idx + on comment (parents_ids); + +create or replace function set_comment_parents_ids() returns trigger + language plpgsql as +$$ +begin + if(new.parent_id is not null) then + new.parents_ids = ( + select com.parents_ids || com.id + from "comment" com + where com.id = new.parent_id + ); + else + new.parents_ids = null; + end if; + + return new; +end; +$$; + +create trigger set_comment_parents_ids_trigger + before insert + on comment + for each row +execute procedure set_comment_parents_ids(); + create table comment_on_article ( target_reference regclass default 'article'::regclass not null, @@ -244,6 +273,15 @@ create table comment_on_article primary key (id) ) inherits (comment); +create index comment_on_article_parents_ids_idx + on comment_on_article (parents_ids); + +create trigger set_comment_on_article_parents_ids_trigger + before insert + on comment_on_article + for each row +execute procedure set_comment_parents_ids(); + create table comment_on_constitution ( target_reference regclass default 'constitution'::regclass not null, @@ -253,6 +291,15 @@ create table comment_on_constitution primary key (id) ) inherits (comment); +create index comment_on_constitution_parents_ids_idx + on comment_on_constitution (parents_ids); + +create trigger set_comment_on_constitution_parents_ids_trigger + before insert + on comment_on_constitution + for each row +execute procedure set_comment_parents_ids(); + create table vote diff --git a/src/test/sql/comment.sql b/src/test/sql/comment.sql index 8cee751..8cdf552 100644 --- a/src/test/sql/comment.sql +++ b/src/test/sql/comment.sql @@ -26,6 +26,8 @@ declare } $json$; _comment_id uuid; + _comment_id_response uuid; + _comment_id_response2 uuid; _selected_comments json; _selected_comments_total int; begin @@ -78,6 +80,27 @@ begin from find_comments_constitution_by_citizen(_citizen_id); assert (_selected_comments_total = 0), 'the number of comments for this citizen must be 0, "' || _selected_comments_total || '" returned'; + + select "comment"( + reference => 'article'::regclass, + target_id => (created_article->>'id')::uuid, + citizen_id => _citizen_id, + content => 'No is not exist'::text, + _parent_id => _comment_id::uuid + ) into _comment_id_response; + + select "comment"( + reference => 'article'::regclass, + target_id => (created_article->>'id')::uuid, + citizen_id => _citizen_id, + content => 'No is not exist'::text, + _parent_id => _comment_id_response::uuid + ) into _comment_id_response2; + assert (select count(*) = 3 from "comment"), 'comment must be inserted'; + assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response); + assert (select com.parents_ids @> ARRAY[_comment_id_response] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id_response::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2); + assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2); + -- delete comment and context delete from "comment"; delete from article;