discourse: consolidate parallel `MAX` queries (#1296)

Summary:
As mentioned in <https://github.com/sourcecred/sourcecred/pull/1266#discussion_r312345441>.

Test Plan:
Running `yarn test` passes.

wchargin-branch: discourse-single-max-query
This commit is contained in:
William Chargin 2019-08-17 03:58:55 -07:00 committed by Dandelion Mané
parent b50ba67797
commit 64f282fff2
1 changed files with 9 additions and 11 deletions

View File

@ -216,17 +216,15 @@ export class Mirror implements DiscourseData {
async update() {
const db = this._db;
const latestTopicId = await this._fetcher.latestTopicId();
const lastLocalPostId =
db
.prepare("SELECT MAX(id) FROM posts")
.pluck()
.get() || 0;
const lastLocalTopicId =
db
.prepare("SELECT MAX(id) FROM topics")
.pluck()
.get() || 0;
const {max_post: lastLocalPostId, max_topic: lastLocalTopicId} = db
.prepare(
dedent`\
SELECT
(SELECT IFNULL(MAX(id), 0) FROM posts) AS max_post,
(SELECT IFNULL(MAX(id), 0) FROM topics) AS max_topic
`
)
.get();
const encounteredPostIds = new Set();