From 4a08016edf1907f2d023f5e223996949442b4177 Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Wed, 27 May 2026 15:04:15 +0200 Subject: [PATCH] fix: SQLite column migration + startup init_schema + missing DB_URL warning - db.py: auto-migrate missing columns (signal_correct, pnl_dkk, etc.) on existing SQLite DBs - db.py: print warning to stderr if DATABASE_URL not set - dashboard.py: call init_schema() at startup so columns always exist --- dashboard.py | 2 ++ db.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/dashboard.py b/dashboard.py index 160fe14..cdc0178 100644 --- a/dashboard.py +++ b/dashboard.py @@ -769,6 +769,8 @@ def main(): parser.add_argument("--port", type=int, default=int(os.getenv("PORT", 5001))) parser.add_argument("--host", default=os.getenv("HOST", "0.0.0.0")) args = parser.parse_args() + from db import init_schema + init_schema() print(f"\n MoneyMaker Dashboard -> http://localhost:{args.port}\n") app.run(host=args.host, port=args.port, debug=False) diff --git a/db.py b/db.py index 46c0397..b77d7a0 100644 --- a/db.py +++ b/db.py @@ -27,6 +27,14 @@ DATABASE_URL = os.getenv("DATABASE_URL", "") SQLITE_PATH = Path(__file__).parent / "ground_news.db" DB_TYPE = "postgres" if DATABASE_URL else "sqlite" +if not DATABASE_URL: + import sys + print( + "⚠️ WARNING: DATABASE_URL not set — using SQLite fallback at " + f"{SQLITE_PATH}", + file=sys.stderr, + ) + # ── DBConn wrapper ──────────────────────────────────────────────────────────── @@ -387,11 +395,34 @@ def init_schema(): else: conn = sqlite3.connect(str(SQLITE_PATH)) conn.executescript(SCHEMA_SQLITE) + _migrate_sqlite_columns(conn) conn.commit() conn.close() print(f" Schema initialiseret ({DB_TYPE})") +def _migrate_sqlite_columns(conn): + """Add any missing columns to existing SQLite tables (forward migrations).""" + migrations = { + "position_events": [ + ("signal_correct", "INTEGER"), + ("pnl_dkk", "REAL"), + ], + "positions": [ + ("stop_loss", "REAL"), + ("take_profit", "REAL"), + ("analyst_rec", "TEXT"), + ("note", "TEXT"), + ], + } + for table, cols in migrations.items(): + existing = {row[1] for row in conn.execute(f"PRAGMA table_info({table})")} + for col, col_type in cols: + if col not in existing: + conn.execute(f"ALTER TABLE {table} ADD COLUMN {col} {col_type}") + print(f" SQLite migration: {table}.{col} ({col_type}) added") + + # ── SQLite → Postgres migration ─────────────────────────────────────────────── def migrate_sqlite_to_postgres():