From dba3388567818cb5c60be9d406d3e1cccc84b3ea Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 29 Apr 2026 14:22:03 +0200 Subject: Refactor database connections and add integration tests - Use context managers for sqlite3 connections in cron, vclogs, and database utils - Add URL helper utility for yt-dlp with clean_url_for_ytdlp function - Add slow integration test markers and new integration test files - Fix test imports to use conftest directly instead of tests.conftest - Improve downloader error handling and use COPYPARTY_URL_UPLOAD env var - Update pytest.ini with slow test markers and warning filters - Document slow test usage in AGENTS.md --- cogs/vclogs.py | 55 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'cogs/vclogs.py') diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 404ffe0..e5ca202 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -23,11 +23,10 @@ init_database() def load_activity_data(): try: - conn = get_connection() - c = conn.cursor() - c.execute("SELECT user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name FROM voice_activity") - rows = c.fetchall() - conn.close() + with get_connection() as conn: + c = conn.cursor() + c.execute("SELECT user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name FROM voice_activity") + rows = c.fetchall() return { row[0]: { @@ -48,30 +47,28 @@ def load_activity_data(): def save_activity_data(data): try: - conn = get_connection() - c = conn.cursor() - c.execute("PRAGMA foreign_keys = ON") - - c.execute("DELETE FROM voice_activity") - - for user_id, user_data in data.items(): - c.execute( - """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name) - VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", - ( - user_id, - user_data.get("join_time"), - user_data.get("total_active", 0), - user_data.get("total_afk", 0), - int(user_data.get("is_afk", False)), - user_data.get("afk_start"), - user_data.get("active_start"), - user_data.get("channel_name"), - ), - ) - - conn.commit() - conn.close() + with get_connection() as conn: + c = conn.cursor() + c.execute("PRAGMA foreign_keys = ON") + + c.execute("DELETE FROM voice_activity") + + for user_id, user_data in data.items(): + c.execute( + """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", + ( + user_id, + user_data.get("join_time"), + user_data.get("total_active", 0), + user_data.get("total_afk", 0), + int(user_data.get("is_afk", False)), + user_data.get("afk_start"), + user_data.get("active_start"), + user_data.get("channel_name"), + ), + ) + conn.commit() logger.info(f"Saved voice activity for {len(data)} users") except Exception as e: logger.error(f"Error saving voice activity: {e}") -- cgit v1.2.3