feat(ssulbox): 썰박스 생성 파이프라인 통합 및 콘텐츠 목록 UNION

This commit is contained in:
skkim committed 2026-08-11 11:03:03 +09:00
1 parent 8096837b13
commit 666c3bf6d1
77 files changed
+15912 -349

No files matched your search

+48 -6
View File
@@ -1,7 +1,18 @@
from datetime import datetime
from typing import TYPE_CHECKING, List, Optional
from sqlalchemy import Boolean, DateTime, ForeignKey, Index, Integer, String, UniqueConstraint, func
from sqlalchemy import (
BigInteger,
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Index,
Integer,
String,
UniqueConstraint,
func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database.session import Base
@@ -136,8 +147,11 @@ class Video(Base):
back_populates="videos",
)
# comment/video_reaction 이 video_id 와 content_id 두 FK 를 갖게 되어
# 어느 쪽으로 조인할지 명시해야 한다(없으면 AmbiguousForeignKeysError).
comments: Mapped[List["Comment"]] = relationship(
"Comment",
foreign_keys="Comment.video_id",
back_populates="video",
cascade="all, delete-orphan",
lazy="noload",
@@ -145,6 +159,7 @@ class Video(Base):
reactions: Mapped[List["VideoReaction"]] = relationship(
"VideoReaction",
foreign_keys="VideoReaction.video_id",
back_populates="video",
cascade="all, delete-orphan",
lazy="noload",
@@ -170,14 +185,30 @@ class VideoReaction(Base):
영상 반응 테이블
사용자가 영상에 반응(현재는 좋아요)을 남기면 생성, 다시 누르면 삭제(토글).
(user_uuid, video_id) 유니크 제약으로 1인 1회 보장.
향후 reaction_type 컬럼 추가로 다양한 반응 종류 확장 가능.
**ADO2 영상과 썰박스 콘텐츠를 모두 담는다.** 대상은 `video_id` 또는 `content_id`
중 **정확히 하나**만 채워지며 DB `CHECK` 로 강제한다.
1인 1회 보장은 유니크 두 개로 나눠서 한다. MySQL 은 NULL 을 서로 다른 값으로
취급하므로, 썰박스 행(video_id IS NULL)이 아무리 많아도
`uq_video_reaction_user_video` 에 걸리지 않는다 — 각자 자기 유니크만 지킨다.
"""
__tablename__ = "video_reaction"
__table_args__ = (
CheckConstraint(
"(video_id IS NULL) <> (content_id IS NULL)",
name="ck_video_reaction_one_target",
),
UniqueConstraint("user_uuid", "video_id", name="uq_video_reaction_user_video"),
UniqueConstraint(
"user_uuid", "content_id", name="uq_video_reaction_user_content"
),
Index("idx_video_reaction_video_id", "video_id"),
# 카운트 집계가 content_id 로 묶으므로 선행 컬럼 인덱스가 필요하다
# (유니크는 user_uuid 가 앞이라 이 용도로 못 쓴다).
Index("idx_video_reaction_content_id", "content_id"),
Index("idx_video_reaction_user_uuid", "user_uuid"),
{
"mysql_engine": "InnoDB",
@@ -189,11 +220,19 @@ class VideoReaction(Base):
id: Mapped[int] = mapped_column(
Integer, primary_key=True, autoincrement=True, comment="고유 식별자"
)
video_id: Mapped[int] = mapped_column(
# 대상은 아래 둘 중 **정확히 하나**만 채워진다 (ck_video_reaction_one_target).
video_id: Mapped[Optional[int]] = mapped_column(
Integer,
ForeignKey("video.id", ondelete="CASCADE"),
nullable=False,
comment="연결된 Video의 id",
nullable=True,
comment="ADO2 영상 id (썰박스 반응이면 NULL)",
)
content_id: Mapped[Optional[int]] = mapped_column(
# ssul_content.id 는 BIGINT 다. INT 로 두면 FK 타입 불일치(errno 3780).
BigInteger,
ForeignKey("ssul_content.id", ondelete="CASCADE"),
nullable=True,
comment="썰박스 콘텐츠 id (ADO2 반응이면 NULL)",
)
user_uuid: Mapped[str] = mapped_column(
String(36),
@@ -208,5 +247,8 @@ class VideoReaction(Base):
comment="반응 일시",
)
video: Mapped["Video"] = relationship("Video", back_populates="reactions")
# 썰박스 반응이면 None 이다. 접근하는 쪽에서 반드시 방어할 것.
video: Mapped[Optional["Video"]] = relationship(
"Video", foreign_keys=[video_id], back_populates="reactions"
)
user: Mapped["User"] = relationship("User", back_populates="video_reactions")