From 16c45275b3406a0d0928bfaaf89325023c51afb7 Mon Sep 17 00:00:00 2001 From: Ben Horowitz Date: Sat, 21 Dec 2024 17:24:02 -0800 Subject: [PATCH] remove unused DB environment variables --- src/discord-cluster-manager/bot.py | 13 +---- src/discord-cluster-manager/consts.py | 5 -- src/discord-cluster-manager/leaderboard_db.py | 50 +++---------------- 3 files changed, 7 insertions(+), 61 deletions(-) diff --git a/src/discord-cluster-manager/bot.py b/src/discord-cluster-manager/bot.py index e786c3a..7f31d81 100644 --- a/src/discord-cluster-manager/bot.py +++ b/src/discord-cluster-manager/bot.py @@ -12,11 +12,6 @@ DISCORD_DEBUG_CLUSTER_STAGING_ID, DISCORD_DEBUG_TOKEN, DISCORD_TOKEN, - POSTGRES_DATABASE, - POSTGRES_HOST, - POSTGRES_PASSWORD, - POSTGRES_PORT, - POSTGRES_USER, init_environment, ) from discord import app_commands @@ -46,13 +41,7 @@ def __init__(self, debug_mode=False): ) self.tree.add_command(self.leaderboard_group) - self.leaderboard_db = LeaderboardDB( - POSTGRES_HOST, - POSTGRES_DATABASE, - POSTGRES_USER, - POSTGRES_PASSWORD, - POSTGRES_PORT, - ) + self.leaderboard_db = LeaderboardDB() async def setup_hook(self): logger.info(f"Syncing commands for staging guild {DISCORD_CLUSTER_STAGING_ID}") diff --git a/src/discord-cluster-manager/consts.py b/src/discord-cluster-manager/consts.py index c1e16ba..4c3f9e9 100644 --- a/src/discord-cluster-manager/consts.py +++ b/src/discord-cluster-manager/consts.py @@ -49,9 +49,4 @@ class ModalGPU(Enum): GITHUB_REPO = os.getenv("GITHUB_REPO") # PostgreSQL-specific constants -POSTGRES_HOST = os.getenv("POSTGRES_HOST") -POSTGRES_DATABASE = os.getenv("POSTGRES_DATABASE") -POSTGRES_USER = os.getenv("POSTGRES_USER") -POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD") -POSTGRES_PORT = os.getenv("POSTGRES_PORT") DATABASE_URL = os.getenv("DATABASE_URL") diff --git a/src/discord-cluster-manager/leaderboard_db.py b/src/discord-cluster-manager/leaderboard_db.py index 40b4a25..3ca2429 100644 --- a/src/discord-cluster-manager/leaderboard_db.py +++ b/src/discord-cluster-manager/leaderboard_db.py @@ -1,46 +1,22 @@ from typing import Optional import psycopg2 -from consts import ( - DATABASE_URL, - POSTGRES_DATABASE, - POSTGRES_HOST, - POSTGRES_PASSWORD, - POSTGRES_PORT, - POSTGRES_USER, -) +from consts import DATABASE_URL from psycopg2 import Error from utils import LeaderboardItem, SubmissionItem class LeaderboardDB: - def __init__( - self, - host: str, - database: str, - user: str, - password: str, - port: str = "5432" - ): - """Initialize database connection parameters""" - self.connection_params = { - "host": host, - "database": database, - "user": user, - "password": password, - "port": port, - } + def __init__(self): self.connection: Optional[psycopg2.extensions.connection] = None self.cursor: Optional[psycopg2.extensions.cursor] = None def connect(self) -> bool: """Establish connection to the database""" try: - self.connection = ( - psycopg2.connect(DATABASE_URL, sslmode="require") - if DATABASE_URL - else psycopg2.connect(**self.connection_params) - ) + if not DATABASE_URL: + raise ValueError("DATABASE_URL is not defined.") + self.connection = psycopg2.connect(DATABASE_URL, sslmode="require") self.cursor = self.connection.cursor() return True except Error as e: @@ -170,20 +146,6 @@ def get_leaderboard_submissions( if __name__ == "__main__": - print( - POSTGRES_HOST, - POSTGRES_DATABASE, - POSTGRES_USER, - POSTGRES_PASSWORD, - POSTGRES_PORT, - ) - - leaderboard_db = LeaderboardDB( - POSTGRES_HOST, - POSTGRES_DATABASE, - POSTGRES_USER, - POSTGRES_PASSWORD, - POSTGRES_PORT, - ) + leaderboard_db = LeaderboardDB() leaderboard_db.connect() leaderboard_db.disconnect()