lemmy/migrations/2022-07-07-182650_comment_ltrees/down.sql
Dessalines be1389420b
Adding SQL format checking via pg_format / pgFormatter (#3740)
* SQL format checking, 1.

* SQL format checking, 2.

* SQL format checking, 3.

* SQL format checking, 4.

* SQL format checking, 5.

* Running pg_format

* Getting rid of comment.

* Upping pg_format version.

* Using git ls-files for sql format check.

* Fixing sql lints.

* Addressing PR comments.
2023-08-02 12:44:51 -04:00

46 lines
957 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;