from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import JSON, DateTime, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Job(Base):
__tablename__ = "jobs"
__table_args__ = (UniqueConstraint("idempotency_key", name="uq_jobs_idempotency_key"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
job_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True, index=True)
module: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
operation: Mapped[str] = mapped_column(String(128), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="queued")
progress: Mapped[float] = mapped_column(default=0.0)
idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False)
payload: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False)
result_ref: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
class Receipt(Base):
__tablename__ = "receipts"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
receipt_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True, index=True)
operation: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
inputs_hash: Mapped[str] = mapped_column(String(64), nullable=False)
actor: Mapped[str] = mapped_column(String(128), nullable=False)
result_ref: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
class AuditLog(Base):
__tablename__ = "audit_logs"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
request_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
tool_name: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
inputs_hash: Mapped[str] = mapped_column(String(64), nullable=False)
output_ref: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
class SecretCredential(Base):
__tablename__ = "secret_credentials"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
credential_name: Mapped[str] = mapped_column(String(128), nullable=False, unique=True, index=True)
provider: Mapped[str] = mapped_column(String(64), nullable=False)
encrypted_value: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)