feat: implement data cleanup chain for batch deletion of expired records

This commit is contained in:
jxxghp
2026-05-09 14:04:10 +08:00
parent 45d47d32f8
commit a7823fb4d1
7 changed files with 502 additions and 3 deletions
+61 -1
View File
@@ -1,7 +1,7 @@
import time
from typing import List, Optional
from sqlalchemy import Column, Integer, String, JSON, select
from sqlalchemy import Column, Integer, String, JSON, select, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
@@ -336,6 +336,33 @@ class DownloadHistory(Base):
.all()
)
@classmethod
@db_update
def delete_before(
cls,
db: Session,
before_time: str,
limit: Optional[int] = 500,
) -> int:
"""
分批删除指定时间之前的下载历史。
"""
ids = [
row[0]
for row in db.query(cls.id)
.filter(cls.date < before_time)
.order_by(cls.id.asc())
.limit(limit)
.all()
]
if not ids:
return 0
return (
db.query(cls)
.filter(cls.id.in_(ids))
.delete(synchronize_session=False)
)
class DownloadFiles(Base):
"""
@@ -399,3 +426,36 @@ class DownloadFiles(Base):
db.query(cls).filter(cls.fullpath == fullpath, cls.state == 1).update(
{"state": 0}
)
@classmethod
@db_update
def delete_orphans(
cls,
db: Session,
limit: Optional[int] = 500,
) -> int:
"""
分批删除已找不到父下载历史的文件记录。
downloadfiles 没有时间字段,无法安全地按时间直接裁剪,
因此只清理明确失去父记录的孤儿数据。
"""
ids = [
row[0]
for row in db.query(cls.id)
.outerjoin(
DownloadHistory,
DownloadHistory.download_hash == cls.download_hash,
)
.filter(DownloadHistory.id.is_(None))
.order_by(cls.id.asc())
.limit(limit)
.all()
]
if not ids:
return 0
return (
db.query(cls)
.filter(cls.id.in_(ids))
.delete(synchronize_session=False)
)
+28 -1
View File
@@ -4,7 +4,7 @@ from sqlalchemy import Column, Integer, String, JSON, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
from app.db import db_query, Base, get_id_column, async_db_query
from app.db import db_query, db_update, Base, get_id_column, async_db_query
class Message(Base):
@@ -47,3 +47,30 @@ class Message(Base):
select(cls).order_by(cls.reg_time.desc()).offset((page - 1) * count).limit(count)
)
return result.scalars().all()
@classmethod
@db_update
def delete_before(
cls,
db: Session,
before_time: str,
limit: Optional[int] = 500,
) -> int:
"""
分批删除指定时间之前的消息记录。
"""
ids = [
row[0]
for row in db.query(cls.id)
.filter(cls.reg_time < before_time)
.order_by(cls.id.asc())
.limit(limit)
.all()
]
if not ids:
return 0
return (
db.query(cls)
.filter(cls.id.in_(ids))
.delete(synchronize_session=False)
)
+29 -1
View File
@@ -5,7 +5,7 @@ from sqlalchemy import Column, Integer, String, Float, JSON, func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
from app.db import db_query, Base, get_id_column, async_db_query
from app.db import db_query, db_update, Base, get_id_column, async_db_query
class SiteUserData(Base):
@@ -129,3 +129,31 @@ class SiteUserData(Base):
(cls.updated_day == subquery.c.latest_update_day)
).order_by(cls.updated_time.desc()))
return result.scalars().all()
@classmethod
@db_update
def delete_before(
cls,
db: Session,
before_day: str,
limit: Optional[int] = 500,
) -> int:
"""
分批删除指定日期之前的站点用户快照。
"""
ids = [
row[0]
for row in db.query(cls.id)
.filter(cls.updated_day < before_day)
.order_by(cls.id.asc())
.limit(limit)
.all()
]
if not ids:
return 0
deleted = (
db.query(cls)
.filter(cls.id.in_(ids))
.delete(synchronize_session=False)
)
return deleted
+27
View File
@@ -344,3 +344,30 @@ class TransferHistory(Base):
查询某时间之后的转移历史
"""
return db.query(cls).filter(cls.date > date).order_by(cls.id.desc()).all()
@classmethod
@db_update
def delete_before(
cls,
db: Session,
before_time: str,
limit: Optional[int] = 500,
) -> int:
"""
分批删除指定时间之前的整理历史。
"""
ids = [
row[0]
for row in db.query(cls.id)
.filter(cls.date < before_time)
.order_by(cls.id.asc())
.limit(limit)
.all()
]
if not ids:
return 0
return (
db.query(cls)
.filter(cls.id.in_(ids))
.delete(synchronize_session=False)
)