use sqlalchemy pattern

This commit is contained in:
2026-03-27 05:19:45 -03:00
parent 291ac8dd40
commit bcf6f3dc71
14 changed files with 451 additions and 669 deletions

43
core/db/checkpoint.py Normal file
View File

@@ -0,0 +1,43 @@
"""Checkpoint queries."""
from __future__ import annotations
from uuid import UUID
from sqlmodel import Session, select
from .tables import Checkpoint
def get_latest_checkpoint(session: Session, timeline_id: UUID, parent_id: UUID | None = None) -> Checkpoint | None:
stmt = select(Checkpoint).where(Checkpoint.timeline_id == timeline_id)
if parent_id is not None:
stmt = stmt.where(Checkpoint.parent_id == parent_id)
stmt = stmt.order_by(Checkpoint.created_at.desc())
return session.exec(stmt).first()
def get_root_checkpoint(session: Session, timeline_id: UUID) -> Checkpoint | None:
stmt = select(Checkpoint).where(
Checkpoint.timeline_id == timeline_id,
Checkpoint.parent_id == None,
)
return session.exec(stmt).first()
def list_checkpoints(session: Session, timeline_id: UUID) -> list[Checkpoint]:
stmt = (
select(Checkpoint)
.where(Checkpoint.timeline_id == timeline_id)
.order_by(Checkpoint.created_at)
)
return list(session.exec(stmt).all())
def list_scenarios(session: Session) -> list[Checkpoint]:
stmt = (
select(Checkpoint)
.where(Checkpoint.is_scenario == True)
.order_by(Checkpoint.created_at.desc())
)
return list(session.exec(stmt).all())