Add auto-migration for ai_provider/ai_model columns on existing DB
This commit is contained in:
+17
-2
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user