Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

disable all chunks status when disable document (#12149)

Jyong5a3fe61f2af8
diff --git a/api/tasks/add_document_to_index_task.py b/api/tasks/add_document_to_index_task.py
index 9a172b2d9d8..bd7fcdadeaa 100644
--- a/api/tasks/add_document_to_index_task.py
+++ b/api/tasks/add_document_to_index_task.py
@@ -38,7 +38,11 @@ def add_document_to_index_task(dataset_document_id: str):
try:
segments = (
db.session.query(DocumentSegment)
- .filter(DocumentSegment.document_id == dataset_document.id, DocumentSegment.enabled == True)
+ .filter(
+ DocumentSegment.document_id == dataset_document.id,
+ DocumentSegment.enabled == False,
+ DocumentSegment.status == "completed",
+ )
.order_by(DocumentSegment.position.asc())
.all()
)
@@ -85,6 +89,16 @@ def add_document_to_index_task(dataset_document_id: str):
db.session.query(DatasetAutoDisableLog).filter(
DatasetAutoDisableLog.document_id == dataset_document.id
).delete()
+
+ # update segment to enable
+ db.session.query(DocumentSegment).filter(DocumentSegment.document_id == dataset_document.id).update(
+ {
+ DocumentSegment.enabled: True,
+ DocumentSegment.disabled_at: None,
+ DocumentSegment.disabled_by: None,
+ DocumentSegment.updated_at: datetime.datetime.now(datetime.UTC).replace(tzinfo=None),
+ }
+ )
db.session.commit()
end_at = time.perf_counter()
diff --git a/api/tasks/remove_document_from_index_task.py b/api/tasks/remove_document_from_index_task.py
index 1d580b38028..d0c4382f58d 100644
--- a/api/tasks/remove_document_from_index_task.py
+++ b/api/tasks/remove_document_from_index_task.py
@@ -1,3 +1,4 @@
+import datetime
import logging
import time
@@ -46,6 +47,16 @@ def remove_document_from_index_task(document_id: str):
index_processor.clean(dataset, index_node_ids, with_keywords=True, delete_child_chunks=False)
except Exception:
logging.exception(f"clean dataset {dataset.id} from index failed")
+ # update segment to disable
+ db.session.query(DocumentSegment).filter(DocumentSegment.document_id == document.id).update(
+ {
+ DocumentSegment.enabled: False,
+ DocumentSegment.disabled_at: datetime.datetime.now(datetime.UTC).replace(tzinfo=None),
+ DocumentSegment.disabled_by: document.disabled_by,
+ DocumentSegment.updated_at: datetime.datetime.now(datetime.UTC).replace(tzinfo=None),
+ }
+ )
+ db.session.commit()
end_at = time.perf_counter()
logging.info(