Ensure all PG tables on init; fix migration commits per table.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-01 08:22:54 +08:00
parent e418c2dcec
commit 6abe06d935
2 changed files with 18 additions and 11 deletions
+9
View File
@@ -301,6 +301,12 @@ def get_stats_data() -> dict:
def init_db(): def init_db():
import strategy.strategy_db as strategy_db
import risk.account_risk_lib as account_risk_lib
strategy_db._TABLES_READY = False
account_risk_lib._SCHEMA_READY = False
conn = get_db() conn = get_db()
c = conn.cursor() c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT)") c.execute("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT)")
@@ -441,6 +447,9 @@ def init_db():
from recommend_store import ensure_recommend_tables from recommend_store import ensure_recommend_tables
ensure_account_risk_schema(conn) ensure_account_risk_schema(conn)
ensure_recommend_tables(conn) ensure_recommend_tables(conn)
from ai_messages import ensure_ai_messages_table
ensure_ai_messages_table(conn)
conn.commit() conn.commit()
conn.close() conn.close()
+3 -5
View File
@@ -38,14 +38,10 @@ def _pg_columns(pg_conn, table: str) -> list[str]:
def _reset_sequences(pg_conn, table: str, pk: str = "id") -> None: def _reset_sequences(pg_conn, table: str, pk: str = "id") -> None:
try:
pg_conn.execute( pg_conn.execute(
f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), " f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), "
f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)" f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)"
) )
pg_conn.commit()
except Exception:
rollback_if_postgres(pg_conn)
def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict: def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
@@ -94,8 +90,10 @@ def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
for row in rows: for row in rows:
dst.execute(insert_sql, tuple(row[c] for c in cols)) dst.execute(insert_sql, tuple(row[c] for c in cols))
if "id" in cols: if "id" in cols:
try:
_reset_sequences(dst, table, "id") _reset_sequences(dst, table, "id")
else: except Exception:
rollback_if_postgres(dst)
dst.commit() dst.commit()
stats[table] = len(rows) stats[table] = len(rows)
print(f" {table}: {len(rows)}") print(f" {table}: {len(rows)}")