feat(db): Add view for ordering thread index by most recent post
This implements the classic thread ordering expected by most forum users (i.e. the thread with the most recent post is at the top).
This commit is contained in:
parent
cf64826e4e
commit
f10bd20276
6 changed files with 45 additions and 7 deletions
1
migrations/2018-04-14-145711_create_index_view/down.sql
Normal file
1
migrations/2018-04-14-145711_create_index_view/down.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
DROP VIEW thread_index;
|
||||
15
migrations/2018-04-14-145711_create_index_view/up.sql
Normal file
15
migrations/2018-04-14-145711_create_index_view/up.sql
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
-- Create a simple view that returns the list of threads ordered by
|
||||
-- the last post that occured in the thread.
|
||||
|
||||
CREATE VIEW thread_index AS
|
||||
SELECT t.id AS thread_id,
|
||||
t.title AS title,
|
||||
t.author_name AS author_name,
|
||||
t.posted AS posted,
|
||||
p.id AS post_id
|
||||
FROM threads t
|
||||
JOIN (SELECT DISTINCT ON (thread_id) id, thread_id
|
||||
FROM posts
|
||||
ORDER BY thread_id, id DESC) AS p
|
||||
ON t.id = p.thread_id
|
||||
ORDER BY p.id DESC;
|
||||
Loading…
Add table
Add a link
Reference in a new issue