from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy import text from app.models import Base import os from dotenv import load_dotenv load_dotenv() DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./stock_analysis.db") engine = create_async_engine(DATABASE_URL, echo=True) AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False) async def init_db(): async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) # Migration: Add ai_model column if it doesn't exist (for SQLite) try: await conn.execute( text("ALTER TABLE reports ADD COLUMN ai_model VARCHAR(100) DEFAULT 'gemini-2.0-flash-exp'") ) print("Migration: Added ai_model column to reports table") except Exception as e: # Column already exists or other error if "duplicate column" not in str(e).lower() and "already exists" not in str(e).lower(): print(f"Migration check: {e}") async def get_db(): async with AsyncSessionLocal() as session: yield session