From 8f9ef747cce312a1623a12e8ab12d3eca537b74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=85=E5=BF=B5?= Date: Thu, 10 Sep 2026 13:54:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20prune=20=E6=94=B9=E9=80=90=E8=A1=8C?= =?UTF-8?q?=E5=88=A0=E2=80=94=E2=80=94asyncmy=20=E6=8B=92=20tuple=20IN=20?= =?UTF-8?q?=E7=9A=84=20list=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Code --- yunke-server/scripts/scrape_via_page.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/yunke-server/scripts/scrape_via_page.py b/yunke-server/scripts/scrape_via_page.py index a6f29fc..ebac360 100644 --- a/yunke-server/scripts/scrape_via_page.py +++ b/yunke-server/scripts/scrape_via_page.py @@ -1890,25 +1890,28 @@ async def scrape_chatroom_stats(cookie_str: str, day: str | None = None): pruned = 0 if day_key is None and group_recs and declared and len(group_recs) >= declared: import sqlalchemy as sa - from sqlalchemy import tuple_ as sa_tuple from app.db.base import async_session_maker from app.db.models import ChatGroup keep = {(r["owner_account_id"], r["room_id"]) for r in group_recs} - keep_list = list(keep) async with async_session_maker() as session: - for i in range(0, len(keep_list), 500): - chunk = keep_list[i:i + 500] - res = await session.execute( + local = (await session.execute( + sa.select(ChatGroup.owner_account_id, ChatGroup.room_id) + .where(ChatGroup.channel == "wechat") + )).all() + stale = [(o, r) for (o, r) in local if (o, r) not in keep] + for owner, room in stale: + # 逐行删(差集常态 0~几行;不用 tuple IN——asyncmy 对其参数 + # 形态挑剔,实测 TypeError) + await session.execute( sa.delete(ChatGroup).where( ChatGroup.channel == "wechat", - sa.tuple_( - ChatGroup.owner_account_id, ChatGroup.room_id - ).not_in(sa_tuple(chunk)), + ChatGroup.owner_account_id == owner, + ChatGroup.room_id == room, ) ) - pruned += int(res.rowcount or 0) + pruned = len(stale) await session.commit() logger.info(