ai-station/init_db.py

27 lines
878 B
Python
Raw Normal View History

2025-12-26 16:48:51 +00:00
import asyncio
2025-12-29 05:50:06 +00:00
import asyncpg
import os
import sys
2025-12-26 16:48:51 +00:00
2025-12-29 05:50:06 +00:00
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://ai_user:secure_password_here@postgres:5432/ai_station")
# Converti da SQLAlchemy URL a asyncpg
db_url = DATABASE_URL.replace("postgresql+asyncpg://", "postgresql://")
2025-12-26 16:48:51 +00:00
async def init_database():
print("🔧 Inizializzazione database...")
try:
2025-12-29 05:50:06 +00:00
conn = await asyncpg.connect(db_url)
# Crea schema se non esiste (Chainlit lo farà automaticamente)
print("✅ Connessione al database riuscita")
print(" Le tabelle verranno create automaticamente da Chainlit")
2025-12-26 16:48:51 +00:00
2025-12-29 05:50:06 +00:00
await conn.close()
2025-12-26 16:48:51 +00:00
except Exception as e:
2025-12-29 05:50:06 +00:00
print(f"❌ Errore connessione database: {e}")
sys.exit(1)
2025-12-26 16:48:51 +00:00
if __name__ == "__main__":
asyncio.run(init_database())
2025-12-29 05:50:06 +00:00
print("✅ Inizializzazione database completata")