수정본
This commit is contained in:
58
backend/clear_test_data.py
Normal file
58
backend/clear_test_data.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
테스트 데이터 삭제 스크립트
|
||||
재시작 시 오래된 테스트 데이터를 자동으로 삭제합니다.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from sqlalchemy import delete, select
|
||||
from datetime import date, timedelta
|
||||
|
||||
from app.core.database import AsyncSessionLocal
|
||||
from app.models.models import StockAnalysis, TradeHistory, Portfolio
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def clear_test_data():
|
||||
"""테스트 데이터 및 오래된 분석 데이터 삭제"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
# 1. 오래된 stock_analysis 데이터 삭제 (7일 이전)
|
||||
cutoff_date = date.today() - timedelta(days=7)
|
||||
result = await session.execute(
|
||||
delete(StockAnalysis).where(StockAnalysis.analysis_date < cutoff_date)
|
||||
)
|
||||
deleted_analysis = result.rowcount
|
||||
logger.info(f"🗑️ Deleted {deleted_analysis} old stock_analysis records (before {cutoff_date})")
|
||||
|
||||
# 2. PENDING/FAILED 상태의 오래된 거래 내역 삭제 (1일 이전)
|
||||
cutoff_datetime = date.today() - timedelta(days=1)
|
||||
result = await session.execute(
|
||||
delete(TradeHistory).where(
|
||||
TradeHistory.status.in_(['PENDING', 'FAILED']),
|
||||
TradeHistory.created_at < cutoff_datetime
|
||||
)
|
||||
)
|
||||
deleted_trades = result.rowcount
|
||||
logger.info(f"🗑️ Deleted {deleted_trades} old PENDING/FAILED trade records")
|
||||
|
||||
# 3. 수량이 0인 포트폴리오 항목 삭제
|
||||
result = await session.execute(
|
||||
delete(Portfolio).where(Portfolio.quantity <= 0)
|
||||
)
|
||||
deleted_portfolio = result.rowcount
|
||||
logger.info(f"🗑️ Deleted {deleted_portfolio} empty portfolio records")
|
||||
|
||||
await session.commit()
|
||||
logger.info("✅ Test data cleanup completed successfully!")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Error during test data cleanup: {e}")
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(clear_test_data())
|
||||
Reference in New Issue
Block a user