diff --git a/src/main/resources/sql/fixtures/07-comment.sql b/src/main/resources/sql/fixtures/07-comment.sql index 5b5f79c..930aad4 100644 --- a/src/main/resources/sql/fixtures/07-comment.sql +++ b/src/main/resources/sql/fixtures/07-comment.sql @@ -14,7 +14,7 @@ begin from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z join (select *, row_number() over () rn from article) a using (rn); - insert into comment_on_article (id, created_by_id, target_id, content, parent_id) + insert into comment_on_article (id, created_by_id, target_id, content, parent_comment_id) select uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring), z.id, @@ -24,7 +24,7 @@ begin from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z join (select *, row_number() over () rn from comment_on_article) a using (rn); - insert into comment_on_article (id, created_by_id, target_id, content, parent_id) + insert into comment_on_article (id, created_by_id, target_id, content, parent_comment_id) select uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring), z.id, @@ -32,7 +32,7 @@ begin 'content' || row_number() over () * g, a.id from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z - join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn); + join (select *, row_number() over () rn from comment_on_article where parent_comment_id is not null) a using (rn); insert into comment_on_constitution (id, created_by_id, target_id, content) select diff --git a/src/main/resources/sql/functions/comment/comment.sql b/src/main/resources/sql/functions/comment/comment.sql index 8796484..57eb7cd 100644 --- a/src/main/resources/sql/functions/comment/comment.sql +++ b/src/main/resources/sql/functions/comment/comment.sql @@ -1,20 +1,20 @@ -create or replace function comment(reference regclass, target_id uuid, created_by_id uuid, content text, parent_id uuid default null, out id uuid) +create or replace function comment(reference regclass, target_id uuid, created_by_id uuid, content text, parent_comment_id uuid default null, out id uuid) language plpgsql as $$ declare _created_by_id alias for created_by_id; _target_id alias for target_id; _content alias for content; - _parent_id alias for parent_id; + _parent_comment_id alias for parent_comment_id; _id alias for id; begin if reference = 'article'::regclass then - insert into comment_on_article (created_by_id, target_id, content, parent_id) - values (_created_by_id, _target_id, _content, _parent_id) + insert into comment_on_article (created_by_id, target_id, content, parent_comment_id) + values (_created_by_id, _target_id, _content, _parent_comment_id) returning comment_on_article.id into _id; elseif reference = 'constitution'::regclass then - insert into comment_on_constitution (created_by_id, target_id, content, parent_id) - values (_created_by_id, _target_id, _content, _parent_id) + insert into comment_on_constitution (created_by_id, target_id, content, parent_comment_id) + values (_created_by_id, _target_id, _content, _parent_comment_id) returning comment_on_constitution.id into _id; else raise exception 'comment with target as "%", is no implemented', reference::text; diff --git a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql index 4c1b25e..9edcdb4 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql @@ -15,7 +15,7 @@ begin find_reference_by_id(com.target_id, com.target_reference) as target, find_citizen_by_id(com.created_by_id) as created_by from "comment" as com - where com.parents_ids @> array[_parent_id] + where parent_id = _parent_id order by com.parents_ids nulls first, created_at desc, com.created_at desc limit "limit" offset "offset" 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 271c3ea..0b0fc48 100644 --- a/src/main/resources/sql/migrations/0000-init_schema.up.sql +++ b/src/main/resources/sql/migrations/0000-init_schema.up.sql @@ -368,11 +368,12 @@ 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 references comment (id), - parents_ids uuid[], - deleted_at timestamptz null, + 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 not null, + parent_comment_id uuid references comment (id), + parents_ids uuid[], + deleted_at timestamptz null, foreign key (created_by_id) references citizen (id), primary key (id) ) inherits (extra); @@ -380,20 +381,25 @@ create table comment create index comment_parents_ids_idx on comment (parents_ids); +create index parent_id + on comment (parent_id); + create or replace function set_comment_parents_ids() returns trigger language plpgsql as $$ begin - if (new.parent_id is not null) then + if (new.parent_comment_id is not null) then new.parents_ids = ( select com.parents_ids || com.id from "comment" com - where com.id = new.parent_id + where com.id = new.parent_comment_id ); else - new.parents_ids = null; + new.parents_ids = array [new.target_id]::uuid[]; end if; + new.parent_id = (new.parents_ids[array_upper(new.parents_ids, 1)]); + return new; end; $$; @@ -409,7 +415,7 @@ create table comment_on_article target_reference regclass default 'article'::regclass not null, foreign key (created_by_id) references citizen (id), foreign key (target_id) references article (id), - foreign key (parent_id) references comment_on_article (id), + foreign key (parent_comment_id) references comment_on_article (id), primary key (id) ) inherits (comment); @@ -427,7 +433,7 @@ create table comment_on_constitution target_reference regclass default 'constitution'::regclass not null, foreign key (created_by_id) references citizen (id), foreign key (target_id) references constitution (id), - foreign key (parent_id) references comment_on_constitution (id), + foreign key (parent_comment_id) references comment_on_constitution (id), primary key (id) ) inherits (comment); diff --git a/src/test/sql/comment.sql b/src/test/sql/comment.sql index bb5fd05..6630acc 100644 --- a/src/test/sql/comment.sql +++ b/src/test/sql/comment.sql @@ -93,7 +93,7 @@ begin target_id => (created_article->>'id')::uuid, created_by_id => _citizen_id, content => 'God not exist'::text, - parent_id => _comment_id::uuid + parent_comment_id => _comment_id::uuid ) into _comment_id_response; select "comment"( @@ -101,7 +101,7 @@ begin target_id => (created_article->>'id')::uuid, created_by_id => _citizen_id, content => 'are you really sure ?'::text, - parent_id => _comment_id_response::uuid + parent_comment_id => _comment_id_response::uuid ) into _comment_id_response2; assert (select count(*) = 3 from "comment"), 'response 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);