Files
crypto_trading/backend/app/db.py
T
matitos 9c3bee87ac
CI/CD / backend (push) Successful in 5m18s
CI/CD / frontend (push) Successful in 5m2s
CI/CD / build (push) Successful in 34s
CI/CD / cleanup (push) Successful in 6s
CI/CD / deploy (push) Successful in 22s
Crypto trading bot: engine, brokers, backtester, dashboard, deploy
FastAPI backend with a single-strategy async engine, paper and Binance
brokers, pure-python indicators, a backtester that shares the engine's
portfolio math, and a Vue dashboard with a live WebSocket feed. Adds the
Gitea Actions CI/CD workflow that deploys to Traefik via docker_default.
2026-09-18 17:02:33 +00:00

39 lines
1.0 KiB
Python

from __future__ import annotations
from collections.abc import Iterator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from app.config import REPO_ROOT, get_settings
from app.models import Base
_settings = get_settings()
def _normalize_url(url: str) -> tuple[str, dict]:
connect_args: dict = {}
if url.startswith("sqlite"):
prefix, _, path = url.partition("sqlite:///")
if path and not path.startswith("/"):
url = f"{prefix}sqlite:///{REPO_ROOT / path}"
connect_args = {"check_same_thread": False}
return url, connect_args
_url, _connect_args = _normalize_url(_settings.database_url)
engine = create_engine(_url, connect_args=_connect_args, pool_pre_ping=True)
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
def init_db() -> None:
Base.metadata.create_all(bind=engine)
def get_session() -> Iterator[Session]:
session = SessionLocal()
try:
yield session
finally:
session.close()