From 33a59a4accf52dcfbd6572925ab70cf9d196a936 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 26 Feb 2024 11:47:52 +0300 Subject: [PATCH] after-shouts-update-fix --- services/event_listeners.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/services/event_listeners.py b/services/event_listeners.py index 87745878..4dcfd6e5 100644 --- a/services/event_listeners.py +++ b/services/event_listeners.py @@ -1,6 +1,6 @@ import asyncio -from sqlalchemy import select, event, or_, exists, and_ +from sqlalchemy import select, event import json from orm.author import Author, AuthorFollower @@ -50,31 +50,22 @@ async def update_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60 @event.listens_for(Shout, 'after_insert') @event.listens_for(Shout, 'after_update') def after_shouts_update(mapper, connection, shout: Shout): - # Создаем подзапрос для проверки наличия авторов в списке shout.authors - subquery = select(1).where( - or_( - Author.id == shout.created_by, - and_( - Shout.id == shout.id, - ShoutAuthor.shout == Shout.id, - ShoutAuthor.author == Author.id, - ), - ) - ) - - # Основной запрос с использованием объединения и подзапроса exists + # Main query to get authors associated with the shout through ShoutAuthor authors_query = ( select(Author) - .select_from(Author) - .join(ShoutAuthor, Author.id == ShoutAuthor.author) - .where(ShoutAuthor.shout == shout.id) - .union(select(Author).where(exists(subquery))) + .select_from(ShoutAuthor) # Select from ShoutAuthor + .join(Author, Author.id == ShoutAuthor.author) # Join with Author + .where(ShoutAuthor.shout == shout.id) # Filter by shout.id ) + + # Execute the query authors = get_with_stat(authors_query) + for author in authors: asyncio.create_task(update_author_cache(author.dict())) + @event.listens_for(Reaction, 'after_insert') def after_reaction_insert(mapper, connection, reaction: Reaction): author_subquery = select(Author).where(Author.id == reaction.created_by)