Skip to content

Commit aa94367

Browse files
Fix Flask app: Replace MySQL with PostgreSQL (psycopg2)
1 parent 797e25a commit aa94367

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

flask-api/app_with_db.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
2-
FPL Team Optimization API with MySQL Database Integration
2+
FPL Team Optimization API with PostgreSQL Database Integration
33
=======================================================
44
55
A comprehensive Fantasy Premier League team optimization API using enhanced ML models,
6-
FPL-Elo-Insights data, and MySQL database for user management and data persistence.
6+
FPL-Elo-Insights data, and PostgreSQL database for user management and data persistence.
77
88
Features:
99
- Real-time FPL data integration
1010
- Enhanced ML model with Elo-Insights data
11-
- MySQL database integration
11+
- PostgreSQL database integration
1212
- User authentication and management
1313
- Team management with persistence
1414
- Transfer suggestions
@@ -30,8 +30,8 @@
3030
import time
3131
import logging
3232
from datetime import datetime
33-
import mysql.connector
34-
from mysql.connector import Error
33+
import psycopg2
34+
from psycopg2 import Error
3535
import hashlib
3636
import jwt
3737
from functools import wraps
@@ -51,10 +51,10 @@
5151
# Database Configuration from env vars
5252
DB_CONFIG = {
5353
'host': os.getenv('DB_HOST', 'localhost'),
54-
'database': os.getenv('DB_NAME', 'fantasy_soccer'),
55-
'user': os.getenv('DB_USER', 'root'),
56-
'password': os.getenv('DB_PASSWORD', 'NewPassword'),
57-
'port': int(os.getenv('DB_PORT', '3306'))
54+
'database': os.getenv('DB_NAME', 'postgres'),
55+
'user': os.getenv('DB_USER', 'postgres'),
56+
'password': os.getenv('DB_PASSWORD'),
57+
'port': int(os.getenv('DB_PORT', '5432'))
5858
}
5959

6060
# FPL API Configuration
@@ -79,12 +79,19 @@ class Config:
7979
feature_columns = []
8080

8181
def get_db_connection():
82-
"""Get MySQL database connection"""
82+
"""Get PostgreSQL database connection"""
8383
try:
84-
connection = mysql.connector.connect(**DB_CONFIG)
84+
connection = psycopg2.connect(
85+
host=DB_CONFIG['host'],
86+
port=DB_CONFIG['port'],
87+
user=DB_CONFIG['user'],
88+
password=DB_CONFIG['password'],
89+
database=DB_CONFIG['database'],
90+
sslmode='require'
91+
)
8592
return connection
8693
except Error as e:
87-
logger.error(f"Error connecting to MySQL: {e}")
94+
logger.error(f"Error connecting to PostgreSQL: {e}")
8895
return None
8996

9097
def load_ml_model():
@@ -481,7 +488,7 @@ def add_player_to_team(current_user_id):
481488
def health_check():
482489
"""Health check endpoint"""
483490
return jsonify({
484-
'message': 'FPL Team Optimization API with MySQL Database',
491+
'message': 'FPL Team Optimization API with PostgreSQL Database',
485492
'status': 'healthy',
486493
'version': '3.0.0',
487494
'model_loaded': ml_model is not None,
@@ -859,7 +866,7 @@ def internal_error(error):
859866
if __name__ == '__main__':
860867
# Load ML model on startup
861868
if load_ml_model():
862-
logger.info("🚀 FPL Team Optimization API with MySQL starting...")
869+
logger.info("🚀 FPL Team Optimization API with PostgreSQL starting...")
863870
app.run(debug=True, host='0.0.0.0', port=5001)
864871
else:
865872
logger.error("❌ Failed to load ML model. API cannot start.")

0 commit comments

Comments
 (0)