2025-08-25 07:13:45 +05:30
|
|
|
import logging
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import click
|
2025-08-28 23:17:25 +08:00
|
|
|
from celery import shared_task
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
from core.db.session_factory import session_factory
|
2025-08-25 07:13:45 +05:30
|
|
|
from models import ConversationVariable
|
|
|
|
|
from models.model import Message, MessageAnnotation, MessageFeedback
|
|
|
|
|
from models.tools import ToolConversationVariables, ToolFile
|
|
|
|
|
from models.web import PinnedConversation
|
|
|
|
|
|
2025-08-26 18:10:31 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2025-08-25 07:13:45 +05:30
|
|
|
|
|
|
|
|
@shared_task(queue="conversation")
|
2025-09-06 04:32:23 +09:00
|
|
|
def delete_conversation_related_data(conversation_id: str):
|
2025-08-25 07:13:45 +05:30
|
|
|
"""
|
|
|
|
|
Delete related data conversation in correct order from datatbase to respect foreign key constraints
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
conversation_id: conversation Id
|
|
|
|
|
"""
|
|
|
|
|
|
2025-08-26 18:10:31 +08:00
|
|
|
logger.info(
|
2025-08-25 07:13:45 +05:30
|
|
|
click.style(f"Starting to delete conversation data from db for conversation_id {conversation_id}", fg="green")
|
|
|
|
|
)
|
|
|
|
|
start_at = time.perf_counter()
|
|
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
with session_factory.create_session() as session:
|
|
|
|
|
try:
|
|
|
|
|
session.query(MessageAnnotation).where(MessageAnnotation.conversation_id == conversation_id).delete(
|
|
|
|
|
synchronize_session=False
|
|
|
|
|
)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(MessageFeedback).where(MessageFeedback.conversation_id == conversation_id).delete(
|
|
|
|
|
synchronize_session=False
|
|
|
|
|
)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(ToolConversationVariables).where(
|
|
|
|
|
ToolConversationVariables.conversation_id == conversation_id
|
|
|
|
|
).delete(synchronize_session=False)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(ToolFile).where(ToolFile.conversation_id == conversation_id).delete(synchronize_session=False)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(ConversationVariable).where(ConversationVariable.conversation_id == conversation_id).delete(
|
|
|
|
|
synchronize_session=False
|
|
|
|
|
)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(Message).where(Message.conversation_id == conversation_id).delete(synchronize_session=False)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.query(PinnedConversation).where(PinnedConversation.conversation_id == conversation_id).delete(
|
|
|
|
|
synchronize_session=False
|
|
|
|
|
)
|
2025-08-25 07:13:45 +05:30
|
|
|
|
2026-01-21 13:43:06 +08:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
end_at = time.perf_counter()
|
|
|
|
|
logger.info(
|
|
|
|
|
click.style(
|
|
|
|
|
(
|
|
|
|
|
f"Succeeded cleaning data from db for conversation_id {conversation_id} "
|
|
|
|
|
f"latency: {end_at - start_at}"
|
|
|
|
|
),
|
|
|
|
|
fg="green",
|
|
|
|
|
)
|
2025-08-25 07:13:45 +05:30
|
|
|
)
|
2026-01-21 13:43:06 +08:00
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Failed to delete data from db for conversation_id: %s failed", conversation_id)
|
|
|
|
|
session.rollback()
|
|
|
|
|
raise
|