forked from mtusi2026/mtusi2026lab5
feat: add variants jsonb column to attachments table
This commit is contained in:
+50
-15
@@ -1,10 +1,10 @@
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, Text
|
from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, Text, UniqueConstraint
|
||||||
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
|
from sqlalchemy.dialects.postgresql import UUID as PG_UUID, JSONB
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
from sqlalchemy import UniqueConstraint
|
|
||||||
from .base import Base
|
from .base import Base
|
||||||
|
|
||||||
|
|
||||||
@@ -99,37 +99,72 @@ class SessionRow(Base):
|
|||||||
DateTime(timezone=True), default=_now, nullable=False
|
DateTime(timezone=True), default=_now, nullable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
from sqlalchemy import UniqueConstraint
|
|
||||||
|
|
||||||
class IdempotencyRecord(Base):
|
class IdempotencyRecord(Base):
|
||||||
__tablename__ = "idempotency_keys"
|
__tablename__ = "idempotency_keys"
|
||||||
id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid4)
|
id: Mapped[UUID] = mapped_column(
|
||||||
user_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
PG_UUID(as_uuid=True), primary_key=True, default=uuid4
|
||||||
|
)
|
||||||
|
user_id: Mapped[UUID] = mapped_column(
|
||||||
|
PG_UUID(as_uuid=True),
|
||||||
|
ForeignKey("users.id", ondelete="CASCADE"),
|
||||||
|
nullable=False
|
||||||
|
)
|
||||||
key: Mapped[str] = mapped_column(String(128), nullable=False)
|
key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||||
request_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
request_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||||
response_body: Mapped[str] = mapped_column(Text, nullable=False)
|
response_body: Mapped[str] = mapped_column(Text, nullable=False)
|
||||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, nullable=False)
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True), default=_now, nullable=False
|
||||||
|
)
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
UniqueConstraint("user_id", "key", name="uq_idempotency_user_key"),
|
UniqueConstraint("user_id", "key", name="uq_idempotency_user_key"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Attachment(Base):
|
class Attachment(Base):
|
||||||
__tablename__ = "attachments"
|
__tablename__ = "attachments"
|
||||||
id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid4)
|
id: Mapped[UUID] = mapped_column(
|
||||||
chat_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), nullable=False)
|
PG_UUID(as_uuid=True), primary_key=True, default=uuid4
|
||||||
owner_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
)
|
||||||
|
chat_id: Mapped[UUID] = mapped_column(
|
||||||
|
PG_UUID(as_uuid=True),
|
||||||
|
ForeignKey("chats.id", ondelete="CASCADE"),
|
||||||
|
nullable=False
|
||||||
|
)
|
||||||
|
owner_id: Mapped[UUID] = mapped_column(
|
||||||
|
PG_UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
||||||
|
)
|
||||||
filename: Mapped[str] = mapped_column(String(255), nullable=False)
|
filename: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||||
content_type: Mapped[str] = mapped_column(String(100), nullable=False)
|
content_type: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||||
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
|
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||||
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||||
status: Mapped[str] = mapped_column(String(20), default="ready", nullable=False)
|
|
||||||
|
# ИЗМЕНЕНО: по умолчанию "queued", так как теперь обработка асинхронная
|
||||||
|
status: Mapped[str] = mapped_column(String(20), default="queued", nullable=False)
|
||||||
error_code: Mapped[str] = mapped_column(String(50), nullable=True)
|
error_code: Mapped[str] = mapped_column(String(50), nullable=True)
|
||||||
s3_key: Mapped[str] = mapped_column(String(500), nullable=False)
|
s3_key: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False)
|
|
||||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False)
|
# ДОБАВЛЕНО: колонка для хранения метаданных миниатюр
|
||||||
|
variants: Mapped[list] = mapped_column(JSONB, default=list, nullable=False)
|
||||||
|
|
||||||
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True), default=_now, nullable=False
|
||||||
|
)
|
||||||
|
updated_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True), default=_now, onupdate=_now, nullable=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MessageAttachment(Base):
|
class MessageAttachment(Base):
|
||||||
__tablename__ = "message_attachments"
|
__tablename__ = "message_attachments"
|
||||||
message_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), primary_key=True)
|
message_id: Mapped[UUID] = mapped_column(
|
||||||
attachment_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("attachments.id", ondelete="CASCADE"), primary_key=True)
|
PG_UUID(as_uuid=True),
|
||||||
|
ForeignKey("messages.id", ondelete="CASCADE"),
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
|
attachment_id: Mapped[UUID] = mapped_column(
|
||||||
|
PG_UUID(as_uuid=True),
|
||||||
|
ForeignKey("attachments.id", ondelete="CASCADE"),
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user