75 lines
3.1 KiB
Python
75 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
||
"""按标题前缀清掉跑废的单据(只给冒烟/调试留下的脏数据用)。
|
||
|
||
正经场景的数据一律保留,界面上要能翻。删的是这些单据在
|
||
oa_work / oa_work_completed / oa_task / oa_task_completed / oa_work_log /
|
||
oa_read / oa_read_completed / oa_work_comment / oa_attachment / oa_notification
|
||
里的行,先按 job 找出父单,再把它派出去的单子(parent_work)一起带上。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||
|
||
from sqlalchemy import delete, select # noqa: E402
|
||
|
||
from app import db, models # noqa: E402
|
||
|
||
|
||
def purge(prefixes: list[str]) -> dict:
|
||
session = db.SessionLocal()
|
||
try:
|
||
jobs: set[str] = set()
|
||
works: set[str] = set()
|
||
for model in (models.Work, models.WorkCompleted):
|
||
for row in session.execute(select(model)).scalars():
|
||
if any(str(row.title or "").startswith(p) for p in prefixes):
|
||
jobs.add(row.job)
|
||
works.add(row.id)
|
||
# 子单据跟父单一起走
|
||
for model in (models.Work, models.WorkCompleted):
|
||
for row in session.execute(select(model)).scalars():
|
||
if row.parent_work in works:
|
||
jobs.add(row.job)
|
||
works.add(row.id)
|
||
counts: dict[str, int] = {}
|
||
targets = [
|
||
(models.Work, models.Work.job), (models.WorkCompleted, models.WorkCompleted.job),
|
||
(models.Task, models.Task.job), (models.TaskCompleted, models.TaskCompleted.job),
|
||
(models.WorkLog, models.WorkLog.job), (models.Read, models.Read.job),
|
||
(models.ReadCompleted, models.ReadCompleted.job),
|
||
(models.Attachment, models.Attachment.job),
|
||
(models.Notification, models.Notification.job),
|
||
]
|
||
for model, column in targets:
|
||
rows = session.execute(select(model).where(column.in_(jobs))).scalars().all()
|
||
counts[model.__tablename__] = len(rows)
|
||
for row in rows:
|
||
session.delete(row)
|
||
for model in (models.Work, models.WorkCompleted):
|
||
rows = session.execute(select(model).where(model.id.in_(works))).scalars().all()
|
||
for row in rows:
|
||
session.delete(row)
|
||
drafts = session.execute(select(models.Draft)).scalars().all()
|
||
drafts = [d for d in drafts if any(str(d.title or "").startswith(x) for x in prefixes)]
|
||
counts[models.Draft.__tablename__] = len(drafts)
|
||
for row in drafts:
|
||
session.delete(row)
|
||
comments = session.execute(
|
||
select(models.WorkComment).where(models.WorkComment.work.in_(works))).scalars().all()
|
||
counts[models.WorkComment.__tablename__] = len(comments)
|
||
for row in comments:
|
||
session.delete(row)
|
||
session.commit()
|
||
return {"jobs": sorted(jobs), "counts": counts}
|
||
finally:
|
||
session.close()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import json
|
||
print(json.dumps(purge(sys.argv[1:] or ["(冒烟"]), ensure_ascii=False, indent=2))
|