Add auto-migration for ai_provider/ai_model columns on existing DB
CI / Lint & Format (push) Failing after 8s
CI / Tests (push) Has been skipped
CI / Security Scan (push) Failing after 7s
CI / Docker Build (push) Has been skipped

This commit is contained in:
2026-06-25 10:43:42 -04:00
parent 8ce64c8578
commit b9c4622ece
+17 -2
View File
@@ -42,9 +42,9 @@ async def get_db() -> AsyncSession:
async def init_db():
"""Crée toutes les tables et initialise un client par défaut si nécessaire."""
"""Crée toutes les tables, applique les migrations, et initialise un client par défaut si nécessaire."""
import secrets
from sqlalchemy import select
from sqlalchemy import select, text
from app.dependencies.auth import hash_api_key
from app.models.client import APIClient, ClientPlan
@@ -53,6 +53,21 @@ async def init_db():
from app.models import client # noqa: F401
await conn.run_sync(Base.metadata.create_all)
# Migrations ponctuelles — ajout de colonnes manquantes (PostgreSQL seulement)
try:
await conn.execute(text(
"ALTER TABLE api_clients ADD COLUMN IF NOT EXISTS ai_provider VARCHAR(64)"
))
except Exception:
pass # SQLite ne supporte pas ADD COLUMN IF NOT EXISTS
try:
await conn.execute(text(
"ALTER TABLE api_clients ADD COLUMN IF NOT EXISTS ai_model VARCHAR(256)"
))
except Exception:
pass
# Vérifier s'il y a déjà des clients
async with AsyncSessionLocal() as session:
result = await session.execute(select(APIClient).limit(1))