1
0

merge aigpt

This commit is contained in:
2025-06-02 18:24:43 +09:00
parent 6dbe630b9d
commit 6cd8014f80
16 changed files with 850 additions and 368 deletions

View File

@ -1,4 +1,6 @@
"""Database base configuration"""
import os
from pathlib import Path
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from app.core.config import settings
@ -6,18 +8,36 @@ from app.core.config import settings
# Create base class for models
Base = declarative_base()
# Ensure database directory exists
db_path = Path.home() / ".config" / "syui" / "ai" / "card"
db_path.mkdir(parents=True, exist_ok=True)
# Select database URL based on configuration
database_url = settings.database_url_supabase if settings.use_supabase else settings.database_url
# Create async engine
engine = create_async_engine(
database_url,
echo=settings.debug,
future=True,
pool_pre_ping=True, # Enable connection health checks
pool_size=5,
max_overflow=10
)
# Expand ~ in database URL
if database_url.startswith("sqlite"):
database_url = database_url.replace("~", str(Path.home()))
# Create async engine (SQLite-optimized settings)
if "sqlite" in database_url:
engine = create_async_engine(
database_url,
echo=settings.debug,
future=True,
# SQLite-specific optimizations
connect_args={"check_same_thread": False}
)
else:
# PostgreSQL settings (fallback)
engine = create_async_engine(
database_url,
echo=settings.debug,
future=True,
pool_pre_ping=True,
pool_size=5,
max_overflow=10
)
# Create async session factory
async_session = async_sessionmaker(
@ -27,14 +47,7 @@ async_session = async_sessionmaker(
)
async def get_db() -> AsyncSession:
async def get_session() -> AsyncSession:
"""Dependency to get database session"""
async with async_session() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
yield session