ЛР7 - БВТ2302 - Зюлина - на 3
This commit is contained in:
+112
@@ -0,0 +1,112 @@
|
||||
"""PostgreSQL schema and bounded connection pool."""
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import (Column, DateTime, ForeignKey, Index, String, Text, Integer, JSON,
|
||||
UniqueConstraint, create_engine)
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
|
||||
from .bao import read_secret
|
||||
|
||||
if os.getenv("MIGRATION_CONTEXT") == "1":
|
||||
DATABASE_URL = os.environ["DATABASE_URL"]
|
||||
else:
|
||||
_db_credentials = read_secret(os.getenv("BAO_SECRET_PATH", "messenger/api"))
|
||||
DATABASE_URL = URL.create(
|
||||
"postgresql+psycopg2", username=_db_credentials["db_username"],
|
||||
password=_db_credentials["db_password"], host=os.getenv("DB_HOST", "db"),
|
||||
port=int(os.getenv("DB_PORT", "5432")), database=os.getenv("DB_NAME", "messenger"),
|
||||
)
|
||||
engine = create_engine(DATABASE_URL, pool_size=5, max_overflow=5, pool_timeout=5,
|
||||
pool_pre_ping=True, connect_args={"connect_timeout": 5,
|
||||
"sslmode": "verify-full", "sslrootcert": os.getenv("DB_CA_FILE", "/run/tls/service-ca.crt"),
|
||||
"options": "-c statement_timeout=5000"})
|
||||
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
Base = declarative_base()
|
||||
now = lambda: datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
username = Column(String(32), nullable=False)
|
||||
display_name = Column(String(100), nullable=False)
|
||||
password_hash = Column(String(255), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (Index("ix_users_username", "username", unique=True),)
|
||||
|
||||
|
||||
class Session(Base):
|
||||
__tablename__ = "sessions"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
token = Column(String(255), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
user = relationship("User")
|
||||
__table_args__ = (Index("ix_sessions_token", "token", unique=True),)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
title = Column(String(100), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
members = relationship("ChatMember", back_populates="chat", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
chat = relationship("Chat", back_populates="members")
|
||||
user = relationship("User")
|
||||
__table_args__ = (UniqueConstraint("chat_id", "user_id", name="uq_chat_member"),
|
||||
Index("ix_chat_members_user_chat", "user_id", "chat_id"))
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
sender_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
text = Column(Text, nullable=False)
|
||||
attachment_ids = Column(JSON, nullable=False, default=list)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
chat = relationship("Chat")
|
||||
sender = relationship("User")
|
||||
__table_args__ = (Index("ix_messages_chat_created_id", "chat_id", "created_at", "id"),)
|
||||
|
||||
|
||||
class Attachment(Base):
|
||||
__tablename__ = "attachments"
|
||||
id = Column(String, primary_key=True)
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False, index=True)
|
||||
owner_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
filename = Column(String(255), nullable=False)
|
||||
content_type = Column(String(32), nullable=False)
|
||||
size_bytes = Column(Integer, nullable=False)
|
||||
sha256 = Column(String(64), nullable=False)
|
||||
object_key = Column(String(128), nullable=False, unique=True)
|
||||
staging_key = Column(String(128), nullable=False, unique=True)
|
||||
status = Column(String(16), nullable=False, default="ready")
|
||||
job_id = Column(String(36), nullable=False, unique=True)
|
||||
error_code = Column(String(64), nullable=True)
|
||||
variants = Column(JSON, nullable=False, default=list)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
updated_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
|
||||
|
||||
class MessageIdempotency(Base):
|
||||
__tablename__ = "message_idempotency"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
method = Column(String(8), nullable=False)
|
||||
chat_path = Column(String(80), nullable=False)
|
||||
key = Column(String(128), nullable=False)
|
||||
request_hash = Column(String(64), nullable=False)
|
||||
message_id = Column(String, ForeignKey("messages.id"), nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (UniqueConstraint("user_id", "method", "chat_path", "key",
|
||||
name="uq_idempotency_scope"),)
|
||||
Reference in New Issue
Block a user