30c32b7714
- db/ package: async SQLAlchemy engine + Player/Game/Guess models - api/auth.py: passwordless TOTP login (pyotp), session token via socket auth - api/history.py: record guesses/points, DB-backed standings, restore unfinished games on startup, host-only end_game - api/__init__.py: auth-gated handlers, accounts map, rejoin via account - frontend: Auth (QR + code) and History pages, resume/end-game in lobby/table - docker-compose: real PostgreSQL service wired via DATABASE_URL - tests_history.py for the persistence/auth layer; refresh CLAUDE.md Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
36 lines
961 B
Python
36 lines
961 B
Python
"""Async SQLAlchemy pripojenie a inicializacia schemy.
|
|
|
|
Connection string z env DATABASE_URL. Default je lokalny SQLite subor (dev);
|
|
v produkcii staci nastavit DATABASE_URL na PostgreSQL (asyncpg), kod sa nemeni.
|
|
"""
|
|
|
|
import os
|
|
|
|
from sqlalchemy.ext.asyncio import (
|
|
AsyncSession,
|
|
async_sessionmaker,
|
|
create_async_engine,
|
|
)
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite+aiosqlite:///bridzik.db")
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
engine = create_async_engine(DATABASE_URL, echo=False)
|
|
async_session: async_sessionmaker[AsyncSession] = async_sessionmaker(
|
|
engine, expire_on_commit=False
|
|
)
|
|
|
|
|
|
async def init_db() -> None:
|
|
"""Vytvori tabulky, ak este neexistuju. Vola sa pri starte servera."""
|
|
# Import modelov registruje tabulky na Base.metadata.
|
|
from db import models # noqa: F401
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|