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/utils/database.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'cogs/utils/database.py') diff --git a/cogs/utils/database.py b/cogs/utils/database.py index 67cbc9f..ea7d356 100644 --- a/cogs/utils/database.py +++ b/cogs/utils/database.py @@ -1,6 +1,7 @@ import os import sqlite3 import logging +from contextlib import contextmanager logger = logging.getLogger(__name__) @@ -10,7 +11,8 @@ _initialized = False def get_connection(): - return sqlite3.connect(get_db_path()) + conn = sqlite3.connect(get_db_path()) + return _ConnectionContextManager(conn) def get_db_path(): @@ -22,6 +24,19 @@ def set_db_path(path): DB_PATH = path +class _ConnectionContextManager: + """Context manager for sqlite3 connections that auto-closes.""" + def __init__(self, conn): + self.conn = conn + + def __enter__(self): + return self.conn + + def __exit__(self, exc_type, exc_val, exc_tb): + self.conn.close() + return False + + def init_database(): global _initialized if _initialized: -- cgit v1.2.3