From 5ec0212af57edd65dd0511ef725cb1ba64256f73 Mon Sep 17 00:00:00 2001 From: h Date: Sun, 6 Sep 2026 21:30:51 +0200 Subject: [PATCH] fix(userbot): mark deletions per row to stay under timescale decompression limit --- .../src/userbot/modules/capture/repository.py | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/backend/src/userbot/modules/capture/repository.py b/backend/src/userbot/modules/capture/repository.py index 7ed42bd..b137e32 100644 --- a/backend/src/userbot/modules/capture/repository.py +++ b/backend/src/userbot/modules/capture/repository.py @@ -30,6 +30,16 @@ ON CONFLICT (account_id, chat_id, message_id, date) DO UPDATE SET edited_at = now() """ +_FIND_DELETED = """ +SELECT chat_id, message_id, date FROM messages +WHERE account_id = $1 AND message_id = ANY($2::bigint[]) AND deleted_at IS NULL +""" + +_MARK_DELETED = """ +UPDATE messages SET deleted_at = now() +WHERE account_id = $1 AND chat_id = $2 AND message_id = $3 AND date = $4 +""" + _INSERT_VERSION = """ INSERT INTO message_versions (account_id, chat_id, message_id, observed_at, edit_date, text, raw) @@ -128,30 +138,34 @@ async def max_message_id( ) +async def _mark_deleted( + pool: asyncpg.Pool, account_id: int, rows: list[asyncpg.Record] +) -> None: + for row in rows: + await pool.execute( + _MARK_DELETED, account_id, row["chat_id"], row["message_id"], row["date"] + ) + + async def mark_deleted_box( pool: asyncpg.Pool, account_id: int, message_ids: list[int] ) -> None: - await pool.execute( - "UPDATE messages SET deleted_at = now() " - "WHERE account_id = $1 AND message_id = ANY($2::bigint[]) " - "AND chat_id > $3 AND deleted_at IS NULL", + rows = await pool.fetch( + f"{_FIND_DELETED} AND chat_id > $3", account_id, message_ids, CHANNEL_ID_THRESHOLD, ) + await _mark_deleted(pool, account_id, rows) async def mark_deleted_channel( pool: asyncpg.Pool, account_id: int, chat_id: int, message_ids: list[int] ) -> None: - await pool.execute( - "UPDATE messages SET deleted_at = now() " - "WHERE account_id = $1 AND chat_id = $2 AND message_id = ANY($3::bigint[]) " - "AND deleted_at IS NULL", - account_id, - chat_id, - message_ids, + rows = await pool.fetch( + f"{_FIND_DELETED} AND chat_id = $3", account_id, message_ids, chat_id ) + await _mark_deleted(pool, account_id, rows) async def add_version( # noqa: PLR0913