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():
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()
c = conn.cursor()
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
ensure_account_risk_schema(conn)
ensure_recommend_tables(conn)
from ai_messages import ensure_ai_messages_table
ensure_ai_messages_table(conn)
conn.commit()
conn.close()
+9 -11
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:
try:
pg_conn.execute(
f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), "
f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)"
)
pg_conn.commit()
except Exception:
rollback_if_postgres(pg_conn)
pg_conn.execute(
f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), "
f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)"
)
def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
@@ -94,9 +90,11 @@ def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
for row in rows:
dst.execute(insert_sql, tuple(row[c] for c in cols))
if "id" in cols:
_reset_sequences(dst, table, "id")
else:
dst.commit()
try:
_reset_sequences(dst, table, "id")
except Exception:
rollback_if_postgres(dst)
dst.commit()
stats[table] = len(rows)
print(f" {table}: {len(rows)}")
except Exception as exc: