cmvr-visual/backend/app/core/database.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

42 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy import event
import asyncio
from contextlib import asynccontextmanager
from app.models import Base
class Database:
def __init__(self, settings):
self.mode = settings.mode
self.engine = create_async_engine(settings.sqlalchemy_url, pool_pre_ping=True)
if settings.sqlalchemy_url.drivername.startswith("sqlite"):
@event.listens_for(self.engine.sync_engine, "connect")
def sqlite_options(connection, _):
cursor = connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.execute("PRAGMA busy_timeout=10000")
cursor.close()
self._sessions = async_sessionmaker(self.engine, expire_on_commit=False)
# 单 API 进程内串行提交短事务,确保 SSE 游标顺序与提交顺序一致;GPU 并行不等于增加 API workers。
self._unit_lock = asyncio.Lock()
@asynccontextmanager
async def session(self):
async with self._unit_lock:
async with self._sessions() as session:
yield session
async def initialize(self):
async with self.engine.begin() as connection:
if self.mode in {"test", "preview"}:
await connection.run_sync(Base.metadata.create_all)
else:
from app.core.schema import check_schema_revision
await connection.run_sync(check_schema_revision, {"007"})
async def close(self):
await self.engine.dispose()