From c0b2116da2c0696b6908da752750fc4e1c966311 Mon Sep 17 00:00:00 2001 From: Stepan Vladovskiy Date: Wed, 5 Mar 2025 20:32:34 +0000 Subject: [PATCH] feat(db.py): added fetch_all_shouts, to populate the search index --- services/db.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/db.py b/services/db.py index bd3072e4..01e3961c 100644 --- a/services/db.py +++ b/services/db.py @@ -181,3 +181,27 @@ def get_json_builder(): # Используем их в коде json_builder, json_array_builder, json_cast = get_json_builder() + +async def fetch_all_shouts(session=None): + """Fetch all published shouts for search indexing""" + from orm.shout import Shout + + close_session = False + if session is None: + session = local_session() + close_session = True + + try: + # Fetch only published and non-deleted shouts + query = session.query(Shout).filter( + Shout.published_at.is_not(None), + Shout.deleted_at.is_(None) + ) + shouts = query.all() + return shouts + except Exception as e: + logger.error(f"Error fetching shouts for search indexing: {e}") + return [] + finally: + if close_session: + session.close() \ No newline at end of file