From 17c0009cdee81ffa0f2444561a849e67c8bbf1a9 Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 12 Aug 2023 17:06:57 -0700 Subject: [PATCH] Fix fetching of latest index attempt --- backend/danswer/db/index_attempt.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/danswer/db/index_attempt.py b/backend/danswer/db/index_attempt.py index 7de9ef50492..6dd8653d517 100644 --- a/backend/danswer/db/index_attempt.py +++ b/backend/danswer/db/index_attempt.py @@ -109,7 +109,9 @@ def get_latest_index_attempts( db_session: Session, ) -> Sequence[IndexAttempt]: ids_stmt = select( - IndexAttempt.id, func.max(IndexAttempt.time_created).label("max_updated_at") + IndexAttempt.connector_id, + IndexAttempt.credential_id, + func.max(IndexAttempt.time_created).label("max_time_created"), ) where_stmts: list[ColumnElement] = [] @@ -124,10 +126,20 @@ def get_latest_index_attempts( ) if where_stmts: ids_stmt = ids_stmt.where(or_(*where_stmts)) - ids_stmt = ids_stmt.group_by(IndexAttempt.id) + ids_stmt = ids_stmt.group_by(IndexAttempt.connector_id, IndexAttempt.credential_id) ids_subqery = ids_stmt.subquery() - stmt = select(IndexAttempt).join(ids_subqery, ids_subqery.c.id == IndexAttempt.id) + stmt = ( + select(IndexAttempt) + .join( + ids_subqery, + and_( + ids_subqery.c.connector_id == IndexAttempt.connector_id, + ids_subqery.c.credential_id == IndexAttempt.credential_id, + ), + ) + .where(IndexAttempt.time_created == ids_subqery.c.max_time_created) + ) return db_session.execute(stmt).scalars().all()