133 lines
7.2 KiB
Python
133 lines
7.2 KiB
Python
from datetime import datetime, timezone
|
|
import uuid
|
|
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
from sqlalchemy.dialects.mysql import DATETIME
|
|
|
|
# MySQL's plain DATETIME truncates fractions, scrambling FIFO for jobs created
|
|
# within one second. Keep the same microsecond precision as SQLite/Python UTC.
|
|
Timestamp = DateTime().with_variant(DATETIME(fsp=6), "mysql")
|
|
|
|
|
|
def utcnow():
|
|
return datetime.now(timezone.utc).replace(tzinfo=None)
|
|
|
|
|
|
def new_id():
|
|
return uuid.uuid4().hex
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "visual_users"
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
|
username: Mapped[str] = mapped_column(String(64), unique=True)
|
|
display_name: Mapped[str] = mapped_column(String(80))
|
|
password_hash: Mapped[str] = mapped_column(String(512))
|
|
role: Mapped[str] = mapped_column(String(16), default="user")
|
|
disabled: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
must_change_password: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
|
|
|
|
|
|
class Session(Base):
|
|
__tablename__ = "visual_sessions"
|
|
token_hash: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
user_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"), index=True)
|
|
csrf_token: Mapped[str] = mapped_column(String(128))
|
|
expires_at: Mapped[datetime] = mapped_column(Timestamp, index=True)
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
|
|
|
|
|
|
class Asset(Base):
|
|
__tablename__ = "visual_assets"
|
|
__table_args__ = (UniqueConstraint("source_job_id", "source_output_index"),
|
|
UniqueConstraint("source_upstream_id", "source_output_index"))
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
|
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"), index=True)
|
|
name: Mapped[str] = mapped_column(String(255))
|
|
storage_key: Mapped[str] = mapped_column(String(512), unique=True)
|
|
thumbnail_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
kind: Mapped[str] = mapped_column(String(16))
|
|
mime_type: Mapped[str] = mapped_column(String(128))
|
|
size_bytes: Mapped[int] = mapped_column(BigInteger)
|
|
sha256: Mapped[str] = mapped_column(String(64))
|
|
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
duration_seconds: Mapped[float | None] = mapped_column(nullable=True)
|
|
tags: Mapped[list] = mapped_column(JSON, default=list)
|
|
favorite: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
origin: Mapped[str] = mapped_column(String(16), default="upload")
|
|
ready: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
source_job_id: Mapped[str | None] = mapped_column(ForeignKey("visual_jobs.id"), nullable=True, index=True)
|
|
source_upstream_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
source_output_index: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow, index=True)
|
|
deleted_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
|
|
|
|
|
class Job(Base):
|
|
__tablename__ = "visual_jobs"
|
|
__table_args__ = (UniqueConstraint("owner_id", "request_key", "batch_index"),
|
|
Index("visual_jobs_dispatch", "state", "next_action_at"))
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
|
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"), index=True)
|
|
task_type: Mapped[str] = mapped_column(String(48))
|
|
execution_mode: Mapped[str] = mapped_column(String(16), default="interactive")
|
|
policy_version: Mapped[str] = mapped_column(String(32), default="night-v1")
|
|
run_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
step_key: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
batch_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
batch_item_index: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
explicit_attempt: Mapped[int] = mapped_column(Integer, default=1)
|
|
desired_state: Mapped[str] = mapped_column(String(16), default="run")
|
|
hold_reasons: Mapped[list] = mapped_column(JSON, default=list)
|
|
current_execution_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
admitted_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
|
state: Mapped[str] = mapped_column(String(24))
|
|
phase: Mapped[str] = mapped_column(String(48))
|
|
version: Mapped[int] = mapped_column(Integer, default=1)
|
|
input_json: Mapped[dict] = mapped_column(JSON)
|
|
parameters: Mapped[dict] = mapped_column(JSON)
|
|
request_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
request_hash: Mapped[str] = mapped_column(String(64))
|
|
batch_index: Mapped[int] = mapped_column(Integer, default=0)
|
|
upstream_key: Mapped[str] = mapped_column(String(128), unique=True)
|
|
upstream_id: Mapped[str | None] = mapped_column(String(128), nullable=True, unique=True)
|
|
upstream_payload: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
upstream_result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
result_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
error_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
cancel_requested: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
attempt_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
work_attempt: Mapped[int] = mapped_column(Integer, default=0)
|
|
preemptions: Mapped[int] = mapped_column(Integer, default=0)
|
|
last_preempted_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
sync_warning: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
sync_failures: Mapped[int] = mapped_column(Integer, default=0)
|
|
next_action_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow, index=True)
|
|
scheduled_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
|
started_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
|
finished_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
|
|
|
|
|
class JobAsset(Base):
|
|
__tablename__ = "visual_job_assets"
|
|
job_id: Mapped[str] = mapped_column(ForeignKey("visual_jobs.id"), primary_key=True)
|
|
asset_id: Mapped[str] = mapped_column(ForeignKey("visual_assets.id"), primary_key=True)
|
|
|
|
|
|
class Event(Base):
|
|
__tablename__ = "visual_job_events"
|
|
__table_args__ = (Index("visual_events_owner_cursor", "owner_id", "id"),)
|
|
id: Mapped[int] = mapped_column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
|
|
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"))
|
|
job_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
event_type: Mapped[str] = mapped_column(String(48))
|
|
data: Mapped[dict] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow)
|