File size: 1,814 Bytes
5111c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import psycopg2
import sys
import os
from datetime import datetime

# Get the absolute path to the log directory
log_dir = os.path.dirname(os.path.abspath(__file__))
connection_log = os.path.join(log_dir, 'connection_log.txt')
error_log = os.path.join(log_dir, 'connection_error.txt')

def write_log(message, is_error=False):
    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    log_file = error_log if is_error else connection_log
    with open(log_file, 'a') as f:
        f.write(f"[{timestamp}] {message}\n")
    # Also try printing to stderr for immediate feedback
    print(message, file=sys.stderr)

def test_sync_connection():
    write_log("Starting synchronous connection test")
    try:
        write_log("Attempting to connect to database...")
        write_log(f"Using host: juycnkjuzylnbruwaqmp.supabase.co")
        
        conn = psycopg2.connect(
            dbname="postgres",
            user="postgres",
            password="Lovyelias5584.",
            host="juycnkjuzylnbruwaqmp.supabase.co",
            port="5432",
            sslmode="require",
            connect_timeout=30
        )
        
        write_log("Connected successfully!")
        
        # Test the connection
        cur = conn.cursor()
        cur.execute('SELECT version()')
        version = cur.fetchone()
        write_log(f"Database version: {version}")
        
        cur.close()
        conn.close()
        write_log("Connection closed successfully")
        return True
        
    except Exception as e:
        error_msg = f"Error connecting to database: {str(e)}"
        write_log(error_msg, is_error=True)
        return False

if __name__ == "__main__":
    success = test_sync_connection()
    if not success:
        sys.exit(1)