File size: 1,112 Bytes
dccc925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""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()