File size: 4,666 Bytes
f1df910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4767f1
 
f1df910
a4767f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1df910
 
 
 
 
 
 
 
 
 
 
 
 
a4767f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1df910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4767f1
 
 
 
 
 
 
 
 
 
 
 
f1df910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4767f1
 
 
 
 
 
 
 
 
 
 
 
f1df910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import logging
from dotenv import load_dotenv

load_dotenv()

# Import PostgreSQL (no MySQL)
try:
    import psycopg2
    from psycopg2 import pool
except ImportError:
    psycopg2 = None
    print("ERROR: psycopg2-binary not installed")

# Database Configuration (Neon Postgres - Global)
# Prioritize DATABASE_URL if available (standard for Neon/Heroku/Railway)
db_url = os.getenv("DATABASE_URL")

if db_url:
    # Use URL directly
    db_config = {"dsn": db_url, "sslmode": "require"}
else:
    # Use individual components as fallback
    db_host = os.getenv("DATABASE_HOST", "localhost")
    db_user = os.getenv("DATABASE_USER", "user")
    db_pass = os.getenv("DATABASE_PASSWORD", "password") 
    db_port = int(os.getenv("DATABASE_PORT", "5432"))
    db_name = os.getenv("DATABASE_NAME", "neon")
    
    db_config = {
        "host": db_host,
        "user": db_user,
        "password": db_pass,
        "port": db_port,
        "database": db_name,
        "sslmode": "require"
    }

# Global connection pool
db_pool = None
last_error = None

def init_pool():
    """Initialize PostgreSQL connection pool"""
    global db_pool
    try:
        if not psycopg2:
            print("WARNING: psycopg2 not available, pool not initialized")
            return
            
        if "dsn" in db_config:
            db_pool = psycopg2.pool.SimpleConnectionPool(
                1, 10,
                dsn=db_config["dsn"],
                connect_timeout=10
            )
        else:
            db_pool = psycopg2.pool.SimpleConnectionPool(
                1, 10,
                host=db_config["host"],
                database=db_config["database"],
                user=db_config["user"],
                password=db_config["password"],
                port=db_config["port"],
                sslmode='require',
                connect_timeout=10
            )
        print("βœ… PostgreSQL Connection Pool initialized")
    except Exception as e:
        print(f"❌ Pool Init Error: {e}")

def get_db_connection():
    """Get PostgreSQL database connection"""
    global last_error
    try:
        if not psycopg2:
            raise ImportError("psycopg2 module not found")
        
        # Try to get from pool first
        if db_pool:
            try:
                return db_pool.getconn()
            except:
                pass
        
        # Fallback to direct connection
        if "dsn" in db_config:
            conn = psycopg2.connect(dsn=db_config["dsn"], connect_timeout=10)
        else:
            conn = psycopg2.connect(
                host=db_config["host"],
                database=db_config["database"],
                user=db_config["user"],
                password=db_config["password"],
                port=db_config["port"],
                sslmode='require',
                connect_timeout=10
            )
        return conn
        
    except Exception as e:
        last_error = str(e)
        print(f"❌ DATABASE CONNECTION ERROR: {e}")
        return None

def init_db():
    """Initialize PostgreSQL database schema"""
    logger = logging.getLogger("StrengerPro")
    logger.info("--- PostgreSQL Database Sync ---")
    
    try:
        if not psycopg2:
            logger.error("❌ psycopg2-binary not installed")
            return
        
        # Connect to database
        if "dsn" in db_config:
            conn = psycopg2.connect(dsn=db_config["dsn"], connect_timeout=10)
        else:
            conn = psycopg2.connect(
                host=db_config["host"],
                database=db_config["database"],
                user=db_config["user"],
                password=db_config["password"],
                port=db_config["port"],
                sslmode='require',
                connect_timeout=10
            )
        
        cursor = conn.cursor()
        
        # Load and execute schema
        schema_file = "schema_pg.sql"
        if os.path.exists(schema_file):
            logger.info(f"Loading schema from {schema_file}")
            with open(schema_file, "r") as f:
                schema = f.read()
                cursor.execute(schema)
            conn.commit()
            logger.info("βœ… PostgreSQL schema initialized")
        else:
            logger.warning(f"⚠️ {schema_file} not found - skipping schema sync")
        
        cursor.close()
        conn.close()
        
        # Initialize connection pool
        init_pool()
        
        logger.info("βœ… Database initialization complete")
        
    except Exception as e:
        logger.error(f"❌ CRITICAL DB INIT ERROR: {e}")
        print(f"❌ Database init failed: {e}")