from datetime import datetime from sqlalchemy import JSON, CheckConstraint, DateTime, Integer, String, Text, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column from app.db import Base class ScrapedPost(Base): __tablename__ = "scraped_posts" __table_args__ = ( UniqueConstraint("board_key", "article_id", name="uq_scraped_posts_board_article"), CheckConstraint( "board_key IN ('notice', 'archive', 'jobs')", name="ck_scraped_posts_board_key", ), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) board_key: Mapped[str] = mapped_column(String(32), nullable=False) board_name: Mapped[str] = mapped_column(String(100), nullable=False) board_id: Mapped[int] = mapped_column(Integer, nullable=False) article_id: Mapped[int] = mapped_column(Integer, nullable=False) title: Mapped[str] = mapped_column(String(500), nullable=False) post_url: Mapped[str] = mapped_column(Text, nullable=False) author: Mapped[str | None] = mapped_column(String(100), nullable=True) published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=False), nullable=True) summary: Mapped[str | None] = mapped_column(Text, nullable=True) content_text: Mapped[str | None] = mapped_column(Text, nullable=True) attachments: Mapped[list[dict]] = mapped_column(JSON, nullable=False, default=list) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=False), nullable=False, server_default=func.now(), ) class CrawlRun(Base): __tablename__ = "crawl_runs" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) started_at: Mapped[datetime] = mapped_column( DateTime(timezone=False), nullable=False, server_default=func.now(), ) finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=False), nullable=True) status: Mapped[str] = mapped_column(String(20), nullable=False, default="running") discovered_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0) inserted_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0) error_message: Mapped[str | None] = mapped_column(Text, nullable=True)