lemmy/migrations/2022-07-07-182650_comment_ltrees/down.sql
Dessalines 9c3efe32e7
First pass at adding comment trees. (#2362)
* First pass at adding comment trees.

- Extracted comment replies into its own table.
- Added ltree column to comment
- Added parent_id param to GetComments to fetch a tree branch
- No paging / limiting yet

* Adding child_count to comment_aggregates.

* Adding parent comment update counts

* Fix unit tests.

* Comment tree paging mostly done.

* Fix clippy

* Fix drone tests wrong postgres version.

* Fix unit tests.

* Add back in delete in unit test.

* Add postgres upgrade script.

* Fixing some PR comments.

* Move update ltree into Comment::create

* Updating based on comments.

* Fix send soft fail.
2022-07-30 05:55:59 +02:00

26 lines
901 B
SQL

alter table comment add column parent_id integer;
-- Constraints and index
alter table comment add constraint comment_parent_id_fkey foreign key (parent_id) REFERENCES comment(id) ON UPDATE CASCADE ON DELETE CASCADE;
create index idx_comment_parent on comment (parent_id);
-- Update the parent_id column
-- subpath(subpath(0, -1), -1) gets the immediate parent but it fails null checks
update comment set parent_id = cast(ltree2text(nullif(subpath(nullif(subpath(path, 0, -1), '0'), -1), '0')) as INTEGER);
alter table comment drop column path;
alter table comment_aggregates drop column child_count;
drop extension ltree;
-- Add back in the read column
alter table comment add column read boolean default false not null;
update comment c set read = cr.read
from comment_reply cr where cr.comment_id = c.id;
create view comment_alias_1 as select * from comment;
drop table comment_reply;