Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,12 +11,12 @@ app = Flask(__name__)
|
|
| 11 |
app.secret_key = os.urandom(24) # Required for session management
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
# Default database configuration from .env
|
| 15 |
default_db_config = {
|
| 16 |
-
'host': os.getenv('DB_HOST'
|
| 17 |
-
'user': os.getenv('DB_USER'
|
| 18 |
-
'password': os.getenv('DB_PASSWORD'
|
| 19 |
-
'port': int(os.getenv('DB_PORT',
|
| 20 |
}
|
| 21 |
|
| 22 |
# Groq API configuration with error handling
|
|
@@ -34,15 +34,15 @@ current_summary = {}
|
|
| 34 |
def get_db_connection(db_name=None):
|
| 35 |
"""Establish a database connection using session or default config."""
|
| 36 |
config = session.get('db_config', default_db_config).copy()
|
| 37 |
-
if not config['host'] or not config['user']:
|
| 38 |
-
return None, "
|
| 39 |
if db_name:
|
| 40 |
config['database'] = db_name
|
| 41 |
try:
|
| 42 |
conn = mysql.connector.connect(**config)
|
| 43 |
return conn, None
|
| 44 |
except Error as e:
|
| 45 |
-
return None, f"Database connection failed: {str(e)}. Ensure the MySQL server is running
|
| 46 |
|
| 47 |
def parse_sql_file(file_content):
|
| 48 |
"""Parse SQL file to extract database name and clean statements."""
|
|
@@ -308,10 +308,10 @@ def configure_db():
|
|
| 308 |
host = request.form.get('host', '').strip()
|
| 309 |
user = request.form.get('user', '').strip()
|
| 310 |
password = request.form.get('password', '')
|
| 311 |
-
port = request.form.get('port', '
|
| 312 |
|
| 313 |
if not host or not user:
|
| 314 |
-
return render_template('index.html', error="Host and user are required.", schema=current_schema, summary=current_summary)
|
| 315 |
|
| 316 |
try:
|
| 317 |
port = int(port)
|
|
@@ -327,7 +327,7 @@ def configure_db():
|
|
| 327 |
# Store in session
|
| 328 |
session['db_config'] = test_config
|
| 329 |
conn.close()
|
| 330 |
-
return render_template('index.html', error=None, schema=current_schema, summary=current_summary, success="MySQL connection configured successfully.")
|
| 331 |
|
| 332 |
if __name__ == '__main__':
|
| 333 |
app.run(host='0.0.0.0', port=int(os.getenv('PORT', 7860)), debug=False)
|
|
|
|
| 11 |
app.secret_key = os.urandom(24) # Required for session management
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# Default database configuration from .env
|
| 15 |
default_db_config = {
|
| 16 |
+
'host': os.getenv('DB_HOST'),
|
| 17 |
+
'user': os.getenv('DB_USER'),
|
| 18 |
+
'password': os.getenv('DB_PASSWORD'),
|
| 19 |
+
'port': int(os.getenv('DB_PORT', 4000)) # Default to TiDB port
|
| 20 |
}
|
| 21 |
|
| 22 |
# Groq API configuration with error handling
|
|
|
|
| 34 |
def get_db_connection(db_name=None):
|
| 35 |
"""Establish a database connection using session or default config."""
|
| 36 |
config = session.get('db_config', default_db_config).copy()
|
| 37 |
+
if not config['host'] or not config['user'] or not config['password']:
|
| 38 |
+
return None, "No valid MySQL credentials provided. Use the 'Configure MySQL Connection' modal to set up your own database, or contact the app owner to ensure default credentials are set."
|
| 39 |
if db_name:
|
| 40 |
config['database'] = db_name
|
| 41 |
try:
|
| 42 |
conn = mysql.connector.connect(**config)
|
| 43 |
return conn, None
|
| 44 |
except Error as e:
|
| 45 |
+
return None, f"Database connection failed: {str(e)}. Ensure the MySQL server is running, accessible, and credentials are correct."
|
| 46 |
|
| 47 |
def parse_sql_file(file_content):
|
| 48 |
"""Parse SQL file to extract database name and clean statements."""
|
|
|
|
| 308 |
host = request.form.get('host', '').strip()
|
| 309 |
user = request.form.get('user', '').strip()
|
| 310 |
password = request.form.get('password', '')
|
| 311 |
+
port = request.form.get('port', '4000').strip()
|
| 312 |
|
| 313 |
if not host or not user:
|
| 314 |
+
return render_template('index.html', error="Host and user are required for custom MySQL configuration.", schema=current_schema, summary=current_summary)
|
| 315 |
|
| 316 |
try:
|
| 317 |
port = int(port)
|
|
|
|
| 327 |
# Store in session
|
| 328 |
session['db_config'] = test_config
|
| 329 |
conn.close()
|
| 330 |
+
return render_template('index.html', error=None, schema=current_schema, summary=current_summary, success="Custom MySQL connection configured successfully. You can now upload .sql files and query your database.")
|
| 331 |
|
| 332 |
if __name__ == '__main__':
|
| 333 |
app.run(host='0.0.0.0', port=int(os.getenv('PORT', 7860)), debug=False)
|