Improve training test 3

This commit is contained in:
Urtzi Alfaro
2025-07-29 12:45:39 +02:00
parent ef62f05031
commit cd6fd875f7
4 changed files with 154 additions and 60 deletions

View File

@@ -22,6 +22,7 @@ import warnings
warnings.filterwarnings('ignore')
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import text
from app.models.training import TrainedModel
from app.core.database import get_db_session
@@ -565,19 +566,27 @@ class BakeryProphetManager:
"""Deactivate previous models for the same product"""
if self.db_session:
try:
# Update previous models to inactive
query = """
# ✅ FIX: Wrap SQL string with text() for SQLAlchemy 2.0
query = text("""
UPDATE trained_models
SET is_active = false, is_production = false
WHERE tenant_id = :tenant_id AND product_name = :product_name
"""
""")
await self.db_session.execute(query, {
"tenant_id": tenant_id,
"product_name": product_name
})
# ✅ ADD: Commit the transaction
await self.db_session.commit()
logger.info(f"Successfully deactivated previous models for {product_name}")
except Exception as e:
logger.error(f"Failed to deactivate previous models: {str(e)}")
# ✅ ADD: Rollback on error
await self.db_session.rollback()
# Keep all existing methods unchanged
async def generate_forecast(self,