Spaces:
Running
π§ Fix SQLAlchemy Session Issues for Sample Data Loading
Browse filesβ
Root Issue: SQLAlchemy session management problems with in-memory DB
β’ Scoped sessions were causing connection pool issues
β’ Thread safety problems in HF Spaces environment
β’ Missing error handling and debugging information
β‘ Complete Fix:
β’ Use SessionLocal() instead of scoped Session() for better control
β’ Added database connection test before loading sample data
β’ Enhanced error handling with full traceback logging
β’ Better debugging output showing exact counts and status
β’ Proper session management with guaranteed cleanup
π― New debugging features:
β’ test_database_connection() - verifies DB connectivity
β’ Detailed environment variable logging (SPACE_ID, etc.)
β’ Verification counts after data insertion
β’ Full exception tracebacks for troubleshooting
π Expected result:
β’ Sample data should now load correctly in HF Spaces
β’ Better error messages if something goes wrong
β’ Robust session handling prevents connection pool issues
- backend/app.py +8 -1
- backend/database/__init__.py +51 -4
|
@@ -76,7 +76,7 @@ async def startup_event():
|
|
| 76 |
# ποΈ Initialize database on startup
|
| 77 |
try:
|
| 78 |
from backend.database.init_db import init_database
|
| 79 |
-
from backend.database import init_db, add_sample_data_for_hf
|
| 80 |
|
| 81 |
init_database(reset=False, force=False)
|
| 82 |
init_db() # Create tables using SQLAlchemy
|
|
@@ -86,7 +86,11 @@ async def startup_event():
|
|
| 86 |
logger.info("π HF Spaces: Using in-memory database for user privacy")
|
| 87 |
logger.info("π Note: Data will be cleared when container restarts")
|
| 88 |
|
|
|
|
|
|
|
|
|
|
| 89 |
# Add sample data for HF Spaces
|
|
|
|
| 90 |
add_sample_data_for_hf()
|
| 91 |
else:
|
| 92 |
logger.info("πΎ Local development: Using persistent database")
|
|
@@ -94,6 +98,9 @@ async def startup_event():
|
|
| 94 |
logger.info("ποΈ Database initialized successfully")
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"β Database initialization failed: {e}")
|
|
|
|
|
|
|
|
|
|
| 97 |
# Don't fail startup - continue with empty database
|
| 98 |
|
| 99 |
logger.info("π Backend API available at: http://0.0.0.0:7860")
|
|
|
|
| 76 |
# ποΈ Initialize database on startup
|
| 77 |
try:
|
| 78 |
from backend.database.init_db import init_database
|
| 79 |
+
from backend.database import init_db, test_database_connection, add_sample_data_for_hf
|
| 80 |
|
| 81 |
init_database(reset=False, force=False)
|
| 82 |
init_db() # Create tables using SQLAlchemy
|
|
|
|
| 86 |
logger.info("π HF Spaces: Using in-memory database for user privacy")
|
| 87 |
logger.info("π Note: Data will be cleared when container restarts")
|
| 88 |
|
| 89 |
+
# Test database connection first
|
| 90 |
+
logger.info("π Testing database connection...")
|
| 91 |
+
|
| 92 |
# Add sample data for HF Spaces
|
| 93 |
+
logger.info("π Loading sample data for demonstration...")
|
| 94 |
add_sample_data_for_hf()
|
| 95 |
else:
|
| 96 |
logger.info("πΎ Local development: Using persistent database")
|
|
|
|
| 98 |
logger.info("ποΈ Database initialized successfully")
|
| 99 |
except Exception as e:
|
| 100 |
logger.error(f"β Database initialization failed: {e}")
|
| 101 |
+
import traceback
|
| 102 |
+
logger.error("Full traceback:")
|
| 103 |
+
logger.error(traceback.format_exc())
|
| 104 |
# Don't fail startup - continue with empty database
|
| 105 |
|
| 106 |
logger.info("π Backend API available at: http://0.0.0.0:7860")
|
|
@@ -60,23 +60,58 @@ def init_db():
|
|
| 60 |
"""Initialize the database by creating all tables."""
|
| 61 |
Base.metadata.create_all(bind=engine)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def add_sample_data_for_hf():
|
| 64 |
"""Add sample data for HF Spaces using SQLAlchemy."""
|
| 65 |
if not os.getenv("SPACE_ID"):
|
| 66 |
return # Only run on HF Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
import json
|
| 69 |
import uuid
|
| 70 |
import hashlib
|
| 71 |
from backend.database.models import Trace, KnowledgeGraph, Entity, Relation
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
try:
|
|
|
|
|
|
|
|
|
|
| 75 |
# Check if data already exists
|
| 76 |
existing_traces = session.query(Trace).count()
|
| 77 |
existing_kgs = session.query(KnowledgeGraph).count()
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if existing_traces > 0 or existing_kgs > 0:
|
|
|
|
| 80 |
return # Data already exists
|
| 81 |
|
| 82 |
print("π― HF Spaces: Adding sample data to in-memory database...")
|
|
@@ -296,13 +331,24 @@ def add_sample_data_for_hf():
|
|
| 296 |
# Commit all data
|
| 297 |
session.commit()
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
print("β
Sample data added successfully to HF Spaces in-memory database!")
|
| 300 |
-
print(f" β’
|
| 301 |
-
print(f" β’
|
| 302 |
-
print(f" β’
|
|
|
|
|
|
|
| 303 |
|
| 304 |
except Exception as e:
|
| 305 |
print(f"β Failed to add sample data: {e}")
|
|
|
|
|
|
|
|
|
|
| 306 |
session.rollback()
|
| 307 |
finally:
|
| 308 |
session.close()
|
|
@@ -311,6 +357,7 @@ __all__ = [
|
|
| 311 |
'get_db',
|
| 312 |
'models',
|
| 313 |
'init_db',
|
|
|
|
| 314 |
'add_sample_data_for_hf',
|
| 315 |
'save_knowledge_graph',
|
| 316 |
'update_knowledge_graph_status',
|
|
|
|
| 60 |
"""Initialize the database by creating all tables."""
|
| 61 |
Base.metadata.create_all(bind=engine)
|
| 62 |
|
| 63 |
+
def test_database_connection():
|
| 64 |
+
"""Test if database connection is working."""
|
| 65 |
+
try:
|
| 66 |
+
session = SessionLocal()
|
| 67 |
+
# Try to create tables
|
| 68 |
+
Base.metadata.create_all(bind=engine)
|
| 69 |
+
|
| 70 |
+
# Test a simple query
|
| 71 |
+
result = session.execute("SELECT 1").fetchone()
|
| 72 |
+
print(f"β
Database connection test successful: {result}")
|
| 73 |
+
|
| 74 |
+
session.close()
|
| 75 |
+
return True
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"β Database connection test failed: {e}")
|
| 78 |
+
import traceback
|
| 79 |
+
traceback.print_exc()
|
| 80 |
+
return False
|
| 81 |
+
|
| 82 |
def add_sample_data_for_hf():
|
| 83 |
"""Add sample data for HF Spaces using SQLAlchemy."""
|
| 84 |
if not os.getenv("SPACE_ID"):
|
| 85 |
return # Only run on HF Spaces
|
| 86 |
+
|
| 87 |
+
# First test database connection
|
| 88 |
+
if not test_database_connection():
|
| 89 |
+
print("β Database connection failed, skipping sample data")
|
| 90 |
+
return
|
| 91 |
|
| 92 |
import json
|
| 93 |
import uuid
|
| 94 |
import hashlib
|
| 95 |
from backend.database.models import Trace, KnowledgeGraph, Entity, Relation
|
| 96 |
|
| 97 |
+
# Use SessionLocal instead of scoped Session for better control
|
| 98 |
+
session = SessionLocal()
|
| 99 |
try:
|
| 100 |
+
# Ensure tables exist first
|
| 101 |
+
Base.metadata.create_all(bind=engine)
|
| 102 |
+
|
| 103 |
# Check if data already exists
|
| 104 |
existing_traces = session.query(Trace).count()
|
| 105 |
existing_kgs = session.query(KnowledgeGraph).count()
|
| 106 |
|
| 107 |
+
print(f"π HF Spaces environment check:")
|
| 108 |
+
print(f" β’ SPACE_ID: {os.getenv('SPACE_ID')}")
|
| 109 |
+
print(f" β’ Database URL: {DATABASE_URL}")
|
| 110 |
+
print(f" β’ Existing traces: {existing_traces}")
|
| 111 |
+
print(f" β’ Existing KGs: {existing_kgs}")
|
| 112 |
+
|
| 113 |
if existing_traces > 0 or existing_kgs > 0:
|
| 114 |
+
print("π Sample data already exists, skipping...")
|
| 115 |
return # Data already exists
|
| 116 |
|
| 117 |
print("π― HF Spaces: Adding sample data to in-memory database...")
|
|
|
|
| 331 |
# Commit all data
|
| 332 |
session.commit()
|
| 333 |
|
| 334 |
+
# Verify data was added
|
| 335 |
+
final_traces = session.query(Trace).count()
|
| 336 |
+
final_kgs = session.query(KnowledgeGraph).count()
|
| 337 |
+
final_entities = session.query(Entity).count()
|
| 338 |
+
final_relations = session.query(Relation).count()
|
| 339 |
+
|
| 340 |
print("β
Sample data added successfully to HF Spaces in-memory database!")
|
| 341 |
+
print(f" β’ Traces added: {final_traces}")
|
| 342 |
+
print(f" β’ Knowledge graphs added: {final_kgs}")
|
| 343 |
+
print(f" β’ Entities added: {final_entities}")
|
| 344 |
+
print(f" β’ Relations added: {final_relations}")
|
| 345 |
+
print(f" β’ Scenario: Multi-Agent Customer Service with error handling and optimization")
|
| 346 |
|
| 347 |
except Exception as e:
|
| 348 |
print(f"β Failed to add sample data: {e}")
|
| 349 |
+
import traceback
|
| 350 |
+
print("Full error traceback:")
|
| 351 |
+
traceback.print_exc()
|
| 352 |
session.rollback()
|
| 353 |
finally:
|
| 354 |
session.close()
|
|
|
|
| 357 |
'get_db',
|
| 358 |
'models',
|
| 359 |
'init_db',
|
| 360 |
+
'test_database_connection',
|
| 361 |
'add_sample_data_for_hf',
|
| 362 |
'save_knowledge_graph',
|
| 363 |
'update_knowledge_graph_status',
|