mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
feat(retention): 删除用户/Key 时保留历史记录,外键改 SET NULL 并添加名称快照
- Usage/RequestCandidate/VideoTask/Stats 等表的 user_id/api_key_id 外键从
CASCADE 改为 SET NULL,删除用户或 Key 后历史记录不再丢失
- 各表添加 username/api_key_name 快照字段,删除后仍可追溯归属
- 新增 bulk_cleanup 模块,分批置空大表外键避免长事务锁
- 删除用户/Key 流程集成预清理步骤,先置空再删除
- 精简 candidate_builder 冗余 debug 日志
- 修复 proxy_nodes 启动日志 format 占位符错误({} -> %s)
- 前端批量操作请求增加 5 分钟超时配置
This commit is contained in:
35
tests/services/test_user_bulk_cleanup.py
Normal file
35
tests/services/test_user_bulk_cleanup.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import Column, Integer, String, create_engine
|
||||
from sqlalchemy.orm import Session, declarative_base, sessionmaker
|
||||
|
||||
from src.services.user.bulk_cleanup import batch_nullify_fk
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class DemoRow(Base):
|
||||
__tablename__ = "demo_rows"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
ref_id = Column(String(36), nullable=True)
|
||||
|
||||
|
||||
def test_batch_nullify_fk_handles_sqlite_batches() -> None:
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
Base.metadata.create_all(engine)
|
||||
session_factory = sessionmaker(bind=engine)
|
||||
db: Session = session_factory()
|
||||
|
||||
try:
|
||||
db.add_all([DemoRow(ref_id="entity-1") for _ in range(905)])
|
||||
db.add_all([DemoRow(ref_id="entity-2") for _ in range(3)])
|
||||
db.commit()
|
||||
|
||||
updated = batch_nullify_fk(db, DemoRow, "ref_id", "entity-1")
|
||||
|
||||
assert updated == 905
|
||||
assert db.query(DemoRow).filter(DemoRow.ref_id.is_(None)).count() == 905
|
||||
assert db.query(DemoRow).filter(DemoRow.ref_id == "entity-2").count() == 3
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user