ЛР 4 - БПИ2302 - Щербацевич - на 4
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
SmallInteger,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("username", name="uq_users_username"),
|
||||
CheckConstraint("username ~ '^[a-z0-9_]{3,32}$'", name="ck_users_username_format"),
|
||||
CheckConstraint(
|
||||
"char_length(btrim(display_name)) >= 1 AND char_length(display_name) <= 100",
|
||||
name="ck_users_display_name_len",
|
||||
),
|
||||
CheckConstraint("char_length(password_hash) >= 20", name="ck_users_password_hash_len"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
username: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
display_name: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"char_length(btrim(title)) >= 1 AND char_length(title) <= 100",
|
||||
name="ck_chats_title",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
title: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
__table_args__ = (
|
||||
CheckConstraint("ordinal >= 0 AND ordinal < 100", name="ck_chat_members_ordinal"),
|
||||
Index("ix_chat_members_user", "user_id", "chat_id"),
|
||||
)
|
||||
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("chats.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
ordinal: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"char_length(body) <= 4000",
|
||||
name="ck_messages_text",
|
||||
),
|
||||
# Keyset: WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4
|
||||
Index("ix_messages_history", "chat_id", "created_at", "id"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("chats.id"), nullable=False)
|
||||
sender_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
||||
text: Mapped[str] = mapped_column("body", Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class IdempotencyRecord(Base):
|
||||
__tablename__ = "idempotency_records"
|
||||
__table_args__ = (
|
||||
CheckConstraint("method = 'POST'", name="ck_idem_method"),
|
||||
CheckConstraint("status_code = 201", name="ck_idem_status"),
|
||||
CheckConstraint("idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'", name="ck_idem_key"),
|
||||
CheckConstraint("char_length(request_path) BETWEEN 1 AND 300", name="ck_idem_path"),
|
||||
CheckConstraint("char_length(request_hash) = 64", name="ck_idem_hash"),
|
||||
Index("ix_idempotency_message", "message_id"),
|
||||
Index("ix_idempotency_created", "created_at"),
|
||||
)
|
||||
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
method: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
request_path: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
idempotency_key: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
request_hash: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
status_code: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
||||
message_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("messages.id"),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class Attachment(Base):
|
||||
__tablename__ = "attachments"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("object_key", name="uq_attachments_object_key"),
|
||||
CheckConstraint("char_length(filename) BETWEEN 1 AND 255", name="ck_attachments_filename"),
|
||||
CheckConstraint("content_type IN ('image/png', 'image/jpeg')", name="ck_attachments_content_type"),
|
||||
CheckConstraint("size_bytes >= 1 AND size_bytes <= 10485760", name="ck_attachments_size"),
|
||||
CheckConstraint("sha256 ~ '^[a-f0-9]{64}$'", name="ck_attachments_sha256"),
|
||||
CheckConstraint("status = 'ready'", name="ck_attachments_status"),
|
||||
CheckConstraint("error_code IS NULL", name="ck_attachments_error"),
|
||||
Index("ix_attachments_chat", "chat_id", "created_at", "id"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("chats.id"), nullable=False)
|
||||
owner_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
||||
filename: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
content_type: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
sha256: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
object_key: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
status: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
error_code: Mapped[str | None] = mapped_column(Text, 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 MessageAttachment(Base):
|
||||
__tablename__ = "message_attachments"
|
||||
__table_args__ = (
|
||||
CheckConstraint("ordinal >= 0 AND ordinal < 10", name="ck_message_attachments_ordinal"),
|
||||
Index("ix_message_attachments_attachment", "attachment_id"),
|
||||
)
|
||||
|
||||
message_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("messages.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
attachment_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("attachments.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
ordinal: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
||||
Reference in New Issue
Block a user