37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
from sqlalchemy import text
|
|
|
|
from config import settings
|
|
|
|
engine = create_async_engine(settings.database_url, echo=False)
|
|
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with session_factory() as session:
|
|
yield session
|
|
|
|
|
|
async def init_db():
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
if settings.database_url.startswith("sqlite"):
|
|
columns = {
|
|
"replay_file": "VARCHAR(500)",
|
|
"chapters_json": "TEXT",
|
|
"transcode_profile": "VARCHAR(64) NOT NULL DEFAULT 'standard'",
|
|
"renditions_json": "TEXT",
|
|
"homepage_visible": "BOOLEAN NOT NULL DEFAULT 1",
|
|
"remote_control_enabled": "BOOLEAN NOT NULL DEFAULT 0",
|
|
}
|
|
rows = await conn.execute(text("PRAGMA table_info(streams)"))
|
|
existing = {row[1] for row in rows}
|
|
for name, definition in columns.items():
|
|
if name not in existing:
|
|
await conn.execute(text(f"ALTER TABLE streams ADD COLUMN {name} {definition}"))
|