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

72 lines
2.1 KiB
MySQL
Raw Permalink Normal View History

-- Creates combined tables for
-- person_content: (comment, post)
-- person_saved: (comment, post)
CREATE TABLE person_content_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
2024-12-08 14:27:29 +00:00
CHECK (num_nonnulls (post_id, comment_id) = 1)
);
CREATE INDEX idx_person_content_combined_published ON person_content_combined (published DESC, id DESC);
CREATE INDEX idx_person_content_combined_published_asc ON person_content_combined (reverse_timestamp_sort (published) DESC, id DESC);
-- Updating the history
2024-12-08 14:27:29 +00:00
INSERT INTO person_content_combined (published, post_id, comment_id)
SELECT
published,
2024-12-08 14:27:29 +00:00
id,
NULL::int
FROM
2024-12-08 14:27:29 +00:00
post
UNION ALL
SELECT
published,
2024-12-08 14:27:29 +00:00
NULL::int,
id
FROM
comment;
-- This one is special, because you use the saved date, not the ordinary published
CREATE TABLE person_saved_combined (
id serial PRIMARY KEY,
published timestamptz NOT NULL,
person_id int NOT NULL REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE,
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
2024-12-08 14:27:29 +00:00
CHECK (num_nonnulls (post_id, comment_id) = 1)
);
CREATE INDEX idx_person_saved_combined_published ON person_saved_combined (published DESC, id DESC);
CREATE INDEX idx_person_saved_combined_published_asc ON person_saved_combined (reverse_timestamp_sort (published) DESC, id DESC);
CREATE INDEX idx_person_saved_combined ON person_saved_combined (person_id);
-- Updating the history
2024-12-08 14:27:29 +00:00
INSERT INTO person_saved_combined (published, person_id, post_id, comment_id)
SELECT
saved,
person_id,
2024-12-08 14:27:29 +00:00
post_id,
NULL::int
FROM
post_actions
WHERE
2024-12-08 14:27:29 +00:00
saved IS NOT NULL
UNION ALL
SELECT
saved,
person_id,
2024-12-08 14:27:29 +00:00
NULL::int,
comment_id
FROM
comment_actions
WHERE
saved IS NOT NULL;