Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,9 @@ 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', 'root'),
|
| 18 |
'password': os.getenv('DB_PASSWORD', ''),
|
| 19 |
'port': int(os.getenv('DB_PORT', 3306))
|
|
@@ -34,13 +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 db_name:
|
| 38 |
config['database'] = db_name
|
| 39 |
try:
|
| 40 |
conn = mysql.connector.connect(**config)
|
| 41 |
return conn, None
|
| 42 |
except Error as e:
|
| 43 |
-
return None, f"Database connection failed: {str(e)}"
|
| 44 |
|
| 45 |
def parse_sql_file(file_content):
|
| 46 |
"""Parse SQL file to extract database name and clean statements."""
|
|
|
|
| 11 |
app.secret_key = os.urandom(24) # Required for session management
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# Default database configuration from .env (no localhost default)
|
| 15 |
default_db_config = {
|
| 16 |
+
'host': os.getenv('DB_HOST', ''),
|
| 17 |
'user': os.getenv('DB_USER', 'root'),
|
| 18 |
'password': os.getenv('DB_PASSWORD', ''),
|
| 19 |
'port': int(os.getenv('DB_PORT', 3306))
|
|
|
|
| 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, "Please configure a valid MySQL connection using the 'Configure MySQL Connection' modal."
|
| 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 and accessible, and check your credentials."
|
| 46 |
|
| 47 |
def parse_sql_file(file_content):
|
| 48 |
"""Parse SQL file to extract database name and clean statements."""
|