Перенести реализацию лабораторной 5

This commit is contained in:
2026-11-17 19:52:39 +03:00
parent 470d9d6f1e
commit 58f3d4c457
72 changed files with 4753 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
from .attachment import AttachmentRow, AttachmentVariantRow, MessageAttachmentRow
from .chat import ChatMemberRow, ChatRow
from .message import MessageRow
from .user import UserRow
from .job import AttachmentJobRow, JobEventRow, OutboxEventRow
__all__ = [
'AttachmentRow',
'AttachmentVariantRow',
'AttachmentJobRow',
'ChatMemberRow',
'ChatRow',
'MessageAttachmentRow',
'MessageRow',
'UserRow',
'JobEventRow',
'OutboxEventRow',
]
+70
View File
@@ -0,0 +1,70 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class AttachmentRow(Base):
__tablename__ = 'attachments'
__table_args__ = (
Index('attachments_chat_created_idx', 'chat_id', 'created_at', 'id'),
UniqueConstraint('original_key', name='attachments_object_key_key'),
UniqueConstraint('staging_key', name='attachments_staging_key_key'),
)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
chat_id: Mapped[UUID] = mapped_column(ForeignKey('chats.id', ondelete='CASCADE'), nullable=False)
owner_id: Mapped[UUID] = mapped_column(ForeignKey('users.id'), nullable=False)
filename: Mapped[str] = mapped_column(String(255), nullable=False)
original_key: Mapped[str | None] = mapped_column(String(255), nullable=True)
staging_key: Mapped[str | None] = mapped_column(String(255), nullable=True)
pipeline_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
content_type: Mapped[str] = mapped_column(String(32), nullable=False)
size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
width: Mapped[int] = mapped_column(Integer, nullable=False)
height: Mapped[int] = mapped_column(Integer, nullable=False)
status: Mapped[str] = mapped_column(String(16), nullable=False)
error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class AttachmentVariantRow(Base):
__tablename__ = 'attachment_variants'
attachment_id: Mapped[UUID] = mapped_column(
ForeignKey('attachments.id', ondelete='CASCADE'), primary_key=True
)
pipeline_version: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(16), primary_key=True)
object_key: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
content_type: Mapped[str] = mapped_column(String(32), nullable=False)
size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
width: Mapped[int] = mapped_column(Integer, nullable=False)
height: Mapped[int] = mapped_column(Integer, nullable=False)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class MessageAttachmentRow(Base):
__tablename__ = 'message_attachments'
__table_args__ = (
UniqueConstraint('message_id', 'position', name='message_attachments_message_position_key'),
Index('message_attachments_attachment_idx', 'attachment_id'),
)
message_id: Mapped[UUID] = mapped_column(
ForeignKey('messages.id', ondelete='CASCADE'), primary_key=True
)
attachment_id: Mapped[UUID] = mapped_column(
ForeignKey('attachments.id', ondelete='CASCADE'), primary_key=True
)
position: Mapped[int] = mapped_column(Integer, nullable=False)
+26
View File
@@ -0,0 +1,26 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import DateTime, ForeignKey, Index, String
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class ChatRow(Base):
__tablename__ = 'chats'
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
title: Mapped[str] = mapped_column(String(100), nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class ChatMemberRow(Base):
__tablename__ = 'chat_members'
__table_args__ = (Index('chat_members_user_chat_idx', 'user_id', 'chat_id'),)
chat_id: Mapped[UUID] = mapped_column(ForeignKey('chats.id', ondelete='CASCADE'), primary_key=True)
user_id: Mapped[UUID] = mapped_column(ForeignKey('users.id', ondelete='CASCADE'), primary_key=True)
+70
View File
@@ -0,0 +1,70 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, Integer, JSON, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class AttachmentJobRow(Base):
__tablename__ = 'attachment_jobs'
__table_args__ = (
UniqueConstraint(
'attachment_id', 'pipeline_version', name='attachment_job_attachment_version_key'
),
Index('attachment_jobs_lease_idx', 'state', 'lease_until'),
)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
attachment_id: Mapped[UUID] = mapped_column(
ForeignKey('attachments.id', ondelete='CASCADE'), nullable=False
)
pipeline_version: Mapped[int] = mapped_column(Integer, nullable=False)
state: Mapped[str] = mapped_column(String(16), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False)
max_attempts: Mapped[int] = mapped_column(Integer, nullable=False)
next_attempt_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
lease_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
replay_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class OutboxEventRow(Base):
__tablename__ = 'outbox_events'
__table_args__ = (Index('outbox_events_pending_idx', 'published_at', 'available_at'),)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
event_type: Mapped[str] = mapped_column(String(64), nullable=False)
job_id: Mapped[UUID] = mapped_column(
ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False
)
payload: Mapped[dict] = mapped_column(JSON, nullable=False)
available_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
claim_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
claim_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
publish_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class JobEventRow(Base):
__tablename__ = 'job_events'
__table_args__ = (Index('job_events_job_created_idx', 'job_id', 'created_at'),)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
job_id: Mapped[UUID] = mapped_column(
ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False
)
kind: Mapped[str] = mapped_column(String(32), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False)
error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+30
View File
@@ -0,0 +1,30 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import DateTime, ForeignKey, Index, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class MessageRow(Base):
__tablename__ = 'messages'
__table_args__ = (
Index('messages_history_idx', 'chat_id', 'created_at', 'id'),
UniqueConstraint(
'sender_id',
'chat_id',
'idempotency_key',
name='messages_sender_chat_idempotency_key',
),
)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
chat_id: Mapped[UUID] = mapped_column(ForeignKey('chats.id', ondelete='CASCADE'), nullable=False)
sender_id: Mapped[UUID] = mapped_column(ForeignKey('users.id'), nullable=False)
text: Mapped[str] = mapped_column(String(4000), nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
idempotency_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
+20
View File
@@ -0,0 +1,20 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import DateTime, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class UserRow(Base):
__tablename__ = 'users'
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
username: Mapped[str] = mapped_column(String(32), unique=True, nullable=False)
display_name: Mapped[str] = mapped_column(String(100), nullable=False)
password_hash: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)