수정본
This commit is contained in:
52
backend/cleanup_portfolio.py
Normal file
52
backend/cleanup_portfolio.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""포트폴리오 수량 0인 종목 정리 스크립트"""
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from app.core.config import settings
|
||||
from app.models.models import Portfolio
|
||||
|
||||
async def cleanup():
|
||||
# asyncpg 드라이버 사용
|
||||
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:
|
||||
# 수량 0인 종목 조회
|
||||
result = await session.execute(
|
||||
select(Portfolio).where(Portfolio.quantity == 0)
|
||||
)
|
||||
zero_qty = result.scalars().all()
|
||||
|
||||
print(f"수량 0인 종목: {len(zero_qty)}개")
|
||||
for p in zero_qty:
|
||||
print(f" - {p.stock_name}({p.stock_code}): {p.quantity}주")
|
||||
|
||||
if zero_qty:
|
||||
# 삭제
|
||||
await session.execute(
|
||||
delete(Portfolio).where(Portfolio.quantity == 0)
|
||||
)
|
||||
await session.commit()
|
||||
print(f"\n✅ {len(zero_qty)}개 종목 삭제 완료")
|
||||
else:
|
||||
print("\n정리할 종목이 없습니다.")
|
||||
|
||||
# 최종 포트폴리오 확인
|
||||
result = await session.execute(select(Portfolio))
|
||||
all_portfolio = result.scalars().all()
|
||||
print(f"\n현재 포트폴리오: {len(all_portfolio)}개")
|
||||
for p in all_portfolio:
|
||||
print(f" - {p.stock_name}({p.stock_code}): {p.quantity}주")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(cleanup())
|
||||
Reference in New Issue
Block a user