Skip to content

Commit

Permalink
Move migration filter into delete query
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed Feb 7, 2024
1 parent 7fb83c4 commit 70e0158
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 63 deletions.
63 changes: 0 additions & 63 deletions notifier/database/migrations/008-split-post-context.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,69 +108,6 @@ FROM
category ON category.id = thread.category_id
LEFT JOIN
post AS parent_post ON parent_post.id = post.parent_post_id
WHERE
-- Remove would-be-notifiable posts older than 1 year
post.posted_timestamp > ((
SELECT MAX(posted_timestamp) FROM post
) - (60 * 60 * 24 * 365))
-- Select posts for which exists a user config that is going to be notified about it
AND EXISTS (
SELECT NULL FROM
user_config
WHERE (
-- Filter out users who would already have been notified about this post, regardless of their user config
user_config.notified_timestamp < post.posted_timestamp
-- Filter out users who are manually unsubscribed to replies to this post's parent OR to this post's thread
AND NOT EXISTS (
SELECT NULL FROM
manual_sub
WHERE
manual_sub.user_id = user_config.user_id
AND manual_sub.thread_id = thread.id
AND (
manual_sub.post_id = parent_post.id
OR manual_sub.post_id IS NULL
)
AND manual_sub.sub = -1
)
-- Filter out posts made by the current user
AND post.user_id <> user_config.user_id
-- Filter out users with chosen frequency outside of defined list, e.g. 'never', who are effectively removed from the service
AND user_config.frequency IN (
"hourly", "8hourly", "daily", "weekly", "monthly"
)
-- Filter out posts that the recipient has already responded to, as while the extant query no longer has this filter, it will already have been applied to old posts in the database
AND NOT EXISTS (
SELECT NULL FROM
post AS user_response_child_post
WHERE
user_response_child_post.user_id = user_config.user_id
AND user_response_child_post.parent_post_id = post.id
)

-- Find users who are waiting to be notified about this post
AND (
-- Find a user who is manually subscribed to replies to this post's parent OR to this post's thread
EXISTS (
SELECT NULL FROM
manual_sub
WHERE
manual_sub.user_id = user_config.user_id
AND manual_sub.thread_id = thread.id
AND (
manual_sub.post_id = parent_post.id
OR manual_sub.post_id IS NULL
)
AND manual_sub.sub = 1
)
-- Find a user who is automatically subscribed to the thread
OR first_post_in_thread.user_id = user_config.user_id
-- Find a user who is automatically subscribed to the post's parent
OR parent_post.user_id = user_config.user_id
)
)
);

SELECT "notifiable_post", COUNT(*) FROM notifiable_post;

-- 2. Context tables
Expand Down
69 changes: 69 additions & 0 deletions notifier/database/queries/delete_non_notifiable_posts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- Purge posts that are no longer considered notifiable

DELETE
notifiable_post
FROM
notifiable_post AS post

INNER JOIN context_thread
ON context_thread.thread_id = post.context_thread_id

LEFT JOIN context_parent_post
ON context_parent_post.post_id = post.context_parent_post_id
WHERE
-- Remove posts older than 1 year, no matter what
post.posted_timestamp < ((
SELECT MAX(posted_timestamp) FROM post
) - (60 * 60 * 24 * 365))

-- Remove posts for which there exist 0 users who will be notified about it
OR NOT EXISTS (
-- Attempt to find a user who will be notified by this post
SELECT NULL FROM
user_config

LEFT JOIN manual_sub AS thread_sub
ON thread_sub.user_id = user_config.user_id
AND thread_sub.thread_id = notifiable_post.context_thread_id
AND thread_sub.post_id IS NULL

LEFT JOIN manual_sub AS post_sub
ON post_sub.user_id = user_config.user_id
AND post_sub.thread_id = notifiable_post.context_thread_id
AND post_sub.post_id = notifiable_post.parent_post_id

WHERE (
-- Include only users with chosen frequency in a defined list - other users e.g. those on the 'never' frequency are effectively unsubscribed
user_config.frequency IN (
"hourly", "8hourly", "daily", "weekly", "monthly"
)

-- Users are not notified about their own posts
AND post.author_user_id <> user_config.user_id

-- Only users last notified before this post was posted
AND post.posted_timestamp > user_config.notified_timestamp

-- Filter out users unsubscribed to this post
AND (
thread_sub.sub IS NULL OR thread_sub.sub = 1
-- Post reply overrides thread unsubscription
OR post_sub.sub = 1
OR post_with_context.parent_post_user_id = user_config.user_id
)
AND (post_sub.sub IS NULL OR post_sub.sub = 1)
)

-- Only users subscribed to this post
AND (
-- Posts in threads started by the user
context_thread.first_post_author_user_id = user_config.user_id

-- Replies to posts made by the user
OR context_parent_post.author_user_id = user_config.user_id

-- Manual subscriptions
OR thread_sub.sub = 1
OR post_sub.sub = 1
)
);

0 comments on commit 70e0158

Please sign in to comment.