Skip to content

Commit

Permalink
rename problem table to leaderboard (#64)
Browse files Browse the repository at this point in the history
* rename problem table to leaderboard
* changes to make ruff happy
  • Loading branch information
b9r5 authored Dec 21, 2024
1 parent ede66fa commit 68e3ba6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/discord-cluster-manager/leaderboard_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_leaderboard(
try:
self.cursor.execute(
"""
INSERT INTO leaderboard.problem (name, deadline, reference_code)
INSERT INTO leaderboard.leaderboard (name, deadline, reference_code)
VALUES (%s, %s, %s)
""",
(
Expand All @@ -89,9 +89,10 @@ def create_submission(self, submission: SubmissionItem):
try:
self.cursor.execute(
"""
INSERT INTO leaderboard.submission (problem_id, name, user_id,
code, submission_time, score)
VALUES ((SELECT id FROM leaderboard.problem WHERE name = %s),
INSERT INTO leaderboard.submission (leaderboard_id, name,
user_id, code, submission_time, score)
VALUES (
(SELECT id FROM leaderboard.leaderboard WHERE name = %s),
%s, %s, %s, %s, %s)
""",
(
Expand All @@ -110,7 +111,10 @@ def create_submission(self, submission: SubmissionItem):

def get_leaderboards(self) -> list[LeaderboardItem]:
self.cursor.execute(
"SELECT id, name, deadline, reference_code FROM leaderboard.problem"
"""
SELECT id, name, deadline, reference_code
FROM leaderboard.leaderboard
"""
)

return [
Expand All @@ -122,7 +126,7 @@ def get_leaderboard(self, leaderboard_name: str) -> int | None:
self.cursor.execute(
"""
SELECT id, name, deadline, reference_code
FROM leaderboard.problem
FROM leaderboard.leaderboard
WHERE name = %s
""",
(leaderboard_name,),
Expand All @@ -144,9 +148,9 @@ def get_leaderboard_submissions(
"""
SELECT s.name, s.user_id, s.code, s.submission_time, s.score
FROM leaderboard.submission s
JOIN leaderboard.problem p
ON s.problem_id = p.id
WHERE p.name = %s
JOIN leaderboard.leaderboard l
ON s.leaderboard_id = l.id
WHERE l.name = %s
ORDER BY s.score ASC
""",
(leaderboard_name,),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
This migration renames the table leaderboard.problem to leaderboard.leaderboard,
and renames associated foreign keys, indexes, and constraints.
"""

from yoyo import step

__depends__ = {'20241214_01_M62BX-drop-old-leaderboard-tables'}

steps = [
# Rename leaderboard.problem to leaderboard.leaderboard:
step("ALTER TABLE leaderboard.problem RENAME TO leaderboard"),
step("""
ALTER SEQUENCE leaderboard.problem_id_seq
RENAME TO leaderboard_id_seq
"""),
step("ALTER INDEX leaderboard.problem_pkey RENAME TO leaderboard_pkey"),
step("""
ALTER TABLE leaderboard.leaderboard
RENAME CONSTRAINT problem_name_key
TO leaderboard_name_key
"""),

# Rename a column in leaderboard.submission:
step("""
ALTER TABLE leaderboard.submission
RENAME COLUMN problem_id
TO leaderboard_id
"""),
step("""
ALTER TABLE leaderboard.submission
RENAME CONSTRAINT submission_problem_id_fkey
TO submission_leaderboard_id_fkey
"""),

# We need to update the name for the index
# leaderboard.submission_problem_id_idx. We can't just rename the index,
# because the index definition refers to the old column name, problem_id.
# This might be confusing, so we drop and re-create the index:
step("DROP INDEX leaderboard.submission_problem_id_idx"),
step("""
CREATE INDEX submission_leaderboard_id_idx
ON leaderboard.submission (leaderboard_id)
""")
]

0 comments on commit 68e3ba6

Please sign in to comment.