lemmy/migrations/2024-12-08-165614_add_modlog_combined_table/up.sql
Dessalines 9c1347c7a0
Adding Combined modlog (#5253)
* Combined tables try 2

* Finishing up combined report table.

* Fix ts optionals.

* Adding tests, triggers, and history updates for report_combined.

* Adding profile.

* Add cursor pagination to report_combined view (#5244)

* add pagination cursor

* store timestamp instead of id in cursor (partial)

* Revert "store timestamp instead of id in cursor (partial)"

This reverts commit 89359dde4bc5fee39fdd2840828330f398444a36.

* use paginated query builder

* Fixing migration and paged API.

* Using dullbananas trigger procedure

* Removing pointless list routes, reorganizing tests.

* Fixing column XOR check.

* Forgot to remove list report actions.

* Cleanup.

* Use internal tagging.

* Fixing api tests.

* Adding a few indexes.

* Fixing migration name.

* Fixing unique constraints.

* Addressing PR comments.

* Start working on profile combined

* Adding views and replaceable schema.

* A few changes to profile view.

- Separating the profile fetch from its combined content fetch.
- Starting to separate saved_only into its own combined view.

* Finishing up combined person_saved and person_content.

* Fixing api tests.

* Moving to api-v4 routes.

* Fixing imports.

* Update crates/db_views/src/report_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update crates/db_views/src/report_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update crates/db_views/src/report_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update migrations/2024-12-02-181601_add_report_combined_table/up.sql

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update migrations/2024-12-02-181601_add_report_combined_table/up.sql

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Fixing import and fmt.

* Fixing null types in postgres.

* Comment out err.

* Fixing TS issues.

* Adding types, fixing allow and blocklist crud.

* Starting to work on combined views.

* Using dullbananas trigger procedure

* Adding the full combined view queries.

* Adding tests.

* taplo fmt.

* Upgrading package.json deps.

* Updating pnpm

* Addressing PR comments.

* Removing serialization

* Removing serialization

* Fixing duped trigger.

* Remove saved_only test.

* Remove pointless post_tags types.

* Remove pointless index.

* Changing published to saved for person_saved_combined.

* Removing comment.

* Renaming modlog when_ columns to published.

- Fixes #5312

* Adding strum and simplifying imports.

* Avoiding clone in map_to_enum

* Changing modded_person to other_person.

* Update crates/db_views_moderator/src/modlog_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update crates/db_views_moderator/src/modlog_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Update crates/db_views_moderator/src/modlog_combined_view.rs

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

* Addressing PR comments.

* Fixing split.

* Revert "Adding strum and simplifying imports."

This reverts commit 15f167110721429dd6e465f522250c8beb3d4dd7.

* Running fmt.

* Using assert + matches instead of filter_map.

* Adding listPersonContent check.

---------

Co-authored-by: dullbananas <dull.bananas0@gmail.com>
2025-01-14 15:14:58 +01:00

201 lines
6 KiB
SQL

-- First, rename all the when_ columns on the modlog to published
ALTER TABLE admin_allow_instance RENAME COLUMN when_ TO published;
ALTER TABLE admin_block_instance RENAME COLUMN when_ TO published;
ALTER TABLE admin_purge_comment RENAME COLUMN when_ TO published;
ALTER TABLE admin_purge_community RENAME COLUMN when_ TO published;
ALTER TABLE admin_purge_person RENAME COLUMN when_ TO published;
ALTER TABLE admin_purge_post RENAME COLUMN when_ TO published;
ALTER TABLE mod_add RENAME COLUMN when_ TO published;
ALTER TABLE mod_add_community RENAME COLUMN when_ TO published;
ALTER TABLE mod_ban RENAME COLUMN when_ TO published;
ALTER TABLE mod_ban_from_community RENAME COLUMN when_ TO published;
ALTER TABLE mod_feature_post RENAME COLUMN when_ TO published;
ALTER TABLE mod_hide_community RENAME COLUMN when_ TO published;
ALTER TABLE mod_lock_post RENAME COLUMN when_ TO published;
ALTER TABLE mod_remove_comment RENAME COLUMN when_ TO published;
ALTER TABLE mod_remove_community RENAME COLUMN when_ TO published;
ALTER TABLE mod_remove_post RENAME COLUMN when_ TO published;
ALTER TABLE mod_transfer_community RENAME COLUMN when_ TO published;
-- Creates combined tables for
-- modlog: (17 tables)
-- admin_allow_instance
-- admin_block_instance
-- admin_purge_comment
-- admin_purge_community
-- admin_purge_person
-- admin_purge_post
-- mod_add
-- mod_add_community
-- mod_ban
-- mod_ban_from_community
-- mod_feature_post
-- mod_hide_community
-- mod_lock_post
-- mod_remove_comment
-- mod_remove_community
-- mod_remove_post
-- mod_transfer_community
CREATE TABLE modlog_combined (
id serial PRIMARY KEY,
published timestamptz NOT NULL,
admin_allow_instance_id int UNIQUE REFERENCES admin_allow_instance ON UPDATE CASCADE ON DELETE CASCADE,
admin_block_instance_id int UNIQUE REFERENCES admin_block_instance ON UPDATE CASCADE ON DELETE CASCADE,
admin_purge_comment_id int UNIQUE REFERENCES admin_purge_comment ON UPDATE CASCADE ON DELETE CASCADE,
admin_purge_community_id int UNIQUE REFERENCES admin_purge_community ON UPDATE CASCADE ON DELETE CASCADE,
admin_purge_person_id int UNIQUE REFERENCES admin_purge_person ON UPDATE CASCADE ON DELETE CASCADE,
admin_purge_post_id int UNIQUE REFERENCES admin_purge_post ON UPDATE CASCADE ON DELETE CASCADE,
mod_add_id int UNIQUE REFERENCES mod_add ON UPDATE CASCADE ON DELETE CASCADE,
mod_add_community_id int UNIQUE REFERENCES mod_add_community ON UPDATE CASCADE ON DELETE CASCADE,
mod_ban_id int UNIQUE REFERENCES mod_ban ON UPDATE CASCADE ON DELETE CASCADE,
mod_ban_from_community_id int UNIQUE REFERENCES mod_ban_from_community ON UPDATE CASCADE ON DELETE CASCADE,
mod_feature_post_id int UNIQUE REFERENCES mod_feature_post ON UPDATE CASCADE ON DELETE CASCADE,
mod_hide_community_id int UNIQUE REFERENCES mod_hide_community ON UPDATE CASCADE ON DELETE CASCADE,
mod_lock_post_id int UNIQUE REFERENCES mod_lock_post ON UPDATE CASCADE ON DELETE CASCADE,
mod_remove_comment_id int UNIQUE REFERENCES mod_remove_comment ON UPDATE CASCADE ON DELETE CASCADE,
mod_remove_community_id int UNIQUE REFERENCES mod_remove_community ON UPDATE CASCADE ON DELETE CASCADE,
mod_remove_post_id int UNIQUE REFERENCES mod_remove_post ON UPDATE CASCADE ON DELETE CASCADE,
mod_transfer_community_id int UNIQUE REFERENCES mod_transfer_community ON UPDATE CASCADE ON DELETE CASCADE,
-- Make sure only one of the columns is not null
CHECK (num_nonnulls (admin_allow_instance_id, admin_block_instance_id, admin_purge_comment_id, admin_purge_community_id, admin_purge_person_id, admin_purge_post_id, mod_add_id, mod_add_community_id, mod_ban_id, mod_ban_from_community_id, mod_feature_post_id, mod_hide_community_id, mod_lock_post_id, mod_remove_comment_id, mod_remove_community_id, mod_remove_post_id, mod_transfer_community_id) = 1)
);
CREATE INDEX idx_modlog_combined_published ON modlog_combined (published DESC, id DESC);
-- Updating the history
-- Not doing a union all here, because there's way too many null columns
INSERT INTO modlog_combined (published, admin_allow_instance_id)
SELECT
published,
id
FROM
admin_allow_instance;
INSERT INTO modlog_combined (published, admin_block_instance_id)
SELECT
published,
id
FROM
admin_block_instance;
INSERT INTO modlog_combined (published, admin_purge_comment_id)
SELECT
published,
id
FROM
admin_purge_comment;
INSERT INTO modlog_combined (published, admin_purge_community_id)
SELECT
published,
id
FROM
admin_purge_community;
INSERT INTO modlog_combined (published, admin_purge_person_id)
SELECT
published,
id
FROM
admin_purge_person;
INSERT INTO modlog_combined (published, admin_purge_post_id)
SELECT
published,
id
FROM
admin_purge_post;
INSERT INTO modlog_combined (published, mod_add_id)
SELECT
published,
id
FROM
mod_add;
INSERT INTO modlog_combined (published, mod_add_community_id)
SELECT
published,
id
FROM
mod_add_community;
INSERT INTO modlog_combined (published, mod_ban_id)
SELECT
published,
id
FROM
mod_ban;
INSERT INTO modlog_combined (published, mod_ban_from_community_id)
SELECT
published,
id
FROM
mod_ban_from_community;
INSERT INTO modlog_combined (published, mod_feature_post_id)
SELECT
published,
id
FROM
mod_feature_post;
INSERT INTO modlog_combined (published, mod_hide_community_id)
SELECT
published,
id
FROM
mod_hide_community;
INSERT INTO modlog_combined (published, mod_lock_post_id)
SELECT
published,
id
FROM
mod_lock_post;
INSERT INTO modlog_combined (published, mod_remove_comment_id)
SELECT
published,
id
FROM
mod_remove_comment;
INSERT INTO modlog_combined (published, mod_remove_community_id)
SELECT
published,
id
FROM
mod_remove_community;
INSERT INTO modlog_combined (published, mod_remove_post_id)
SELECT
published,
id
FROM
mod_remove_post;
INSERT INTO modlog_combined (published, mod_transfer_community_id)
SELECT
published,
id
FROM
mod_transfer_community;