"""포트폴리오 강제 동기화 스크립트""" import asyncio import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from app.api.endpoints.portfolio import sync_portfolio, portfolio_cache from app.core.database import get_db async def force_sync(): print("🔄 포트폴리오 강제 동기화 시작...") # 캐시 초기화 portfolio_cache.holdings = [] portfolio_cache.summary = None portfolio_cache.last_synced = None portfolio_cache.change_flag = True print("✅ 캐시 초기화 완료") # DB 세션 생성 from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from app.core.config import settings database_url = settings.DATABASE_URL if database_url.startswith("postgresql://"): database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1) engine = create_async_engine(database_url) AsyncSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async with AsyncSessionLocal() as session: try: result = await sync_portfolio(session, force=True) print(f"✅ 동기화 완료: {result}") print(f"📊 보유 종목: {len(portfolio_cache.holdings)}개") for h in portfolio_cache.holdings: print(f" - {h.stock_name}({h.stock_code}): {h.quantity}주") except Exception as e: print(f"❌ 동기화 실패: {e}") if __name__ == "__main__": asyncio.run(force_sync())