fix: prune 改逐行删——asyncmy 拒 tuple IN 的 list 参数
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -1890,25 +1890,28 @@ async def scrape_chatroom_stats(cookie_str: str, day: str | None = None):
|
|||||||
pruned = 0
|
pruned = 0
|
||||||
if day_key is None and group_recs and declared and len(group_recs) >= declared:
|
if day_key is None and group_recs and declared and len(group_recs) >= declared:
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import tuple_ as sa_tuple
|
|
||||||
|
|
||||||
from app.db.base import async_session_maker
|
from app.db.base import async_session_maker
|
||||||
from app.db.models import ChatGroup
|
from app.db.models import ChatGroup
|
||||||
|
|
||||||
keep = {(r["owner_account_id"], r["room_id"]) for r in group_recs}
|
keep = {(r["owner_account_id"], r["room_id"]) for r in group_recs}
|
||||||
keep_list = list(keep)
|
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
for i in range(0, len(keep_list), 500):
|
local = (await session.execute(
|
||||||
chunk = keep_list[i:i + 500]
|
sa.select(ChatGroup.owner_account_id, ChatGroup.room_id)
|
||||||
res = await session.execute(
|
.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(
|
sa.delete(ChatGroup).where(
|
||||||
ChatGroup.channel == "wechat",
|
ChatGroup.channel == "wechat",
|
||||||
sa.tuple_(
|
ChatGroup.owner_account_id == owner,
|
||||||
ChatGroup.owner_account_id, ChatGroup.room_id
|
ChatGroup.room_id == room,
|
||||||
).not_in(sa_tuple(chunk)),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
pruned += int(res.rowcount or 0)
|
pruned = len(stale)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user