feat(sprint2): инфраструктура БД — SQLAlchemy 2.0 async + Alembic
Первый кусок Спринта 2: подключаем SQLite через SQLAlchemy 2.0 (async, ORM-стиль) и Alembic для миграций. Схема выбрана под будущий рост — в threads сразу заведены nullable user_id и agent_config_id, чтобы Спринты 3+ не тащили миграции задним числом. - requirements.txt: sqlalchemy[asyncio]==2.0.36, aiosqlite==0.20.0, alembic==1.14.0. - config: database_url + sqlite_path (./data/sqlite/app.db). - db/base.py: DeclarativeBase; db/session.py: async engine, async_sessionmaker, get_session — FastAPI-dependency. - db/models/Thread: id, name, user_id?, agent_config_id?, created_at, updated_at; relationship messages с cascade all, delete-orphan. - db/models/Message: id, thread_id FK CASCADE, role, text, sources_json, assembled_prompt, created_at. - Alembic инициализирован через async-шаблон, env.py доработан: sys.path, url из settings, target_metadata = Base.metadata. - Начальная миграция e7199587be4b применена, таблицы threads/messages с индексами на FK и nullable-колонки созданы в data/sqlite/app.db. - .gitignore: исключаем data/sqlite/ (БД — артефакт, не исходник). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from db.base import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from db.models.thread import Thread
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
thread_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("threads.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
role: Mapped[str] = mapped_column(String(20), nullable=False) # "user" | "assistant"
|
||||
text: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
sources_json: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
assembled_prompt: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_utcnow, nullable=False)
|
||||
|
||||
thread: Mapped["Thread"] = relationship(back_populates="messages")
|
||||
Reference in New Issue
Block a user