feat(backend): data model — identity / clinical / audit, repositories, create_all
CI / ruff · pytest (pull_request) Successful in 45s

Two DeclarativeBases on two engines (ADR-003): identity holds direct
identifiers, clinical holds token-keyed pseudonymised data plus audit_log.
Portable types (JSONB variant, tz-aware UTC timestamps rejecting naive
values) so tests run on SQLite. init_db() runs create_all at startup.
Guardrail #5 checked by AST and import-graph tests.

Refs #6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 14:58:26 +03:00
co-authored by Claude Opus 5.5
parent 10faec73e0
commit 121b607414
10 changed files with 1174 additions and 1 deletions
+12 -1
View File
@@ -5,8 +5,11 @@ from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from fastapi import FastAPI
from sqlalchemy.exc import SQLAlchemyError
from app.config import get_settings
from app.db.bootstrap import init_db
from app.db.session import dispose
# Logs carry only tokens, states and event codes — never PII or raw message text.
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
@@ -17,8 +20,16 @@ log = logging.getLogger("app")
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
settings = get_settings()
log.info("startup llm_provider=%s demo_clock=%s", settings.llm_provider, settings.demo_clock)
# DB init (#6), scheduler (#13) and Telegram bot (#11) hook in here.
try:
await init_db()
log.info("db schema ready")
except (OSError, SQLAlchemyError) as exc:
# In compose Postgres is required and healthy before the app starts; this branch is for running the
# API (and its tests) without a database. Endpoints touching the DB will fail until it is reachable.
log.error("db init failed, continuing without db: %s", type(exc).__name__)
# Scheduler (#13) and Telegram bot (#11) hook in here.
yield
await dispose()
app = FastAPI(title="PTSRaiAgent API", version="0.1.0", lifespan=lifespan)