fix: SQLite column migration + startup init_schema + missing DB_URL warning
Some checks failed
Build and Deploy MoneyMaker / build-and-deploy (push) Has been cancelled

- 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
This commit is contained in:
Henrik Jess Nielsen
2026-05-27 15:04:15 +02:00
parent 1e5e1074e2
commit 4a08016edf
2 changed files with 33 additions and 0 deletions

31
db.py
View File

@@ -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():