From 270272278c21d4d1fa292b48ea6ecfaef65fbfc3 Mon Sep 17 00:00:00 2001 From: Ben Horowitz Date: Sat, 21 Dec 2024 11:48:57 -0800 Subject: [PATCH 1/2] rename problem table to leaderboard --- src/discord-cluster-manager/leaderboard_db.py | 22 +++++---- .../20241221_01_54Oeg-rename-problem-table.py | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py diff --git a/src/discord-cluster-manager/leaderboard_db.py b/src/discord-cluster-manager/leaderboard_db.py index c25f173..40b4a25 100644 --- a/src/discord-cluster-manager/leaderboard_db.py +++ b/src/discord-cluster-manager/leaderboard_db.py @@ -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) """, ( @@ -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) """, ( @@ -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 [ @@ -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,), @@ -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,), diff --git a/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py b/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py new file mode 100644 index 0000000..4b4e2e0 --- /dev/null +++ b/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py @@ -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) + """) +] \ No newline at end of file From 1266c999453463082c8437199eca56d0924a5166 Mon Sep 17 00:00:00 2001 From: Ben Horowitz Date: Sat, 21 Dec 2024 11:52:15 -0800 Subject: [PATCH 2/2] changes to make ruff happy --- .../migrations/20241221_01_54Oeg-rename-problem-table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py b/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py index 4b4e2e0..aa6c5db 100644 --- a/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py +++ b/src/discord-cluster-manager/migrations/20241221_01_54Oeg-rename-problem-table.py @@ -23,7 +23,7 @@ # Rename a column in leaderboard.submission: step(""" - ALTER TABLE leaderboard.submission + ALTER TABLE leaderboard.submission RENAME COLUMN problem_id TO leaderboard_id """), @@ -42,4 +42,4 @@ CREATE INDEX submission_leaderboard_id_idx ON leaderboard.submission (leaderboard_id) """) -] \ No newline at end of file +]