33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from datetime import datetime, timedelta
|
|
|
|
from sqlalchemy import DateTime, Integer, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, Session
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class Mindmap(Base):
|
|
__tablename__ = "mindmaps"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
unique_id: Mapped[str] = mapped_column(
|
|
String(32), unique=True, index=True, nullable=False
|
|
)
|
|
session_id: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
raw_json: Mapped[str] = mapped_column(Text, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, nullable=False
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
|
|
)
|
|
|
|
@classmethod
|
|
def delete_expired_records(cls, db: Session, days: int = 5) -> int:
|
|
"""删除超过指定天数的记录"""
|
|
cutoff_date = datetime.utcnow() - timedelta(days=days)
|
|
deleted = db.query(cls).filter(cls.created_at < cutoff_date).delete()
|
|
db.commit()
|
|
return deleted
|