This commit is contained in:
aaron 2026-05-14 22:17:21 +08:00
parent 4f025c3736
commit 13ffe63dbd

View File

@ -35,9 +35,10 @@ class Database:
@contextmanager @contextmanager
def connect(self) -> Iterator[sqlite3.Connection]: def connect(self) -> Iterator[sqlite3.Connection]:
conn = sqlite3.connect(self.path) conn = sqlite3.connect(self.path, timeout=30)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA foreign_keys = ON")
conn.execute("PRAGMA busy_timeout = 30000")
try: try:
yield conn yield conn
conn.commit() conn.commit()
@ -136,13 +137,14 @@ class Database:
conn.execute( conn.execute(
"ALTER TABLE routing_rules ADD COLUMN card_body_template TEXT NOT NULL DEFAULT '{{symbol}} {{timeframe}} {{strategy}} {{action}} @ {{price}}'" "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() now = now_iso()
if not admin: conn.execute(
now = now_iso() """
conn.execute( INSERT OR IGNORE INTO admin_settings (id, password_hash, created_at, updated_at)
"INSERT INTO admin_settings (id, password_hash, created_at, updated_at) VALUES (1, ?, ?, ?)", VALUES (1, ?, ?, ?)
(hash_password(settings.admin_password), now, now), """,
) (hash_password(settings.admin_password), now, now),
)
def cleanup_old_logs(self, retention_days: int) -> int: def cleanup_old_logs(self, retention_days: int) -> int:
cutoff = (datetime.now(UTC) - timedelta(days=retention_days)).replace(microsecond=0).isoformat() cutoff = (datetime.now(UTC) - timedelta(days=retention_days)).replace(microsecond=0).isoformat()