Add PostgreSQL production backend to eliminate SQLite lock contention.

Support DATABASE_URL with connection pooling, pg_dump backups, SQLite migration script, and deploy_postgres.sh with docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-01 08:11:42 +08:00
parent 39eac983ff
commit 52aca456e9
23 changed files with 1208 additions and 150 deletions
+11 -9
View File
@@ -9,7 +9,6 @@ from locale_fix import ensure_process_locale
ensure_process_locale()
import sqlite3
import time
import threading
import requests
@@ -57,7 +56,7 @@ from kline_store import ensure_kline_tables
from kline_stream import kline_hub, sse_format
from kline_chart import generate_review_kline_chart, fetch_market_klines, MARKET_PERIODS
from market import get_price as market_get_price, set_ths_refresh_token, get_quote_source_label
from db_conn import connect_db
from db_conn import OperationalError, connect_db, database_label, is_benign_migration_error, is_db_contention_error
from admin_settings import save_admin_credentials
from db_backup import (
backup_dir,
@@ -292,10 +291,10 @@ def get_stats_data() -> dict:
return data
try:
return refresh_stats_cache(conn, capital)
except sqlite3.OperationalError as exc:
if "locked" not in str(exc).lower():
except OperationalError as exc:
if not is_db_contention_error(exc):
raise
app.logger.warning("stats cache refresh locked, compute without save: %s", exc)
app.logger.warning("stats cache refresh contention, compute without save: %s", exc)
return build_all_stats(conn, capital)
finally:
conn.close()
@@ -373,8 +372,9 @@ def init_db():
for sql in migrations:
try:
c.execute(sql)
except sqlite3.OperationalError:
pass
except Exception as exc:
if not is_benign_migration_error(exc):
raise
c.execute('''CREATE TABLE IF NOT EXISTS review_records
(id INTEGER PRIMARY KEY AUTOINCREMENT,
open_time TEXT, close_time TEXT,
@@ -426,8 +426,9 @@ def init_db():
):
try:
c.execute(sql)
except sqlite3.OperationalError:
pass
except Exception as exc:
if not is_benign_migration_error(exc):
raise
ensure_kline_tables(conn)
init_strategy_tables(conn)
from risk.account_risk_lib import ensure_account_risk_schema
@@ -532,6 +533,7 @@ def sync_admin_from_env():
init_db()
app.logger.info("数据库: %s", database_label())
def sync_ths_token():