From 4607eaf30791f14b7624c46940fa4d08e3b02220 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Wed, 24 Feb 2021 20:28:02 -0700 Subject: [PATCH] Move ordering from views to queries.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Turns out views aren't guaranteed to keep their order ¯\_(ツ)_/¯ Fixes #24 --- db/90-views.sql | 6 ++---- src/queries.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/db/90-views.sql b/db/90-views.sql index b0d3f72..14896b0 100644 --- a/db/90-views.sql +++ b/db/90-views.sql @@ -2,8 +2,7 @@ CREATE OR REPLACE VIEW requests_vw AS SELECT url,COALESCE(videoTitle,url) AS title,displayName AS requester,imageUrl,state,score,reqTimestamp FROM requests JOIN requestMetadata USING (url) JOIN scores USING (url) - JOIN users ON requests.requester = users.userid - ORDER BY score DESC, reqTimestamp ASC; + JOIN users ON requests.requester = users.userid; CREATE OR REPLACE FUNCTION get_requests_voted(votinguserid int) RETURNS TABLE ( @@ -21,8 +20,7 @@ CREATE OR REPLACE FUNCTION get_requests_voted(votinguserid int) SELECT url,title,requester,imageUrl,state,score,reqTimestamp, (CASE WHEN votes.userid IS NULL THEN FALSE ELSE TRUE END) AS voted FROM requests_vw - LEFT JOIN votes ON (requests_vw.url = votes.requesturl AND votes.userid = votinguserid) - ORDER BY score DESC, reqTimestamp ASC; + LEFT JOIN votes ON (requests_vw.url = votes.requesturl AND votes.userid = votinguserid); $$; /* diff --git a/src/queries.ts b/src/queries.ts index 5c7c7fd..9b1712d 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -57,24 +57,26 @@ export const getRequests = { name: "getRequests", text: "SELECT * FROM requests_vw \ JOIN states ON requests_vw.state = states.state \ - WHERE active LIMIT $1" + WHERE active ORDER BY score DESC, reqTimestamp ASC LIMIT $1" } export const getRequestsVoted = { name: "getRequestsVoted", text: "SELECT * FROM get_requests_voted($2) \ JOIN states ON get_requests_voted.state = states.state \ - WHERE active LIMIT $1" + WHERE active ORDER BY score DESC, reqTimestamp ASC LIMIT $1" } export const getAllRequests = { name: "getAllRequests", - text: "SELECT * FROM requests_vw LIMIT $1" + text: "SELECT * FROM requests_vw \ + ORDER BY score DESC, reqTimestamp ASC LIMIT $1" } export const getAllRequestsVoted = { name: "getAllRequestsVoted", - text: "SELECT * FROM get_requests_voted($2) LIMIT $1" + text: "SELECT * FROM get_requests_voted($2) \ + ORDER BY score DESC, reqTimestamp ASC LIMIT $1" } export const checkRequestExists = { @@ -137,7 +139,7 @@ export const getAndLockCronJob = { text: `SELECT * FROM cron WHERE (lastSuccess + runInterval) < now() AND jobName = $1 - FOR UPDATE SKIP LOCKED;` + FOR UPDATE SKIP LOCKED` } export const updateCronJobLastSuccess = {