"""Database connection and query utilities.""" import psycopg2 from supabase import create_client, Client from backend.core.config import settings def get_supabase_client() -> Client | None: """Get authenticated Supabase client.""" if settings.SUPABASE_URL and settings.SUPABASE_SERVICE_KEY: return create_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_KEY) return None def get_task_data_by_id(task_id: int) -> dict | None: """ Fetch task_data JSON from tasks table. Args: task_id: The task ID to fetch Returns: task_data dict or None if not found """ if not settings.SUPABASE_DB_URL: return None conn = None try: conn = psycopg2.connect(settings.SUPABASE_DB_URL) cur = conn.cursor() cur.execute("SELECT task_data FROM tasks WHERE id = %s", (task_id,)) result = cur.fetchone() return result[0] if result else None except Exception as e: print(f"Database error fetching task {task_id}: {e}") return None finally: if conn: conn.close()