fix: PostgreSQL cannot use get_chat_ids_by_model_id
Co-Authored-By: EntropyYue <164553692+EntropyYue@users.noreply.github.com>
This commit is contained in:
co-authored by
EntropyYue
parent
ddcec9842f
commit
7bda6bf767
@@ -15,6 +15,7 @@ from sqlalchemy import (
|
|||||||
Text,
|
Text,
|
||||||
JSON,
|
JSON,
|
||||||
Index,
|
Index,
|
||||||
|
func,
|
||||||
)
|
)
|
||||||
|
|
||||||
####################
|
####################
|
||||||
@@ -279,25 +280,26 @@ class ChatMessageTable:
|
|||||||
db: Optional[Session] = None,
|
db: Optional[Session] = None,
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""Get distinct chat_ids that used a specific model."""
|
"""Get distinct chat_ids that used a specific model."""
|
||||||
from sqlalchemy import distinct
|
|
||||||
|
|
||||||
with get_db_context(db) as db:
|
with get_db_context(db) as db:
|
||||||
query = db.query(distinct(ChatMessage.chat_id)).filter(
|
query = db.query(
|
||||||
ChatMessage.model_id == model_id
|
ChatMessage.chat_id,
|
||||||
)
|
func.max(ChatMessage.created_at).label("last_message_at"),
|
||||||
|
).filter(ChatMessage.model_id == model_id)
|
||||||
if start_date:
|
if start_date:
|
||||||
query = query.filter(ChatMessage.created_at >= start_date)
|
query = query.filter(ChatMessage.created_at >= start_date)
|
||||||
if end_date:
|
if end_date:
|
||||||
query = query.filter(ChatMessage.created_at <= end_date)
|
query = query.filter(ChatMessage.created_at <= end_date)
|
||||||
|
|
||||||
# Order by most recent message in each chat
|
# Group by chat_id and order by most recent message in each chat
|
||||||
chat_ids = (
|
chat_ids = (
|
||||||
query.order_by(ChatMessage.created_at.desc())
|
query.group_by(ChatMessage.chat_id)
|
||||||
|
.order_by(func.max(ChatMessage.created_at).desc())
|
||||||
.offset(skip)
|
.offset(skip)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
return [chat_id for (chat_id,) in chat_ids]
|
return [chat_id for chat_id, _ in chat_ids]
|
||||||
|
|
||||||
def delete_messages_by_chat_id(
|
def delete_messages_by_chat_id(
|
||||||
self, chat_id: str, db: Optional[Session] = None
|
self, chat_id: str, db: Optional[Session] = None
|
||||||
|
|||||||
Reference in New Issue
Block a user