forked from mtusi2026/mtusi2026lab5
72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
"""Метаданные оригиналов и журнал операций до записи в S3."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import CheckConstraint, ForeignKey, Index, String, func, text
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
class Attachment(Base):
|
|
__tablename__ = "attachments"
|
|
__table_args__ = (
|
|
CheckConstraint("size_bytes BETWEEN 1 AND 10485760", name="size_bytes"),
|
|
CheckConstraint(
|
|
"content_type IN ('image/png', 'image/jpeg')", name="content_type"
|
|
),
|
|
CheckConstraint("sha256 ~ '^[a-f0-9]{64}$'", name="sha256"),
|
|
CheckConstraint(
|
|
"status IN ('queued', 'processing', 'ready', 'failed')", name="status"
|
|
),
|
|
CheckConstraint(
|
|
"(status = 'failed' AND error_code IS NOT NULL) OR (status <> 'failed' AND error_code IS NULL)",
|
|
name="error_code",
|
|
),
|
|
Index("ix_attachments_chat_id", "chat_id"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("chats.id"))
|
|
owner_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
|
|
object_key: Mapped[str] = mapped_column(String(128), unique=True)
|
|
filename: Mapped[str] = mapped_column(String(255))
|
|
content_type: Mapped[str] = mapped_column(String(32))
|
|
size_bytes: Mapped[int]
|
|
sha256: Mapped[str] = mapped_column(String(64))
|
|
status: Mapped[str] = mapped_column(String(16), server_default="queued")
|
|
error_code: Mapped[str | None] = mapped_column(String(64))
|
|
thumbnail_key: Mapped[str | None] = mapped_column(String(128))
|
|
crop_key: Mapped[str | None] = mapped_column(String(128))
|
|
optimized_key: Mapped[str | None] = mapped_column(String(128))
|
|
variants: Mapped[list[dict]] = mapped_column(
|
|
JSONB, default=list, server_default=text("'[]'::jsonb")
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|
|
|
|
|
|
class MessageAttachment(Base):
|
|
__tablename__ = "message_attachments"
|
|
|
|
message_id: Mapped[uuid.UUID] = mapped_column(
|
|
ForeignKey("messages.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
attachment_id: Mapped[uuid.UUID] = mapped_column(
|
|
ForeignKey("attachments.id"), primary_key=True
|
|
)
|
|
position: Mapped[int]
|
|
|
|
|
|
class UploadIntent(Base):
|
|
"""Фиксируется до PUT; удаляется атомарно с принятием queued metadata/job."""
|
|
|
|
__tablename__ = "upload_intents"
|
|
__table_args__ = (Index("ix_upload_intents_created_at", "created_at"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
object_key: Mapped[str] = mapped_column(String(128), unique=True)
|
|
created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp())
|