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

31 lines
885 B
MySQL
Raw Normal View History

2024-12-06 13:18:08 +00:00
-- 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,
2024-12-06 14:49:11 +00:00
comment_id int UNIQUE REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
2024-12-06 13:18:08 +00:00
-- 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;
2024-12-06 14:49:11 +00:00