change parent_id in comment

This commit is contained in:
2019-09-22 01:16:42 +02:00
parent a37afc1ada
commit dc490dcac3
5 changed files with 28 additions and 22 deletions

View File

@@ -14,7 +14,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z 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); 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 select
uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring), uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring),
z.id, 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 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); 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 select
uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring), uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring),
z.id, z.id,
@@ -32,7 +32,7 @@ begin
'content' || row_number() over () * g, 'content' || row_number() over () * g,
a.id a.id
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z 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) insert into comment_on_constitution (id, created_by_id, target_id, content)
select select

View File

@@ -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 language plpgsql as
$$ $$
declare declare
_created_by_id alias for created_by_id; _created_by_id alias for created_by_id;
_target_id alias for target_id; _target_id alias for target_id;
_content alias for content; _content alias for content;
_parent_id alias for parent_id; _parent_comment_id alias for parent_comment_id;
_id alias for id; _id alias for id;
begin begin
if reference = 'article'::regclass then if reference = 'article'::regclass then
insert into comment_on_article (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_id) values (_created_by_id, _target_id, _content, _parent_comment_id)
returning comment_on_article.id into _id; returning comment_on_article.id into _id;
elseif reference = 'constitution'::regclass then elseif reference = 'constitution'::regclass then
insert into comment_on_constitution (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_id) values (_created_by_id, _target_id, _content, _parent_comment_id)
returning comment_on_constitution.id into _id; returning comment_on_constitution.id into _id;
else else
raise exception 'comment with target as "%", is no implemented', reference::text; raise exception 'comment with target as "%", is no implemented', reference::text;

View File

@@ -15,7 +15,7 @@ begin
find_reference_by_id(com.target_id, com.target_reference) as target, find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id(com.created_by_id) as created_by find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com 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, order by com.parents_ids nulls first, created_at desc,
com.created_at desc com.created_at desc
limit "limit" offset "offset" limit "limit" offset "offset"

View File

@@ -368,11 +368,12 @@ create table follow_citizen
create table comment create table comment
( (
updated_at timestamptz default now() not null check ( updated_at >= created_at ), updated_at timestamptz default now() not null check ( updated_at >= created_at ),
"content" text not null check ( content != '' and length(content) < 4096), "content" text not null check ( content != '' and length(content) < 4096),
parent_id uuid references comment (id), parent_id uuid not null,
parents_ids uuid[], parent_comment_id uuid references comment (id),
deleted_at timestamptz null, parents_ids uuid[],
deleted_at timestamptz null,
foreign key (created_by_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id) primary key (id)
) inherits (extra); ) inherits (extra);
@@ -380,20 +381,25 @@ create table comment
create index comment_parents_ids_idx create index comment_parents_ids_idx
on comment (parents_ids); on comment (parents_ids);
create index parent_id
on comment (parent_id);
create or replace function set_comment_parents_ids() returns trigger create or replace function set_comment_parents_ids() returns trigger
language plpgsql as language plpgsql as
$$ $$
begin begin
if (new.parent_id is not null) then if (new.parent_comment_id is not null) then
new.parents_ids = ( new.parents_ids = (
select com.parents_ids || com.id select com.parents_ids || com.id
from "comment" com from "comment" com
where com.id = new.parent_id where com.id = new.parent_comment_id
); );
else else
new.parents_ids = null; new.parents_ids = array [new.target_id]::uuid[];
end if; end if;
new.parent_id = (new.parents_ids[array_upper(new.parents_ids, 1)]);
return new; return new;
end; end;
$$; $$;
@@ -409,7 +415,7 @@ create table comment_on_article
target_reference regclass default 'article'::regclass not null, target_reference regclass default 'article'::regclass not null,
foreign key (created_by_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references article (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) primary key (id)
) inherits (comment); ) inherits (comment);
@@ -427,7 +433,7 @@ create table comment_on_constitution
target_reference regclass default 'constitution'::regclass not null, target_reference regclass default 'constitution'::regclass not null,
foreign key (created_by_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references constitution (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) primary key (id)
) inherits (comment); ) inherits (comment);

View File

@@ -93,7 +93,7 @@ begin
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
created_by_id => _citizen_id, created_by_id => _citizen_id,
content => 'God not exist'::text, content => 'God not exist'::text,
parent_id => _comment_id::uuid parent_comment_id => _comment_id::uuid
) into _comment_id_response; ) into _comment_id_response;
select "comment"( select "comment"(
@@ -101,7 +101,7 @@ begin
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
created_by_id => _citizen_id, created_by_id => _citizen_id,
content => 'are you really sure ?'::text, content => 'are you really sure ?'::text,
parent_id => _comment_id_response::uuid parent_comment_id => _comment_id_response::uuid
) into _comment_id_response2; ) into _comment_id_response2;
assert (select count(*) = 3 from "comment"), 'response must be inserted'; 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); 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);