Skip to content

Commit

Permalink
sync delete app table record when delete app (#5819)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnJyong authored Jul 2, 2024
1 parent 49d9c60 commit af308b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 2 additions & 0 deletions api/services/app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ def delete_app(self, app: App) -> None:
Delete app
:param app: App instance
"""
db.session.delete(app)
db.session.commit()
# Trigger asynchronous deletion of app and related data
remove_app_and_related_data_task.delay(app.id)

Expand Down
41 changes: 22 additions & 19 deletions api/tasks/remove_app_and_related_data_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from models.dataset import AppDatasetJoin
from models.model import (
ApiToken,
App,
AppAnnotationHitHistory,
AppAnnotationSetting,
AppModelConfig,
Expand All @@ -36,17 +35,9 @@
def remove_app_and_related_data_task(self, app_id: str):
logging.info(click.style(f'Start deleting app and related data: {app_id}', fg='green'))
start_at = time.perf_counter()

deletion_cache_key = f'app_{app_id}_deletion'

try:
# Use a transaction to ensure all deletions succeed or none do
with db.session.begin_nested():
app = db.session.query(App).filter(App.id == app_id).first()
if not app:
logging.warning(click.style(f"App {app_id} not found", fg='yellow'))
return

# Delete related data
_delete_app_model_configs(app_id)
_delete_app_site(app_id)
Expand All @@ -62,10 +53,6 @@ def remove_app_and_related_data_task(self, app_id: str):
_delete_app_tag_bindings(app_id)
_delete_end_users(app_id)

# Delete the app itself
db.session.delete(app)


# If we reach here, the transaction was successful
db.session.commit()

Expand All @@ -74,7 +61,8 @@ def remove_app_and_related_data_task(self, app_id: str):

except SQLAlchemyError as e:
db.session.rollback()
logging.exception(click.style(f"Database error occurred while deleting app {app_id} and related data", fg='red'))
logging.exception(
click.style(f"Database error occurred while deleting app {app_id} and related data", fg='red'))
raise self.retry(exc=e, countdown=60) # Retry after 60 seconds

except Exception as e:
Expand All @@ -85,25 +73,32 @@ def remove_app_and_related_data_task(self, app_id: str):
def _delete_app_model_configs(app_id: str):
db.session.query(AppModelConfig).filter(AppModelConfig.app_id == app_id).delete()


def _delete_app_site(app_id: str):
db.session.query(Site).filter(Site.app_id == app_id).delete()


def _delete_app_api_tokens(app_id: str):
db.session.query(ApiToken).filter(ApiToken.app_id == app_id).delete()


def _delete_installed_apps(app_id: str):
db.session.query(InstalledApp).filter(InstalledApp.app_id == app_id).delete()


def _delete_recommended_apps(app_id: str):
db.session.query(RecommendedApp).filter(RecommendedApp.app_id == app_id).delete()


def _delete_app_annotation_data(app_id: str):
db.session.query(AppAnnotationHitHistory).filter(AppAnnotationHitHistory.app_id == app_id).delete()
db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app_id).delete()


def _delete_app_dataset_joins(app_id: str):
db.session.query(AppDatasetJoin).filter(AppDatasetJoin.app_id == app_id).delete()


def _delete_app_workflows(app_id: str):
db.session.query(WorkflowRun).filter(
WorkflowRun.workflow_id.in_(
Expand All @@ -118,6 +113,7 @@ def _delete_app_workflows(app_id: str):
db.session.query(WorkflowAppLog).filter(WorkflowAppLog.app_id == app_id).delete(synchronize_session=False)
db.session.query(Workflow).filter(Workflow.app_id == app_id).delete(synchronize_session=False)


def _delete_app_conversations(app_id: str):
db.session.query(PinnedConversation).filter(
PinnedConversation.conversation_id.in_(
Expand All @@ -126,25 +122,32 @@ def _delete_app_conversations(app_id: str):
).delete(synchronize_session=False)
db.session.query(Conversation).filter(Conversation.app_id == app_id).delete()


def _delete_app_messages(app_id: str):
message_ids = select(Message.id).filter(Message.app_id == app_id).scalar_subquery()
db.session.query(MessageFeedback).filter(MessageFeedback.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(MessageAnnotation).filter(MessageAnnotation.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(MessageFeedback).filter(MessageFeedback.message_id.in_(message_ids)).delete(
synchronize_session=False)
db.session.query(MessageAnnotation).filter(MessageAnnotation.message_id.in_(message_ids)).delete(
synchronize_session=False)
db.session.query(MessageChain).filter(MessageChain.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(MessageAgentThought).filter(MessageAgentThought.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(MessageAgentThought).filter(MessageAgentThought.message_id.in_(message_ids)).delete(
synchronize_session=False)
db.session.query(MessageFile).filter(MessageFile.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(SavedMessage).filter(SavedMessage.message_id.in_(message_ids)).delete(synchronize_session=False)
db.session.query(Message).filter(Message.app_id == app_id).delete(synchronize_session=False)


def _delete_workflow_tool_providers(app_id: str):
db.session.query(WorkflowToolProvider).filter(
WorkflowToolProvider.app_id == app_id
).delete(synchronize_session=False)
).delete(synchronize_session=False)


def _delete_app_tag_bindings(app_id: str):
db.session.query(TagBinding).filter(
TagBinding.target_id == app_id
).delete(synchronize_session=False)



def _delete_end_users(app_id: str):
db.session.query(EndUser).filter(EndUser.app_id == app_id).delete()

0 comments on commit af308b9

Please sign in to comment.