fix(tools): guard csv replace against newer fan records
This commit is contained in:
1 parent
dd6b09807d
commit
998d0fd0c0
4 files changed
+43
-1
No files matched your search
@@ -149,12 +149,24 @@ def import_fans_series(
|
||||
mid: int = DEFAULT_MID,
|
||||
force: bool = False,
|
||||
replace: bool = False,
|
||||
allow_lossy_replace: bool = False,
|
||||
dry_run: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""把解析后的序列写入 ``up_stat``。"""
|
||||
points = list(series.get("points", []))
|
||||
repo = Repository(db)
|
||||
deleted = 0
|
||||
last_csv = str(series.get("last_date") or "")
|
||||
if replace and last_csv:
|
||||
newer = db.read(lambda conn: int(conn.execute(
|
||||
"SELECT COUNT(*) FROM up_stat WHERE mid = ? AND stat_date > ?",
|
||||
(int(mid), last_csv),
|
||||
).fetchone()[0]))
|
||||
if newer and not allow_lossy_replace:
|
||||
raise ValueError(
|
||||
f"数据库中存在 {newer} 条晚于 CSV 末尾 {last_csv} 的粉丝记录;"
|
||||
"确认要用 CSV 覆盖并删除这些记录时请加 --allow-lossy-replace"
|
||||
)
|
||||
if replace:
|
||||
if dry_run:
|
||||
deleted = db.read(lambda conn: int(conn.execute(
|
||||
@@ -226,6 +238,10 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
"--replace", action="store_true",
|
||||
help="先清空该 mid 的全部粉丝历史,再用 CSV 重建",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--allow-lossy-replace", action="store_true",
|
||||
help="配合 --replace:允许删除数据库中晚于 CSV 末尾的记录",
|
||||
)
|
||||
parser.add_argument("--dry-run", action="store_true", help="只计算,不写数据库")
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
|
||||
@@ -242,6 +258,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
mid=args.mid,
|
||||
force=args.force,
|
||||
replace=args.replace,
|
||||
allow_lossy_replace=args.allow_lossy_replace,
|
||||
dry_run=args.dry_run,
|
||||
)
|
||||
finally:
|
||||
|
||||
Reference in new issue
Block a user