From 6e4b2a4cc5985d9c7339018ddfb2611b44f51802 Mon Sep 17 00:00:00 2001 From: Bill Date: Sun, 2 Nov 2025 15:46:36 -0500 Subject: [PATCH] debug: add connection-level diagnostics to trace database access Enhanced diagnostics to trace database path resolution and table existence at connection time. This will help identify if get_db_connection() is resolving paths correctly and accessing the right database file. Added diagnostics to: - get_db_connection(): Show input path, resolved path, file existence, and tables found - initialize_dev_database(): Verify tables exist after creation This will reveal whether the path resolution is working correctly or if there's a timing/caching issue with database file access. --- api/database.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api/database.py b/api/database.py index 3ebb257..ea77a27 100644 --- a/api/database.py +++ b/api/database.py @@ -32,15 +32,26 @@ def get_db_connection(db_path: str = "data/jobs.db") -> sqlite3.Connection: """ # Resolve path based on deployment mode resolved_path = get_db_path(db_path) + print(f"🔍 DIAGNOSTIC [get_db_connection]: Input path='{db_path}', Resolved path='{resolved_path}'") # Ensure data directory exists db_path_obj = Path(resolved_path) db_path_obj.parent.mkdir(parents=True, exist_ok=True) + # Check if database file exists + file_exists = db_path_obj.exists() + print(f"🔍 DIAGNOSTIC [get_db_connection]: Database file exists: {file_exists}") + conn = sqlite3.connect(resolved_path, check_same_thread=False) conn.execute("PRAGMA foreign_keys = ON") conn.row_factory = sqlite3.Row + # Verify tables exist + cursor = conn.cursor() + cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") + tables = [row[0] for row in cursor.fetchall()] + print(f"🔍 DIAGNOSTIC [get_db_connection]: Tables in database: {tables}") + return conn @@ -274,6 +285,15 @@ def initialize_dev_database(db_path: str = "data/trading_dev.db") -> None: initialize_database(db_path) print(f"🔍 DIAGNOSTIC: initialize_dev_database() COMPLETED successfully") + # Verify tables were created + print(f"🔍 DIAGNOSTIC: Verifying tables exist in {db_path}") + verify_conn = sqlite3.connect(db_path) + verify_cursor = verify_conn.cursor() + verify_cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") + tables = [row[0] for row in verify_cursor.fetchall()] + verify_conn.close() + print(f"🔍 DIAGNOSTIC: Tables found: {tables}") + def cleanup_dev_database(db_path: str = "data/trading_dev.db", data_path: str = "./data/dev_agent_data") -> None: """