fix: keep one douyu cookie per account
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
"""清理斗鱼账号重复成功 Cookie 记录
|
||||
|
||||
Revision ID: 20260808_0019
|
||||
Revises: 20260807_0018
|
||||
Create Date: 2026-08-08
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = "20260808_0019"
|
||||
down_revision: Union[str, None] = "20260807_0018"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
if not inspector.has_table("login_tasks"):
|
||||
return
|
||||
|
||||
task_table = sa.table(
|
||||
"login_tasks",
|
||||
sa.column("id", sa.Integer),
|
||||
sa.column("account_id", sa.Integer),
|
||||
sa.column("status", sa.String),
|
||||
sa.column("finished_at", sa.DateTime),
|
||||
)
|
||||
rows = bind.execute(
|
||||
sa.select(
|
||||
task_table.c.id,
|
||||
task_table.c.account_id,
|
||||
task_table.c.finished_at,
|
||||
)
|
||||
.where(task_table.c.status == "success")
|
||||
.order_by(
|
||||
task_table.c.account_id.asc(),
|
||||
task_table.c.finished_at.desc(),
|
||||
task_table.c.id.desc(),
|
||||
)
|
||||
).all()
|
||||
|
||||
delete_ids: list[int] = []
|
||||
seen_accounts: set[int] = set()
|
||||
for row in rows:
|
||||
if row.account_id in seen_accounts:
|
||||
delete_ids.append(row.id)
|
||||
continue
|
||||
seen_accounts.add(row.account_id)
|
||||
|
||||
if not delete_ids:
|
||||
return
|
||||
|
||||
for index in range(0, len(delete_ids), 500):
|
||||
chunk = delete_ids[index:index + 500]
|
||||
bind.execute(
|
||||
task_table.delete().where(task_table.c.id.in_(chunk))
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
pass
|
||||
Reference in New Issue
Block a user