Skip to content

Commit

Permalink
Begin writing user notification query
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed Jan 30, 2024
1 parent 6860288 commit 4792095
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
32 changes: 16 additions & 16 deletions notifier/database/migrations/007-split-post-context.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ CREATE TABLE notifiable_post (
UNIQUE (post_id)
);

CREATE TABLE context_thread (
thread_id VARCHAR(20) NOT NULL,
thread_created_timestamp INT UNSIGNED NOT NULL,

thread_title VARCHAR(200) NOT NULL,
thread_snippet VARCHAR(200) NOT NULL,
thread_creator_username VARCHAR(20),

first_post_id VARCHAR(20) NOT NULL,
first_post_author_user_id VARCHAR(20) NOT NULL,
first_post_author_username VARCHAR(20) NOT NULL,
first_post_created_timestamp INT UNSIGNED NOT NULL,

UNIQUE (thread_id)
);

CREATE TABLE context_wiki (
wiki_id VARCHAR(20) NOT NULL,
wiki_name VARCHAR(200) NOT NULL,
Expand All @@ -64,6 +48,22 @@ CREATE TABLE context_forum_category (
UNIQUE (category_id)
);

CREATE TABLE context_thread (
thread_id VARCHAR(20) NOT NULL,
thread_created_timestamp INT UNSIGNED NOT NULL,

thread_title VARCHAR(200) NOT NULL,
thread_snippet VARCHAR(200) NOT NULL,
thread_creator_username VARCHAR(20),

first_post_id VARCHAR(20) NOT NULL,
first_post_author_user_id VARCHAR(20) NOT NULL,
first_post_author_username VARCHAR(20) NOT NULL,
first_post_created_timestamp INT UNSIGNED NOT NULL,

UNIQUE (thread_id)
);

CREATE TABLE context_parent_post (
post_id VARCHAR(20) NOT NULL,
posted_timestamp INT UNSIGNED NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- Pre-joins posts to context tables to avoid repeating that for each user queried
CREATE TEMPORARY TABLE IF NOT EXISTS post_with_context
WITH cte AS (
SELECT
Expand Down
50 changes: 50 additions & 0 deletions notifier/database/queries/get_notifiable_posts_for_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
SELECT
post.post_id AS id,
post.posted_timestamp AS posted_timestamp,
post.post_title AS title,
post.post_snippet AS snippet,
post.author_username AS username,

context_wiki.wiki_id AS wiki_id,
context_wiki.wiki_name AS wiki_name,
context_wiki.wiki_uses_https AS wiki_secure,

context_forum_category.category_id AS category_id,
context_forum_category.category_name AS category_name,

context_thread.thread_id AS thread_id,
context_thread.thread_created_timestamp AS thread_timestamp,
context_thread.thread_title AS thread_title,
context_thread.thread_creator_username AS thread_creator,

context_parent_post.post_id AS parent_post_id,
context_parent_post.posted_timestamp AS parent_posted_timestamp,
context_parent_post.post_title AS parent_title,
context_parent_post.author_username AS parent_username

FROM
notifiable_post AS post

LEFT JOIN context_wiki
ON context_wiki.wiki_id = post.context_wiki_id

LEFT JOIN context_forum_category
ON context_forum_category.category_id = post.context_forum_category_id

LEFT 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 made by the user
post.author_user_id <> %(user_id)s

-- Remove posts from before the user was last notified
AND post.posted_timestamp >= %(lower_timestamp)s

-- Remove posts from after the start of this run
AND post.posted_timestamp <= %(upper_timestamp)s


0 comments on commit 4792095

Please sign in to comment.