Server: basic implementation. Not tested => no python installed; bugfix in ESP (uvicorn on port 8090)

This commit is contained in:
Thomas Karner committed 2026-09-20 23:49:40 +02:00
1 parent 3694cdf207
commit 8a51e3d95e
38 files changed
+2442 -21

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
"""Operational tables: settings, audit log, backup runs, and the job queue."""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import (
BigInteger,
CheckConstraint,
DateTime,
ForeignKey,
Index,
Integer,
String,
Text,
func,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
JOB_STATUSES = ("queued", "running", "done", "failed")
class Setting(Base):
"""SPEC 3.4. "Fully configurable" needs a boundary or it never finishes.
Server scope: retention policy, thumbnail size, backup schedule + target,
alarm thresholds and channels, default theme, default locale, session
lifetime. Anything not on that list is a code change, deliberately.
Backup schedule lives here rather than in a systemd timer, which is why
scheduling runs in the worker (TODO Phase 1.2).
"""
__tablename__ = "settings"
__table_args__ = (CheckConstraint("scope IN ('server', 'device')", name="scope_valid"),)
key: Mapped[str] = mapped_column(String(64), primary_key=True)
value: Mapped[str | None] = mapped_column(Text)
value_type: Mapped[str] = mapped_column(String(16), default="str", server_default="'str'")
scope: Mapped[str] = mapped_column(String(16), default="server", server_default="'server'")
updated_by: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
class AuditLog(Base):
__tablename__ = "audit_log"
__table_args__ = (Index("ix_audit_log_entity", "entity", "entity_id", "at"),)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"))
action: Mapped[str] = mapped_column(String(64))
entity: Mapped[str] = mapped_column(String(64))
entity_id: Mapped[str | None] = mapped_column(String(64))
before: Mapped[dict | None] = mapped_column(JSONB)
after: Mapped[dict | None] = mapped_column(JSONB)
ip: Mapped[str | None] = mapped_column(String(45))
class BackupRun(Base):
__tablename__ = "backup_runs"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
started_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
status: Mapped[str] = mapped_column(String(16), default="running")
bytes: Mapped[int | None] = mapped_column(BigInteger)
snapshot_id: Mapped[str | None] = mapped_column(String(64))
# A backup you have never restored is a hypothesis: set by the monthly
# restore test (TODO Phase 5).
restore_tested_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
error: Mapped[str | None] = mapped_column(Text)
class Job(Base):
"""Postgres-backed queue, claimed with FOR UPDATE SKIP LOCKED. No Redis.
This second writer is the concrete reason SPEC C8 pushed past SQLite.
"""
__tablename__ = "jobs"
__table_args__ = (
CheckConstraint("status IN " + str(JOB_STATUSES), name="status_valid"),
# The claim query's covering index.
Index("ix_jobs_claimable", "status", "run_at", "priority"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
kind: Mapped[str] = mapped_column(String(64))
payload: Mapped[dict] = mapped_column(JSONB, default=dict, server_default="{}")
status: Mapped[str] = mapped_column(String(16), default="queued", server_default="'queued'")
priority: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
run_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
attempts: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
max_attempts: Mapped[int] = mapped_column(Integer, default=3, server_default="3")
locked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
locked_by: Mapped[str | None] = mapped_column(String(128))
last_error: Mapped[str | None] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))