feat: 添加 SQLAlchemy 模型 + Alembic 数据库迁移
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import hashlib
|
||||
from datetime import datetime, date
|
||||
from sqlalchemy import String, Boolean, DateTime, Integer, Text, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class Announcement(Base):
|
||||
__tablename__ = "announcements"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
title: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
publish_date: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
||||
purchase_name: Mapped[str] = mapped_column(String(200), default="")
|
||||
content_url: Mapped[str] = mapped_column(Text, default="")
|
||||
source_code: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
source_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
announcement_type: Mapped[str] = mapped_column(String(50), default="purchase")
|
||||
content_hash: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
crawl_mode: Mapped[str] = mapped_column(String(20), default="auto")
|
||||
is_new: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
is_sent: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
keyword_matched: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
|
||||
|
||||
@staticmethod
|
||||
def generate_hash(title: str, publish_date: str, purchase_name: str,
|
||||
content_url: str, source_code: str) -> str:
|
||||
content = f"{title}|{publish_date}|{purchase_name}|{content_url}|{source_code}"
|
||||
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def source_map() -> dict:
|
||||
import json
|
||||
from app.config import settings
|
||||
return json.loads(settings.announcement_sources)
|
||||
Reference in New Issue
Block a user