diff --git a/src/app/services.py b/src/app/services.py index dd1989a..123d19a 100644 --- a/src/app/services.py +++ b/src/app/services.py @@ -42,15 +42,17 @@ async def update_books(ctx) -> bool: index = meili.index("books") postgres = await get_postgres_connection() - cursor = await postgres.cursor( - "SELECT id, title, lang FROM books WHERE is_deleted = 'f';" - ) - while rows := await cursor.fetch(1024): - await loop.run_in_executor( - thread_pool, index.add_documents, [dict(row) for row in rows] + async with postgres.transaction(): + cursor = await postgres.cursor( + "SELECT id, title, lang FROM books WHERE is_deleted = 'f';" ) + while rows := await cursor.fetch(1024): + await loop.run_in_executor( + thread_pool, index.add_documents, [dict(row) for row in rows] + ) + index.update_searchable_attributes(["title"]) index.update_filterable_attributes(["lang"]) @@ -66,32 +68,34 @@ async def update_authors(ctx) -> bool: index = meili.index("authors") postgres = await get_postgres_connection() - cursor = await postgres.cursor( - "SELECT id, first_name, last_name, middle_name, " - " array(" - " SELECT DISTINCT lang FROM book_authors " - " LEFT JOIN books ON book = books.id " - " WHERE authors.id = book_authors.author " - " AND books.is_deleted = 'f' " - " ) as author_langs, " - " array(" - " SELECT DISTINCT lang FROM translations " - " LEFT JOIN books ON book = books.id " - " WHERE authors.id = translations.author " - " AND books.is_deleted = 'f' " - " ) as translator_langs, " - " (SELECT count(books.id) FROM book_authors " - " LEFT JOIN books ON book = books.id " - " WHERE authors.id = book_authors.author " - " AND books.is_deleted = 'f') as books_count " - "FROM authors;" - ) - while rows := await cursor.fetch(1024): - await loop.run_in_executor( - thread_pool, index.add_documents, [dict(row) for row in rows] + async with postgres.transaction(): + cursor = await postgres.cursor( + "SELECT id, first_name, last_name, middle_name, " + " array(" + " SELECT DISTINCT lang FROM book_authors " + " LEFT JOIN books ON book = books.id " + " WHERE authors.id = book_authors.author " + " AND books.is_deleted = 'f' " + " ) as author_langs, " + " array(" + " SELECT DISTINCT lang FROM translations " + " LEFT JOIN books ON book = books.id " + " WHERE authors.id = translations.author " + " AND books.is_deleted = 'f' " + " ) as translator_langs, " + " (SELECT count(books.id) FROM book_authors " + " LEFT JOIN books ON book = books.id " + " WHERE authors.id = book_authors.author " + " AND books.is_deleted = 'f') as books_count " + "FROM authors;" ) + while rows := await cursor.fetch(1024): + await loop.run_in_executor( + thread_pool, index.add_documents, [dict(row) for row in rows] + ) + index.update_searchable_attributes(["first_name", "last_name", "middle_name"]) index.update_filterable_attributes(["author_langs", "translator_langs"]) index.update_ranking_rules([*DEFAULT_RANKING_RULES, "books_count:desc"]) @@ -108,26 +112,28 @@ async def update_sequences(ctx) -> bool: index = meili.index("sequences") postgres = await get_postgres_connection() - cursor = await postgres.cursor( - "SELECT id, name, " - " array(" - " SELECT DISTINCT lang FROM book_sequences " - " LEFT JOIN books ON book = books.id " - " WHERE sequences.id = book_sequences.sequence " - " AND books.is_deleted = 'f' " - " ) as langs, " - " (SELECT count(books.id) FROM book_sequences " - " LEFT JOIN books ON book = books.id " - " WHERE sequences.id = book_sequences.sequence " - " AND books.is_deleted = 'f') as books_count " - "FROM sequences;" - ) - while rows := await cursor.fetch(1024): - await loop.run_in_executor( - thread_pool, index.add_documents, [dict(row) for row in rows] + async with postgres.transaction(): + cursor = await postgres.cursor( + "SELECT id, name, " + " array(" + " SELECT DISTINCT lang FROM book_sequences " + " LEFT JOIN books ON book = books.id " + " WHERE sequences.id = book_sequences.sequence " + " AND books.is_deleted = 'f' " + " ) as langs, " + " (SELECT count(books.id) FROM book_sequences " + " LEFT JOIN books ON book = books.id " + " WHERE sequences.id = book_sequences.sequence " + " AND books.is_deleted = 'f') as books_count " + "FROM sequences;" ) + while rows := await cursor.fetch(1024): + await loop.run_in_executor( + thread_pool, index.add_documents, [dict(row) for row in rows] + ) + index.update_searchable_attributes(["name"]) index.update_filterable_attributes(["langs"]) index.update_ranking_rules([*DEFAULT_RANKING_RULES, "books_count:desc"])