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
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:
@@ -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)
|
||||
|
||||
|
||||
31
db.py
31
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():
|
||||
|
||||
Reference in New Issue
Block a user