Add FastAPI catalog backend (games/snapshots/diff) + tests
Modular-monolith backend over SQLAlchemy (SQLite by default, Postgres-ready
via DATABASE_URL). The full snapshot.json is stored verbatim; diffing reads it
back through the ams.diff engine, so the DB never mirrors the snapshot schema.
- ams.api.db/models/schemas/service : Game 1-N Snapshot, sha256-deduped upsert
- routes: POST/GET /games, POST/GET /snapshots (import, deduped), GET /diff
(?old&new[&owner]) running compute_diff on stored snapshots, /health
- ams.api.importer : bulk CLI loader (python -m ams.api.importer --game ...)
- run: uvicorn ams.api.app:create_app --factory
11 tests pass (6 diff + 5 API via TestClient over the golden pair). Smoke-tested
live on uvicorn: import -> /snapshots -> /diff returns the BlooMoo deltas.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
51
ams/api/models.py
Normal file
51
ams/api/models.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""ORM models: a Game has many Snapshots. The full snapshot.json is stored verbatim in `data`
|
||||
(JSON / JSONB); axis counts are denormalised for cheap listing. Diffing reads `data` back through
|
||||
the existing ams.diff engine, so the DB never has to mirror the snapshot schema."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import ForeignKey, JSON, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from .db import Base
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class Game(Base):
|
||||
__tablename__ = "games"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
||||
notes: Mapped[str | None] = mapped_column(String, default=None)
|
||||
|
||||
snapshots: Mapped[list["Snapshot"]] = relationship(
|
||||
back_populates="game", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class Snapshot(Base):
|
||||
__tablename__ = "snapshots"
|
||||
__table_args__ = (UniqueConstraint("sha256", name="uq_snapshot_sha256"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
game_id: Mapped[int | None] = mapped_column(ForeignKey("games.id"), default=None, index=True)
|
||||
|
||||
binary_name: Mapped[str] = mapped_column(String)
|
||||
sha256: Mapped[str] = mapped_column(String, index=True)
|
||||
engine: Mapped[str | None] = mapped_column(String, default=None)
|
||||
compiler: Mapped[str | None] = mapped_column(String, default=None)
|
||||
schema_version: Mapped[int | None] = mapped_column(default=None)
|
||||
|
||||
n_types: Mapped[int] = mapped_column(default=0)
|
||||
n_methods: Mapped[int] = mapped_column(default=0)
|
||||
n_events: Mapped[int] = mapped_column(default=0)
|
||||
n_fields: Mapped[int] = mapped_column(default=0)
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(default=_utcnow)
|
||||
data: Mapped[dict] = mapped_column(JSON)
|
||||
|
||||
game: Mapped["Game | None"] = relationship(back_populates="snapshots")
|
||||
Reference in New Issue
Block a user