import logging import sqlite3 import threading logger = logging.getLogger(__name__) def ensure_database(db_path="trades.db"): logger.info(f"Ensuring database exists at: {db_path}") conn = sqlite3.connect(db_path, check_same_thread=False) cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS trades ( id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, symbol TEXT NOT NULL, action TEXT NOT NULL, quantity INTEGER NOT NULL, price REAL NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) """) conn.commit() logger.info("Database is ready.") db_write_lock = threading.Lock() return conn, cursor, db_write_lock