lemmy/migrations/2024-12-02-181601_add_report_combined_table/up.sql
Dessalines 10f5e373cf
Update migrations/2024-12-02-181601_add_report_combined_table/up.sql
Co-authored-by: dullbananas <dull.bananas0@gmail.com>
2024-12-08 08:45:49 -05:00

42 lines
1.2 KiB
SQL

-- Creates combined tables for
-- Reports: (comment, post, and private_message)
CREATE TABLE report_combined (
id serial PRIMARY KEY,
published timestamptz NOT NULL,
post_report_id int UNIQUE REFERENCES post_report ON UPDATE CASCADE ON DELETE CASCADE,
comment_report_id int UNIQUE REFERENCES comment_report ON UPDATE CASCADE ON DELETE CASCADE,
private_message_report_id int UNIQUE REFERENCES private_message_report ON UPDATE CASCADE ON DELETE CASCADE,
-- Make sure only one of the columns is not null
CHECK (num_nonnulls (post_report_id, comment_report_id, private_message_report_id) = 1)
);
CREATE INDEX idx_report_combined_published ON report_combined (published DESC, id DESC);
CREATE INDEX idx_report_combined_published_asc ON report_combined (reverse_timestamp_sort (published) DESC, id DESC);
-- Updating the history
INSERT INTO report_combined (published, post_report_id, comment_report_id, private_message_report_id)
SELECT
published,
id,
NULL,
NULL
FROM
post_report
UNION ALL
SELECT
published,
NULL,
id,
NULL
FROM
comment_report
UNION ALL
SELECT
published,
NULL,
NULL,
id
FROM
private_message_report;