fix: delete stale cached albums that no longer exist in VK

After refresh_vk_catalog syncs the album list from VK, remove any
VkCachedAlbum rows whose album_id was not returned by the API.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
mguschin
2026-05-12 21:47:43 +03:00
parent bb9fc71ed8
commit cad0b10fbb

View File

@@ -62,6 +62,16 @@ def _sync_user(db, user_id: int, token: str, group_id: str) -> None:
count=a.get("count"), count=a.get("count"),
fetched_at=now, fetched_at=now,
)) ))
# Delete cached albums that no longer exist in VK
(
db.query(VkCachedAlbum)
.filter(
VkCachedAlbum.user_id == user_id,
VkCachedAlbum.vk_group_id == group_id,
VkCachedAlbum.album_id.notin_(album_ids),
)
.delete(synchronize_session=False)
)
db.flush() db.flush()
# ── products (extended=1 gives albums_ids per product) ─────────────────── # ── products (extended=1 gives albums_ids per product) ───────────────────