Commit 65293ff9 65293ff9412649bf7692cf7950db50f9fb3c0cb6 by 沈秋雨

feat(audit): 新增 --delete-zero-duration 软删 song_time=0 记录

1 parent 666fd63f
...@@ -558,9 +558,52 @@ def main() -> int: ...@@ -558,9 +558,52 @@ def main() -> int:
558 help="补处理暂存表中 merge+skipped 记录:existing_into_new 入库+软删旧记录,new_into_existing 补作者合并", 558 help="补处理暂存表中 merge+skipped 记录:existing_into_new 入库+软删旧记录,new_into_existing 补作者合并",
559 ) 559 )
560 parser.add_argument("--skip-oss", action="store_true", help="--fix-merge 时跳过歌词 OSS 上传,保留原始文本") 560 parser.add_argument("--skip-oss", action="store_true", help="--fix-merge 时跳过歌词 OSS 上传,保留原始文本")
561 parser.add_argument("--dry-run", action="store_true", help="仅打印计划,不写库(配合 --fix-merge 使用)") 561 parser.add_argument("--dry-run", action="store_true", help="仅打印计划,不写库(配合 --fix-merge / --delete-zero-duration 使用)")
562 parser.add_argument(
563 "--delete-zero-duration",
564 action="store_true",
565 help="软删除 song_time=0 的记录(置 deleted='1')",
566 )
562 args = parser.parse_args() 567 args = parser.parse_args()
563 568
569 # --delete-zero-duration 模式
570 if args.delete_zero_duration:
571 conn = pymysql.connect(**TARGET_DB_CONFIG)
572 try:
573 table = quote_identifier(DEFAULT_TARGET_TABLE)
574 with conn.cursor() as cursor:
575 cursor.execute(
576 f"SELECT id, name, lyricist, composer, song_time "
577 f"FROM {table} WHERE deleted = '0' AND song_time = 0"
578 )
579 rows = list(cursor.fetchall())
580 print(f"song_time=0 记录数: {len(rows)}")
581 if args.dry_run:
582 for row in rows[:20]:
583 print(row)
584 if len(rows) > 20:
585 print(f"... {len(rows) - 20} more rows")
586 return 0
587 if not rows:
588 print("无需处理")
589 return 0
590 ids = [row["id"] for row in rows]
591 placeholders = ",".join(["%s"] * len(ids))
592 with conn.cursor() as cursor:
593 cursor.execute(
594 f"UPDATE {table} "
595 f"SET deleted = '1', modify_time = NOW(), "
596 f" off_shelf_remark = 'song_time=0 soft-deleted by audit script' "
597 f"WHERE deleted = '0' AND id IN ({placeholders})",
598 ids,
599 )
600 affected = cursor.rowcount
601 conn.commit()
602 print(f"soft_deleted={affected}")
603 finally:
604 conn.close()
605 return 0
606
564 # --fix-merge 模式:连双库,执行合并方向后处理 607 # --fix-merge 模式:连双库,执行合并方向后处理
565 if args.fix_merge: 608 if args.fix_merge:
566 target_conn = pymysql.connect(**TARGET_DB_CONFIG) 609 target_conn = pymysql.connect(**TARGET_DB_CONFIG)
......