diff --git a/app/db.py b/app/db.py index 8a5a463..39c2562 100644 --- a/app/db.py +++ b/app/db.py @@ -35,9 +35,10 @@ class Database: @contextmanager def connect(self) -> Iterator[sqlite3.Connection]: - conn = sqlite3.connect(self.path) + conn = sqlite3.connect(self.path, timeout=30) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") + conn.execute("PRAGMA busy_timeout = 30000") try: yield conn conn.commit() @@ -136,13 +137,14 @@ class Database: conn.execute( "ALTER TABLE routing_rules ADD COLUMN card_body_template TEXT NOT NULL DEFAULT '{{symbol}} {{timeframe}} {{strategy}} {{action}} @ {{price}}'" ) - admin = conn.execute("SELECT id FROM admin_settings WHERE id = 1").fetchone() - if not admin: - now = now_iso() - conn.execute( - "INSERT INTO admin_settings (id, password_hash, created_at, updated_at) VALUES (1, ?, ?, ?)", - (hash_password(settings.admin_password), now, now), - ) + now = now_iso() + conn.execute( + """ + INSERT OR IGNORE INTO admin_settings (id, password_hash, created_at, updated_at) + VALUES (1, ?, ?, ?) + """, + (hash_password(settings.admin_password), now, now), + ) def cleanup_old_logs(self, retention_days: int) -> int: cutoff = (datetime.now(UTC) - timedelta(days=retention_days)).replace(microsecond=0).isoformat()