Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions lib/db-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class DatabaseStore {
ON github_users(stale_after)
WHERE country IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS idx_github_users_username_lower
ON github_users(LOWER(username));

CREATE TABLE IF NOT EXISTS leaderboard_calculation (
country_slug VARCHAR(100) PRIMARY KEY,
country_title TEXT NOT NULL DEFAULT '',
Expand Down Expand Up @@ -193,7 +196,8 @@ export class DatabaseStore {
$8, $9, $10, $11,
NOW(), NOW() + ($12 || ' days')::INTERVAL, NOW()
)
ON CONFLICT (username) DO UPDATE SET
ON CONFLICT (LOWER(username)) DO UPDATE SET
username = EXCLUDED.username,
name = EXCLUDED.name,
avatar_url = EXCLUDED.avatar_url,
location = EXCLUDED.location,
Expand Down Expand Up @@ -251,8 +255,13 @@ export class DatabaseStore {
): Promise<GitHubUserRow[]> {
const client = getPool();
const result = await client.query(
`SELECT * FROM github_users
WHERE country = $1
`SELECT *
FROM (
SELECT DISTINCT ON (LOWER(username)) *
FROM github_users
WHERE country = $1
ORDER BY LOWER(username), final_score DESC, updated_at DESC
) deduped_users
ORDER BY final_score DESC
LIMIT $2`,
[country, limit],
Expand All @@ -263,7 +272,7 @@ export class DatabaseStore {
async getLeaderboardCount(country: string): Promise<number> {
const client = getPool();
const result = await client.query(
"SELECT COUNT(*) FROM github_users WHERE country = $1",
"SELECT COUNT(DISTINCT LOWER(username)) FROM github_users WHERE country = $1",
[country],
);
return Number(result.rows[0].count);
Expand All @@ -279,8 +288,13 @@ export class DatabaseStore {
): Promise<GitHubUserRow[]> {
const client = getPool();
const result = await client.query(
`SELECT * FROM github_users
WHERE country = $1 AND stale_after < NOW()
`SELECT *
FROM (
SELECT DISTINCT ON (LOWER(username)) *
FROM github_users
WHERE country = $1 AND stale_after < NOW()
ORDER BY LOWER(username), final_score DESC, updated_at DESC
) deduped_users
ORDER BY final_score DESC
LIMIT $2`,
[country, limit],
Expand All @@ -298,8 +312,13 @@ export class DatabaseStore {
): Promise<GitHubUserRow[]> {
const client = getPool();
const result = await client.query(
`SELECT * FROM github_users
WHERE country = $1
`SELECT *
FROM (
SELECT DISTINCT ON (LOWER(username)) *
FROM github_users
WHERE country = $1
ORDER BY LOWER(username), final_score DESC, updated_at DESC
) deduped_users
ORDER BY final_score DESC
LIMIT $2`,
[country, limit],
Expand Down Expand Up @@ -329,4 +348,4 @@ export function getDatabaseStore(): DatabaseStore {
defaultStore = new DatabaseStore();
}
return defaultStore;
}
}
Loading