lemmy/migrations/2024-12-05-233704_add_profile_combined_table/up.sql

30 lines
885 B
SQL

-- Creates combined tables for
-- Profile: (comment, post)
CREATE TABLE profile_combined (
id serial PRIMARY KEY,
published timestamptz NOT NULL,
post_id int UNIQUE REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE,
comment_id int UNIQUE REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
-- Make sure only one of the columns is not null
CHECK ((post_id IS NOT NULL)::integer + (comment_id IS NOT NULL)::integer = 1)
);
CREATE INDEX idx_profile_combined_published ON profile_combined (published DESC, id DESC);
CREATE INDEX idx_profile_combined_published_asc ON profile_combined (reverse_timestamp_sort (published) DESC, id DESC);
-- Updating the history
INSERT INTO profile_combined (published, post_id)
SELECT
published,
id
FROM
post;
INSERT INTO profile_combined (published, comment_id)
SELECT
published,
id
FROM
comment;